feat(import): upsert ProductVendorPrice from imported PO line items

For each line item with a price and a resolved vendor, upsert a
ProductVendorPrice record (productId + vendorId → price). This keeps
the per-vendor price list in the item catalogue up to date from imports.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hardik 2026-05-31 06:07:59 +05:30
parent 0d6df57a88
commit 734f96107f

View file

@ -88,13 +88,13 @@ export async function importPo(
let productId: string | undefined;
if (existing) {
productId = existing.id;
// Update lastPrice if we have a better price
// Update lastPrice / lastVendor on the product
if (item.unitPrice > 0) {
await db.product.update({
where: { id: existing.id },
data: {
lastPrice: item.unitPrice,
lastVendorId: resolvedVendorId ?? undefined,
...(resolvedVendorId ? { lastVendorId: resolvedVendorId } : {}),
},
});
}
@ -113,6 +113,15 @@ export async function importPo(
productId = newProduct.id;
}
// Upsert per-vendor price so the item catalogue reflects actual invoice prices
if (productId && resolvedVendorId && item.unitPrice > 0) {
await db.productVendorPrice.upsert({
where: { productId_vendorId: { productId, vendorId: resolvedVendorId } },
update: { price: item.unitPrice },
create: { productId, vendorId: resolvedVendorId, price: item.unitPrice },
});
}
resolvedLineItems.push({ ...item, productId });
}