fix: use Drizzle inArray() for ventesProduits classification query

Replaces raw sql ANY() template (which had array type issues) with
proper Drizzle inArray(), deduplicating by codein in JS.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
MichaelandClaude Sonnet 4.6 committed 2026-03-18 19:15:24 +01:00
1 parent 09be89b557
commit 84aa84cf41
1 file changed
+24 -16
+24 -16
View File
@@ -9,8 +9,8 @@ import {
buildLast12MonthsRange, buildLast12MonthsRange,
} from "@/lib/api-ff-client"; } from "@/lib/api-ff-client";
import { db } from "@/db"; import { db } from "@/db";
import { sessionSnapshots } from "@/db/schema"; import { sessionSnapshots, ventesProduits } from "@/db/schema";
import { eq, desc, sql } from "drizzle-orm"; import { eq, desc, inArray } from "drizzle-orm";
interface GetProductRowsInput { interface GetProductRowsInput {
codeFournisseur: string; codeFournisseur: string;
@@ -156,23 +156,31 @@ export async function getProductRows(input: GetProductRowsInput): Promise<Produc
try { try {
const codeins = Array.from(productMap.keys()); const codeins = Array.from(productMap.keys());
if (codeins.length > 0) { if (codeins.length > 0) {
const classifRows = await db.execute(sql` const classifRows = await db
SELECT DISTINCT ON (codein) .select({
codein, code_gamme, code_gamme_init, code3, libelle3 codein: ventesProduits.codein,
FROM ventes_produits codeGamme: ventesProduits.codeGamme,
WHERE codein = ANY(${codeins}) codeGammeInit: ventesProduits.codeGammeInit,
ORDER BY codein, updated_at DESC code3: ventesProduits.code3,
`); libelle3: ventesProduits.libelle3,
for (const row of classifRows.rows) { })
const product = productMap.get(row.codein as string); .from(ventesProduits)
.where(inArray(ventesProduits.codein, codeins));
// Déduplique en JS : premier occurrence par codein
const seen = new Set<string>();
for (const row of classifRows) {
if (seen.has(row.codein)) continue;
seen.add(row.codein);
const product = productMap.get(row.codein);
if (product) { if (product) {
product.codeGamme = (row.code_gamme as string | null) ?? null; product.codeGamme = row.codeGamme ?? null;
product.codeGammeInit = (row.code_gamme_init as string | null) ?? null; product.codeGammeInit = row.codeGammeInit ?? null;
product.code3 = (row.code3 as string | null) ?? ""; product.code3 = row.code3 ?? "";
product.libelle3 = (row.libelle3 as string | null) ?? ""; product.libelle3 = row.libelle3 ?? "";
} }
} }
console.log(`[getProductRows] Classification: ${classifRows.rows.length} lignes trouvées pour ${codeins.length} articles`); console.log(`[getProductRows] Classification: ${seen.size} codeins trouvés sur ${codeins.length} articles`);
} }
} catch (classifErr) { } catch (classifErr) {
console.warn("[getProductRows] ventesProduits classification non disponible:", classifErr); console.warn("[getProductRows] ventesProduits classification non disponible:", classifErr);