import { type EmptyFields } from "@tailor-platform/erp-kit/core"; import { type TailorAnyDBField } from "@tailor-platform/sdk"; import type { TailorDBInsertable } from "@tailor-platform/sdk/kysely"; import type { InventoryModule } from "../inventory"; import type { ItemManagementModule } from "../item-management"; import type { PrimitivesModule } from "../primitives"; import type { PurchaseModule } from "../purchase"; import { cancelInboundShipment } from "./command/cancelInboundShipment.generated"; import { createInboundShipment } from "./command/createInboundShipment.generated"; import { postInboundShipment } from "./command/postInboundShipment.generated"; import { updateInboundShipment } from "./command/updateInboundShipment.generated"; import { createInboundShipmentType, type CreateInboundShipmentTypeParams, } from "./db/inboundShipment"; import { createInboundShipmentLineType, type CreateInboundShipmentLineTypeParams, } from "./db/inboundShipmentLine"; import { getInboundShipment } from "./query/getInboundShipment.generated"; import { listInboundShipments } from "./query/listInboundShipments.generated"; type ItemManagementDB = ItemManagementModule["db"]; export type ItemManagementQueries = ItemManagementModule["queries"]; type PrimitivesDB = PrimitivesModule["db"]; type InventoryDB = InventoryModule["db"]; export type InventoryCommands = InventoryModule["commands"]; export type InventoryQueries = InventoryModule["queries"]; export type PurchaseCommands = PurchaseModule["commands"]; export type PurchaseQueries = PurchaseModule["queries"]; export interface DefineModuleParams< ISF extends Record = EmptyFields, ISLF extends Record = EmptyFields, > { inboundShipment?: CreateInboundShipmentTypeParams; inboundShipmentLine?: Omit< CreateInboundShipmentLineTypeParams, "itemType" | "unitType" | "storageLocationType" >; itemManagement: { db: { item: ItemManagementDB["item"] }; queries: Pick; }; primitives: { db: { unit: PrimitivesDB["unit"] }; }; inventory: { db: { storageLocation: InventoryDB["storageLocation"]; }; commands: Pick; queries: Pick; }; purchase: { commands: Pick; queries: Pick; }; } /* @__NO_SIDE_EFFECTS__ */ export const defineModule = < const ISF extends Record = EmptyFields, const ISLF extends Record = EmptyFields, >( params: DefineModuleParams, ) => { const inboundShipment = createInboundShipmentType({ ...params.inboundShipment, }); const inboundShipmentLine = createInboundShipmentLineType({ ...params.inboundShipmentLine, itemType: params.itemManagement.db.item, unitType: params.primitives.db.unit, storageLocationType: params.inventory.db.storageLocation, }); const itemQueries = params.itemManagement.queries; const inventoryQueries = params.inventory.queries; return { db: { inboundShipment, inboundShipmentLine, }, queries: { getInboundShipment, listInboundShipments, }, commands: { createInboundShipment: createInboundShipment< TailorDBInsertable, TailorDBInsertable >(itemQueries, inventoryQueries), updateInboundShipment: updateInboundShipment< TailorDBInsertable, TailorDBInsertable >(itemQueries, inventoryQueries), cancelInboundShipment: cancelInboundShipment(), postInboundShipment: postInboundShipment( params.inventory.commands, params.purchase.commands, params.purchase.queries, ), }, }; }; /** EmptyFields-instantiated alias — use instead of `ReturnType`, which widens field generics to their constraint. */ export type InboundShipmentModule = ReturnType>;