From 5009bd7612408a0316bb6a1a5d4e64fb2380121e Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Tue, 10 Mar 2026 15:30:43 +0100 Subject: [PATCH] feat: Implement `getProductRows` API to fetch, aggregate, and score product sales data, including a self-healing schema initialization for `code_gamme_init`. --- src/features/grid/api/get-product-rows.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/features/grid/api/get-product-rows.ts b/src/features/grid/api/get-product-rows.ts index 4166028..025f539 100644 --- a/src/features/grid/api/get-product-rows.ts +++ b/src/features/grid/api/get-product-rows.ts @@ -73,19 +73,20 @@ export async function getProductRows(input: GetProductRowsInput): Promise(); - // DEBUG: Log counts - console.log(`\n--- [getProductRows] DIAGNOSTIC for Supplier ${codeFournisseur} ---`); - const rawCount = await db.execute(sql` - SELECT COUNT(DISTINCT codein) as count FROM ventes_produits - WHERE code_fournisseur = ${codeFournisseur} - `); - console.log(`- TOTAL DISTINCT codein for this supplier in DB (unfiltered): ${rawCount.rows[0].count}`); + // EXTRA VERBOSE DIAGNOSTIC + console.log(`\n--- [getProductRows] ULTRA-DIAGNOSTIC for Supplier ${codeFournisseur} ---`); - console.log(`- DB Rows retrieved for current filters: ${rows.length}`); + const countAll = await db.execute(sql`SELECT COUNT(*) FROM ventes_produits WHERE code_fournisseur = ${codeFournisseur}`); + const countDistinctTotal = await db.execute(sql`SELECT COUNT(DISTINCT codein) FROM ventes_produits WHERE code_fournisseur = ${codeFournisseur}`); + const countNoTotal = await db.execute(sql`SELECT COUNT(DISTINCT codein) FROM ventes_produits WHERE code_fournisseur = ${codeFournisseur} AND magasin != 'TOTAL' AND periode != 'TOTAL'`); - // Count distinct products in the DB results - const distinctInDb = new Set(rows.map(r => r.codein)).size; - console.log(`- Distinct Products in DB results: ${distinctInDb}`); + console.log(`- Gross total rows in DB: ${countAll.rows[0].count}`); + console.log(`- Total distinct products (any store/period): ${countDistinctTotal.rows[0].count}`); + console.log(`- Distinct products excluding TOTAL store/period: ${countNoTotal.rows[0].count}`); + console.log(`- Current Filters rows retrieved: ${rows.length}`); + + const distinctInFetched = new Set(rows.map(r => r.codein)).size; + console.log(`- Distinct products in currently fetched rows: ${distinctInFetched}`); const storeParticipationMap = new Map>>(); // codein -> magasin -> Set of periods