fix(item-history): query pos collection for stock_qty (was stock_purchase_items, which doesn't exist)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
parent
52fd7fa303
commit
3f8836d981
2 changed files with 9 additions and 10 deletions
|
|
@ -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),
|
||||
};
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ export async function getItemHistory(itemId: string): Promise<ItemHistory> {
|
|||
|
||||
// 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<ItemHistory> {
|
|||
})
|
||||
: 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<ItemHistory> {
|
|||
}
|
||||
: 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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue