diff --git a/src/hooks/useImages.ts b/src/hooks/useImages.ts index 1505045..8a1275e 100644 --- a/src/hooks/useImages.ts +++ b/src/hooks/useImages.ts @@ -276,15 +276,28 @@ export function useImages() { try { const base64 = await blobToBase64(item.processedBlob) - // Utilise le nom de fichier original (sans extension) pour distinguer les images multiples - // ex: "32263.heic" → "32263.jpg", "32263-.heic" → "32263-.jpg" const basename = item.filename.split('/').pop()?.replace(/\.[^.]+$/, '') ?? item.ref const filename = `${basename}.jpg` - const res = await fetch('/api/images/upload', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ shopifyProductId: item.shopifyProductId, imageBase64: base64, filename }), - }) + + const doUpload = async (shopifyProductId: string) => + fetch('/api/images/upload', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ shopifyProductId, imageBase64: base64, filename }), + }) + + let res = await doUpload(item.shopifyProductId) + + // 404 = ID périmé (produit supprimé/recréé) → relancer la recherche par ref + if (res.status === 404) { + const searchRes = await fetch(`/api/products/search?ref=${encodeURIComponent(item.ref)}`) + const searchData = await searchRes.json() + if (searchData.found && searchData.shopifyId) { + update(id, { shopifyProductId: searchData.shopifyId, shopifyProductTitle: searchData.title }) + res = await doUpload(searchData.shopifyId) + } + } + const data = await res.json() if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`) update(id, { status: data.skipped ? 'skipped' : 'uploaded', uploadedUrl: data.url })