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