diff --git a/src/lib/server/collection-confirm-service.test.ts b/src/lib/server/collection-confirm-service.test.ts index 1691168..c43580b 100644 --- a/src/lib/server/collection-confirm-service.test.ts +++ b/src/lib/server/collection-confirm-service.test.ts @@ -350,11 +350,9 @@ describe("updateOrderItemCost", () => { expect(typeof entry.ts).toBe("string"); // Returned row carries the new cost price (mock resolution merges - // the patch into the existing row). `ConfirmRow` is the consumer- - // facing shape and omits `cost_price` (the raw PB record has it), - // so cast to read the field the test is asserting on. + // the patch into the existing row). expect(result.id).toBe("row-1"); - expect((result as { cost_price?: number }).cost_price).toBe(50); + expect(result.cost_price).toBe(50); }); it("is a no-op when the new cost equals the current cost (no write, no note)", async () => { @@ -412,6 +410,28 @@ describe("updateOrderItemCost", () => { expect(parsed[1].actorName).toBe("sj"); expect(parsed[1].body).toEqual({ prevCost: 45, newCost: 50 }); }); + + it("propagates errors from pb.collection.getOne", async () => { + const failingGetOne = vi.fn().mockRejectedValue(new Error("not found")); + const dummyUpdate = vi.fn(); + const admin = { + collection: vi.fn().mockReturnValue({ + getOne: failingGetOne, + update: dummyUpdate, + }), + }; + vi.mocked(getAdminPocketBase).mockResolvedValue(admin as never); + + await expect( + updateOrderItemCost("row-1", { + cost_price: 50, + actor: { id: "u-1", name: "sj" }, + }), + ).rejects.toThrow("not found"); + + expect(failingGetOne).toHaveBeenCalledWith("row-1"); + expect(dummyUpdate).not.toHaveBeenCalled(); + }); }); describe("stubs for plan task 5", () => { diff --git a/src/lib/server/collection-confirm-service.ts b/src/lib/server/collection-confirm-service.ts index 4c3f22b..c5fda16 100644 --- a/src/lib/server/collection-confirm-service.ts +++ b/src/lib/server/collection-confirm-service.ts @@ -33,6 +33,7 @@ export type ConfirmRow = { order_qty?: number; qty_collected?: number; quantity?: number; + cost_price?: number | null; item_status?: string; problem_flag?: boolean | null; problem_type?: string | null; @@ -227,24 +228,24 @@ export async function listCollectedItems(): Promise { */ export async function updateOrderItemCost( id: string, - body: { cost_price: number; actor: ConfirmActor }, + args: { cost_price: number; actor: ConfirmActor }, ): Promise { const pb = await getAdminPocketBase(); const collectionName = getOrderItemsCollectionName(); const existing = await pb.collection(collectionName).getOne(id); const prevCost = (existing as { cost_price?: number | null }).cost_price ?? null; - if (prevCost === body.cost_price) { + if (prevCost === args.cost_price) { return existing as ConfirmRow; } const newNotes = appendCostNote( (existing as { notes?: string | null }).notes ?? null, - body.actor, + args.actor, prevCost ?? 0, - body.cost_price, + args.cost_price, ); const updated = await pb .collection(collectionName) - .update(id, { cost_price: body.cost_price, notes: newNotes }); + .update(id, { cost_price: args.cost_price, notes: newNotes }); return updated as ConfirmRow; }