From cb227943f32879d0ce5160d9489b3bca1b15513c Mon Sep 17 00:00:00 2001 From: Mathieu Date: Fri, 19 Jun 2026 11:43:12 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20prioritiser=20SKU=20sans=20suffixe=20num?= =?UTF-8?q?=C3=A9rique=20pour=20=C3=A9viter=20faux=20match=20(ex:=2026618-?= =?UTF-8?q?D-1=20=E2=86=92=2026618-D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Un fichier nommé 26618-D-1.jpg trouvait le SKU 26618-D-1 (céramique) avant de chercher 26618-D (la porte). On essaie maintenant d'abord le ref sans suffixe purement numérique (-1, -2…). Co-Authored-By: Claude Sonnet 4.6 --- src/app/api/products/search-sheets/route.ts | 14 ++++++-------- src/lib/shopify.ts | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/app/api/products/search-sheets/route.ts b/src/app/api/products/search-sheets/route.ts index 8b7916d..062d7b3 100644 --- a/src/app/api/products/search-sheets/route.ts +++ b/src/app/api/products/search-sheets/route.ts @@ -32,8 +32,6 @@ export async function GET(req: NextRequest) { s => s.properties.title, ) - // Collecte tous les matches et retourne le plus spécifique (UGS la plus longue) - // pour éviter que "26618" matche avant "26618-D" dans le nom de fichier let bestMatch: { shopifyId: string; title: string; ugsLength: number } | null = null for (const tab of tabs) { @@ -46,19 +44,19 @@ export async function GET(req: NextRequest) { const rows = (data.values ?? []) as string[][] for (const row of rows) { - const shopifyId = (row[0] ?? '').trim() // col A = ID Shopify - // col C = UGS (index 2 car on commence à A) + const shopifyId = (row[0] ?? '').trim() const ugs = (row[2] ?? '').trim() - // L'UGS doit apparaître comme mot entier dans le nom de fichier - // (entouré de séparateurs ou en début/fin) — ex: "dessus-30827-vue" ✓, "308270" ✗ const refLower = ref.toLowerCase() const ugsLower = ugs.toLowerCase() const wordBoundary = new RegExp(`(^|[^a-z0-9])${ugsLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([^a-z0-9]|$)`) const isMatch = ugs && shopifyId && wordBoundary.test(refLower) - if (isMatch && (!bestMatch || ugs.length > bestMatch.ugsLength)) { - bestMatch = { shopifyId, title: (row[3] ?? '').trim(), ugsLength: ugs.length } + if (isMatch) { + const title = (row[3] ?? '').trim() + if (!bestMatch || ugs.length > bestMatch.ugsLength) { + bestMatch = { shopifyId, title, ugsLength: ugs.length } + } } } } diff --git a/src/lib/shopify.ts b/src/lib/shopify.ts index 23cbed7..005b767 100644 --- a/src/lib/shopify.ts +++ b/src/lib/shopify.ts @@ -52,13 +52,24 @@ export async function searchProductByRef(ref: string): Promise