fix: vérifier l'existence Shopify avant création pour éviter les doublons
- Chargement du catalogue Shopify au début de l'exécution (fetchAllShopifyProducts) - Vérification par SKU (ref) : si déjà présent dans Shopify → skipped, pas de création - Nouveau type d'événement 'skipped' dans CreationStreamEvent - CreationProgress affiche les produits ignorés en amber avec le SKU concerné - Résumé final inclut le compte de produits ignorés Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@ import { fetchPendingProducts } from '@/lib/creationSheets'
|
||||
import { generateDescriptions } from '@/lib/openaiClient'
|
||||
import { ensureMetafieldDefinition, setProductMetafield } from '@/lib/shopifyMetafields'
|
||||
import { addProductToCollection } from '@/lib/shopifyCollections'
|
||||
import { createShopifyProduct } from '@/lib/shopifySync'
|
||||
import { createShopifyProduct, fetchAllShopifyProducts } from '@/lib/shopifySync'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import type { MetafieldResolution, CreationStreamEvent } from '@/types/creation'
|
||||
|
||||
@@ -28,13 +28,17 @@ export async function POST(req: NextRequest) {
|
||||
async start(controller) {
|
||||
const send = (event: CreationStreamEvent) => controller.enqueue(encode(event))
|
||||
try {
|
||||
const existing = await prisma.productCreation.findMany({ select: { sheetTab: true, sheetRow: true } })
|
||||
const alreadyCreated = new Set(existing.map(e => `${e.sheetTab}:${e.sheetRow}`))
|
||||
const [existingRows, shopifyProducts] = await Promise.all([
|
||||
prisma.productCreation.findMany({ select: { sheetTab: true, sheetRow: true } }),
|
||||
fetchAllShopifyProducts(),
|
||||
])
|
||||
const alreadyCreated = new Set(existingRows.map(e => `${e.sheetTab}:${e.sheetRow}`))
|
||||
const shopifyRefs = new Set(shopifyProducts.map(p => p.ref.trim().toLowerCase()))
|
||||
const pending = await fetchPendingProducts(alreadyCreated)
|
||||
const total = pending.length
|
||||
|
||||
if (total === 0) {
|
||||
send({ type: 'done', summary: { created: 0, errors: 0, status: 'success' } })
|
||||
send({ type: 'done', summary: { created: 0, skipped: 0, errors: 0, status: 'success' } })
|
||||
controller.close()
|
||||
return
|
||||
}
|
||||
@@ -45,10 +49,19 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
let created = 0
|
||||
let skipped = 0
|
||||
let errors = 0
|
||||
|
||||
for (let i = 0; i < pending.length; i++) {
|
||||
const product = pending[i]
|
||||
|
||||
// Vérifier si le produit existe déjà dans Shopify par SKU
|
||||
if (shopifyRefs.has(product.sku.trim().toLowerCase())) {
|
||||
send({ type: 'skipped', title: product.title, sku: product.sku })
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
send({ type: 'progress', current: i + 1, total, tab: product.tab, title: product.title, step: 'Génération descriptions…' })
|
||||
try {
|
||||
const { longDesc } = await generateDescriptions({
|
||||
@@ -90,10 +103,10 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
}
|
||||
const status = errors === 0 ? 'success' : created === 0 ? 'error' : 'partial'
|
||||
send({ type: 'done', summary: { created, errors, status } })
|
||||
send({ type: 'done', summary: { created, skipped, errors, status } })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
||||
send({ type: 'done', summary: { created: 0, errors: 1, status: 'error' } })
|
||||
send({ type: 'done', summary: { created: 0, skipped: 0, errors: 1, status: 'error' } })
|
||||
console.error('[creation/execute]', message)
|
||||
} finally {
|
||||
controller.close()
|
||||
|
||||
Reference in New Issue
Block a user