fix(stock-quick-draft): supplier-name schema + remove dead resolvePb + pagination cap
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
parent
8acb6dcf95
commit
98e1f25fd5
2 changed files with 13 additions and 17 deletions
|
|
@ -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).
|
||||
|
|
|
|||
|
|
@ -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<StockQuickDraftPb> {
|
||||
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<DraftPurchaseSummary[]> {
|
||||
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),
|
||||
|
|
|
|||
Loading…
Reference in a new issue