fix: recherche produit par SKU Shopify avec fallback sans suffixe (30827-dessus → 30827)
This commit is contained in:
@@ -47,12 +47,11 @@ export async function GET(req: NextRequest) {
|
||||
const ugs = (row[2] ?? '').trim()
|
||||
|
||||
// Le nom de fichier contient l'UGS (ex: "30827-dessus" contient "30827")
|
||||
// Les UGS étant uniques, un simple contains suffit
|
||||
const refLower = ref.toLowerCase()
|
||||
const ugsLower = ugs.toLowerCase()
|
||||
const isMatch = ugs && refLower.includes(ugsLower)
|
||||
const isMatch = ugs && shopifyId && refLower.includes(ugsLower)
|
||||
|
||||
if (isMatch && shopifyId) {
|
||||
if (isMatch) {
|
||||
const title = (row[3] ?? '').trim() // col D = Nom
|
||||
return NextResponse.json({ found: true, shopifyId, title })
|
||||
}
|
||||
|
||||
+46
-18
@@ -21,34 +21,62 @@ export type ShopifyProductResult =
|
||||
|
||||
/**
|
||||
* Recherche un produit Shopify par sa référence.
|
||||
* Tente d'abord par handle (ex: "ref-1042"), puis par titre exact.
|
||||
* Stratégie : SKU exact → SKU sans suffixe → handle → titre.
|
||||
* Les images peuvent être nommées "30827-dessus.jpg" alors que le SKU est "30827".
|
||||
*/
|
||||
export async function searchProductByRef(ref: string): Promise<ShopifyProductResult> {
|
||||
const { baseUrl, headers } = getConfig()
|
||||
|
||||
// Normalise la référence en handle Shopify (minuscules, tirets)
|
||||
const handle = ref.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')
|
||||
|
||||
const tryFetch = async (params: string) => {
|
||||
const res = await fetch(`${baseUrl}/products.json?${params}&fields=id,title,handle&limit=1`, { headers })
|
||||
if (!res.ok) throw new Error(`Shopify API error: ${res.status}`)
|
||||
// Recherche par SKU via l'API variants
|
||||
const searchBySku = async (sku: string): Promise<ShopifyProductResult> => {
|
||||
const res = await fetch(
|
||||
`${baseUrl}/variants.json?sku=${encodeURIComponent(sku)}&fields=id,sku,product_id&limit=1`,
|
||||
{ headers },
|
||||
)
|
||||
if (!res.ok) return { found: false }
|
||||
const data = await res.json()
|
||||
return (data.products ?? []) as Array<{ id: number; title: string; handle: string }>
|
||||
const variants = (data.variants ?? []) as Array<{ id: number; sku: string; product_id: number }>
|
||||
if (variants.length === 0) return { found: false }
|
||||
|
||||
// Récupère le titre du produit
|
||||
const productRes = await fetch(
|
||||
`${baseUrl}/products/${variants[0].product_id}.json?fields=id,title`,
|
||||
{ headers },
|
||||
)
|
||||
if (!productRes.ok) return { found: false }
|
||||
const productData = await productRes.json()
|
||||
return {
|
||||
found: true,
|
||||
shopifyId: String(variants[0].product_id),
|
||||
title: productData.product?.title ?? sku,
|
||||
}
|
||||
}
|
||||
|
||||
// 1. SKU exact (ex: "30827")
|
||||
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)
|
||||
const withoutSuffix = ref.replace(/[-_ ][^-_ ]*$/, '')
|
||||
if (withoutSuffix !== ref) {
|
||||
result = await searchBySku(withoutSuffix)
|
||||
if (result.found) return result
|
||||
}
|
||||
|
||||
// 3. Fallback handle Shopify
|
||||
const handle = ref.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')
|
||||
const tryFetch = async (params: string) => {
|
||||
const res = await fetch(`${baseUrl}/products.json?${params}&fields=id,title&limit=1`, { headers })
|
||||
if (!res.ok) return []
|
||||
const data = await res.json()
|
||||
return (data.products ?? []) as Array<{ id: number; title: string }>
|
||||
}
|
||||
|
||||
let products = await tryFetch(`handle=${encodeURIComponent(handle)}`)
|
||||
|
||||
if (products.length === 0) {
|
||||
products = await tryFetch(`title=${encodeURIComponent(ref)}`)
|
||||
}
|
||||
|
||||
if (products.length === 0) products = await tryFetch(`title=${encodeURIComponent(ref)}`)
|
||||
if (products.length === 0) return { found: false }
|
||||
|
||||
return {
|
||||
found: true,
|
||||
shopifyId: String(products[0].id),
|
||||
title: products[0].title,
|
||||
}
|
||||
return { found: true, shopifyId: String(products[0].id), title: products[0].title }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user