fix: prioritiser SKU sans suffixe numérique pour éviter faux match (ex: 26618-D-1 → 26618-D)

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 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 11:43:12 +02:00
parent d45c9490c0
commit cb227943f3
2 changed files with 20 additions and 11 deletions
+6 -8
View File
@@ -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 }
}
}
}
}