@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.
Files changed (251) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cli.mjs +103 -23
  3. package/package.json +1 -1
  4. package/skills/erp-kit-app-5-impl-backend/SKILL.md +7 -4
  5. package/skills/erp-kit-app-7-impl-review/SKILL.md +1 -1
  6. package/skills/erp-kit-module-6-impl-review/SKILL.md +39 -17
  7. package/src/commands/generate-doc.ts +1 -1
  8. package/src/commands/lib/discovery.test.ts +13 -3
  9. package/src/commands/lib/discovery.ts +10 -2
  10. package/src/commands/lib/paths.ts +4 -2
  11. package/src/commands/lib/sync-check-tests.test.ts +84 -6
  12. package/src/commands/lib/sync-check-tests.ts +63 -3
  13. package/src/commands/sync-check.ts +7 -3
  14. package/src/generator/generate-app-code.ts +51 -16
  15. package/src/generator/generate-stubs.ts +4 -0
  16. package/src/generator/stub-templates.test.ts +11 -0
  17. package/src/generator/stub-templates.ts +22 -1
  18. package/src/modules/inventory/docs/features/inventory-adjustment.md +2 -1
  19. package/src/modules/inventory/docs/features/scrap-management.md +39 -1
  20. package/src/modules/manufacturing/README.md +63 -0
  21. package/src/modules/manufacturing/command/.gitkeep +0 -0
  22. package/src/modules/manufacturing/command/activateBillOfMaterial.generated.ts +6 -0
  23. package/src/modules/manufacturing/command/activateBillOfMaterial.test.ts +166 -0
  24. package/src/modules/manufacturing/command/activateBillOfMaterial.ts +173 -0
  25. package/src/modules/manufacturing/command/activateRouting.generated.ts +6 -0
  26. package/src/modules/manufacturing/command/activateRouting.test.ts +152 -0
  27. package/src/modules/manufacturing/command/activateRouting.ts +92 -0
  28. package/src/modules/manufacturing/command/activateWorkCenter.generated.ts +6 -0
  29. package/src/modules/manufacturing/command/activateWorkCenter.test.ts +135 -0
  30. package/src/modules/manufacturing/command/activateWorkCenter.ts +91 -0
  31. package/src/modules/manufacturing/command/cancelProductionOrder.generated.ts +6 -0
  32. package/src/modules/manufacturing/command/cancelProductionOrder.test.ts +151 -0
  33. package/src/modules/manufacturing/command/cancelProductionOrder.ts +114 -0
  34. package/src/modules/manufacturing/command/closeProductionOrder.generated.ts +6 -0
  35. package/src/modules/manufacturing/command/closeProductionOrder.test.ts +126 -0
  36. package/src/modules/manufacturing/command/closeProductionOrder.ts +87 -0
  37. package/src/modules/manufacturing/command/completeProductionOrder.generated.ts +6 -0
  38. package/src/modules/manufacturing/command/completeProductionOrder.test.ts +132 -0
  39. package/src/modules/manufacturing/command/completeProductionOrder.ts +97 -0
  40. package/src/modules/manufacturing/command/completeWorkOrder.generated.ts +6 -0
  41. package/src/modules/manufacturing/command/completeWorkOrder.test.ts +369 -0
  42. package/src/modules/manufacturing/command/completeWorkOrder.ts +212 -0
  43. package/src/modules/manufacturing/command/createBillOfMaterial.generated.ts +6 -0
  44. package/src/modules/manufacturing/command/createBillOfMaterial.test.ts +210 -0
  45. package/src/modules/manufacturing/command/createBillOfMaterial.ts +176 -0
  46. package/src/modules/manufacturing/command/createProductionOrder.generated.ts +6 -0
  47. package/src/modules/manufacturing/command/createProductionOrder.test.ts +160 -0
  48. package/src/modules/manufacturing/command/createProductionOrder.ts +129 -0
  49. package/src/modules/manufacturing/command/createRouting.generated.ts +6 -0
  50. package/src/modules/manufacturing/command/createRouting.test.ts +168 -0
  51. package/src/modules/manufacturing/command/createRouting.ts +128 -0
  52. package/src/modules/manufacturing/command/createWorkCenter.generated.ts +6 -0
  53. package/src/modules/manufacturing/command/createWorkCenter.test.ts +148 -0
  54. package/src/modules/manufacturing/command/createWorkCenter.ts +131 -0
  55. package/src/modules/manufacturing/command/deactivateBillOfMaterial.generated.ts +6 -0
  56. package/src/modules/manufacturing/command/deactivateBillOfMaterial.test.ts +103 -0
  57. package/src/modules/manufacturing/command/deactivateBillOfMaterial.ts +78 -0
  58. package/src/modules/manufacturing/command/deactivateRouting.generated.ts +6 -0
  59. package/src/modules/manufacturing/command/deactivateRouting.test.ts +112 -0
  60. package/src/modules/manufacturing/command/deactivateRouting.ts +76 -0
  61. package/src/modules/manufacturing/command/deactivateWorkCenter.generated.ts +6 -0
  62. package/src/modules/manufacturing/command/deactivateWorkCenter.test.ts +113 -0
  63. package/src/modules/manufacturing/command/deactivateWorkCenter.ts +85 -0
  64. package/src/modules/manufacturing/command/pauseWorkOrder.generated.ts +6 -0
  65. package/src/modules/manufacturing/command/pauseWorkOrder.test.ts +118 -0
  66. package/src/modules/manufacturing/command/pauseWorkOrder.ts +82 -0
  67. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.generated.ts +6 -0
  68. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.test.ts +183 -0
  69. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.ts +139 -0
  70. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.generated.ts +6 -0
  71. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.test.ts +120 -0
  72. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.ts +110 -0
  73. package/src/modules/manufacturing/command/releaseProductionOrder.generated.ts +6 -0
  74. package/src/modules/manufacturing/command/releaseProductionOrder.test.ts +220 -0
  75. package/src/modules/manufacturing/command/releaseProductionOrder.ts +450 -0
  76. package/src/modules/manufacturing/command/reopenProductionOrder.generated.ts +6 -0
  77. package/src/modules/manufacturing/command/reopenProductionOrder.test.ts +196 -0
  78. package/src/modules/manufacturing/command/reopenProductionOrder.ts +98 -0
  79. package/src/modules/manufacturing/command/reportWorkOrderProgress.generated.ts +6 -0
  80. package/src/modules/manufacturing/command/reportWorkOrderProgress.test.ts +204 -0
  81. package/src/modules/manufacturing/command/reportWorkOrderProgress.ts +129 -0
  82. package/src/modules/manufacturing/command/rescheduleProductionOrder.generated.ts +6 -0
  83. package/src/modules/manufacturing/command/rescheduleProductionOrder.test.ts +185 -0
  84. package/src/modules/manufacturing/command/rescheduleProductionOrder.ts +95 -0
  85. package/src/modules/manufacturing/command/resumeWorkOrder.generated.ts +6 -0
  86. package/src/modules/manufacturing/command/resumeWorkOrder.test.ts +122 -0
  87. package/src/modules/manufacturing/command/resumeWorkOrder.ts +94 -0
  88. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.generated.ts +6 -0
  89. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.test.ts +231 -0
  90. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.ts +137 -0
  91. package/src/modules/manufacturing/command/startWorkOrder.generated.ts +6 -0
  92. package/src/modules/manufacturing/command/startWorkOrder.test.ts +118 -0
  93. package/src/modules/manufacturing/command/startWorkOrder.ts +126 -0
  94. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.generated.ts +6 -0
  95. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.test.ts +153 -0
  96. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.ts +106 -0
  97. package/src/modules/manufacturing/command/unreleaseProductionOrder.generated.ts +6 -0
  98. package/src/modules/manufacturing/command/unreleaseProductionOrder.test.ts +140 -0
  99. package/src/modules/manufacturing/command/unreleaseProductionOrder.ts +131 -0
  100. package/src/modules/manufacturing/command/updateBillOfMaterial.generated.ts +6 -0
  101. package/src/modules/manufacturing/command/updateBillOfMaterial.test.ts +149 -0
  102. package/src/modules/manufacturing/command/updateBillOfMaterial.ts +174 -0
  103. package/src/modules/manufacturing/command/updateProductionOrder.generated.ts +6 -0
  104. package/src/modules/manufacturing/command/updateProductionOrder.test.ts +112 -0
  105. package/src/modules/manufacturing/command/updateProductionOrder.ts +145 -0
  106. package/src/modules/manufacturing/command/updateRouting.generated.ts +6 -0
  107. package/src/modules/manufacturing/command/updateRouting.test.ts +211 -0
  108. package/src/modules/manufacturing/command/updateRouting.ts +124 -0
  109. package/src/modules/manufacturing/command/updateWorkCenter.generated.ts +6 -0
  110. package/src/modules/manufacturing/command/updateWorkCenter.test.ts +152 -0
  111. package/src/modules/manufacturing/command/updateWorkCenter.ts +137 -0
  112. package/src/modules/manufacturing/db/.gitkeep +0 -0
  113. package/src/modules/manufacturing/db/billOfMaterial.ts +70 -0
  114. package/src/modules/manufacturing/db/billOfMaterialLine.ts +49 -0
  115. package/src/modules/manufacturing/db/costVarianceLine.ts +53 -0
  116. package/src/modules/manufacturing/db/manufacturingCostLine.ts +35 -0
  117. package/src/modules/manufacturing/db/manufacturingCostSettlementRecord.ts +39 -0
  118. package/src/modules/manufacturing/db/manufacturingCostSummary.ts +59 -0
  119. package/src/modules/manufacturing/db/productionOrder.ts +83 -0
  120. package/src/modules/manufacturing/db/productionOrderBomSnapshot.ts +44 -0
  121. package/src/modules/manufacturing/db/productionOrderCostBaseline.ts +44 -0
  122. package/src/modules/manufacturing/db/productionOrderMaterialRequirement.ts +57 -0
  123. package/src/modules/manufacturing/db/productionOrderRoutingSnapshot.ts +43 -0
  124. package/src/modules/manufacturing/db/routing.ts +63 -0
  125. package/src/modules/manufacturing/db/routingOperation.ts +57 -0
  126. package/src/modules/manufacturing/db/workCenter.ts +87 -0
  127. package/src/modules/manufacturing/db/workOrder.ts +65 -0
  128. package/src/modules/manufacturing/db/workOrderExecutionEvent.ts +54 -0
  129. package/src/modules/manufacturing/docs/commands/ActivateBillOfMaterial.md +50 -0
  130. package/src/modules/manufacturing/docs/commands/ActivateRouting.md +48 -0
  131. package/src/modules/manufacturing/docs/commands/ActivateWorkCenter.md +49 -0
  132. package/src/modules/manufacturing/docs/commands/CancelProductionOrder.md +48 -0
  133. package/src/modules/manufacturing/docs/commands/CloseProductionOrder.md +46 -0
  134. package/src/modules/manufacturing/docs/commands/CompleteProductionOrder.md +48 -0
  135. package/src/modules/manufacturing/docs/commands/CompleteWorkOrder.md +66 -0
  136. package/src/modules/manufacturing/docs/commands/CreateBillOfMaterial.md +54 -0
  137. package/src/modules/manufacturing/docs/commands/CreateProductionOrder.md +49 -0
  138. package/src/modules/manufacturing/docs/commands/CreateRouting.md +50 -0
  139. package/src/modules/manufacturing/docs/commands/CreateWorkCenter.md +51 -0
  140. package/src/modules/manufacturing/docs/commands/DeactivateBillOfMaterial.md +45 -0
  141. package/src/modules/manufacturing/docs/commands/DeactivateRouting.md +45 -0
  142. package/src/modules/manufacturing/docs/commands/DeactivateWorkCenter.md +45 -0
  143. package/src/modules/manufacturing/docs/commands/PauseWorkOrder.md +44 -0
  144. package/src/modules/manufacturing/docs/commands/RecordInventoryIssueOutcome.md +59 -0
  145. package/src/modules/manufacturing/docs/commands/RecordManufacturingCostSettlementAcknowledgment.md +49 -0
  146. package/src/modules/manufacturing/docs/commands/ReleaseProductionOrder.md +57 -0
  147. package/src/modules/manufacturing/docs/commands/ReopenProductionOrder.md +54 -0
  148. package/src/modules/manufacturing/docs/commands/ReportWorkOrderProgress.md +53 -0
  149. package/src/modules/manufacturing/docs/commands/RescheduleProductionOrder.md +45 -0
  150. package/src/modules/manufacturing/docs/commands/ResumeWorkOrder.md +44 -0
  151. package/src/modules/manufacturing/docs/commands/ReviewManufacturingCostSummary.md +52 -0
  152. package/src/modules/manufacturing/docs/commands/StartWorkOrder.md +46 -0
  153. package/src/modules/manufacturing/docs/commands/TechnicallyCompleteProductionOrder.md +51 -0
  154. package/src/modules/manufacturing/docs/commands/UnreleaseProductionOrder.md +46 -0
  155. package/src/modules/manufacturing/docs/commands/UpdateBillOfMaterial.md +48 -0
  156. package/src/modules/manufacturing/docs/commands/UpdateProductionOrder.md +48 -0
  157. package/src/modules/manufacturing/docs/commands/UpdateRouting.md +52 -0
  158. package/src/modules/manufacturing/docs/commands/UpdateWorkCenter.md +48 -0
  159. package/src/modules/manufacturing/docs/features/bill-of-material-management.md +83 -0
  160. package/src/modules/manufacturing/docs/features/manufacturing-cost-and-variance.md +191 -0
  161. package/src/modules/manufacturing/docs/features/production-order-lifecycle.md +103 -0
  162. package/src/modules/manufacturing/docs/features/routing-and-work-center-definition.md +63 -0
  163. package/src/modules/manufacturing/docs/features/work-order-execution.md +115 -0
  164. package/src/modules/manufacturing/docs/models/BillOfMaterial.md +60 -0
  165. package/src/modules/manufacturing/docs/models/ManufacturingCostSummary.md +66 -0
  166. package/src/modules/manufacturing/docs/models/ProductionOrder.md +76 -0
  167. package/src/modules/manufacturing/docs/models/Routing.md +58 -0
  168. package/src/modules/manufacturing/docs/models/WorkCenter.md +56 -0
  169. package/src/modules/manufacturing/docs/models/WorkOrder.md +63 -0
  170. package/src/modules/manufacturing/docs/queries/DetectBillOfMaterialCircularReference.md +39 -0
  171. package/src/modules/manufacturing/docs/queries/ExplodeBillOfMaterial.md +56 -0
  172. package/src/modules/manufacturing/docs/queries/GetBillOfMaterial.md +37 -0
  173. package/src/modules/manufacturing/docs/queries/GetManufacturingCostSummary.md +39 -0
  174. package/src/modules/manufacturing/docs/queries/GetProductionOrder.md +37 -0
  175. package/src/modules/manufacturing/docs/queries/GetRouting.md +39 -0
  176. package/src/modules/manufacturing/docs/queries/GetWorkCenter.md +35 -0
  177. package/src/modules/manufacturing/docs/queries/GetWorkOrder.md +38 -0
  178. package/src/modules/manufacturing/docs/queries/ListBillOfMaterialsByItem.md +42 -0
  179. package/src/modules/manufacturing/docs/queries/ListManufacturingCostSummariesByStatus.md +41 -0
  180. package/src/modules/manufacturing/docs/queries/ListProductionOrdersByStatus.md +41 -0
  181. package/src/modules/manufacturing/docs/queries/ListRoutingsByItem.md +42 -0
  182. package/src/modules/manufacturing/docs/queries/ListWorkCentersBySite.md +38 -0
  183. package/src/modules/manufacturing/docs/queries/ListWorkOrdersByProductionOrder.md +39 -0
  184. package/src/modules/manufacturing/docs/queries/ListWorkOrdersByWorkCenter.md +43 -0
  185. package/src/modules/manufacturing/executor/.gitkeep +0 -0
  186. package/src/modules/manufacturing/generated/enums.ts +113 -0
  187. package/src/modules/manufacturing/generated/kysely-tailordb.ts +247 -0
  188. package/src/modules/manufacturing/index.ts +2 -0
  189. package/src/modules/manufacturing/lib/_db_deps.ts +22 -0
  190. package/src/modules/manufacturing/lib/errors.generated.ts +592 -0
  191. package/src/modules/manufacturing/lib/permissions.generated.ts +35 -0
  192. package/src/modules/manufacturing/lib/types.ts +111 -0
  193. package/src/modules/manufacturing/module.ts +226 -0
  194. package/src/modules/manufacturing/permissions.ts +3 -0
  195. package/src/modules/manufacturing/query/.gitkeep +0 -0
  196. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.generated.ts +5 -0
  197. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.test.ts +115 -0
  198. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.ts +79 -0
  199. package/src/modules/manufacturing/query/explodeBillOfMaterial.generated.ts +5 -0
  200. package/src/modules/manufacturing/query/explodeBillOfMaterial.test.ts +445 -0
  201. package/src/modules/manufacturing/query/explodeBillOfMaterial.ts +306 -0
  202. package/src/modules/manufacturing/query/getBillOfMaterial.generated.ts +5 -0
  203. package/src/modules/manufacturing/query/getBillOfMaterial.test.ts +64 -0
  204. package/src/modules/manufacturing/query/getBillOfMaterial.ts +27 -0
  205. package/src/modules/manufacturing/query/getManufacturingCostSummary.generated.ts +5 -0
  206. package/src/modules/manufacturing/query/getManufacturingCostSummary.test.ts +147 -0
  207. package/src/modules/manufacturing/query/getManufacturingCostSummary.ts +46 -0
  208. package/src/modules/manufacturing/query/getProductionOrder.generated.ts +5 -0
  209. package/src/modules/manufacturing/query/getProductionOrder.test.ts +139 -0
  210. package/src/modules/manufacturing/query/getProductionOrder.ts +84 -0
  211. package/src/modules/manufacturing/query/getRouting.generated.ts +5 -0
  212. package/src/modules/manufacturing/query/getRouting.test.ts +71 -0
  213. package/src/modules/manufacturing/query/getRouting.ts +34 -0
  214. package/src/modules/manufacturing/query/getWorkCenter.generated.ts +5 -0
  215. package/src/modules/manufacturing/query/getWorkCenter.test.ts +37 -0
  216. package/src/modules/manufacturing/query/getWorkCenter.ts +21 -0
  217. package/src/modules/manufacturing/query/getWorkOrder.generated.ts +5 -0
  218. package/src/modules/manufacturing/query/getWorkOrder.test.ts +73 -0
  219. package/src/modules/manufacturing/query/getWorkOrder.ts +28 -0
  220. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.generated.ts +5 -0
  221. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.test.ts +107 -0
  222. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.ts +58 -0
  223. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.generated.ts +5 -0
  224. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.test.ts +96 -0
  225. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.ts +77 -0
  226. package/src/modules/manufacturing/query/listProductionOrdersByStatus.generated.ts +5 -0
  227. package/src/modules/manufacturing/query/listProductionOrdersByStatus.test.ts +121 -0
  228. package/src/modules/manufacturing/query/listProductionOrdersByStatus.ts +83 -0
  229. package/src/modules/manufacturing/query/listRoutingsByItem.generated.ts +5 -0
  230. package/src/modules/manufacturing/query/listRoutingsByItem.test.ts +110 -0
  231. package/src/modules/manufacturing/query/listRoutingsByItem.ts +54 -0
  232. package/src/modules/manufacturing/query/listWorkCentersBySite.generated.ts +5 -0
  233. package/src/modules/manufacturing/query/listWorkCentersBySite.test.ts +81 -0
  234. package/src/modules/manufacturing/query/listWorkCentersBySite.ts +70 -0
  235. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.generated.ts +5 -0
  236. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.test.ts +102 -0
  237. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.ts +53 -0
  238. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.generated.ts +5 -0
  239. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.test.ts +143 -0
  240. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.ts +56 -0
  241. package/src/modules/manufacturing/seed/index.ts +19 -0
  242. package/src/modules/manufacturing/tailor.config.ts +13 -0
  243. package/src/modules/manufacturing/tailor.d.ts +13 -0
  244. package/src/modules/manufacturing/testing/commandTestUtils.ts +29 -0
  245. package/src/modules/manufacturing/testing/fixtures.ts +402 -0
  246. package/templates/scaffold/app/backend/package.json +9 -2
  247. package/templates/scaffold/app/backend/src/tests/utils/graphql-client.ts +66 -0
  248. package/templates/scaffold/app/backend/src/tests/utils/setup.ts +21 -0
  249. package/templates/scaffold/app/backend/tsconfig.json +9 -2
  250. package/templates/scaffold/app/backend/vitest.config.ts +35 -0
  251. package/templates/scaffold/app/frontend/package.json +2 -2
