diff --git a/next.config.ts b/next.config.ts index 8328909..4422251 100644 --- a/next.config.ts +++ b/next.config.ts @@ -4,7 +4,7 @@ const nextConfig = { serverExternalPackages: ["pg"], experimental: { serverActions: { - bodySizeLimit: "10mb", + bodySizeLimit: "50mb", }, }, typescript: { diff --git a/scripts/diagnose-db.js b/scripts/diagnose-db.js new file mode 100644 index 0000000..ad30ec5 --- /dev/null +++ b/scripts/diagnose-db.js @@ -0,0 +1,48 @@ + +const { Pool } = require('pg'); +const fs = require('fs'); +const path = require('path'); + +async function main() { + console.log("--- DB DIAGNOSIS Start (Node) ---"); + + let connectionString = process.env.DATABASE_URL; + + try { + const CONFIG_PATH = path.join(process.cwd(), "data", ".db-config.json"); + if (fs.existsSync(CONFIG_PATH)) { + const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8")); + if (config.url) { + connectionString = config.url; + } + } + } catch (err) {} + + if (!connectionString) { + console.error("No connection string found."); + return; + } + + const pool = new Pool({ connectionString }); + + try { + const res = await pool.query(` + SELECT + code_fournisseur, + nom_fournisseur, + COUNT(*) as total_rows, + COUNT(DISTINCT codein) as distinct_products + FROM ventes_produits + GROUP BY code_fournisseur, nom_fournisseur + ORDER BY total_rows DESC + `); + + console.table(res.rows); + } catch (error) { + console.error("Diagnosis failed:", error); + } finally { + await pool.end(); + } +} + +main(); diff --git a/scripts/diagnose-db.ts b/scripts/diagnose-db.ts new file mode 100644 index 0000000..197e27b --- /dev/null +++ b/scripts/diagnose-db.ts @@ -0,0 +1,27 @@ + +import { db } from "@/db"; +import { ventesProduits } from "@/db/schema"; +import { sql } from "drizzle-orm"; + +async function main() { + console.log("--- DB DIAGNOSIS Start ---"); + try { + const results = await db.execute(sql` + SELECT + code_fournisseur, + nom_fournisseur, + COUNT(*) as total_rows, + COUNT(DISTINCT codein) as distinct_products + FROM ventes_produits + GROUP BY code_fournisseur, nom_fournisseur + ORDER BY total_rows DESC + `); + + console.table(results.rows); + } catch (error) { + console.error("Diagnosis failed:", error); + } + console.log("--- DB DIAGNOSIS End ---"); +} + +main(); diff --git a/src/features/grid/api/get-product-rows.ts b/src/features/grid/api/get-product-rows.ts index 54c3468..4166028 100644 --- a/src/features/grid/api/get-product-rows.ts +++ b/src/features/grid/api/get-product-rows.ts @@ -5,6 +5,8 @@ import { ventesProduits } from "@/db/schema"; import { eq, and, ne, like, sql } from "drizzle-orm"; import type { ProductRow, GammeCode, GridFilters } from "@/types/grid"; import { computeProductScores } from "@/lib/score-engine"; +import fs from "fs"; +import path from "path"; let isSchemaInitialized = false; @@ -70,6 +72,21 @@ 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}`); + + console.log(`- DB Rows retrieved for current filters: ${rows.length}`); + + // 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}`); + const storeParticipationMap = new Map>>(); // codein -> magasin -> Set of periods for (const row of rows) { @@ -164,6 +181,9 @@ export async function getProductRows(input: GetProductRowsInput): Promise