diff --git a/App/app/(portal)/payments/actions.ts b/App/app/(portal)/payments/actions.ts index 7b3d513..4dc4219 100644 --- a/App/app/(portal)/payments/actions.ts +++ b/App/app/(portal)/payments/actions.ts @@ -31,18 +31,20 @@ async function syncProductCatalog( for (const li of lineItems) { const unitPrice = typeof li.unitPrice === "number" ? li.unitPrice : li.unitPrice.toNumber(); let productId = li.productId; + let priceChanged = false; if (!productId) { // Try to find an existing product by name (case-insensitive) const existing = await db.product.findFirst({ where: { name: { equals: li.name, mode: "insensitive" }, isActive: true }, - select: { id: true }, + select: { id: true, lastPrice: true }, }); if (existing) { productId = existing.id; + priceChanged = Number(existing.lastPrice ?? 0) !== unitPrice; } else { - // Create a new product + // Create a new product — first-time registration, not a price update const code = nameToCode(li.name); try { const created = await db.product.create({ @@ -65,6 +67,12 @@ async function syncProductCatalog( // Link the line item to the product for future reference await db.pOLineItem.update({ where: { id: li.id }, data: { productId } }); + } else { + const current = await db.product.findUnique({ + where: { id: productId }, + select: { lastPrice: true }, + }); + priceChanged = !current || Number(current.lastPrice ?? 0) !== unitPrice; } // Always update lastPrice / lastVendorId on the product @@ -82,7 +90,7 @@ async function syncProductCatalog( }); } - updatedProductIds.push(productId); + if (priceChanged) updatedProductIds.push(productId); } if (updatedProductIds.length > 0) {