@tailor-platform/erp-kit 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/dist/cli.mjs +103 -23
- package/package.json +1 -1
- package/skills/erp-kit-app-5-impl-backend/SKILL.md +7 -4
- package/skills/erp-kit-app-7-impl-review/SKILL.md +1 -1
- package/skills/erp-kit-module-6-impl-review/SKILL.md +39 -17
- package/src/commands/generate-doc.ts +1 -1
- package/src/commands/lib/discovery.test.ts +13 -3
- package/src/commands/lib/discovery.ts +10 -2
- package/src/commands/lib/paths.ts +4 -2
- package/src/commands/lib/sync-check-tests.test.ts +84 -6
- package/src/commands/lib/sync-check-tests.ts +63 -3
- package/src/commands/sync-check.ts +7 -3
- package/src/generator/generate-app-code.ts +51 -16
- package/src/generator/generate-stubs.ts +4 -0
- package/src/generator/stub-templates.test.ts +11 -0
- package/src/generator/stub-templates.ts +22 -1
- package/src/modules/inventory/docs/features/inventory-adjustment.md +2 -1
- package/src/modules/inventory/docs/features/scrap-management.md +39 -1
- package/src/modules/manufacturing/README.md +63 -0
- package/src/modules/manufacturing/command/.gitkeep +0 -0
- package/src/modules/manufacturing/command/activateBillOfMaterial.generated.ts +6 -0
- package/src/modules/manufacturing/command/activateBillOfMaterial.test.ts +166 -0
- package/src/modules/manufacturing/command/activateBillOfMaterial.ts +173 -0
- package/src/modules/manufacturing/command/activateRouting.generated.ts +6 -0
- package/src/modules/manufacturing/command/activateRouting.test.ts +152 -0
- package/src/modules/manufacturing/command/activateRouting.ts +92 -0
- package/src/modules/manufacturing/command/activateWorkCenter.generated.ts +6 -0
- package/src/modules/manufacturing/command/activateWorkCenter.test.ts +135 -0
- package/src/modules/manufacturing/command/activateWorkCenter.ts +91 -0
- package/src/modules/manufacturing/command/cancelProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/cancelProductionOrder.test.ts +151 -0
- package/src/modules/manufacturing/command/cancelProductionOrder.ts +114 -0
- package/src/modules/manufacturing/command/closeProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/closeProductionOrder.test.ts +126 -0
- package/src/modules/manufacturing/command/closeProductionOrder.ts +87 -0
- package/src/modules/manufacturing/command/completeProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/completeProductionOrder.test.ts +132 -0
- package/src/modules/manufacturing/command/completeProductionOrder.ts +97 -0
- package/src/modules/manufacturing/command/completeWorkOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/completeWorkOrder.test.ts +369 -0
- package/src/modules/manufacturing/command/completeWorkOrder.ts +212 -0
- package/src/modules/manufacturing/command/createBillOfMaterial.generated.ts +6 -0
- package/src/modules/manufacturing/command/createBillOfMaterial.test.ts +210 -0
- package/src/modules/manufacturing/command/createBillOfMaterial.ts +176 -0
- package/src/modules/manufacturing/command/createProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/createProductionOrder.test.ts +160 -0
- package/src/modules/manufacturing/command/createProductionOrder.ts +129 -0
- package/src/modules/manufacturing/command/createRouting.generated.ts +6 -0
- package/src/modules/manufacturing/command/createRouting.test.ts +168 -0
- package/src/modules/manufacturing/command/createRouting.ts +128 -0
- package/src/modules/manufacturing/command/createWorkCenter.generated.ts +6 -0
- package/src/modules/manufacturing/command/createWorkCenter.test.ts +148 -0
- package/src/modules/manufacturing/command/createWorkCenter.ts +131 -0
- package/src/modules/manufacturing/command/deactivateBillOfMaterial.generated.ts +6 -0
- package/src/modules/manufacturing/command/deactivateBillOfMaterial.test.ts +103 -0
- package/src/modules/manufacturing/command/deactivateBillOfMaterial.ts +78 -0
- package/src/modules/manufacturing/command/deactivateRouting.generated.ts +6 -0
- package/src/modules/manufacturing/command/deactivateRouting.test.ts +112 -0
- package/src/modules/manufacturing/command/deactivateRouting.ts +76 -0
- package/src/modules/manufacturing/command/deactivateWorkCenter.generated.ts +6 -0
- package/src/modules/manufacturing/command/deactivateWorkCenter.test.ts +113 -0
- package/src/modules/manufacturing/command/deactivateWorkCenter.ts +85 -0
- package/src/modules/manufacturing/command/pauseWorkOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/pauseWorkOrder.test.ts +118 -0
- package/src/modules/manufacturing/command/pauseWorkOrder.ts +82 -0
- package/src/modules/manufacturing/command/recordInventoryIssueOutcome.generated.ts +6 -0
- package/src/modules/manufacturing/command/recordInventoryIssueOutcome.test.ts +183 -0
- package/src/modules/manufacturing/command/recordInventoryIssueOutcome.ts +139 -0
- package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.generated.ts +6 -0
- package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.test.ts +120 -0
- package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.ts +110 -0
- package/src/modules/manufacturing/command/releaseProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/releaseProductionOrder.test.ts +220 -0
- package/src/modules/manufacturing/command/releaseProductionOrder.ts +450 -0
- package/src/modules/manufacturing/command/reopenProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/reopenProductionOrder.test.ts +196 -0
- package/src/modules/manufacturing/command/reopenProductionOrder.ts +98 -0
- package/src/modules/manufacturing/command/reportWorkOrderProgress.generated.ts +6 -0
- package/src/modules/manufacturing/command/reportWorkOrderProgress.test.ts +204 -0
- package/src/modules/manufacturing/command/reportWorkOrderProgress.ts +129 -0
- package/src/modules/manufacturing/command/rescheduleProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/rescheduleProductionOrder.test.ts +185 -0
- package/src/modules/manufacturing/command/rescheduleProductionOrder.ts +95 -0
- package/src/modules/manufacturing/command/resumeWorkOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/resumeWorkOrder.test.ts +122 -0
- package/src/modules/manufacturing/command/resumeWorkOrder.ts +94 -0
- package/src/modules/manufacturing/command/reviewManufacturingCostSummary.generated.ts +6 -0
- package/src/modules/manufacturing/command/reviewManufacturingCostSummary.test.ts +231 -0
- package/src/modules/manufacturing/command/reviewManufacturingCostSummary.ts +137 -0
- package/src/modules/manufacturing/command/startWorkOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/startWorkOrder.test.ts +118 -0
- package/src/modules/manufacturing/command/startWorkOrder.ts +126 -0
- package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.test.ts +153 -0
- package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.ts +106 -0
- package/src/modules/manufacturing/command/unreleaseProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/unreleaseProductionOrder.test.ts +140 -0
- package/src/modules/manufacturing/command/unreleaseProductionOrder.ts +131 -0
- package/src/modules/manufacturing/command/updateBillOfMaterial.generated.ts +6 -0
- package/src/modules/manufacturing/command/updateBillOfMaterial.test.ts +149 -0
- package/src/modules/manufacturing/command/updateBillOfMaterial.ts +174 -0
- package/src/modules/manufacturing/command/updateProductionOrder.generated.ts +6 -0
- package/src/modules/manufacturing/command/updateProductionOrder.test.ts +112 -0
- package/src/modules/manufacturing/command/updateProductionOrder.ts +145 -0
- package/src/modules/manufacturing/command/updateRouting.generated.ts +6 -0
- package/src/modules/manufacturing/command/updateRouting.test.ts +211 -0
- package/src/modules/manufacturing/command/updateRouting.ts +124 -0
- package/src/modules/manufacturing/command/updateWorkCenter.generated.ts +6 -0
- package/src/modules/manufacturing/command/updateWorkCenter.test.ts +152 -0
- package/src/modules/manufacturing/command/updateWorkCenter.ts +137 -0
- package/src/modules/manufacturing/db/.gitkeep +0 -0
- package/src/modules/manufacturing/db/billOfMaterial.ts +70 -0
- package/src/modules/manufacturing/db/billOfMaterialLine.ts +49 -0
- package/src/modules/manufacturing/db/costVarianceLine.ts +53 -0
- package/src/modules/manufacturing/db/manufacturingCostLine.ts +35 -0
- package/src/modules/manufacturing/db/manufacturingCostSettlementRecord.ts +39 -0
- package/src/modules/manufacturing/db/manufacturingCostSummary.ts +59 -0
- package/src/modules/manufacturing/db/productionOrder.ts +83 -0
- package/src/modules/manufacturing/db/productionOrderBomSnapshot.ts +44 -0
- package/src/modules/manufacturing/db/productionOrderCostBaseline.ts +44 -0
- package/src/modules/manufacturing/db/productionOrderMaterialRequirement.ts +57 -0
- package/src/modules/manufacturing/db/productionOrderRoutingSnapshot.ts +43 -0
- package/src/modules/manufacturing/db/routing.ts +63 -0
- package/src/modules/manufacturing/db/routingOperation.ts +57 -0
- package/src/modules/manufacturing/db/workCenter.ts +87 -0
- package/src/modules/manufacturing/db/workOrder.ts +65 -0
- package/src/modules/manufacturing/db/workOrderExecutionEvent.ts +54 -0
- package/src/modules/manufacturing/docs/commands/ActivateBillOfMaterial.md +50 -0
- package/src/modules/manufacturing/docs/commands/ActivateRouting.md +48 -0
- package/src/modules/manufacturing/docs/commands/ActivateWorkCenter.md +49 -0
- package/src/modules/manufacturing/docs/commands/CancelProductionOrder.md +48 -0
- package/src/modules/manufacturing/docs/commands/CloseProductionOrder.md +46 -0
- package/src/modules/manufacturing/docs/commands/CompleteProductionOrder.md +48 -0
- package/src/modules/manufacturing/docs/commands/CompleteWorkOrder.md +66 -0
- package/src/modules/manufacturing/docs/commands/CreateBillOfMaterial.md +54 -0
- package/src/modules/manufacturing/docs/commands/CreateProductionOrder.md +49 -0
- package/src/modules/manufacturing/docs/commands/CreateRouting.md +50 -0
- package/src/modules/manufacturing/docs/commands/CreateWorkCenter.md +51 -0
- package/src/modules/manufacturing/docs/commands/DeactivateBillOfMaterial.md +45 -0
- package/src/modules/manufacturing/docs/commands/DeactivateRouting.md +45 -0
- package/src/modules/manufacturing/docs/commands/DeactivateWorkCenter.md +45 -0
- package/src/modules/manufacturing/docs/commands/PauseWorkOrder.md +44 -0
- package/src/modules/manufacturing/docs/commands/RecordInventoryIssueOutcome.md +59 -0
- package/src/modules/manufacturing/docs/commands/RecordManufacturingCostSettlementAcknowledgment.md +49 -0
- package/src/modules/manufacturing/docs/commands/ReleaseProductionOrder.md +57 -0
- package/src/modules/manufacturing/docs/commands/ReopenProductionOrder.md +54 -0
- package/src/modules/manufacturing/docs/commands/ReportWorkOrderProgress.md +53 -0
- package/src/modules/manufacturing/docs/commands/RescheduleProductionOrder.md +45 -0
- package/src/modules/manufacturing/docs/commands/ResumeWorkOrder.md +44 -0
- package/src/modules/manufacturing/docs/commands/ReviewManufacturingCostSummary.md +52 -0
- package/src/modules/manufacturing/docs/commands/StartWorkOrder.md +46 -0
- package/src/modules/manufacturing/docs/commands/TechnicallyCompleteProductionOrder.md +51 -0
- package/src/modules/manufacturing/docs/commands/UnreleaseProductionOrder.md +46 -0
- package/src/modules/manufacturing/docs/commands/UpdateBillOfMaterial.md +48 -0
- package/src/modules/manufacturing/docs/commands/UpdateProductionOrder.md +48 -0
- package/src/modules/manufacturing/docs/commands/UpdateRouting.md +52 -0
- package/src/modules/manufacturing/docs/commands/UpdateWorkCenter.md +48 -0
- package/src/modules/manufacturing/docs/features/bill-of-material-management.md +83 -0
- package/src/modules/manufacturing/docs/features/manufacturing-cost-and-variance.md +191 -0
- package/src/modules/manufacturing/docs/features/production-order-lifecycle.md +103 -0
- package/src/modules/manufacturing/docs/features/routing-and-work-center-definition.md +63 -0
- package/src/modules/manufacturing/docs/features/work-order-execution.md +115 -0
- package/src/modules/manufacturing/docs/models/BillOfMaterial.md +60 -0
- package/src/modules/manufacturing/docs/models/ManufacturingCostSummary.md +66 -0
- package/src/modules/manufacturing/docs/models/ProductionOrder.md +76 -0
- package/src/modules/manufacturing/docs/models/Routing.md +58 -0
- package/src/modules/manufacturing/docs/models/WorkCenter.md +56 -0
- package/src/modules/manufacturing/docs/models/WorkOrder.md +63 -0
- package/src/modules/manufacturing/docs/queries/DetectBillOfMaterialCircularReference.md +39 -0
- package/src/modules/manufacturing/docs/queries/ExplodeBillOfMaterial.md +56 -0
- package/src/modules/manufacturing/docs/queries/GetBillOfMaterial.md +37 -0
- package/src/modules/manufacturing/docs/queries/GetManufacturingCostSummary.md +39 -0
- package/src/modules/manufacturing/docs/queries/GetProductionOrder.md +37 -0
- package/src/modules/manufacturing/docs/queries/GetRouting.md +39 -0
- package/src/modules/manufacturing/docs/queries/GetWorkCenter.md +35 -0
- package/src/modules/manufacturing/docs/queries/GetWorkOrder.md +38 -0
- package/src/modules/manufacturing/docs/queries/ListBillOfMaterialsByItem.md +42 -0
- package/src/modules/manufacturing/docs/queries/ListManufacturingCostSummariesByStatus.md +41 -0
- package/src/modules/manufacturing/docs/queries/ListProductionOrdersByStatus.md +41 -0
- package/src/modules/manufacturing/docs/queries/ListRoutingsByItem.md +42 -0
- package/src/modules/manufacturing/docs/queries/ListWorkCentersBySite.md +38 -0
- package/src/modules/manufacturing/docs/queries/ListWorkOrdersByProductionOrder.md +39 -0
- package/src/modules/manufacturing/docs/queries/ListWorkOrdersByWorkCenter.md +43 -0
- package/src/modules/manufacturing/executor/.gitkeep +0 -0
- package/src/modules/manufacturing/generated/enums.ts +113 -0
- package/src/modules/manufacturing/generated/kysely-tailordb.ts +247 -0
- package/src/modules/manufacturing/index.ts +2 -0
- package/src/modules/manufacturing/lib/_db_deps.ts +22 -0
- package/src/modules/manufacturing/lib/errors.generated.ts +592 -0
- package/src/modules/manufacturing/lib/permissions.generated.ts +35 -0
- package/src/modules/manufacturing/lib/types.ts +111 -0
- package/src/modules/manufacturing/module.ts +226 -0
- package/src/modules/manufacturing/permissions.ts +3 -0
- package/src/modules/manufacturing/query/.gitkeep +0 -0
- package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.generated.ts +5 -0
- package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.test.ts +115 -0
- package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.ts +79 -0
- package/src/modules/manufacturing/query/explodeBillOfMaterial.generated.ts +5 -0
- package/src/modules/manufacturing/query/explodeBillOfMaterial.test.ts +445 -0
- package/src/modules/manufacturing/query/explodeBillOfMaterial.ts +306 -0
- package/src/modules/manufacturing/query/getBillOfMaterial.generated.ts +5 -0
- package/src/modules/manufacturing/query/getBillOfMaterial.test.ts +64 -0
- package/src/modules/manufacturing/query/getBillOfMaterial.ts +27 -0
- package/src/modules/manufacturing/query/getManufacturingCostSummary.generated.ts +5 -0
- package/src/modules/manufacturing/query/getManufacturingCostSummary.test.ts +147 -0
- package/src/modules/manufacturing/query/getManufacturingCostSummary.ts +46 -0
- package/src/modules/manufacturing/query/getProductionOrder.generated.ts +5 -0
- package/src/modules/manufacturing/query/getProductionOrder.test.ts +139 -0
- package/src/modules/manufacturing/query/getProductionOrder.ts +84 -0
- package/src/modules/manufacturing/query/getRouting.generated.ts +5 -0
- package/src/modules/manufacturing/query/getRouting.test.ts +71 -0
- package/src/modules/manufacturing/query/getRouting.ts +34 -0
- package/src/modules/manufacturing/query/getWorkCenter.generated.ts +5 -0
- package/src/modules/manufacturing/query/getWorkCenter.test.ts +37 -0
- package/src/modules/manufacturing/query/getWorkCenter.ts +21 -0
- package/src/modules/manufacturing/query/getWorkOrder.generated.ts +5 -0
- package/src/modules/manufacturing/query/getWorkOrder.test.ts +73 -0
- package/src/modules/manufacturing/query/getWorkOrder.ts +28 -0
- package/src/modules/manufacturing/query/listBillOfMaterialsByItem.generated.ts +5 -0
- package/src/modules/manufacturing/query/listBillOfMaterialsByItem.test.ts +107 -0
- package/src/modules/manufacturing/query/listBillOfMaterialsByItem.ts +58 -0
- package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.generated.ts +5 -0
- package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.test.ts +96 -0
- package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.ts +77 -0
- package/src/modules/manufacturing/query/listProductionOrdersByStatus.generated.ts +5 -0
- package/src/modules/manufacturing/query/listProductionOrdersByStatus.test.ts +121 -0
- package/src/modules/manufacturing/query/listProductionOrdersByStatus.ts +83 -0
- package/src/modules/manufacturing/query/listRoutingsByItem.generated.ts +5 -0
- package/src/modules/manufacturing/query/listRoutingsByItem.test.ts +110 -0
- package/src/modules/manufacturing/query/listRoutingsByItem.ts +54 -0
- package/src/modules/manufacturing/query/listWorkCentersBySite.generated.ts +5 -0
- package/src/modules/manufacturing/query/listWorkCentersBySite.test.ts +81 -0
- package/src/modules/manufacturing/query/listWorkCentersBySite.ts +70 -0
- package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.generated.ts +5 -0
- package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.test.ts +102 -0
- package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.ts +53 -0
- package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.generated.ts +5 -0
- package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.test.ts +143 -0
- package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.ts +56 -0
- package/src/modules/manufacturing/seed/index.ts +19 -0
- package/src/modules/manufacturing/tailor.config.ts +13 -0
- package/src/modules/manufacturing/tailor.d.ts +13 -0
- package/src/modules/manufacturing/testing/commandTestUtils.ts +29 -0
- package/src/modules/manufacturing/testing/fixtures.ts +402 -0
- package/templates/scaffold/app/backend/package.json +9 -2
- package/templates/scaffold/app/backend/src/tests/utils/graphql-client.ts +66 -0
- package/templates/scaffold/app/backend/src/tests/utils/setup.ts +21 -0
- package/templates/scaffold/app/backend/tsconfig.json +9 -2
- package/templates/scaffold/app/backend/vitest.config.ts +35 -0
- package/templates/scaffold/app/frontend/package.json +2 -2
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
2
|
+
import { CompanyScopeRequiredError, ParentItemRequiredError } from "../lib/errors.generated";
|
|
3
|
+
import {
|
|
4
|
+
ok,
|
|
5
|
+
err,
|
|
6
|
+
type ReadonlyDB,
|
|
7
|
+
type PaginationInput,
|
|
8
|
+
buildPaginatedResult,
|
|
9
|
+
DEFAULT_PAGE_SIZE,
|
|
10
|
+
} from "@tailor-platform/erp-kit/module";
|
|
11
|
+
|
|
12
|
+
type OrderByField = "revisionNumber" | "effectivityStartDate";
|
|
13
|
+
|
|
14
|
+
export interface ListBillOfMaterialsByItemInput extends PaginationInput<OrderByField> {
|
|
15
|
+
companyId: string;
|
|
16
|
+
parentItemId: string;
|
|
17
|
+
siteId?: string;
|
|
18
|
+
status?: string;
|
|
19
|
+
bomType?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function run(db: ReadonlyDB<DB>, input: ListBillOfMaterialsByItemInput) {
|
|
23
|
+
if (!input.companyId) {
|
|
24
|
+
return err(new CompanyScopeRequiredError("missing"));
|
|
25
|
+
}
|
|
26
|
+
if (!input.parentItemId) {
|
|
27
|
+
return err(new ParentItemRequiredError("missing"));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const limit = input.limit ?? DEFAULT_PAGE_SIZE;
|
|
31
|
+
const offset = input.offset ?? 0;
|
|
32
|
+
const orderBy = input.orderBy ?? "effectivityStartDate";
|
|
33
|
+
const orderDirection = input.orderDirection ?? "asc";
|
|
34
|
+
|
|
35
|
+
let query = db
|
|
36
|
+
.selectFrom("BillOfMaterial")
|
|
37
|
+
.selectAll()
|
|
38
|
+
.where("companyId", "=", input.companyId)
|
|
39
|
+
.where("parentItemId", "=", input.parentItemId);
|
|
40
|
+
|
|
41
|
+
if (input.siteId) {
|
|
42
|
+
query = query.where("siteId", "=", input.siteId);
|
|
43
|
+
}
|
|
44
|
+
if (input.status) {
|
|
45
|
+
query = query.where("status", "=", input.status as "DRAFT" | "ACTIVE" | "INACTIVE");
|
|
46
|
+
}
|
|
47
|
+
if (input.bomType) {
|
|
48
|
+
query = query.where("bomType", "=", input.bomType as "MANUFACTURE" | "PHANTOM" | "KIT");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const items = await query
|
|
52
|
+
.orderBy(orderBy, orderDirection)
|
|
53
|
+
.limit(limit + 1)
|
|
54
|
+
.offset(offset)
|
|
55
|
+
.execute();
|
|
56
|
+
|
|
57
|
+
return ok(buildPaginatedResult(items, limit));
|
|
58
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMockDb } from "../../../testing/index";
|
|
3
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
4
|
+
import { basePendingReviewCostSummary, baseReviewedCostSummary } from "../testing/fixtures";
|
|
5
|
+
import { expectOk, expectErr } from "../testing/commandTestUtils";
|
|
6
|
+
import { StatusFilterRequiredError, CompanyScopeRequiredError } from "../lib/errors.generated";
|
|
7
|
+
import { run } from "./listManufacturingCostSummariesByStatus";
|
|
8
|
+
|
|
9
|
+
describe("listManufacturingCostSummariesByStatus", () => {
|
|
10
|
+
it("lists pending-review cost summaries for a company", async () => {
|
|
11
|
+
const { db, spies } = createMockDb<DB>();
|
|
12
|
+
spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);
|
|
13
|
+
|
|
14
|
+
const result = await run(db, {
|
|
15
|
+
companyId: "company-1",
|
|
16
|
+
status: "PENDING_VARIANCE_REVIEW",
|
|
17
|
+
});
|
|
18
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
19
|
+
|
|
20
|
+
expect(items).toHaveLength(1);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("lists reviewed cost summaries awaiting settlement", async () => {
|
|
24
|
+
const { db, spies } = createMockDb<DB>();
|
|
25
|
+
spies.select.mockReturnValueOnce([baseReviewedCostSummary]);
|
|
26
|
+
|
|
27
|
+
const result = await run(db, {
|
|
28
|
+
companyId: "company-1",
|
|
29
|
+
status: "VARIANCE_REVIEWED",
|
|
30
|
+
});
|
|
31
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
32
|
+
|
|
33
|
+
expect(items).toHaveLength(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("filters summaries by site when provided", async () => {
|
|
37
|
+
const { db, spies } = createMockDb<DB>();
|
|
38
|
+
spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);
|
|
39
|
+
|
|
40
|
+
const result = await run(db, {
|
|
41
|
+
companyId: "company-1",
|
|
42
|
+
status: "PENDING_VARIANCE_REVIEW",
|
|
43
|
+
siteId: "site-1",
|
|
44
|
+
});
|
|
45
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
46
|
+
|
|
47
|
+
expect(items).toHaveLength(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("filters summaries by production order when provided", async () => {
|
|
51
|
+
const { db, spies } = createMockDb<DB>();
|
|
52
|
+
spies.select.mockReturnValueOnce([basePendingReviewCostSummary]);
|
|
53
|
+
|
|
54
|
+
const result = await run(db, {
|
|
55
|
+
companyId: "company-1",
|
|
56
|
+
status: "PENDING_VARIANCE_REVIEW",
|
|
57
|
+
productionOrderId: "production-order-2",
|
|
58
|
+
});
|
|
59
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
60
|
+
|
|
61
|
+
expect(items).toHaveLength(1);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns summary rows with production-order and status context", async () => {
|
|
65
|
+
const { db, spies } = createMockDb<DB>();
|
|
66
|
+
spies.select.mockReturnValueOnce([basePendingReviewCostSummary, baseReviewedCostSummary]);
|
|
67
|
+
|
|
68
|
+
const result = await run(db, {
|
|
69
|
+
companyId: "company-1",
|
|
70
|
+
status: ["PENDING_VARIANCE_REVIEW", "VARIANCE_REVIEWED"],
|
|
71
|
+
});
|
|
72
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
73
|
+
|
|
74
|
+
expect(items).toHaveLength(2);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("returns error when no status filter is provided", async () => {
|
|
78
|
+
const { db } = createMockDb<DB>();
|
|
79
|
+
|
|
80
|
+
const result = await run(db, {
|
|
81
|
+
companyId: "company-1",
|
|
82
|
+
status: [],
|
|
83
|
+
});
|
|
84
|
+
expectErr(result as never, StatusFilterRequiredError);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("returns error when company scope is missing", async () => {
|
|
88
|
+
const { db } = createMockDb<DB>();
|
|
89
|
+
|
|
90
|
+
const result = await run(db, {
|
|
91
|
+
companyId: "",
|
|
92
|
+
status: "PENDING_VARIANCE_REVIEW",
|
|
93
|
+
});
|
|
94
|
+
expectErr(result as never, CompanyScopeRequiredError);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
2
|
+
import { StatusFilterRequiredError, CompanyScopeRequiredError } from "../lib/errors.generated";
|
|
3
|
+
import {
|
|
4
|
+
ok,
|
|
5
|
+
err,
|
|
6
|
+
type ReadonlyDB,
|
|
7
|
+
type PaginationInput,
|
|
8
|
+
buildPaginatedResult,
|
|
9
|
+
DEFAULT_PAGE_SIZE,
|
|
10
|
+
} from "@tailor-platform/erp-kit/module";
|
|
11
|
+
|
|
12
|
+
export interface ListManufacturingCostSummariesByStatusInput extends PaginationInput {
|
|
13
|
+
companyId: string;
|
|
14
|
+
status: string | string[];
|
|
15
|
+
siteId?: string;
|
|
16
|
+
productionOrderId?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function run(db: ReadonlyDB<DB>, input: ListManufacturingCostSummariesByStatusInput) {
|
|
20
|
+
if (!input.companyId) {
|
|
21
|
+
return err(new CompanyScopeRequiredError("companyId"));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const statuses = Array.isArray(input.status) ? input.status : input.status ? [input.status] : [];
|
|
25
|
+
if (statuses.length === 0) {
|
|
26
|
+
return err(new StatusFilterRequiredError("status"));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const limit = input.limit ?? DEFAULT_PAGE_SIZE;
|
|
30
|
+
const offset = input.offset ?? 0;
|
|
31
|
+
const orderBy = input.orderBy ?? "id";
|
|
32
|
+
const orderDirection = input.orderDirection ?? "asc";
|
|
33
|
+
|
|
34
|
+
let query = db
|
|
35
|
+
.selectFrom("ManufacturingCostSummary")
|
|
36
|
+
.innerJoin(
|
|
37
|
+
"ProductionOrder",
|
|
38
|
+
"ProductionOrder.id",
|
|
39
|
+
"ManufacturingCostSummary.productionOrderId",
|
|
40
|
+
)
|
|
41
|
+
.select([
|
|
42
|
+
"ManufacturingCostSummary.id",
|
|
43
|
+
"ManufacturingCostSummary.productionOrderId",
|
|
44
|
+
"ManufacturingCostSummary.plannedMaterialCost",
|
|
45
|
+
"ManufacturingCostSummary.plannedLaborCost",
|
|
46
|
+
"ManufacturingCostSummary.plannedMachineCost",
|
|
47
|
+
"ManufacturingCostSummary.plannedOverheadCost",
|
|
48
|
+
"ManufacturingCostSummary.actualMaterialCost",
|
|
49
|
+
"ManufacturingCostSummary.actualLaborCost",
|
|
50
|
+
"ManufacturingCostSummary.actualMachineCost",
|
|
51
|
+
"ManufacturingCostSummary.actualOverheadCost",
|
|
52
|
+
"ManufacturingCostSummary.currencyCode",
|
|
53
|
+
"ManufacturingCostSummary.reviewedDate",
|
|
54
|
+
"ManufacturingCostSummary.reviewerNotes",
|
|
55
|
+
"ManufacturingCostSummary.status",
|
|
56
|
+
"ManufacturingCostSummary.createdAt",
|
|
57
|
+
"ManufacturingCostSummary.updatedAt",
|
|
58
|
+
])
|
|
59
|
+
.where("ProductionOrder.companyId", "=", input.companyId)
|
|
60
|
+
.where("ManufacturingCostSummary.status", "in", statuses as never[]);
|
|
61
|
+
|
|
62
|
+
if (input.siteId) {
|
|
63
|
+
query = query.where("ProductionOrder.siteId", "=", input.siteId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (input.productionOrderId) {
|
|
67
|
+
query = query.where("ManufacturingCostSummary.productionOrderId", "=", input.productionOrderId);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const items = await query
|
|
71
|
+
.orderBy(`ManufacturingCostSummary.${orderBy}` as never, orderDirection)
|
|
72
|
+
.limit(limit + 1)
|
|
73
|
+
.offset(offset)
|
|
74
|
+
.execute();
|
|
75
|
+
|
|
76
|
+
return ok(buildPaginatedResult(items, limit));
|
|
77
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMockDb } from "../../../testing/index";
|
|
3
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
4
|
+
import { expectOk, expectErr } from "../testing/commandTestUtils";
|
|
5
|
+
import { baseReleasedProductionOrder, baseInProgressProductionOrder } from "../testing/fixtures";
|
|
6
|
+
import { StatusFilterRequiredError, CompanyScopeRequiredError } from "../lib/errors.generated";
|
|
7
|
+
import { run } from "./listProductionOrdersByStatus";
|
|
8
|
+
|
|
9
|
+
describe("listProductionOrdersByStatus", () => {
|
|
10
|
+
it("lists released production orders for a company", async () => {
|
|
11
|
+
const { db, spies } = createMockDb<DB>();
|
|
12
|
+
spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
|
|
13
|
+
spies.select.mockReturnValueOnce([
|
|
14
|
+
{ productionOrderId: "production-order-2", status: "COLLECTING" },
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
const result = await run(db, {
|
|
18
|
+
companyId: "company-1",
|
|
19
|
+
status: "RELEASED",
|
|
20
|
+
});
|
|
21
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
22
|
+
|
|
23
|
+
expect(items).toHaveLength(1);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("lists in-progress production orders for a site", async () => {
|
|
27
|
+
const { db, spies } = createMockDb<DB>();
|
|
28
|
+
spies.select.mockReturnValueOnce([baseInProgressProductionOrder]);
|
|
29
|
+
spies.select.mockReturnValueOnce([]);
|
|
30
|
+
|
|
31
|
+
const result = await run(db, {
|
|
32
|
+
companyId: "company-1",
|
|
33
|
+
status: "IN_PROGRESS",
|
|
34
|
+
siteId: "site-1",
|
|
35
|
+
});
|
|
36
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
37
|
+
|
|
38
|
+
expect(items).toHaveLength(1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("filters production orders by produced item when provided", async () => {
|
|
42
|
+
const { db, spies } = createMockDb<DB>();
|
|
43
|
+
spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
|
|
44
|
+
spies.select.mockReturnValueOnce([]);
|
|
45
|
+
|
|
46
|
+
const result = await run(db, {
|
|
47
|
+
companyId: "company-1",
|
|
48
|
+
status: "RELEASED",
|
|
49
|
+
orderedItemId: "item-1",
|
|
50
|
+
});
|
|
51
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
52
|
+
|
|
53
|
+
expect(items).toHaveLength(1);
|
|
54
|
+
expect(items[0]).toMatchObject({ orderedItemId: "item-1" });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("filters production orders by planned date range when provided", async () => {
|
|
58
|
+
const { db, spies } = createMockDb<DB>();
|
|
59
|
+
spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
|
|
60
|
+
spies.select.mockReturnValueOnce([]);
|
|
61
|
+
|
|
62
|
+
const result = await run(db, {
|
|
63
|
+
companyId: "company-1",
|
|
64
|
+
status: "RELEASED",
|
|
65
|
+
plannedStartDateFrom: new Date("2024-01-01T00:00:00.000Z"),
|
|
66
|
+
plannedStartDateTo: new Date("2024-12-31T23:59:59.000Z"),
|
|
67
|
+
});
|
|
68
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
69
|
+
|
|
70
|
+
expect(items).toHaveLength(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("returns summary fields with site and cost-summary status", async () => {
|
|
74
|
+
const { db, spies } = createMockDb<DB>();
|
|
75
|
+
spies.select.mockReturnValueOnce([baseReleasedProductionOrder]);
|
|
76
|
+
spies.select.mockReturnValueOnce([
|
|
77
|
+
{ productionOrderId: "production-order-2", status: "COLLECTING" },
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const result = await run(db, {
|
|
81
|
+
companyId: "company-1",
|
|
82
|
+
status: "RELEASED",
|
|
83
|
+
});
|
|
84
|
+
const { items } = expectOk<{
|
|
85
|
+
items: {
|
|
86
|
+
siteId: string;
|
|
87
|
+
status: string;
|
|
88
|
+
plannedQuantity: number;
|
|
89
|
+
plannedStartDate: Date;
|
|
90
|
+
costSummaryStatus: string | null;
|
|
91
|
+
}[];
|
|
92
|
+
hasNextPage: boolean;
|
|
93
|
+
}>(result as never);
|
|
94
|
+
|
|
95
|
+
expect(items[0].siteId).toBe("site-1");
|
|
96
|
+
expect(items[0].status).toBe("RELEASED");
|
|
97
|
+
expect(items[0].plannedQuantity).toBe(100);
|
|
98
|
+
expect(items[0].plannedStartDate).toEqual(new Date("2024-03-01T00:00:00.000Z"));
|
|
99
|
+
expect(items[0].costSummaryStatus).toBe("COLLECTING");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("returns error when no status filter is provided", async () => {
|
|
103
|
+
const { db } = createMockDb<DB>();
|
|
104
|
+
|
|
105
|
+
const result = await run(db, {
|
|
106
|
+
companyId: "company-1",
|
|
107
|
+
status: "",
|
|
108
|
+
});
|
|
109
|
+
expectErr(result as never, StatusFilterRequiredError);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("returns error when company scope is missing", async () => {
|
|
113
|
+
const { db } = createMockDb<DB>();
|
|
114
|
+
|
|
115
|
+
const result = await run(db, {
|
|
116
|
+
companyId: "",
|
|
117
|
+
status: "RELEASED",
|
|
118
|
+
});
|
|
119
|
+
expectErr(result as never, CompanyScopeRequiredError);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
2
|
+
import { StatusFilterRequiredError, CompanyScopeRequiredError } from "../lib/errors.generated";
|
|
3
|
+
import {
|
|
4
|
+
ok,
|
|
5
|
+
err,
|
|
6
|
+
type ReadonlyDB,
|
|
7
|
+
type PaginationInput,
|
|
8
|
+
buildPaginatedResult,
|
|
9
|
+
DEFAULT_PAGE_SIZE,
|
|
10
|
+
} from "@tailor-platform/erp-kit/module";
|
|
11
|
+
|
|
12
|
+
export interface ListProductionOrdersByStatusInput extends PaginationInput<"plannedStartDate"> {
|
|
13
|
+
companyId: string;
|
|
14
|
+
status: string | string[];
|
|
15
|
+
siteId?: string;
|
|
16
|
+
orderedItemId?: string;
|
|
17
|
+
plannedStartDateFrom?: Date;
|
|
18
|
+
plannedStartDateTo?: Date;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function run(db: ReadonlyDB<DB>, input: ListProductionOrdersByStatusInput) {
|
|
22
|
+
if (!input.companyId) {
|
|
23
|
+
return err(new CompanyScopeRequiredError("companyId"));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const statuses = Array.isArray(input.status) ? input.status : input.status ? [input.status] : [];
|
|
27
|
+
if (statuses.length === 0) {
|
|
28
|
+
return err(new StatusFilterRequiredError("status"));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const limit = input.limit ?? DEFAULT_PAGE_SIZE;
|
|
32
|
+
const offset = input.offset ?? 0;
|
|
33
|
+
const orderBy = input.orderBy ?? "plannedStartDate";
|
|
34
|
+
const orderDirection = input.orderDirection ?? "asc";
|
|
35
|
+
|
|
36
|
+
let query = db
|
|
37
|
+
.selectFrom("ProductionOrder")
|
|
38
|
+
.selectAll()
|
|
39
|
+
.where("companyId", "=", input.companyId)
|
|
40
|
+
.where("status", "in", statuses as never[]);
|
|
41
|
+
|
|
42
|
+
if (input.siteId) {
|
|
43
|
+
query = query.where("siteId", "=", input.siteId);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (input.orderedItemId) {
|
|
47
|
+
query = query.where("orderedItemId", "=", input.orderedItemId);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (input.plannedStartDateFrom) {
|
|
51
|
+
query = query.where("plannedStartDate", ">=", input.plannedStartDateFrom as never);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (input.plannedStartDateTo) {
|
|
55
|
+
query = query.where("plannedStartDate", "<=", input.plannedStartDateTo as never);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const rows = await query
|
|
59
|
+
.orderBy(orderBy, orderDirection)
|
|
60
|
+
.limit(limit + 1)
|
|
61
|
+
.offset(offset)
|
|
62
|
+
.execute();
|
|
63
|
+
|
|
64
|
+
// Fetch linked cost-summary statuses for the returned production orders
|
|
65
|
+
const orderIds = rows.map((r) => r.id);
|
|
66
|
+
const costSummaries =
|
|
67
|
+
orderIds.length > 0
|
|
68
|
+
? await db
|
|
69
|
+
.selectFrom("ManufacturingCostSummary")
|
|
70
|
+
.select(["productionOrderId", "status"])
|
|
71
|
+
.where("productionOrderId", "in", orderIds)
|
|
72
|
+
.execute()
|
|
73
|
+
: [];
|
|
74
|
+
|
|
75
|
+
const costStatusMap = new Map(costSummaries.map((cs) => [cs.productionOrderId, cs.status]));
|
|
76
|
+
|
|
77
|
+
const items = rows.map((row) => ({
|
|
78
|
+
...row,
|
|
79
|
+
costSummaryStatus: costStatusMap.get(row.id) ?? null,
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
return ok(buildPaginatedResult(items, limit));
|
|
83
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMockDb } from "../../../testing/index";
|
|
3
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
4
|
+
import { CompanyScopeRequiredError, ParentItemRequiredError } from "../lib/errors.generated";
|
|
5
|
+
import { baseActiveRouting, baseDraftRouting, baseInactiveRouting } from "../testing/fixtures";
|
|
6
|
+
import { expectOk, expectErr } from "../testing/commandTestUtils";
|
|
7
|
+
import { run } from "./listRoutingsByItem";
|
|
8
|
+
|
|
9
|
+
describe("listRoutingsByItem", () => {
|
|
10
|
+
it("lists routing revisions for a produced item", async () => {
|
|
11
|
+
const { db, spies } = createMockDb<DB>();
|
|
12
|
+
spies.select.mockReturnValueOnce([baseActiveRouting, baseDraftRouting]);
|
|
13
|
+
|
|
14
|
+
const result = await run(db, {
|
|
15
|
+
companyId: "company-1",
|
|
16
|
+
parentItemId: "item-1",
|
|
17
|
+
});
|
|
18
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
19
|
+
|
|
20
|
+
expect(items).toHaveLength(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("filters routing revisions by site when provided", async () => {
|
|
24
|
+
const { db, spies } = createMockDb<DB>();
|
|
25
|
+
spies.select.mockReturnValueOnce([baseActiveRouting]);
|
|
26
|
+
|
|
27
|
+
const result = await run(db, {
|
|
28
|
+
companyId: "company-1",
|
|
29
|
+
parentItemId: "item-1",
|
|
30
|
+
siteId: "site-1",
|
|
31
|
+
});
|
|
32
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
33
|
+
|
|
34
|
+
expect(items).toHaveLength(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("filters routing revisions by status when provided", async () => {
|
|
38
|
+
const { db, spies } = createMockDb<DB>();
|
|
39
|
+
spies.select.mockReturnValueOnce([baseActiveRouting]);
|
|
40
|
+
|
|
41
|
+
const result = await run(db, {
|
|
42
|
+
companyId: "company-1",
|
|
43
|
+
parentItemId: "item-1",
|
|
44
|
+
status: "ACTIVE",
|
|
45
|
+
});
|
|
46
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
47
|
+
|
|
48
|
+
expect(items).toHaveLength(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns all statuses by default", async () => {
|
|
52
|
+
const { db, spies } = createMockDb<DB>();
|
|
53
|
+
spies.select.mockReturnValueOnce([baseActiveRouting, baseDraftRouting, baseInactiveRouting]);
|
|
54
|
+
|
|
55
|
+
const result = await run(db, {
|
|
56
|
+
companyId: "company-1",
|
|
57
|
+
parentItemId: "item-1",
|
|
58
|
+
});
|
|
59
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
60
|
+
|
|
61
|
+
expect(items).toHaveLength(3);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns an empty list when no routings exist", async () => {
|
|
65
|
+
const { db, spies } = createMockDb<DB>();
|
|
66
|
+
spies.select.mockReturnValueOnce([]);
|
|
67
|
+
|
|
68
|
+
const result = await run(db, {
|
|
69
|
+
companyId: "company-1",
|
|
70
|
+
parentItemId: "item-1",
|
|
71
|
+
});
|
|
72
|
+
const { items } = expectOk<{ items: unknown[]; hasNextPage: boolean }>(result as never);
|
|
73
|
+
|
|
74
|
+
expect(items).toHaveLength(0);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("returns routing revisions for the requested company scope", async () => {
|
|
78
|
+
const { db, spies } = createMockDb<DB>();
|
|
79
|
+
spies.select.mockReturnValueOnce([baseActiveRouting]);
|
|
80
|
+
|
|
81
|
+
const result = await run(db, {
|
|
82
|
+
companyId: "company-1",
|
|
83
|
+
parentItemId: "item-1",
|
|
84
|
+
});
|
|
85
|
+
const { items } = expectOk<{
|
|
86
|
+
items: { companyId: string }[];
|
|
87
|
+
hasNextPage: boolean;
|
|
88
|
+
}>(result as never);
|
|
89
|
+
|
|
90
|
+
expect(items).toHaveLength(1);
|
|
91
|
+
expect(items[0].companyId).toBe("company-1");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("returns error when the parent item reference is missing", async () => {
|
|
95
|
+
const { db } = createMockDb<DB>();
|
|
96
|
+
|
|
97
|
+
const result = await run(db, {
|
|
98
|
+
companyId: "company-1",
|
|
99
|
+
parentItemId: "",
|
|
100
|
+
});
|
|
101
|
+
expectErr(result as never, ParentItemRequiredError);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("returns error when company scope is missing", async () => {
|
|
105
|
+
const { db } = createMockDb<DB>();
|
|
106
|
+
|
|
107
|
+
const result = await run(db, { companyId: "", parentItemId: "item-1" });
|
|
108
|
+
expectErr(result as never, CompanyScopeRequiredError);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { DB } from "../generated/kysely-tailordb";
|
|
2
|
+
import { CompanyScopeRequiredError, ParentItemRequiredError } from "../lib/errors.generated";
|
|
3
|
+
import {
|
|
4
|
+
ok,
|
|
5
|
+
err,
|
|
6
|
+
type ReadonlyDB,
|
|
7
|
+
type PaginationInput,
|
|
8
|
+
buildPaginatedResult,
|
|
9
|
+
DEFAULT_PAGE_SIZE,
|
|
10
|
+
} from "@tailor-platform/erp-kit/module";
|
|
11
|
+
|
|
12
|
+
type OrderByField = "revisionNumber";
|
|
13
|
+
|
|
14
|
+
export interface ListRoutingsByItemInput extends PaginationInput<OrderByField> {
|
|
15
|
+
companyId: string;
|
|
16
|
+
parentItemId: string;
|
|
17
|
+
siteId?: string;
|
|
18
|
+
status?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function run(db: ReadonlyDB<DB>, input: ListRoutingsByItemInput) {
|
|
22
|
+
if (!input.companyId) {
|
|
23
|
+
return err(new CompanyScopeRequiredError("missing"));
|
|
24
|
+
}
|
|
25
|
+
if (!input.parentItemId) {
|
|
26
|
+
return err(new ParentItemRequiredError("missing"));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const limit = input.limit ?? DEFAULT_PAGE_SIZE;
|
|
30
|
+
const offset = input.offset ?? 0;
|
|
31
|
+
const orderBy = input.orderBy ?? "revisionNumber";
|
|
32
|
+
const orderDirection = input.orderDirection ?? "asc";
|
|
33
|
+
|
|
34
|
+
let query = db
|
|
35
|
+
.selectFrom("Routing")
|
|
36
|
+
.selectAll()
|
|
37
|
+
.where("companyId", "=", input.companyId)
|
|
38
|
+
.where("parentItemId", "=", input.parentItemId);
|
|
39
|
+
|
|
40
|
+
if (input.siteId) {
|
|
41
|
+
query = query.where("siteId", "=", input.siteId);
|
|
42
|
+
}
|
|
43
|
+
if (input.status) {
|
|
44
|
+
query = query.where("status", "=", input.status as "DRAFT" | "ACTIVE" | "INACTIVE");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const items = await query
|
|
48
|
+
.orderBy(orderBy, orderDirection)
|
|
49
|
+
.limit(limit + 1)
|
|
50
|
+
.offset(offset)
|
|
51
|
+
.execute();
|
|
52
|
+
|
|
53
|
+
return ok(buildPaginatedResult(items, limit));
|
|
54
|
+
}
|