fix: recherche produit via Google Sheets (UGS col C → Shopify ID col A) + fallback Shopify
This commit is contained in:
@@ -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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-5
@@ -57,6 +57,19 @@ export function useImages() {
|
|||||||
async (id: string, ref: string) => {
|
async (id: string, ref: string) => {
|
||||||
update(id, { status: 'searching' })
|
update(id, { status: 'searching' })
|
||||||
try {
|
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 res = await fetch(`/api/products/search?ref=${encodeURIComponent(ref)}`)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
if (data.found) {
|
if (data.found) {
|
||||||
@@ -88,11 +101,8 @@ export function useImages() {
|
|||||||
try {
|
try {
|
||||||
update(id, { status: 'converting' })
|
update(id, { status: 'converting' })
|
||||||
jpegBlob = await convertHeicToJpeg(blob)
|
jpegBlob = await convertHeicToJpeg(blob)
|
||||||
// Met à jour l'aperçu original avec le JPEG converti (Chrome ne peut pas afficher HEIC)
|
// Met à jour originalBlob avec le JPEG converti (Chrome ne peut pas afficher HEIC)
|
||||||
update(id, {
|
update(id, { originalBlob: jpegBlob })
|
||||||
originalBlob: jpegBlob,
|
|
||||||
originalUrl: URL.createObjectURL(jpegBlob),
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
update(id, { status: 'error', error: `Conversion HEIC : ${(err as Error).message}` })
|
update(id, { status: 'error', error: `Conversion HEIC : ${(err as Error).message}` })
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user