@@ -0,0 +1,445 @@
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 {
6
+ BomReferenceRequiredError,
7
+ EffectivityDateRequiredError,
8
+ BomNotFoundError,
9
+ BomNotEffectiveOnDateError,
10
+ ChildBomNotResolvedError,
11
+ BomCircularReferenceDetectedError,
12
+ } from "../lib/errors.generated";
13
+ import { run } from "./explodeBillOfMaterial";
14
+
15
+ const effectivityDate = new Date("2024-06-01T00:00:00.000Z");
16
+
17
+ const rootBom = {
18
+ id: "bom-1",
19
+ parentItemId: "item-1",
20
+ companyId: "company-1",
21
+ siteId: null,
22
+ bomType: "MANUFACTURE",
23
+ effectivityStartDate: null,
24
+ effectivityEndDate: null,
25
+ defaultSelection: true,
26
+ revisionNumber: "V1",
27
+ status: "ACTIVE",
28
+ createdAt: new Date("2024-01-01T00:00:00.000Z"),
29
+ updatedAt: null,
30
+ };
31
+
32
+ describe("explodeBillOfMaterial", () => {
33
+ it("explodes a multi-level manufacture BOM into recursive component requirements", async () => {
34
+ const { db, spies } = createMockDb<DB>();
35
+ // root BOM
36
+ spies.select.mockReturnValueOnce(rootBom);
37
+ // root BOM lines
38
+ spies.select.mockReturnValueOnce([
39
+ {
40
+ itemId: "item-2",
41
+ requiredQuantity: 2,
42
+ unitOfMeasure: "EA",
43
+ isSubassembly: false,
44
+ billOfMaterialId: "bom-1",
45
+ },
46
+ {
47
+ itemId: "item-3",
48
+ requiredQuantity: 1,
49
+ unitOfMeasure: "EA",
50
+ isSubassembly: true,
51
+ billOfMaterialId: "bom-1",
52
+ },
53
+ ]);
54
+ // active child BOMs for item-3
55
+ spies.select.mockReturnValueOnce([
56
+ {
57
+ id: "bom-child",
58
+ parentItemId: "item-3",
59
+ companyId: "company-1",
60
+ status: "ACTIVE",
61
+ bomType: "MANUFACTURE",
62
+ effectivityStartDate: null,
63
+ effectivityEndDate: null,
64
+ },
65
+ ]);
66
+ // child BOM lines
67
+ spies.select.mockReturnValueOnce([
68
+ {
69
+ itemId: "item-4",
70
+ requiredQuantity: 3,
71
+ unitOfMeasure: "EA",
72
+ isSubassembly: false,
73
+ billOfMaterialId: "bom-child",
74
+ },
75
+ ]);
76
+
77
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
78
+ const value = expectOk<{ components: unknown[] }>(result as never);
79
+
80
+ expect(value.components).toHaveLength(3);
81
+ });
82
+
83
+ it("flattens phantom subassemblies into the nearest non-phantom parent path", async () => {
84
+ const { db, spies } = createMockDb<DB>();
85
+ // root BOM (MANUFACTURE type with a PHANTOM subassembly)
86
+ spies.select.mockReturnValueOnce(rootBom);
87
+ // root BOM lines - one subassembly that is phantom
88
+ spies.select.mockReturnValueOnce([
89
+ {
90
+ itemId: "item-phantom",
91
+ requiredQuantity: 2,
92
+ unitOfMeasure: "EA",
93
+ isSubassembly: true,
94
+ billOfMaterialId: "bom-1",
95
+ },
96
+ ]);
97
+ // Resolve child BOMs for item-phantom — it's a PHANTOM BOM
98
+ spies.select.mockReturnValueOnce([
99
+ {
100
+ id: "bom-phantom",
101
+ parentItemId: "item-phantom",
102
+ companyId: "company-1",
103
+ status: "ACTIVE",
104
+ bomType: "PHANTOM",
105
+ effectivityStartDate: null,
106
+ effectivityEndDate: null,
107
+ },
108
+ ]);
109
+ // PHANTOM BOM lines — these should be flattened
110
+ spies.select.mockReturnValueOnce([
111
+ {
112
+ itemId: "item-raw-1",
113
+ requiredQuantity: 3,
114
+ unitOfMeasure: "EA",
115
+ isSubassembly: false,
116
+ billOfMaterialId: "bom-phantom",
117
+ },
118
+ {
119
+ itemId: "item-raw-2",
120
+ requiredQuantity: 5,
121
+ unitOfMeasure: "EA",
122
+ isSubassembly: false,
123
+ billOfMaterialId: "bom-phantom",
124
+ },
125
+ ]);
126
+
127
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
128
+ const value = expectOk<{
129
+ components: { itemId: string; requiredQuantity: number; depth: number }[];
130
+ }>(result as never);
131
+
132
+ // Phantom subassembly is NOT in the output — its children are flattened
133
+ expect(value.components).toHaveLength(2);
134
+ const raw1 = value.components.find((c) => c.itemId === "item-raw-1");
135
+ expect(raw1).toBeDefined();
136
+ expect(raw1!.requiredQuantity).toBe(6); // 2 * 3
137
+ expect(raw1!.depth).toBe(1); // flattened to parent depth
138
+ const raw2 = value.components.find((c) => c.itemId === "item-raw-2");
139
+ expect(raw2).toBeDefined();
140
+ expect(raw2!.requiredQuantity).toBe(10); // 2 * 5
141
+ expect(raw2!.depth).toBe(1);
142
+ });
143
+
144
+ it("expands a kit BOM into component issue requirements without work-order context for the kit parent", async () => {
145
+ const { db, spies } = createMockDb<DB>();
146
+ const kitBom = { ...rootBom, bomType: "KIT" };
147
+ // root BOM (kit type)
148
+ spies.select.mockReturnValueOnce(kitBom);
149
+ // kit BOM lines — includes one marked as subassembly
150
+ spies.select.mockReturnValueOnce([
151
+ {
152
+ itemId: "item-2",
153
+ requiredQuantity: 1,
154
+ unitOfMeasure: "EA",
155
+ isSubassembly: false,
156
+ billOfMaterialId: "bom-1",
157
+ },
158
+ {
159
+ itemId: "item-3",
160
+ requiredQuantity: 2,
161
+ unitOfMeasure: "EA",
162
+ isSubassembly: true, // even subassemblies treated as issue-only
163
+ billOfMaterialId: "bom-1",
164
+ },
165
+ ]);
166
+
167
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
168
+ const value = expectOk<{
169
+ components: { itemId: string; isSubassembly: boolean }[];
170
+ }>(result as never);
171
+
172
+ expect(value.components).toHaveLength(2);
173
+ // All components are issue-only (isSubassembly = false)
174
+ expect(value.components.every((c) => c.isSubassembly === false)).toBe(true);
175
+ });
176
+
177
+ it("resolves lower-level child BOM versions using the requested effectivity date", async () => {
178
+ const { db, spies } = createMockDb<DB>();
179
+ // root BOM
180
+ spies.select.mockReturnValueOnce(rootBom);
181
+ // root BOM lines - one subassembly
182
+ spies.select.mockReturnValueOnce([
183
+ {
184
+ itemId: "item-3",
185
+ requiredQuantity: 1,
186
+ unitOfMeasure: "EA",
187
+ isSubassembly: true,
188
+ billOfMaterialId: "bom-1",
189
+ },
190
+ ]);
191
+ // active child BOMs for item-3 - two versions, only one effective on date
192
+ spies.select.mockReturnValueOnce([
193
+ {
194
+ id: "bom-child-old",
195
+ parentItemId: "item-3",
196
+ companyId: "company-1",
197
+ status: "ACTIVE",
198
+ bomType: "MANUFACTURE",
199
+ effectivityStartDate: null,
200
+ effectivityEndDate: "2024-01-01T00:00:00.000Z",
201
+ },
202
+ {
203
+ id: "bom-child-new",
204
+ parentItemId: "item-3",
205
+ companyId: "company-1",
206
+ status: "ACTIVE",
207
+ bomType: "MANUFACTURE",
208
+ effectivityStartDate: "2024-02-01T00:00:00.000Z",
209
+ effectivityEndDate: null,
210
+ },
211
+ ]);
212
+ // child BOM lines for the effective version
213
+ spies.select.mockReturnValueOnce([
214
+ {
215
+ itemId: "item-5",
216
+ requiredQuantity: 4,
217
+ unitOfMeasure: "EA",
218
+ isSubassembly: false,
219
+ billOfMaterialId: "bom-child-new",
220
+ },
221
+ ]);
222
+
223
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
224
+ const value = expectOk<{ components: unknown[] }>(result as never);
225
+
226
+ // subassembly line + its child component
227
+ expect(value.components).toHaveLength(2);
228
+ });
229
+
230
+ it("resolves lower-level child BOMs using the applicable site scope", async () => {
231
+ const { db, spies } = createMockDb<DB>();
232
+ spies.select.mockReturnValueOnce({ ...rootBom, siteId: "site-1" });
233
+ spies.select.mockReturnValueOnce([
234
+ {
235
+ itemId: "item-3",
236
+ requiredQuantity: 1,
237
+ unitOfMeasure: "EA",
238
+ isSubassembly: true,
239
+ billOfMaterialId: "bom-1",
240
+ },
241
+ ]);
242
+ spies.select.mockReturnValueOnce([
243
+ {
244
+ id: "bom-child-other-site",
245
+ parentItemId: "item-3",
246
+ companyId: "company-1",
247
+ siteId: "site-2",
248
+ status: "ACTIVE",
249
+ bomType: "MANUFACTURE",
250
+ effectivityStartDate: null,
251
+ effectivityEndDate: null,
252
+ },
253
+ {
254
+ id: "bom-child-site-1",
255
+ parentItemId: "item-3",
256
+ companyId: "company-1",
257
+ siteId: "site-1",
258
+ status: "ACTIVE",
259
+ bomType: "MANUFACTURE",
260
+ effectivityStartDate: null,
261
+ effectivityEndDate: null,
262
+ },
263
+ ]);
264
+ spies.select.mockReturnValueOnce([
265
+ {
266
+ itemId: "item-5",
267
+ requiredQuantity: 4,
268
+ unitOfMeasure: "EA",
269
+ isSubassembly: false,
270
+ billOfMaterialId: "bom-child-site-1",
271
+ },
272
+ ]);
273
+
274
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
275
+ const value = expectOk<{ components: { bomPath: string[] }[] }>(result as never);
276
+
277
+ expect(
278
+ value.components.some((component) => component.bomPath.includes("bom-child-site-1")),
279
+ ).toBe(true);
280
+ expect(
281
+ value.components.some((component) => component.bomPath.includes("bom-child-other-site")),
282
+ ).toBe(false);
283
+ });
284
+
285
+ it("returns cumulative quantities and source BOM path metadata for exploded components", async () => {
286
+ const { db, spies } = createMockDb<DB>();
287
+ // root BOM
288
+ spies.select.mockReturnValueOnce(rootBom);
289
+ // root BOM lines - subassembly with quantity multiplier
290
+ spies.select.mockReturnValueOnce([
291
+ {
292
+ itemId: "item-3",
293
+ requiredQuantity: 2,
294
+ unitOfMeasure: "EA",
295
+ isSubassembly: true,
296
+ billOfMaterialId: "bom-1",
297
+ },
298
+ ]);
299
+ // active child BOMs for item-3
300
+ spies.select.mockReturnValueOnce([
301
+ {
302
+ id: "bom-child",
303
+ parentItemId: "item-3",
304
+ companyId: "company-1",
305
+ status: "ACTIVE",
306
+ bomType: "MANUFACTURE",
307
+ effectivityStartDate: null,
308
+ effectivityEndDate: null,
309
+ },
310
+ ]);
311
+ // child BOM lines
312
+ spies.select.mockReturnValueOnce([
313
+ {
314
+ itemId: "item-4",
315
+ requiredQuantity: 3,
316
+ unitOfMeasure: "EA",
317
+ isSubassembly: false,
318
+ billOfMaterialId: "bom-child",
319
+ },
320
+ ]);
321
+
322
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
323
+ const value = expectOk<{
324
+ components: {
325
+ itemId: string;
326
+ requiredQuantity: number;
327
+ depth: number;
328
+ bomPath: string[];
329
+ }[];
330
+ }>(result as never);
331
+
332
+ // item-3 subassembly: qty 2, depth 1
333
+ const subassembly = value.components.find((c) => c.itemId === "item-3");
334
+ expect(subassembly).toBeDefined();
335
+ expect(subassembly!.requiredQuantity).toBe(2);
336
+ expect(subassembly!.depth).toBe(1);
337
+
338
+ // item-4 leaf: qty 2 * 3 = 6, depth 2
339
+ const leaf = value.components.find((c) => c.itemId === "item-4");
340
+ expect(leaf).toBeDefined();
341
+ expect(leaf!.requiredQuantity).toBe(6);
342
+ expect(leaf!.depth).toBe(2);
343
+ expect(leaf!.bomPath).toContain("bom-1");
344
+ expect(leaf!.bomPath).toContain("bom-child");
345
+ });
346
+
347
+ it("returns error when the BOM reference is missing", async () => {
348
+ const { db } = createMockDb<DB>();
349
+
350
+ const result = await run(db, { bomId: "", effectivityDate });
351
+ expectErr(result as never, BomReferenceRequiredError);
352
+ });
353
+
354
+ it("returns error when the effectivity date is missing", async () => {
355
+ const { db } = createMockDb<DB>();
356
+
357
+ const result = await run(db, {
358
+ bomId: "bom-1",
359
+ effectivityDate: undefined as unknown as Date,
360
+ });
361
+ expectErr(result as never, EffectivityDateRequiredError);
362
+ });
363
+
364
+ it("returns error when the root BOM is not found", async () => {
365
+ const { db, spies } = createMockDb<DB>();
366
+ spies.select.mockReturnValueOnce(undefined);
367
+
368
+ const result = await run(db, { bomId: "nonexistent", effectivityDate });
369
+ expectErr(result as never, BomNotFoundError);
370
+ });
371
+
372
+ it("returns error when the root BOM is not effective on the requested date", async () => {
373
+ const { db, spies } = createMockDb<DB>();
374
+ const expiredBom = {
375
+ ...rootBom,
376
+ effectivityEndDate: "2023-01-01T00:00:00.000Z",
377
+ };
378
+ spies.select.mockReturnValueOnce(expiredBom);
379
+
380
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
381
+ expectErr(result as never, BomNotEffectiveOnDateError);
382
+ });
383
+
384
+ it("returns error when a required lower-level child BOM cannot be resolved", async () => {
385
+ const { db, spies } = createMockDb<DB>();
386
+ // root BOM
387
+ spies.select.mockReturnValueOnce(rootBom);
388
+ // root BOM lines - one subassembly
389
+ spies.select.mockReturnValueOnce([
390
+ {
391
+ itemId: "item-3",
392
+ requiredQuantity: 1,
393
+ unitOfMeasure: "EA",
394
+ isSubassembly: true,
395
+ billOfMaterialId: "bom-1",
396
+ },
397
+ ]);
398
+ // no active child BOMs for item-3
399
+ spies.select.mockReturnValueOnce([]);
400
+
401
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
402
+ expectErr(result as never, ChildBomNotResolvedError);
403
+ });
404
+
405
+ it("returns error when the explosion detects a circular BOM reference", async () => {
406
+ const { db, spies } = createMockDb<DB>();
407
+ // root BOM
408
+ spies.select.mockReturnValueOnce(rootBom);
409
+ // root BOM lines - subassembly item-3
410
+ spies.select.mockReturnValueOnce([
411
+ {
412
+ itemId: "item-3",
413
+ requiredQuantity: 1,
414
+ unitOfMeasure: "EA",
415
+ isSubassembly: true,
416
+ billOfMaterialId: "bom-1",
417
+ },
418
+ ]);
419
+ // active child BOMs for item-3
420
+ spies.select.mockReturnValueOnce([
421
+ {
422
+ id: "bom-child",
423
+ parentItemId: "item-3",
424
+ companyId: "company-1",
425
+ status: "ACTIVE",
426
+ bomType: "MANUFACTURE",
427
+ effectivityStartDate: null,
428
+ effectivityEndDate: null,
429
+ },
430
+ ]);
431
+ // child BOM lines - subassembly pointing back to item-3 (circular!)
432
+ spies.select.mockReturnValueOnce([
433
+ {
434
+ itemId: "item-3",
435
+ requiredQuantity: 1,
436
+ unitOfMeasure: "EA",
437
+ isSubassembly: true,
438
+ billOfMaterialId: "bom-child",
439
+ },
440
+ ]);
441
+
442
+ const result = await run(db, { bomId: "bom-1", effectivityDate });
443
+ expectErr(result as never, BomCircularReferenceDetectedError);
444
+ });
445
+ });