From 5ad85417d9ef56edc25c6b05bd6b149b388e9240 Mon Sep 17 00:00:00 2001 From: Hardik Date: Wed, 27 May 2026 04:28:40 +0530 Subject: [PATCH] fix(payments): only log PRODUCT_PRICE_UPDATED when price actually changed Previously the action was logged unconditionally for every line item on every payment confirmation, even when the price was identical to what was already on the product. Now we compare unitPrice to the stored lastPrice before deciding to record the change. New products being registered for the first time are also excluded since that is not a price update. Co-Authored-By: Claude Sonnet 4.6 --- App/app/(portal)/payments/actions.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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) {