feat: vérification image existante sur Shopify avant upload (statut 'Déjà sur Shopify')

This commit is contained in:
2026-06-11 13:35:47 +02:00
parent 4df074f02d
commit 725a7508b0
4 changed files with 30 additions and 2 deletions
+27 -1
View File
@@ -85,13 +85,39 @@ export async function searchProductByRef(ref: string): Promise<ShopifyProductRes
* @param imageBase64 — contenu JPEG encodé en base64 (sans le préfixe data:)
* @param filename — nom de fichier avec extension (ex: "REF-1042.jpg")
*/
/**
* Vérifie si une image avec ce nom de fichier existe déjà sur le produit.
* Compare le nom de base (sans extension) avec les src des images existantes.
*/
export async function productImageExists(
shopifyProductId: string,
filename: string,
): Promise<string | null> {
const { baseUrl, headers } = getConfig()
const res = await fetch(`${baseUrl}/products/${shopifyProductId}/images.json?fields=id,src`, { headers })
if (!res.ok) return null
const data = await res.json()
const images = (data.images ?? []) as Array<{ id: number; src: string }>
// Compare le nom de base sans extension (ex: "30827" dans "30827.jpg")
const baseName = filename.replace(/\.[^.]+$/, '').toLowerCase()
const existing = images.find(img => {
const imgFile = img.src.split('/').pop()?.split('?')[0] ?? ''
return imgFile.replace(/\.[^.]+$/, '').toLowerCase() === baseName
})
return existing ? existing.src : null
}
export async function uploadProductImage(
shopifyProductId: string,
imageBase64: string,
filename: string,
): Promise<{ url: string }> {
): Promise<{ url: string; skipped?: boolean }> {
const { baseUrl, headers } = getConfig()
// Vérifie si l'image existe déjà
const existingUrl = await productImageExists(shopifyProductId, filename)
if (existingUrl) return { url: existingUrl, skipped: true }
const res = await fetch(`${baseUrl}/products/${shopifyProductId}/images.json`, {
method: 'POST',
headers,