fix(collection-confirm): add cost_price to ConfirmRow, rename body to args

Co-Authored-By: Claude <noreply@anthropic.com>

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
SJCoetz 2026-08-03 00:29:56 +02:00
parent 2997b8dcba
commit 77d2c32df4
2 changed files with 30 additions and 9 deletions

View file

@ -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", () => {

View file

@ -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<ConfirmRow[]> {
*/
export async function updateOrderItemCost(
id: string,
body: { cost_price: number; actor: ConfirmActor },
args: { cost_price: number; actor: ConfirmActor },
): Promise<ConfirmRow> {
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;
}