feat: add pg-ff-client to replace HTTP API calls with bulk PostgreSQL queries

This commit is contained in:
Michael committed 2026-04-23 10:36:37 +02:00
1 parent 32c1e06782
commit bd8339b27d
3 files changed
+62 -5

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
require('dotenv').config({ path: 'c:/GIT/CollectFlow/.env.local' });
const { Pool } = require('pg');
async function test() {
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
try {
const res = await pool.query("SELECT * FROM cube_stock WHERE cs.site = '292' LIMIT 5");
console.log("CUBE_STOCK:", res.rows);
} catch(e) {
console.log("ERROR cube_stock:", e.message);
}
try {
const res2 = await pool.query("SELECT a.codein, a.no_id FROM articles a LIMIT 5");
console.log("ARTICLES:", res2.rows);
} catch(e) {
console.log("ERROR articles:", e.message);
}
pool.end();
}
test();
+30
View File
@@ -0,0 +1,30 @@
import { NextResponse } from 'next/server';
import { db } from '@/db';
import { sql } from 'drizzle-orm';
import { pgGetStockForCodeins } from '@/lib/pg-ff-client';
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const codein = searchParams.get('codein') || '334152';
const map = await pgGetStockForCodeins([codein]);
const fouResult = await db.execute(sql`
SELECT
a.codein::text AS codein,
COALESCE(fi.nom, af.code, 'Sans fournisseur')::text AS fournisseur
FROM articles a
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
LEFT JOIN fouident fi ON fi.code = af.code
WHERE a.codein::text = ${codein}
`);
return NextResponse.json({
stock: Object.fromEntries(map),
fou: fouResult.rows,
});
} catch (e: any) {
return NextResponse.json({ error: e.message, stack: e.stack }, { status: 500 });
}
}
+7 -5
View File
@@ -616,15 +616,18 @@ export async function pgGetDashboardData(): Promise<DashboardData> {
FROM articles a
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
LEFT JOIN fouident fi ON fi.code = af.code
WHERE a.codein::text = ANY(string_to_array(${allCodeins.join(",")}, ','))
WHERE a.codein IN (${sql.join(allCodeins.map(c => sql`${c}`), sql`, `)})
`),
]);
stockMap = sm;
for (const row of fouResult.rows as unknown as { codein: string; fournisseur: string }[]) {
if (row.codein) fouMap.set(row.codein, row.fournisseur);
}
} catch (enrichErr) {
} catch (enrichErr: any) {
console.warn("[Dashboard] DB enrichment unavailable, continuing with API data only:", enrichErr);
try {
require('fs').writeFileSync('c:/GIT/CollectFlow/scripts/db-error.log', enrichErr.stack || enrichErr.message || String(enrichErr));
} catch (e) {}
}
}
@@ -848,12 +851,11 @@ export interface StockBySite {
export async function pgGetStockForCodeins(codeins: string[]): Promise<Map<string, StockBySite>> {
if (codeins.length === 0) return new Map();
const codeinsStr = codeins.join(",");
const result = await pgNoParallel(
sql`SELECT a.codein::text AS codein, cs.site, COALESCE(cs.qte, 0)::float AS stockdispo
sql`SELECT a.codein AS codein, cs.site, COALESCE(cs.qte, 0)::float AS stockdispo
FROM cube_stock cs
JOIN articles a ON a.no_id = cs.artnoid
WHERE a.codein::text = ANY(string_to_array(${codeinsStr}, ','))`
WHERE a.codein IN (${sql.join(codeins.map(c => sql`${c}`), sql`, `)})`
);
const rows = result.rows as { codein: string; site: string; stockdispo: number }[];