From 98e1f25fd597ea58dd5de1c84cfb4a4cf12a72e5 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Thu, 30 Jul 2026 23:14:11 +0200 Subject: [PATCH] fix(stock-quick-draft): supplier-name schema + remove dead resolvePb + pagination cap Co-authored-by: Orca --- .../server/stock-quick-draft-service.test.ts | 11 ++++------- src/lib/server/stock-quick-draft-service.ts | 19 +++++++++---------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/lib/server/stock-quick-draft-service.test.ts b/src/lib/server/stock-quick-draft-service.test.ts index 57cd259..2b86d94 100644 --- a/src/lib/server/stock-quick-draft-service.test.ts +++ b/src/lib/server/stock-quick-draft-service.test.ts @@ -155,9 +155,6 @@ const { store, pbMock, clearStore } = vi.hoisted(() => { }); vi.mock("pocketbase", () => ({ default: vi.fn().mockImplementation(() => pbMock) })); -vi.mock("./pocketbase-admin", () => ({ - getAdminPocketBase: vi.fn().mockResolvedValue(pbMock), -})); import { addQuickDraftLine, @@ -629,10 +626,10 @@ describe("listOpenDrafts", () => { it("returns draft purchases with supplier name + lines denormalised, sorted by created desc", async () => { const supplierA = ( - await pbMock.collection("suppliers").create({ name: "Steel & Pipe Co" }) + await pbMock.collection("suppliers").create({ suppliername: "Steel & Pipe Co" }) ).id; const supplierB = ( - await pbMock.collection("suppliers").create({ name: "Hardware Hub" }) + await pbMock.collection("suppliers").create({ suppliername: "Hardware Hub" }) ).id; // First (older) draft. @@ -703,7 +700,7 @@ describe("listOpenDrafts", () => { it("ignores non-draft stock_purchases in the result", async () => { const supplierId = ( - await pbMock.collection("suppliers").create({ name: "S" }) + await pbMock.collection("suppliers").create({ suppliername: "S" }) ).id; // Sent and received rows must NOT appear in the drafts list. await pbMock.collection("stock_purchases").create({ @@ -726,7 +723,7 @@ describe("listOpenDrafts", () => { it("returns an empty lines array for a draft with no child items", async () => { const supplierId = ( - await pbMock.collection("suppliers").create({ name: "Hardware Hub" }) + await pbMock.collection("suppliers").create({ suppliername: "Hardware Hub" }) ).id; // A brand-new draft with no lines yet (possible if the row was // created by the agent, not by the modal — or after a mass delete). diff --git a/src/lib/server/stock-quick-draft-service.ts b/src/lib/server/stock-quick-draft-service.ts index 58ac7aa..57b3c08 100644 --- a/src/lib/server/stock-quick-draft-service.ts +++ b/src/lib/server/stock-quick-draft-service.ts @@ -7,7 +7,6 @@ * statuses. Admin PB only (consistent with the other service modules). */ import type PocketBase from "pocketbase"; -import { getAdminPocketBase } from "./pocketbase-admin"; import { generateStockPurchaseNumber } from "./stock-purchases-service"; const STOCK_ITEMS = "stock_items"; @@ -134,11 +133,6 @@ function escapePbFilterString(s: string): string { return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); } -async function resolvePb(pb?: StockQuickDraftPb): Promise { - if (pb) return pb; - return (await getAdminPocketBase()) as unknown as StockQuickDraftPb; -} - /* ============================================================================ * searchStockItemsForQuickDraft * ========================================================================== */ @@ -522,11 +516,17 @@ export async function removeQuickDraftLine( * * Returns the summary array sorted by `created` desc. Empty when * no drafts exist (the modal renders an empty-state on length 0). + * + * NOTE: capped at `perPage=200` because the Quick Draft modal does not + * paginate. If a buyer ever accumulates more than 200 open drafts the + * oldest ones are silently truncated — at that point consider adding + * pagination to the modal or filtering by date. */ export async function listOpenDrafts( pb: StockQuickDraftPb, ): Promise { - const result = await pb.collection(STOCK_PURCHASES).getList(1, 200, { + const perPage = 200; // Upper bound for the modal's "open drafts" view; modal does not paginate. + const result = await pb.collection(STOCK_PURCHASES).getList(1, perPage, { filter: `status = "draft"`, sort: "-created", expand: "supplier", @@ -577,10 +577,9 @@ export async function listOpenDrafts( } return purchases.map((row) => { - type ExpandedSupplier = { id: string; name?: string; suppliername?: string }; + type ExpandedSupplier = { id: string; suppliername?: string }; const expand = row.expand as { supplier?: ExpandedSupplier } | undefined; - const supplierName = - expand?.supplier?.name ?? expand?.supplier?.suppliername ?? ""; + const supplierName = String(expand?.supplier?.suppliername ?? ""); return { id: String(row.id),