import { ok } from "@tailor-platform/erp-kit/core"; import type { BusinessPartnerQueries, InventoryCommands, InventoryQueries, ItemManagementQueries, OrganizationQueries, PrimitivesQueries, } from "../module"; export type Company = NonNullable< Awaited>["value"]["company"] >; export type Site = NonNullable< Awaited>["value"]["site"] >; export type SupplierAccountReference = NonNullable< Awaited>["value"]["account"] >; export type Item = NonNullable< Awaited>["value"]["item"] >; export type Unit = NonNullable>["value"]["unit"]>; export type SupplyPlan = NonNullable< Awaited>["value"]["supplyPlan"] >; export const mockOrgQueries = ( overrides: { company?: Company | null; site?: Site | null } = {}, ): Pick => ({ getCompany: () => Promise.resolve(ok({ company: overrides.company ?? null })), getSite: () => Promise.resolve(ok({ site: overrides.site ?? null })), }); export const mockBPQueries = ( overrides: { account?: SupplierAccountReference | null } = {}, ): Pick => ({ getSupplierAccount: () => Promise.resolve(ok({ account: overrides.account ?? null })), }); export const mockItemQueries = ( overrides: { item?: Item | null } = {}, ): Pick => ({ getItem: () => Promise.resolve(ok({ item: overrides.item ?? null })), }); function buildSupplyPlan(input: Partial = {}): SupplyPlan { return { id: input.id ?? "supply-plan-1", sourceType: input.sourceType ?? "PURCHASE_ORDER", sourceId: input.sourceId ?? "purchase-order-1", sourceLineId: input.sourceLineId ?? "po-line-1", itemId: input.itemId ?? "item-1", siteId: input.siteId ?? "site-1", expectedQuantity: input.expectedQuantity ?? "10", receivedQuantity: input.receivedQuantity ?? "0", status: input.status ?? "OPEN", expectedDate: input.expectedDate ?? new Date("2024-01-01T00:00:00.000Z"), openQuantity: input.openQuantity ?? "0", createdAt: input.createdAt ?? new Date("2024-01-01T00:00:00.000Z"), updatedAt: input.updatedAt ?? new Date("2024-01-01T00:00:00.000Z"), }; } export const mockInventoryCommands = (): Pick< InventoryCommands, | "createInventorySupplyPlan" | "updateInventorySupplyPlan" | "closeInventorySupplyPlan" | "postAcquisitionCostAdjustment" > => ({ createInventorySupplyPlan: (_db, input) => Promise.resolve( ok({ supplyPlans: input.supplyPlans.map((supplyPlanInput, index) => ({ ...buildSupplyPlan({ id: `supply-plan-${index + 1}`, sourceType: input.sourceType, sourceId: input.sourceId, }), ...supplyPlanInput, })), }), ), updateInventorySupplyPlan: (_db, input) => Promise.resolve( ok({ supplyPlan: buildSupplyPlan({ id: input.id, sourceType: input.sourceType, sourceId: input.sourceId, sourceLineId: input.sourceLineId, itemId: input.itemId, siteId: input.siteId, expectedQuantity: input.expectedQuantity, }), }), ), closeInventorySupplyPlan: (_db, input) => Promise.resolve( input.target === "SOURCE_DOCUMENT" || input.target === "SOURCE_LINES" ? ok({ supplyPlans: [] }) : ok({ supplyPlan: buildSupplyPlan({ id: input.id, status: "CLOSED" }) }), ), postAcquisitionCostAdjustment: () => Promise.resolve(ok({ acquisitionCostAdjustments: [] })), }); export const mockInventoryQueries = ( overrides: { supplyPlans?: Partial[] } = {}, ): Pick => ({ getInventorySupplyPlan: () => { const supplyPlan = overrides.supplyPlans?.[0]; return Promise.resolve( supplyPlan ? ok({ supplyPlan: buildSupplyPlan(supplyPlan) }) : ok({ supplyPlan: null }), ); }, listInventorySupplyPlans: () => Promise.resolve( ok({ items: (overrides.supplyPlans ?? []).map((plan) => buildSupplyPlan(plan)), hasNextPage: false, }), ), });