From 5e1b864ce73012f4930e4e8f0c1e0f86d14d4ea1 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Wed, 24 Jun 2026 20:35:43 +0200 Subject: [PATCH] fix: retry upload with fresh Shopify ID on 404 (stale cached ID) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quand l'ID en cache est périmé (produit supprimé/recréé), relance la recherche par ref pour obtenir le nouvel ID et réessaie l'upload automatiquement. Co-Authored-By: Claude Sonnet 4.6 --- src/hooks/useImages.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) 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 })