feat: introduce PostgreSQL client module to replace per-article HTTP requests with bulk SQL queries

This commit is contained in:
Michael committed 2026-04-23 11:11:23 +02:00
1 parent c7d5bbe3b1
commit 038e87f554
1 file changed
+33 -9
+33 -9
View File
@@ -633,16 +633,40 @@ export async function pgGetDashboardData(): Promise<DashboardData> {
}
const refData = await refRes.json();
// --- Supplier: pick fournisseur with preference=1 (true main supplier, not depot) ---
let fou = article.nom_fou_principal || article.codefou_principal || 'Sans fournisseur';
// --- Supplier: exclude known depot platform codes, pick best real supplier ---
type FouEntry = { codefou: string; nom_fou: string | null; preference: number | null; suspendu: string | null };
let fou = '—';
// Known depot/platform codes that are NOT real commercial suppliers
const DEPOT_CODES = new Set(['D005', 'B0071', 'D005 ', 'B0071 ']);
if (refData.fournisseurs && Array.isArray(refData.fournisseurs)) {
// preference=1 means the main declared supplier
const mainFou = (refData.fournisseurs as { codefou: string; nom_fou: string | null; preference: number }[])
.find(f => f.preference === 1);
if (mainFou?.nom_fou) {
fou = mainFou.nom_fou;
} else if (mainFou?.codefou) {
fou = mainFou.codefou;
const fous = refData.fournisseurs as FouEntry[];
const pickLabel = (f: FouEntry): string => f.nom_fou?.trim() || f.codefou.trim();
// Epoch sentinel 1901 = never actually suspended in this system
const isActive = (f: FouEntry): boolean =>
!f.suspendu || f.suspendu.startsWith('1901');
// Filter out known depot platform codes only (not all D* codes — DART FRANCE etc. are real suppliers)
const realFous = fous.filter(f => !DEPOT_CODES.has(f.codefou.trim()));
if (realFous.length > 0) {
// Priority 1: preference=1 AND active
const p1Active = realFous.find(f => f.preference === 1 && isActive(f));
// Priority 2: preference=1 (even if suspended — at least declared as main)
const p1Any = realFous.find(f => f.preference === 1);
// Priority 3: any active non-depot supplier
const anyActive = realFous.find(f => isActive(f));
// Priority 4: first non-depot (last resort)
const first = realFous[0];
const best = p1Active ?? p1Any ?? anyActive ?? first;
fou = pickLabel(best);
} else {
// Only depot codes found — this article is sourced through the depot platform
fou = 'Dépôt';
}
}
fouMap.set(codein, fou);