fix: retourner l'UGS la plus spécifique lors de la recherche par nom de fichier

Évite que "26618" matche avant "26618-D" car le tiret est un séparateur
de mot dans la regex — on collecte tous les matches et on garde le plus long.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-19 11:02:13 +02:00
parent d8a3f26d0b
commit c1fad5d0ed
+9 -3
View File
@@ -32,6 +32,10 @@ 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) {
const range = encodeURIComponent(`${tab}!A2:D`)
const res = await fetch(
@@ -53,13 +57,15 @@ export async function GET(req: NextRequest) {
const wordBoundary = new RegExp(`(^|[^a-z0-9])${ugsLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([^a-z0-9]|$)`)
const isMatch = ugs && shopifyId && wordBoundary.test(refLower)
if (isMatch) {
const title = (row[3] ?? '').trim() // col D = Nom
return NextResponse.json({ found: true, shopifyId, title })
if (isMatch && (!bestMatch || ugs.length > bestMatch.ugsLength)) {
bestMatch = { shopifyId, title: (row[3] ?? '').trim(), ugsLength: ugs.length }
}
}
}
if (bestMatch) {
return NextResponse.json({ found: true, shopifyId: bestMatch.shopifyId, title: bestMatch.title })
}
return NextResponse.json({ found: false })
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur'