refactor(purchasing): include orphan IDs in console.warn, single rollup pass

This commit is contained in:
DEls 2026-08-27 00:15:39 +02:00
parent cb17760e5a
commit e3c2e0396d

View file

@ -48,30 +48,26 @@ export default async function PurchasingPage() {
// Sum received_items.qty_received per PO item so we can compute
// per-PO fulfilment % from authoritative data instead of the stale
// denormalized field on purchase_order_items.
// denormalized field on purchase_order_items. Rows without a
// purchase_order_item are collected as orphans (they can't roll up to
// any PO) and surfaced with their IDs so a dev can query PB directly.
const receivedByItem = new Map<string, number>();
const orphanIds: string[] = [];
for (const r of receivedAll) {
const k = String(
(r as { purchase_order_item?: string }).purchase_order_item ?? "",
);
if (!k) continue;
const poi = (r as { purchase_order_item?: string }).purchase_order_item;
if (!poi) {
orphanIds.push(String((r as { id?: string }).id ?? ""));
continue;
}
receivedByItem.set(
k,
(receivedByItem.get(k) ?? 0) +
poi,
(receivedByItem.get(poi) ?? 0) +
((r as { qty_received?: number }).qty_received ?? 0),
);
}
// Surface orphan received_items rows: those without a
// purchase_order_item can't roll up to any PO and silently disappear
// from fulfilment. Logged in dev so the issue is visible; production
// consoles can ignore this.
const orphanCount = receivedAll.filter(
(r) => !(r as { purchase_order_item?: string }).purchase_order_item,
).length;
if (orphanCount > 0) {
if (orphanIds.length > 0) {
console.warn(
`[purchasing] ${orphanCount} received_items row(s) have empty purchase_order_item and are excluded from fulfilment.`,
`[purchasing] ${orphanIds.length} received_items row(s) have empty purchase_order_item and are excluded from fulfilment. ids=${orphanIds.join(",")}`,
);
}