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 <noreply@anthropic.com>
This commit is contained in:
parent
d4007ee222
commit
5ad85417d9
1 changed files with 11 additions and 3 deletions
|
|
@ -31,18 +31,20 @@ async function syncProductCatalog(
|
||||||
for (const li of lineItems) {
|
for (const li of lineItems) {
|
||||||
const unitPrice = typeof li.unitPrice === "number" ? li.unitPrice : li.unitPrice.toNumber();
|
const unitPrice = typeof li.unitPrice === "number" ? li.unitPrice : li.unitPrice.toNumber();
|
||||||
let productId = li.productId;
|
let productId = li.productId;
|
||||||
|
let priceChanged = false;
|
||||||
|
|
||||||
if (!productId) {
|
if (!productId) {
|
||||||
// Try to find an existing product by name (case-insensitive)
|
// Try to find an existing product by name (case-insensitive)
|
||||||
const existing = await db.product.findFirst({
|
const existing = await db.product.findFirst({
|
||||||
where: { name: { equals: li.name, mode: "insensitive" }, isActive: true },
|
where: { name: { equals: li.name, mode: "insensitive" }, isActive: true },
|
||||||
select: { id: true },
|
select: { id: true, lastPrice: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
productId = existing.id;
|
productId = existing.id;
|
||||||
|
priceChanged = Number(existing.lastPrice ?? 0) !== unitPrice;
|
||||||
} else {
|
} else {
|
||||||
// Create a new product
|
// Create a new product — first-time registration, not a price update
|
||||||
const code = nameToCode(li.name);
|
const code = nameToCode(li.name);
|
||||||
try {
|
try {
|
||||||
const created = await db.product.create({
|
const created = await db.product.create({
|
||||||
|
|
@ -65,6 +67,12 @@ async function syncProductCatalog(
|
||||||
|
|
||||||
// Link the line item to the product for future reference
|
// Link the line item to the product for future reference
|
||||||
await db.pOLineItem.update({ where: { id: li.id }, data: { productId } });
|
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
|
// 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) {
|
if (updatedProductIds.length > 0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue