mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-20 13:38:08 +02:00
Please provide the list of changes or file diffs so I can generate the commit message for you.
This commit is contained in:
1 parent
db90ed6e41
commit
79bc66f4d5
2 files changed
+58
-19
No files matched your search
@@ -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();
|
||||||
+43
-19
@@ -612,29 +612,53 @@ export async function pgGetDashboardData(): Promise<DashboardData> {
|
|||||||
// s'affiche même si la DB n'est pas configurée).
|
// s'affiche même si la DB n'est pas configurée).
|
||||||
let stockMap = new Map<string, { stock292: number; stock579: number; stockTotal: number }>();
|
let stockMap = new Map<string, { stock292: number; stock579: number; stockTotal: number }>();
|
||||||
let fouMap = new Map<string, string>();
|
let fouMap = new Map<string, string>();
|
||||||
|
const FF_API_BASE = process.env.FF_API_BASE_URL ?? "https://api.ffnancy.fr";
|
||||||
|
|
||||||
if (allCodeins.length > 0) {
|
if (allCodeins.length > 0) {
|
||||||
try {
|
try {
|
||||||
const [sm, fouResult] = await Promise.all([
|
// Batch the API requests to avoid overwhelming the server (e.g., batch of 5)
|
||||||
pgGetStockForCodeins(allCodeins),
|
for (let i = 0; i < allCodeins.length; i += 5) {
|
||||||
pgNoParallel(sql`
|
const batch = allCodeins.slice(i, i + 5);
|
||||||
SELECT
|
await Promise.all(batch.map(async (codein) => {
|
||||||
TRIM(a.codein::text) AS codein,
|
try {
|
||||||
COALESCE(fi.nom, af.code, 'Sans fournisseur')::text AS fournisseur
|
// 1. Fetch article search by codein
|
||||||
FROM articles a
|
const artRes = await fetch(`${FF_API_BASE}/api/articles?codein=${encodeURIComponent(codein)}`);
|
||||||
LEFT JOIN artfou1 af ON af.art_no_id = a.no_id AND af.preference = 1
|
if (!artRes.ok) return;
|
||||||
LEFT JOIN fouident fi ON fi.code = af.code
|
const artData = await artRes.json();
|
||||||
WHERE TRIM(a.codein::text) IN (${sql.join(allCodeins.map(c => sql`${c}`), sql`, `)})
|
if (!artData.articles || artData.articles.length === 0) return;
|
||||||
`),
|
|
||||||
]);
|
const article = artData.articles[0];
|
||||||
stockMap = sm;
|
const noId = article.no_id;
|
||||||
for (const row of fouResult.rows as unknown as { codein: string; fournisseur: string }[]) {
|
const fou = article.nom_fou_principal || article.codefou_principal || 'Sans fournisseur';
|
||||||
if (row.codein) fouMap.set(row.codein, row.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) {
|
} catch (enrichErr: any) {
|
||||||
console.warn("[Dashboard] DB enrichment unavailable, continuing with API data only:", enrichErr);
|
console.warn("[Dashboard] API enrichment unavailable:", enrichErr);
|
||||||
try {
|
|
||||||
require('fs').writeFileSync('c:/GIT/CollectFlow/scripts/db-error.log', enrichErr.stack || enrichErr.message || String(enrichErr));
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user