fix: retry upload with fresh Shopify ID on 404 (stale cached ID)
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 <noreply@anthropic.com>
This commit is contained in:
+20
-7
@@ -276,15 +276,28 @@ export function useImages() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const base64 = await blobToBase64(item.processedBlob)
|
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 basename = item.filename.split('/').pop()?.replace(/\.[^.]+$/, '') ?? item.ref
|
||||||
const filename = `${basename}.jpg`
|
const filename = `${basename}.jpg`
|
||||||
const res = await fetch('/api/images/upload', {
|
|
||||||
method: 'POST',
|
const doUpload = async (shopifyProductId: string) =>
|
||||||
headers: { 'Content-Type': 'application/json' },
|
fetch('/api/images/upload', {
|
||||||
body: JSON.stringify({ shopifyProductId: item.shopifyProductId, imageBase64: base64, filename }),
|
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()
|
const data = await res.json()
|
||||||
if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`)
|
if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`)
|
||||||
update(id, { status: data.skipped ? 'skipped' : 'uploaded', uploadedUrl: data.url })
|
update(id, { status: data.skipped ? 'skipped' : 'uploaded', uploadedUrl: data.url })
|
||||||
|
|||||||
Reference in New Issue
Block a user