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 }
}
}
}
}
+14 -3
View File
@@ -52,13 +52,24 @@ export async function searchProductByRef(ref: string): Promise<ShopifyProductRes
}
}
// 1. SKU exact (ex: "30827")
// Si le ref se termine par un suffixe purement numérique (ex: "26618-D-1" → "26618-D"),
// on essaie d'abord sans ce suffixe pour éviter de matcher un autre produit qui aurait
// accidentellement ce SKU complet
const withoutNumericSuffix = /[-_]\d+$/.test(ref) ? ref.replace(/[-_]\d+$/, '') : null
// 1. SKU sans suffixe numérique en priorité (ex: "26618-D-1" → "26618-D")
if (withoutNumericSuffix) {
const result = await searchBySku(withoutNumericSuffix)
if (result.found) return result
}
// 2. SKU exact
let result = await searchBySku(ref)
if (result.found) return result
// 2. SKU sans suffixe : "30827-dessus" → "30827" (retire tout après le dernier séparateur)
// 3. SKU sans suffixe non-numérique : "30827-dessus" → "30827"
const withoutSuffix = ref.replace(/[-_ ][^-_ ]*$/, '')
if (withoutSuffix !== ref) {
if (withoutSuffix !== ref && withoutSuffix !== withoutNumericSuffix) {
result = await searchBySku(withoutSuffix)
if (result.found) return result
}