From 79bc66f4d50e833d08491ff85ab2d4c109e35f47 Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Thu, 23 Apr 2026 10:48:09 +0200 Subject: [PATCH] Please provide the list of changes or file diffs so I can generate the commit message for you. --- scripts/check-api-articles.js | 15 +++++++++ src/lib/pg-ff-client.ts | 62 ++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 scripts/check-api-articles.js diff --git a/scripts/check-api-articles.js b/scripts/check-api-articles.js new file mode 100644 index 0000000..adf7de4 --- /dev/null +++ b/scripts/check-api-articles.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +async function test() { + try { + const res = await fetch("https://api.ffnancy.fr/api/articles?search=334152"); + const data = await res.json(); + console.log("ARTICLES SEARCH:", JSON.stringify(data, null, 2)); + + const res2 = await fetch("https://api.ffnancy.fr/api/articles/334152/referentiel"); + const data2 = await res2.json(); + console.log("ARTICLES REFERENTIEL BY CODEIN:", JSON.stringify(data2, null, 2)); + } catch(e) { + console.log(e.message); + } +} +test(); diff --git a/src/lib/pg-ff-client.ts b/src/lib/pg-ff-client.ts index f4b58fe..d9108cd 100644 --- a/src/lib/pg-ff-client.ts +++ b/src/lib/pg-ff-client.ts @@ -612,29 +612,53 @@ export async function pgGetDashboardData(): Promise { // s'affiche même si la DB n'est pas configurée). let stockMap = new Map(); let fouMap = new Map(); + const FF_API_BASE = process.env.FF_API_BASE_URL ?? "https://api.ffnancy.fr"; + if (allCodeins.length > 0) { try { - const [sm, fouResult] = await Promise.all([ - pgGetStockForCodeins(allCodeins), - pgNoParallel(sql` - SELECT - TRIM(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 TRIM(a.codein::text) 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); + // Batch the API requests to avoid overwhelming the server (e.g., batch of 5) + for (let i = 0; i < allCodeins.length; i += 5) { + const batch = allCodeins.slice(i, i + 5); + await Promise.all(batch.map(async (codein) => { + try { + // 1. Fetch article search by codein + const artRes = await fetch(`${FF_API_BASE}/api/articles?codein=${encodeURIComponent(codein)}`); + if (!artRes.ok) return; + const artData = await artRes.json(); + if (!artData.articles || artData.articles.length === 0) return; + + const article = artData.articles[0]; + const noId = article.no_id; + const fou = article.nom_fou_principal || article.codefou_principal || 'Sans fournisseur'; + fouMap.set(codein, fou); + + // 2. Fetch stock via referentiel using no_id + if (noId) { + const refRes = await fetch(`${FF_API_BASE}/api/articles/${encodeURIComponent(noId)}/referentiel`); + if (!refRes.ok) return; + const refData = await refRes.json(); + + let s292 = 0; + let s579 = 0; + let sTot = 0; + + if (refData.stock && Array.isArray(refData.stock)) { + for (const s of refData.stock) { + const qte = Number(s.qte) || 0; + if (s.site === '292') s292 += qte; + else if (s.site === '579') s579 += qte; + sTot += qte; + } + } + stockMap.set(codein, { stock292: s292, stock579: s579, stockTotal: sTot }); + } + } catch (err) { + console.error(`[Dashboard API enrichment] Error for codein ${codein}:`, err); + } + })); } } 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) {} + console.warn("[Dashboard] API enrichment unavailable:", enrichErr); } }