diff --git a/src/lib/server/item-history-service.test.ts b/src/lib/server/item-history-service.test.ts index 2895e9b..ec0e533 100644 --- a/src/lib/server/item-history-service.test.ts +++ b/src/lib/server/item-history-service.test.ts @@ -179,7 +179,7 @@ describe("getItemHistory service", () => { ], }; const lastSold = { items: [] }; - const stock = { items: [{ sku: "DRL-6MM", in_stock: 240, status: "in_stock" }] }; + const stock = { items: [{ sku: "DRL-6MM", stock_qty: 240 }] }; const targetItem = { id: "qi-target", sku: "DRL-6MM", @@ -205,7 +205,7 @@ describe("getItemHistory service", () => { getList: vi.fn().mockResolvedValue(ordersBySupplier), }; } - if (name === "stock_purchase_items") { + if (name === "pos") { return { getList: vi.fn().mockResolvedValue(stock), }; diff --git a/src/lib/server/item-history-service.ts b/src/lib/server/item-history-service.ts index 427fe47..198c058 100644 --- a/src/lib/server/item-history-service.ts +++ b/src/lib/server/item-history-service.ts @@ -152,7 +152,7 @@ export async function getItemHistory(itemId: string): Promise { // 2. Fan out 5 PB reads in parallel: order_items by SKU, quote_items by SKU // (other quotes), order_items delivered by SKU (last sold), - // stock_purchase_items by SKU (stock), suppliers (for name enrichment). + // pos by SKU (stock), suppliers (for name enrichment). const [orderItems, quoteItems, lastSoldRows, stockRows, suppliersRows] = await Promise.all([ target.sku ? pb.collection("order_items").getList<{ @@ -188,12 +188,11 @@ export async function getItemHistory(itemId: string): Promise { }) : Promise.resolve({ items: [] as Array<{ id: string; selling_price: number; updated: string; order: string; expand?: { order?: { order_number: string; company?: string } } }> }), target.sku - ? pb.collection("stock_purchase_items").getList<{ + ? pb.collection("pos").getList<{ sku: string; - in_stock: number; - status: string; + stock_qty: number; }>(1, 50, { filter: `sku = "${target.sku.replace(/"/g, '\\"')}"` }) - : Promise.resolve({ items: [] as Array<{ sku: string; in_stock: number; status: string }> }), + : Promise.resolve({ items: [] as Array<{ sku: string; stock_qty: number }> }), // Suppliers list — capped at 200, full names for any ID we encounter. pb.collection("suppliers").getList<{ id: string; suppliername: string }>(1, 200), ]); @@ -230,12 +229,12 @@ export async function getItemHistory(itemId: string): Promise { } : null; - // 6. Stock. + // 6. Stock. `pos.stock_qty` is a number — derive `status` from it. const stockRow = stockRows.items[0]; const stock: ItemHistoryStock | null = stockRow ? { - in_stock: stockRow.in_stock, - status: stockRow.status as ItemHistoryStock["status"], + in_stock: stockRow.stock_qty, + status: stockRow.stock_qty > 0 ? "in_stock" : "out", } : null;