From 15f6ec638d4d7871140b1151d2909d7864bdc6c7 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Mon, 3 Aug 2026 00:12:02 +0200 Subject: [PATCH] fix(collection-confirm): make listCollectedItems stock leg fail-soft Co-authored-by: Orca --- .../server/collection-confirm-service.test.ts | 41 +++++++++++++++++++ src/lib/server/collection-confirm-service.ts | 5 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/lib/server/collection-confirm-service.test.ts b/src/lib/server/collection-confirm-service.test.ts index dea7f60..38cbb9b 100644 --- a/src/lib/server/collection-confirm-service.test.ts +++ b/src/lib/server/collection-confirm-service.test.ts @@ -220,6 +220,47 @@ describe("listCollectedItems", () => { const sources = rows.map((r) => r.source).sort(); expect(sources).toEqual(["customer", "stock"]); }); + + it("returns order_items rows when stock_purchase_items leg throws", async () => { + // Mirrors the page's `load()` fail-soft behaviour at page.tsx:294: + // the stock leg is wrapped in `.catch(() => [])` so a stock-side + // outage doesn't kill the customer-leg results. + const orderItem = { + id: "oi-1", + item_status: "collected", + description: "M20 × 40 hex nuts", + order_qty: 100, + qty_collected: 100, + expand: { + purchase_order: { id: "po-1", po_number: "PO-1" }, + order: { id: "o-1" }, + }, + }; + const orderItemsGetFullList = vi.fn().mockResolvedValue([orderItem]); + const stockItemsGetFullList = vi + .fn() + .mockRejectedValue(new Error("stock side down")); + const admin = { + collection: vi.fn().mockImplementation((name: string) => { + if (name === "order_items") { + return { getFullList: orderItemsGetFullList }; + } + if (name === "stock_purchase_items") { + return { getFullList: stockItemsGetFullList }; + } + throw new Error(`unexpected collection ${name}`); + }), + }; + vi.mocked(getAdminPocketBase).mockResolvedValue(admin as never); + + const rows = await listCollectedItems(); + + expect(rows).toHaveLength(1); + const row = rows[0]!; + expect(row.id).toBe("oi-1"); + expect(row.source).toBe("customer"); + expect(stockItemsGetFullList).toHaveBeenCalledTimes(1); + }); }); describe("stubs for plan tasks 4 and 5", () => { diff --git a/src/lib/server/collection-confirm-service.ts b/src/lib/server/collection-confirm-service.ts index 4b724be..1635176 100644 --- a/src/lib/server/collection-confirm-service.ts +++ b/src/lib/server/collection-confirm-service.ts @@ -134,7 +134,10 @@ export async function listCollectedItems(): Promise { filter: 'status = "collected"', sort: "-created", expand: "purchase.supplier,purchase,stock_item", - }), + }) + // Fail-soft: matches the page's `load()` at page.tsx:294 — a + // stock-side outage must not wipe the customer-deliveries view. + .catch(() => [] as unknown[]), ]); // Normalise stock rows into ConfirmRow shape. Stock fields that don't