From 8ede847ba070accb92bce5112035a45dd7867bf4 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 9 Jun 2026 13:41:53 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20recherche=20produit=20via=20Google=20She?= =?UTF-8?q?ets=20(UGS=20col=20C=20=E2=86=92=20Shopify=20ID=20col=20A)=20+?= =?UTF-8?q?=20fallback=20Shopify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/products/search-sheets/route.ts | 61 +++++++++++++++++++++ src/hooks/useImages.ts | 20 +++++-- 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/app/api/products/search-sheets/route.ts diff --git a/src/app/api/products/search-sheets/route.ts b/src/app/api/products/search-sheets/route.ts new file mode 100644 index 0000000..8ddba3e --- /dev/null +++ b/src/app/api/products/search-sheets/route.ts @@ -0,0 +1,61 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getServerSession } from 'next-auth' +import { authOptions } from '@/lib/auth' + +/** + * Cherche un produit dans Google Sheets par sa référence (UGS, col C). + * Retourne le Shopify ID (col A) et le titre (col D) si trouvé. + * Parcourt tous les onglets. + */ +export async function GET(req: NextRequest) { + const session = await getServerSession(authOptions) + if (!session) return NextResponse.json({ found: false }, { status: 401 }) + + const { searchParams } = new URL(req.url) + const ref = searchParams.get('ref')?.trim() + if (!ref) return NextResponse.json({ found: false }, { status: 400 }) + + const apiKey = process.env.GOOGLE_API_KEY + const sheetId = process.env.GOOGLE_SHEETS_ID + if (!apiKey || !sheetId) { + return NextResponse.json({ found: false, error: 'GOOGLE_API_KEY et GOOGLE_SHEETS_ID requis' }, { status: 500 }) + } + + try { + // Récupère la liste des onglets + const metaRes = await fetch( + `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}?key=${apiKey}&fields=sheets.properties.title`, + ) + if (!metaRes.ok) throw new Error(`Sheets metadata error: ${metaRes.status}`) + const meta = await metaRes.json() + const tabs: string[] = (meta.sheets as Array<{ properties: { title: string } }>).map( + s => s.properties.title, + ) + + for (const tab of tabs) { + const range = encodeURIComponent(`${tab}!A2:D`) + const res = await fetch( + `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?key=${apiKey}`, + ) + if (!res.ok) continue + const data = await res.json() + const rows = (data.values ?? []) as string[][] + + for (const row of rows) { + const shopifyId = (row[0] ?? '').trim() // col A = ID Shopify + // col C = UGS (index 2 car on commence à A) + const ugs = (row[2] ?? '').trim() + + if (ugs.toLowerCase() === ref.toLowerCase() && shopifyId) { + const title = (row[3] ?? '').trim() // col D = Nom + return NextResponse.json({ found: true, shopifyId, title }) + } + } + } + + return NextResponse.json({ found: false }) + } catch (err) { + const message = err instanceof Error ? err.message : 'Erreur' + return NextResponse.json({ found: false, error: message }, { status: 500 }) + } +} diff --git a/src/hooks/useImages.ts b/src/hooks/useImages.ts index c80116f..c40500a 100644 --- a/src/hooks/useImages.ts +++ b/src/hooks/useImages.ts @@ -57,6 +57,19 @@ export function useImages() { async (id: string, ref: string) => { update(id, { status: 'searching' }) try { + // 1. Cherche d'abord dans Google Sheets (col C = UGS → col A = Shopify ID) + const sheetsRes = await fetch(`/api/products/search-sheets?ref=${encodeURIComponent(ref)}`) + const sheetsData = await sheetsRes.json() + if (sheetsData.found) { + update(id, { + status: 'ready', + shopifyProductId: sheetsData.shopifyId, + shopifyProductTitle: sheetsData.title, + }) + return + } + + // 2. Fallback : recherche directe dans Shopify par handle/titre const res = await fetch(`/api/products/search?ref=${encodeURIComponent(ref)}`) const data = await res.json() if (data.found) { @@ -88,11 +101,8 @@ export function useImages() { try { update(id, { status: 'converting' }) jpegBlob = await convertHeicToJpeg(blob) - // Met à jour l'aperçu original avec le JPEG converti (Chrome ne peut pas afficher HEIC) - update(id, { - originalBlob: jpegBlob, - originalUrl: URL.createObjectURL(jpegBlob), - }) + // Met à jour originalBlob avec le JPEG converti (Chrome ne peut pas afficher HEIC) + update(id, { originalBlob: jpegBlob }) } catch (err) { update(id, { status: 'error', error: `Conversion HEIC : ${(err as Error).message}` }) return