From 5a3ac3ed7f3718d4f1775eecc1a693bb9812cadf Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Thu, 23 Apr 2026 11:26:15 +0200 Subject: [PATCH] feat: implement high-performance PostgreSQL bulk fetchers to replace per-article HTTP API calls --- src/lib/pg-ff-client.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/lib/pg-ff-client.ts b/src/lib/pg-ff-client.ts index db80197..a5e6e84 100644 --- a/src/lib/pg-ff-client.ts +++ b/src/lib/pg-ff-client.ts @@ -633,7 +633,9 @@ export async function pgGetDashboardData(): Promise { } const refData = await refRes.json(); - // --- Supplier: exclude known depot platform codes, pick best real supplier --- + // --- Supplier: pick the last fournisseur who entered stock --- + // Business rule: attribute the article to the most recently active supplier + // = non-depot fournisseur with suspendu=null (currently active delivery relationship) type FouEntry = { codefou: string; nom_fou: string | null; preference: number | null; suspendu: string | null }; let fou = '—'; @@ -642,30 +644,27 @@ export async function pgGetDashboardData(): Promise { if (refData.fournisseurs && Array.isArray(refData.fournisseurs)) { 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 => + // Truly active = suspendu is null or the 1901 epoch sentinel (never suspended) + const isTrulyActive = (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) + // Non-depot fournisseurs only (exclude known platform/depot codes) 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) + // Priority 1: currently active (suspendu=null) non-depot — most likely to have made last reception + const activeNow = realFous.find(f => isTrulyActive(f)); + // Priority 2: preference=1 (declared main supplier, even if suspended) + const p1 = realFous.find(f => f.preference === 1); + // Priority 3: first non-depot available (last resort) const first = realFous[0]; - const best = p1Active ?? p1Any ?? anyActive ?? first; + const best = activeNow ?? p1 ?? first; fou = pickLabel(best); } else { - // Only depot codes found — this article is sourced through the depot platform + // Only depot codes found → article sourced exclusively through depot fou = 'Dépôt'; } }