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:
@@ -32,6 +32,10 @@ export async function GET(req: NextRequest) {
|
|||||||
s => s.properties.title,
|
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) {
|
for (const tab of tabs) {
|
||||||
const range = encodeURIComponent(`${tab}!A2:D`)
|
const range = encodeURIComponent(`${tab}!A2:D`)
|
||||||
const res = await fetch(
|
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 wordBoundary = new RegExp(`(^|[^a-z0-9])${ugsLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([^a-z0-9]|$)`)
|
||||||
const isMatch = ugs && shopifyId && wordBoundary.test(refLower)
|
const isMatch = ugs && shopifyId && wordBoundary.test(refLower)
|
||||||
|
|
||||||
if (isMatch) {
|
if (isMatch && (!bestMatch || ugs.length > bestMatch.ugsLength)) {
|
||||||
const title = (row[3] ?? '').trim() // col D = Nom
|
bestMatch = { shopifyId, title: (row[3] ?? '').trim(), ugsLength: ugs.length }
|
||||||
return NextResponse.json({ found: true, shopifyId, title })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bestMatch) {
|
||||||
|
return NextResponse.json({ found: true, shopifyId: bestMatch.shopifyId, title: bestMatch.title })
|
||||||
|
}
|
||||||
return NextResponse.json({ found: false })
|
return NextResponse.json({ found: false })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : 'Erreur'
|
const message = err instanceof Error ? err.message : 'Erreur'
|
||||||
|
|||||||
Reference in New Issue
Block a user