@tailor-platform/erp-kit 0.5.0 → 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 (262) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/cli.mjs +139 -35
  3. package/package.json +1 -1
  4. package/skills/erp-kit-app-5-impl-backend/SKILL.md +10 -5
  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/init-module.test.ts +17 -3
  9. package/src/commands/init-module.ts +0 -12
  10. package/src/commands/lib/discovery.test.ts +13 -3
  11. package/src/commands/lib/discovery.ts +10 -2
  12. package/src/commands/lib/paths.ts +4 -2
  13. package/src/commands/lib/sync-check-tests.test.ts +84 -6
  14. package/src/commands/lib/sync-check-tests.ts +63 -3
  15. package/src/commands/sync-check.ts +7 -3
  16. package/src/generator/generate-app-code.ts +51 -16
  17. package/src/generator/generate-code-boilerplate.test.ts +9 -1
  18. package/src/generator/generate-stubs.ts +4 -0
  19. package/src/generator/scaffold.ts +6 -2
  20. package/src/generator/stub-templates.test.ts +11 -0
  21. package/src/generator/stub-templates.ts +22 -1
  22. package/src/mdschema.ts +39 -3
  23. package/src/modules/inventory/docs/features/inventory-adjustment.md +2 -1
  24. package/src/modules/inventory/docs/features/scrap-management.md +39 -1
  25. package/src/modules/manufacturing/README.md +63 -0
  26. package/src/modules/manufacturing/command/activateBillOfMaterial.generated.ts +6 -0
  27. package/src/modules/manufacturing/command/activateBillOfMaterial.test.ts +166 -0
  28. package/src/modules/manufacturing/command/activateBillOfMaterial.ts +173 -0
  29. package/src/modules/manufacturing/command/activateRouting.generated.ts +6 -0
  30. package/src/modules/manufacturing/command/activateRouting.test.ts +152 -0
  31. package/src/modules/manufacturing/command/activateRouting.ts +92 -0
  32. package/src/modules/manufacturing/command/activateWorkCenter.generated.ts +6 -0
  33. package/src/modules/manufacturing/command/activateWorkCenter.test.ts +135 -0
  34. package/src/modules/manufacturing/command/activateWorkCenter.ts +91 -0
  35. package/src/modules/manufacturing/command/cancelProductionOrder.generated.ts +6 -0
  36. package/src/modules/manufacturing/command/cancelProductionOrder.test.ts +151 -0
  37. package/src/modules/manufacturing/command/cancelProductionOrder.ts +114 -0
  38. package/src/modules/manufacturing/command/closeProductionOrder.generated.ts +6 -0
  39. package/src/modules/manufacturing/command/closeProductionOrder.test.ts +126 -0
  40. package/src/modules/manufacturing/command/closeProductionOrder.ts +87 -0
  41. package/src/modules/manufacturing/command/completeProductionOrder.generated.ts +6 -0
  42. package/src/modules/manufacturing/command/completeProductionOrder.test.ts +132 -0
  43. package/src/modules/manufacturing/command/completeProductionOrder.ts +97 -0
  44. package/src/modules/manufacturing/command/completeWorkOrder.generated.ts +6 -0
  45. package/src/modules/manufacturing/command/completeWorkOrder.test.ts +369 -0
  46. package/src/modules/manufacturing/command/completeWorkOrder.ts +212 -0
  47. package/src/modules/manufacturing/command/createBillOfMaterial.generated.ts +6 -0
  48. package/src/modules/manufacturing/command/createBillOfMaterial.test.ts +210 -0
  49. package/src/modules/manufacturing/command/createBillOfMaterial.ts +176 -0
  50. package/src/modules/manufacturing/command/createProductionOrder.generated.ts +6 -0
  51. package/src/modules/manufacturing/command/createProductionOrder.test.ts +160 -0
  52. package/src/modules/manufacturing/command/createProductionOrder.ts +129 -0
  53. package/src/modules/manufacturing/command/createRouting.generated.ts +6 -0
  54. package/src/modules/manufacturing/command/createRouting.test.ts +168 -0
  55. package/src/modules/manufacturing/command/createRouting.ts +128 -0
  56. package/src/modules/manufacturing/command/createWorkCenter.generated.ts +6 -0
  57. package/src/modules/manufacturing/command/createWorkCenter.test.ts +148 -0
  58. package/src/modules/manufacturing/command/createWorkCenter.ts +131 -0
  59. package/src/modules/manufacturing/command/deactivateBillOfMaterial.generated.ts +6 -0
  60. package/src/modules/manufacturing/command/deactivateBillOfMaterial.test.ts +103 -0
  61. package/src/modules/manufacturing/command/deactivateBillOfMaterial.ts +78 -0
  62. package/src/modules/manufacturing/command/deactivateRouting.generated.ts +6 -0
  63. package/src/modules/manufacturing/command/deactivateRouting.test.ts +112 -0
  64. package/src/modules/manufacturing/command/deactivateRouting.ts +76 -0
  65. package/src/modules/manufacturing/command/deactivateWorkCenter.generated.ts +6 -0
  66. package/src/modules/manufacturing/command/deactivateWorkCenter.test.ts +113 -0
  67. package/src/modules/manufacturing/command/deactivateWorkCenter.ts +85 -0
  68. package/src/modules/manufacturing/command/pauseWorkOrder.generated.ts +6 -0
  69. package/src/modules/manufacturing/command/pauseWorkOrder.test.ts +118 -0
  70. package/src/modules/manufacturing/command/pauseWorkOrder.ts +82 -0
  71. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.generated.ts +6 -0
  72. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.test.ts +183 -0
  73. package/src/modules/manufacturing/command/recordInventoryIssueOutcome.ts +139 -0
  74. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.generated.ts +6 -0
  75. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.test.ts +120 -0
  76. package/src/modules/manufacturing/command/recordManufacturingCostSettlementAcknowledgment.ts +110 -0
  77. package/src/modules/manufacturing/command/releaseProductionOrder.generated.ts +6 -0
  78. package/src/modules/manufacturing/command/releaseProductionOrder.test.ts +220 -0
  79. package/src/modules/manufacturing/command/releaseProductionOrder.ts +450 -0
  80. package/src/modules/manufacturing/command/reopenProductionOrder.generated.ts +6 -0
  81. package/src/modules/manufacturing/command/reopenProductionOrder.test.ts +196 -0
  82. package/src/modules/manufacturing/command/reopenProductionOrder.ts +98 -0
  83. package/src/modules/manufacturing/command/reportWorkOrderProgress.generated.ts +6 -0
  84. package/src/modules/manufacturing/command/reportWorkOrderProgress.test.ts +204 -0
  85. package/src/modules/manufacturing/command/reportWorkOrderProgress.ts +129 -0
  86. package/src/modules/manufacturing/command/rescheduleProductionOrder.generated.ts +6 -0
  87. package/src/modules/manufacturing/command/rescheduleProductionOrder.test.ts +185 -0
  88. package/src/modules/manufacturing/command/rescheduleProductionOrder.ts +95 -0
  89. package/src/modules/manufacturing/command/resumeWorkOrder.generated.ts +6 -0
  90. package/src/modules/manufacturing/command/resumeWorkOrder.test.ts +122 -0
  91. package/src/modules/manufacturing/command/resumeWorkOrder.ts +94 -0
  92. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.generated.ts +6 -0
  93. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.test.ts +231 -0
  94. package/src/modules/manufacturing/command/reviewManufacturingCostSummary.ts +137 -0
  95. package/src/modules/manufacturing/command/startWorkOrder.generated.ts +6 -0
  96. package/src/modules/manufacturing/command/startWorkOrder.test.ts +118 -0
  97. package/src/modules/manufacturing/command/startWorkOrder.ts +126 -0
  98. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.generated.ts +6 -0
  99. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.test.ts +153 -0
  100. package/src/modules/manufacturing/command/technicallyCompleteProductionOrder.ts +106 -0
  101. package/src/modules/manufacturing/command/unreleaseProductionOrder.generated.ts +6 -0
  102. package/src/modules/manufacturing/command/unreleaseProductionOrder.test.ts +140 -0
  103. package/src/modules/manufacturing/command/unreleaseProductionOrder.ts +131 -0
  104. package/src/modules/manufacturing/command/updateBillOfMaterial.generated.ts +6 -0
  105. package/src/modules/manufacturing/command/updateBillOfMaterial.test.ts +149 -0
  106. package/src/modules/manufacturing/command/updateBillOfMaterial.ts +174 -0
  107. package/src/modules/manufacturing/command/updateProductionOrder.generated.ts +6 -0
  108. package/src/modules/manufacturing/command/updateProductionOrder.test.ts +112 -0
  109. package/src/modules/manufacturing/command/updateProductionOrder.ts +145 -0
  110. package/src/modules/manufacturing/command/updateRouting.generated.ts +6 -0
  111. package/src/modules/manufacturing/command/updateRouting.test.ts +211 -0
  112. package/src/modules/manufacturing/command/updateRouting.ts +124 -0
  113. package/src/modules/manufacturing/command/updateWorkCenter.generated.ts +6 -0
  114. package/src/modules/manufacturing/command/updateWorkCenter.test.ts +152 -0
  115. package/src/modules/manufacturing/command/updateWorkCenter.ts +137 -0
  116. package/src/modules/manufacturing/db/.gitkeep +0 -0
  117. package/src/modules/manufacturing/db/billOfMaterial.ts +70 -0
  118. package/src/modules/manufacturing/db/billOfMaterialLine.ts +49 -0
  119. package/src/modules/manufacturing/db/costVarianceLine.ts +53 -0
  120. package/src/modules/manufacturing/db/manufacturingCostLine.ts +35 -0
  121. package/src/modules/manufacturing/db/manufacturingCostSettlementRecord.ts +39 -0
  122. package/src/modules/manufacturing/db/manufacturingCostSummary.ts +59 -0
  123. package/src/modules/manufacturing/db/productionOrder.ts +83 -0
  124. package/src/modules/manufacturing/db/productionOrderBomSnapshot.ts +44 -0
  125. package/src/modules/manufacturing/db/productionOrderCostBaseline.ts +44 -0
  126. package/src/modules/manufacturing/db/productionOrderMaterialRequirement.ts +57 -0
  127. package/src/modules/manufacturing/db/productionOrderRoutingSnapshot.ts +43 -0
  128. package/src/modules/manufacturing/db/routing.ts +63 -0
  129. package/src/modules/manufacturing/db/routingOperation.ts +57 -0
  130. package/src/modules/manufacturing/db/workCenter.ts +87 -0
  131. package/src/modules/manufacturing/db/workOrder.ts +65 -0
  132. package/src/modules/manufacturing/db/workOrderExecutionEvent.ts +54 -0
  133. package/src/modules/manufacturing/docs/commands/ActivateBillOfMaterial.md +50 -0
  134. package/src/modules/manufacturing/docs/commands/ActivateRouting.md +48 -0
  135. package/src/modules/manufacturing/docs/commands/ActivateWorkCenter.md +49 -0
  136. package/src/modules/manufacturing/docs/commands/CancelProductionOrder.md +48 -0
  137. package/src/modules/manufacturing/docs/commands/CloseProductionOrder.md +46 -0
  138. package/src/modules/manufacturing/docs/commands/CompleteProductionOrder.md +48 -0
  139. package/src/modules/manufacturing/docs/commands/CompleteWorkOrder.md +66 -0
  140. package/src/modules/manufacturing/docs/commands/CreateBillOfMaterial.md +54 -0
  141. package/src/modules/manufacturing/docs/commands/CreateProductionOrder.md +49 -0
  142. package/src/modules/manufacturing/docs/commands/CreateRouting.md +50 -0
  143. package/src/modules/manufacturing/docs/commands/CreateWorkCenter.md +51 -0
  144. package/src/modules/manufacturing/docs/commands/DeactivateBillOfMaterial.md +45 -0
  145. package/src/modules/manufacturing/docs/commands/DeactivateRouting.md +45 -0
  146. package/src/modules/manufacturing/docs/commands/DeactivateWorkCenter.md +45 -0
  147. package/src/modules/manufacturing/docs/commands/PauseWorkOrder.md +44 -0
  148. package/src/modules/manufacturing/docs/commands/RecordInventoryIssueOutcome.md +59 -0
  149. package/src/modules/manufacturing/docs/commands/RecordManufacturingCostSettlementAcknowledgment.md +49 -0
  150. package/src/modules/manufacturing/docs/commands/ReleaseProductionOrder.md +57 -0
  151. package/src/modules/manufacturing/docs/commands/ReopenProductionOrder.md +54 -0
  152. package/src/modules/manufacturing/docs/commands/ReportWorkOrderProgress.md +53 -0
  153. package/src/modules/manufacturing/docs/commands/RescheduleProductionOrder.md +45 -0
  154. package/src/modules/manufacturing/docs/commands/ResumeWorkOrder.md +44 -0
  155. package/src/modules/manufacturing/docs/commands/ReviewManufacturingCostSummary.md +52 -0
  156. package/src/modules/manufacturing/docs/commands/StartWorkOrder.md +46 -0
  157. package/src/modules/manufacturing/docs/commands/TechnicallyCompleteProductionOrder.md +51 -0
  158. package/src/modules/manufacturing/docs/commands/UnreleaseProductionOrder.md +46 -0
  159. package/src/modules/manufacturing/docs/commands/UpdateBillOfMaterial.md +48 -0
  160. package/src/modules/manufacturing/docs/commands/UpdateProductionOrder.md +48 -0
  161. package/src/modules/manufacturing/docs/commands/UpdateRouting.md +52 -0
  162. package/src/modules/manufacturing/docs/commands/UpdateWorkCenter.md +48 -0
  163. package/src/modules/manufacturing/docs/features/bill-of-material-management.md +83 -0
  164. package/src/modules/manufacturing/docs/features/manufacturing-cost-and-variance.md +191 -0
  165. package/src/modules/manufacturing/docs/features/production-order-lifecycle.md +103 -0
  166. package/src/modules/manufacturing/docs/features/routing-and-work-center-definition.md +63 -0
  167. package/src/modules/manufacturing/docs/features/work-order-execution.md +115 -0
  168. package/src/modules/manufacturing/docs/models/BillOfMaterial.md +60 -0
  169. package/src/modules/manufacturing/docs/models/ManufacturingCostSummary.md +66 -0
  170. package/src/modules/manufacturing/docs/models/ProductionOrder.md +76 -0
  171. package/src/modules/manufacturing/docs/models/Routing.md +58 -0
  172. package/src/modules/manufacturing/docs/models/WorkCenter.md +56 -0
  173. package/src/modules/manufacturing/docs/models/WorkOrder.md +63 -0
  174. package/src/modules/manufacturing/docs/queries/DetectBillOfMaterialCircularReference.md +39 -0
  175. package/src/modules/manufacturing/docs/queries/ExplodeBillOfMaterial.md +56 -0
  176. package/src/modules/manufacturing/docs/queries/GetBillOfMaterial.md +37 -0
  177. package/src/modules/manufacturing/docs/queries/GetManufacturingCostSummary.md +39 -0
  178. package/src/modules/manufacturing/docs/queries/GetProductionOrder.md +37 -0
  179. package/src/modules/manufacturing/docs/queries/GetRouting.md +39 -0
  180. package/src/modules/manufacturing/docs/queries/GetWorkCenter.md +35 -0
  181. package/src/modules/manufacturing/docs/queries/GetWorkOrder.md +38 -0
  182. package/src/modules/manufacturing/docs/queries/ListBillOfMaterialsByItem.md +42 -0
  183. package/src/modules/manufacturing/docs/queries/ListManufacturingCostSummariesByStatus.md +41 -0
  184. package/src/modules/manufacturing/docs/queries/ListProductionOrdersByStatus.md +41 -0
  185. package/src/modules/manufacturing/docs/queries/ListRoutingsByItem.md +42 -0
  186. package/src/modules/manufacturing/docs/queries/ListWorkCentersBySite.md +38 -0
  187. package/src/modules/manufacturing/docs/queries/ListWorkOrdersByProductionOrder.md +39 -0
  188. package/src/modules/manufacturing/docs/queries/ListWorkOrdersByWorkCenter.md +43 -0
  189. package/src/modules/manufacturing/executor/.gitkeep +0 -0
  190. package/src/modules/manufacturing/generated/enums.ts +113 -0
  191. package/src/modules/manufacturing/generated/kysely-tailordb.ts +247 -0
  192. package/src/modules/manufacturing/index.ts +2 -0
  193. package/src/modules/manufacturing/lib/_db_deps.ts +22 -0
  194. package/src/modules/manufacturing/lib/errors.generated.ts +592 -0
  195. package/src/modules/manufacturing/lib/permissions.generated.ts +35 -0
  196. package/src/modules/manufacturing/lib/types.ts +111 -0
  197. package/src/modules/manufacturing/module.ts +226 -0
  198. package/src/modules/manufacturing/permissions.ts +3 -0
  199. package/src/modules/manufacturing/query/.gitkeep +0 -0
  200. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.generated.ts +5 -0
  201. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.test.ts +115 -0
  202. package/src/modules/manufacturing/query/detectBillOfMaterialCircularReference.ts +79 -0
  203. package/src/modules/manufacturing/query/explodeBillOfMaterial.generated.ts +5 -0
  204. package/src/modules/manufacturing/query/explodeBillOfMaterial.test.ts +445 -0
  205. package/src/modules/manufacturing/query/explodeBillOfMaterial.ts +306 -0
  206. package/src/modules/manufacturing/query/getBillOfMaterial.generated.ts +5 -0
  207. package/src/modules/manufacturing/query/getBillOfMaterial.test.ts +64 -0
  208. package/src/modules/manufacturing/query/getBillOfMaterial.ts +27 -0
  209. package/src/modules/manufacturing/query/getManufacturingCostSummary.generated.ts +5 -0
  210. package/src/modules/manufacturing/query/getManufacturingCostSummary.test.ts +147 -0
  211. package/src/modules/manufacturing/query/getManufacturingCostSummary.ts +46 -0
  212. package/src/modules/manufacturing/query/getProductionOrder.generated.ts +5 -0
  213. package/src/modules/manufacturing/query/getProductionOrder.test.ts +139 -0
  214. package/src/modules/manufacturing/query/getProductionOrder.ts +84 -0
  215. package/src/modules/manufacturing/query/getRouting.generated.ts +5 -0
  216. package/src/modules/manufacturing/query/getRouting.test.ts +71 -0
  217. package/src/modules/manufacturing/query/getRouting.ts +34 -0
  218. package/src/modules/manufacturing/query/getWorkCenter.generated.ts +5 -0
  219. package/src/modules/manufacturing/query/getWorkCenter.test.ts +37 -0
  220. package/src/modules/manufacturing/query/getWorkCenter.ts +21 -0
  221. package/src/modules/manufacturing/query/getWorkOrder.generated.ts +5 -0
  222. package/src/modules/manufacturing/query/getWorkOrder.test.ts +73 -0
  223. package/src/modules/manufacturing/query/getWorkOrder.ts +28 -0
  224. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.generated.ts +5 -0
  225. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.test.ts +107 -0
  226. package/src/modules/manufacturing/query/listBillOfMaterialsByItem.ts +58 -0
  227. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.generated.ts +5 -0
  228. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.test.ts +96 -0
  229. package/src/modules/manufacturing/query/listManufacturingCostSummariesByStatus.ts +77 -0
  230. package/src/modules/manufacturing/query/listProductionOrdersByStatus.generated.ts +5 -0
  231. package/src/modules/manufacturing/query/listProductionOrdersByStatus.test.ts +121 -0
  232. package/src/modules/manufacturing/query/listProductionOrdersByStatus.ts +83 -0
  233. package/src/modules/manufacturing/query/listRoutingsByItem.generated.ts +5 -0
  234. package/src/modules/manufacturing/query/listRoutingsByItem.test.ts +110 -0
  235. package/src/modules/manufacturing/query/listRoutingsByItem.ts +54 -0
  236. package/src/modules/manufacturing/query/listWorkCentersBySite.generated.ts +5 -0
  237. package/src/modules/manufacturing/query/listWorkCentersBySite.test.ts +81 -0
  238. package/src/modules/manufacturing/query/listWorkCentersBySite.ts +70 -0
  239. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.generated.ts +5 -0
  240. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.test.ts +102 -0
  241. package/src/modules/manufacturing/query/listWorkOrdersByProductionOrder.ts +53 -0
  242. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.generated.ts +5 -0
  243. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.test.ts +143 -0
  244. package/src/modules/manufacturing/query/listWorkOrdersByWorkCenter.ts +56 -0
  245. package/src/modules/manufacturing/seed/index.ts +19 -0
  246. package/src/modules/manufacturing/tailor.config.ts +13 -0
  247. package/src/modules/manufacturing/tailor.d.ts +13 -0
  248. package/src/modules/manufacturing/testing/commandTestUtils.ts +29 -0
  249. package/src/modules/manufacturing/testing/fixtures.ts +402 -0
  250. package/templates/scaffold/app/backend/package.json +9 -2
  251. package/templates/scaffold/app/backend/src/tests/utils/graphql-client.ts +66 -0
  252. package/templates/scaffold/app/backend/src/tests/utils/setup.ts +21 -0
  253. package/templates/scaffold/app/backend/tsconfig.json +9 -2
  254. package/templates/scaffold/app/backend/vitest.config.ts +35 -0
  255. package/templates/scaffold/app/frontend/package.json +2 -2
  256. package/templates/scaffold/module/__dot__gitignore +3 -0
  257. package/templates/scaffold/module/eslint.config.js +31 -0
  258. package/templates/scaffold/module/generated/kysely-tailordb.ts +3 -0
  259. package/templates/scaffold/module/lib/types.ts +1 -6
  260. package/templates/scaffold/module/package.json +26 -0
  261. package/templates/scaffold/module/tsconfig.json +16 -0
  262. /package/{templates/scaffold/module/generated → src/modules/manufacturing/command}/.gitkeep +0 -0
@@ -0,0 +1,137 @@
1
+ import type { Transaction } from "../generated/kysely-tailordb";
2
+ import {
3
+ WorkCenterNotFoundError,
4
+ InvalidCapacityError,
5
+ InvalidRateError,
6
+ InvalidOverheadMethodError,
7
+ OverheadCurrencyRequiredError,
8
+ ImmutableScopeFieldError,
9
+ } from "../lib/errors.generated";
10
+ import { ok, err, type CommandContext } from "@tailor-platform/erp-kit/module";
11
+
12
+ const VALID_OVERHEAD_METHODS = [
13
+ "PERCENT_OF_LABOR_COST",
14
+ "PERCENT_OF_MACHINE_COST",
15
+ "FIXED_AMOUNT_PER_GOOD_UNIT",
16
+ ] as const;
17
+
18
+ const IMMUTABLE_FIELDS = ["companyId", "siteId", "code"] as const;
19
+
20
+ export interface UpdateWorkCenterInput {
21
+ id: string;
22
+ capacityAssumptions?: number | null;
23
+ laborRate?: number | null;
24
+ machineRate?: number | null;
25
+ calendarReference?: string | null;
26
+ overheadAbsorptionMethod?: string | null;
27
+ overheadAbsorptionCurrency?: string | null;
28
+ }
29
+
30
+ /**
31
+ * Function: updateWorkCenter
32
+ *
33
+ * Revises mutable work-center master data such as capacity, calendar,
34
+ * rate assumptions, and overhead policy. Only DRAFT or ACTIVE work centers
35
+ * may be updated.
36
+ */
37
+ export async function run<CF extends Record<string, unknown>>(
38
+ db: Transaction,
39
+ input: UpdateWorkCenterInput & CF,
40
+ _ctx: CommandContext,
41
+ ) {
42
+ const {
43
+ id,
44
+ capacityAssumptions,
45
+ laborRate,
46
+ machineRate,
47
+ calendarReference,
48
+ overheadAbsorptionMethod,
49
+ overheadAbsorptionCurrency,
50
+ ...customFields
51
+ } = input;
52
+
53
+ // 1. Reject immutable scope fields
54
+ for (const field of IMMUTABLE_FIELDS) {
55
+ if (field in customFields) {
56
+ return err(new ImmutableScopeFieldError(field));
57
+ }
58
+ }
59
+
60
+ // 2. Check work center exists
61
+ const workCenter = await db
62
+ .selectFrom("WorkCenter")
63
+ .selectAll()
64
+ .where("id", "=", id)
65
+ .forUpdate()
66
+ .executeTakeFirst();
67
+
68
+ if (!workCenter) {
69
+ return err(new WorkCenterNotFoundError(id));
70
+ }
71
+
72
+ // 3. Only DRAFT or ACTIVE allowed
73
+ if (workCenter.status !== "DRAFT" && workCenter.status !== "ACTIVE") {
74
+ return err(new WorkCenterNotFoundError(id));
75
+ }
76
+
77
+ // 4. Validate capacity if provided
78
+ if (capacityAssumptions != null && capacityAssumptions <= 0) {
79
+ return err(new InvalidCapacityError(id));
80
+ }
81
+
82
+ // 5. Validate rates if provided
83
+ if (laborRate != null && laborRate < 0) {
84
+ return err(new InvalidRateError(id));
85
+ }
86
+ if (machineRate != null && machineRate < 0) {
87
+ return err(new InvalidRateError(id));
88
+ }
89
+
90
+ // Resolve the effective overhead method after update
91
+ const effectiveMethod =
92
+ overheadAbsorptionMethod !== undefined
93
+ ? overheadAbsorptionMethod
94
+ : workCenter.overheadAbsorptionMethod;
95
+ const effectiveCurrency =
96
+ overheadAbsorptionCurrency !== undefined
97
+ ? overheadAbsorptionCurrency
98
+ : workCenter.overheadAbsorptionCurrency;
99
+
100
+ // 6. Validate overhead method
101
+ if (
102
+ effectiveMethod != null &&
103
+ !VALID_OVERHEAD_METHODS.includes(effectiveMethod as (typeof VALID_OVERHEAD_METHODS)[number])
104
+ ) {
105
+ return err(new InvalidOverheadMethodError(id));
106
+ }
107
+
108
+ // 7. Overhead currency required for FIXED_AMOUNT_PER_GOOD_UNIT
109
+ if (effectiveMethod === "FIXED_AMOUNT_PER_GOOD_UNIT" && !effectiveCurrency) {
110
+ return err(new OverheadCurrencyRequiredError(id));
111
+ }
112
+
113
+ // 8. Build update set
114
+ const updateSet: Record<string, unknown> = {
115
+ ...(customFields as Record<string, unknown>),
116
+ updatedAt: new Date(),
117
+ };
118
+
119
+ if (capacityAssumptions !== undefined) updateSet.capacityAssumptions = capacityAssumptions;
120
+ if (laborRate !== undefined) updateSet.laborRate = laborRate;
121
+ if (machineRate !== undefined) updateSet.machineRate = machineRate;
122
+ if (calendarReference !== undefined) updateSet.calendarReference = calendarReference;
123
+ if (overheadAbsorptionMethod !== undefined)
124
+ updateSet.overheadAbsorptionMethod = overheadAbsorptionMethod;
125
+ if (overheadAbsorptionCurrency !== undefined)
126
+ updateSet.overheadAbsorptionCurrency = overheadAbsorptionCurrency;
127
+
128
+ // 9. Persist changes
129
+ const updated = await db
130
+ .updateTable("WorkCenter")
131
+ .set(updateSet)
132
+ .where("id", "=", id)
133
+ .returningAll()
134
+ .executeTakeFirstOrThrow();
135
+
136
+ return ok({ workCenter: updated });
137
+ }
File without changes
@@ -0,0 +1,70 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBField,
4
+ type TailorAnyDBType,
5
+ unsafeAllowAllGqlPermission,
6
+ unsafeAllowAllTypePermission,
7
+ } from "@tailor-platform/sdk";
8
+ import { company as companyStub, site as siteStub, item as itemStub } from "../lib/_db_deps";
9
+
10
+ export const BOM_TYPES = ["MANUFACTURE", "PHANTOM", "KIT"] as const;
11
+ export const BASE_BOM_STATUSES = ["DRAFT", "ACTIVE", "INACTIVE"] as const;
12
+
13
+ export interface CreateBillOfMaterialTypeParams<F extends Record<string, TailorAnyDBField>> {
14
+ fields?: F;
15
+ companyType: TailorAnyDBType;
16
+ siteType: TailorAnyDBType;
17
+ itemType: TailorAnyDBType;
18
+ }
19
+
20
+ export function createBillOfMaterialType<const F extends Record<string, TailorAnyDBField>>(
21
+ params: CreateBillOfMaterialTypeParams<F>,
22
+ ) {
23
+ return db
24
+ .type("BillOfMaterial", {
25
+ parentItemId: db
26
+ .uuid()
27
+ .relation({
28
+ type: "n-1",
29
+ toward: { type: params.itemType },
30
+ backward: "billOfMaterials",
31
+ })
32
+ .description("Foreign key to Item – the parent/finished item"),
33
+ companyId: db
34
+ .uuid()
35
+ .relation({
36
+ type: "n-1",
37
+ toward: { type: params.companyType },
38
+ backward: "billOfMaterials",
39
+ })
40
+ .description("Foreign key to Company"),
41
+ siteId: db
42
+ .uuid({ optional: true })
43
+ .relation({
44
+ type: "n-1",
45
+ toward: { type: params.siteType },
46
+ backward: "billOfMaterials",
47
+ })
48
+ .description("Optional foreign key to Site – site scope"),
49
+ bomType: db
50
+ .enum([...BOM_TYPES] as [string, ...string[]])
51
+ .description("BOM type: MANUFACTURE, PHANTOM, KIT"),
52
+ effectivityStartDate: db.datetime({ optional: true }).description("Effectivity start date"),
53
+ effectivityEndDate: db.datetime({ optional: true }).description("Effectivity end date"),
54
+ defaultSelection: db.bool({ optional: true }).description("Default selection flag"),
55
+ revisionNumber: db.string({ optional: true }).description("Revision number"),
56
+ status: db
57
+ .enum([...BASE_BOM_STATUSES] as [string, ...string[]])
58
+ .description("Lifecycle status: DRAFT, ACTIVE, INACTIVE"),
59
+ ...((params.fields ?? {}) as F),
60
+ ...db.fields.timestamps(),
61
+ })
62
+ .permission(unsafeAllowAllTypePermission)
63
+ .gqlPermission(unsafeAllowAllGqlPermission);
64
+ }
65
+
66
+ export const billOfMaterial = createBillOfMaterialType({
67
+ companyType: companyStub,
68
+ siteType: siteStub,
69
+ itemType: itemStub,
70
+ });
@@ -0,0 +1,49 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBType,
4
+ unsafeAllowAllGqlPermission,
5
+ unsafeAllowAllTypePermission,
6
+ } from "@tailor-platform/sdk";
7
+ import { item as itemStub } from "../lib/_db_deps";
8
+ import { billOfMaterial } from "./billOfMaterial";
9
+
10
+ export interface CreateBillOfMaterialLineTypeParams {
11
+ billOfMaterialType: TailorAnyDBType;
12
+ itemType: TailorAnyDBType;
13
+ }
14
+
15
+ export function createBillOfMaterialLineType(params: CreateBillOfMaterialLineTypeParams) {
16
+ return db
17
+ .type("BillOfMaterialLine", {
18
+ billOfMaterialId: db
19
+ .uuid()
20
+ .relation({
21
+ type: "n-1",
22
+ toward: { type: params.billOfMaterialType },
23
+ backward: "billOfMaterialLines",
24
+ })
25
+ .description("Foreign key to BillOfMaterial"),
26
+ itemId: db
27
+ .uuid()
28
+ .relation({
29
+ type: "n-1",
30
+ toward: { type: params.itemType },
31
+ backward: "bomLineReferences",
32
+ })
33
+ .description("Foreign key to Item – component item"),
34
+ requiredQuantity: db.float().description("Required quantity, must be greater than 0"),
35
+ unitOfMeasure: db.string({ optional: true }).description("Compatible unit of measure"),
36
+ scrapAssumption: db.float({ optional: true }).description("Scrap assumption percentage"),
37
+ isSubassembly: db
38
+ .bool({ optional: true })
39
+ .description("Whether this component is a subassembly"),
40
+ ...db.fields.timestamps(),
41
+ })
42
+ .permission(unsafeAllowAllTypePermission)
43
+ .gqlPermission(unsafeAllowAllGqlPermission);
44
+ }
45
+
46
+ export const billOfMaterialLine = createBillOfMaterialLineType({
47
+ billOfMaterialType: billOfMaterial,
48
+ itemType: itemStub,
49
+ });
@@ -0,0 +1,53 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBType,
4
+ unsafeAllowAllGqlPermission,
5
+ unsafeAllowAllTypePermission,
6
+ } from "@tailor-platform/sdk";
7
+ import { manufacturingCostSummary } from "./manufacturingCostSummary";
8
+
9
+ export const VARIANCE_TYPES = [
10
+ "MATERIAL_PRICE",
11
+ "MATERIAL_USAGE",
12
+ "LABOR_RATE",
13
+ "LABOR_EFFICIENCY",
14
+ "MACHINE_RATE",
15
+ "MACHINE_EFFICIENCY",
16
+ "SCRAP",
17
+ "YIELD",
18
+ ] as const;
19
+
20
+ export interface CreateCostVarianceLineTypeParams {
21
+ costSummaryType: TailorAnyDBType;
22
+ }
23
+
24
+ export function createCostVarianceLineType(params: CreateCostVarianceLineTypeParams) {
25
+ return db
26
+ .type("CostVarianceLine", {
27
+ costSummaryId: db
28
+ .uuid()
29
+ .relation({
30
+ type: "n-1",
31
+ toward: { type: params.costSummaryType },
32
+ backward: "varianceLines",
33
+ })
34
+ .description("Foreign key to ManufacturingCostSummary"),
35
+ varianceType: db
36
+ .enum([...VARIANCE_TYPES] as [string, ...string[]])
37
+ .description(
38
+ "Variance type: MATERIAL_PRICE, MATERIAL_USAGE, LABOR_RATE, LABOR_EFFICIENCY, MACHINE_RATE, MACHINE_EFFICIENCY, SCRAP, YIELD",
39
+ ),
40
+ amount: db.float().description("Variance amount"),
41
+ accountReference: db
42
+ .string({ optional: true })
43
+ .description("Optional account reference for settlement"),
44
+ variancePercentage: db.float({ optional: true }).description("Variance as a percentage"),
45
+ ...db.fields.timestamps(),
46
+ })
47
+ .permission(unsafeAllowAllTypePermission)
48
+ .gqlPermission(unsafeAllowAllGqlPermission);
49
+ }
50
+
51
+ export const costVarianceLine = createCostVarianceLineType({
52
+ costSummaryType: manufacturingCostSummary,
53
+ });
@@ -0,0 +1,35 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBType,
4
+ unsafeAllowAllGqlPermission,
5
+ unsafeAllowAllTypePermission,
6
+ } from "@tailor-platform/sdk";
7
+ import { manufacturingCostSummary } from "./manufacturingCostSummary";
8
+
9
+ export interface CreateManufacturingCostLineTypeParams {
10
+ costSummaryType: TailorAnyDBType;
11
+ }
12
+
13
+ export function createManufacturingCostLineType(params: CreateManufacturingCostLineTypeParams) {
14
+ return db
15
+ .type("ManufacturingCostLine", {
16
+ costSummaryId: db
17
+ .uuid()
18
+ .relation({
19
+ type: "n-1",
20
+ toward: { type: params.costSummaryType },
21
+ backward: "costLines",
22
+ })
23
+ .description("Foreign key to ManufacturingCostSummary"),
24
+ costType: db.string().description("Cost type: e.g. MATERIAL, LABOR, MACHINE, OVERHEAD"),
25
+ plannedAmount: db.float().description("Planned cost amount"),
26
+ actualAmount: db.float().description("Actual cost amount"),
27
+ ...db.fields.timestamps(),
28
+ })
29
+ .permission(unsafeAllowAllTypePermission)
30
+ .gqlPermission(unsafeAllowAllGqlPermission);
31
+ }
32
+
33
+ export const manufacturingCostLine = createManufacturingCostLineType({
34
+ costSummaryType: manufacturingCostSummary,
35
+ });
@@ -0,0 +1,39 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBType,
4
+ unsafeAllowAllGqlPermission,
5
+ unsafeAllowAllTypePermission,
6
+ } from "@tailor-platform/sdk";
7
+ import { manufacturingCostSummary } from "./manufacturingCostSummary";
8
+
9
+ export interface CreateManufacturingCostSettlementRecordTypeParams {
10
+ costSummaryType: TailorAnyDBType;
11
+ }
12
+
13
+ export function createManufacturingCostSettlementRecordType(
14
+ params: CreateManufacturingCostSettlementRecordTypeParams,
15
+ ) {
16
+ return db
17
+ .type("ManufacturingCostSettlementRecord", {
18
+ costSummaryId: db
19
+ .uuid()
20
+ .relation({
21
+ type: "n-1",
22
+ toward: { type: params.costSummaryType },
23
+ backward: "settlementRecords",
24
+ })
25
+ .description("Foreign key to ManufacturingCostSummary"),
26
+ settlementDate: db.datetime().description("Date of settlement"),
27
+ settlementReference: db.string().description("Reference identifier for the settlement"),
28
+ acknowledgedDate: db
29
+ .datetime({ optional: true })
30
+ .description("Date when settlement was acknowledged"),
31
+ ...db.fields.timestamps(),
32
+ })
33
+ .permission(unsafeAllowAllTypePermission)
34
+ .gqlPermission(unsafeAllowAllGqlPermission);
35
+ }
36
+
37
+ export const manufacturingCostSettlementRecord = createManufacturingCostSettlementRecordType({
38
+ costSummaryType: manufacturingCostSummary,
39
+ });
@@ -0,0 +1,59 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBType,
4
+ unsafeAllowAllGqlPermission,
5
+ unsafeAllowAllTypePermission,
6
+ } from "@tailor-platform/sdk";
7
+ import { productionOrder } from "./productionOrder";
8
+
9
+ export const BASE_COST_SUMMARY_STATUSES = [
10
+ "COLLECTING",
11
+ "PENDING_VARIANCE_REVIEW",
12
+ "VARIANCE_REVIEWED",
13
+ "SETTLED",
14
+ ] as const;
15
+
16
+ export interface CreateManufacturingCostSummaryTypeParams {
17
+ productionOrderType: TailorAnyDBType;
18
+ }
19
+
20
+ export function createManufacturingCostSummaryType(
21
+ params: CreateManufacturingCostSummaryTypeParams,
22
+ ) {
23
+ return db
24
+ .type("ManufacturingCostSummary", {
25
+ productionOrderId: db
26
+ .uuid()
27
+ .relation({
28
+ type: "1-1",
29
+ toward: { type: params.productionOrderType },
30
+ backward: "manufacturingCostSummary",
31
+ })
32
+ .description("Foreign key to ProductionOrder"),
33
+ plannedMaterialCost: db.float().description("Planned material cost"),
34
+ plannedLaborCost: db.float().description("Planned labor cost"),
35
+ plannedMachineCost: db.float().description("Planned machine cost"),
36
+ plannedOverheadCost: db.float().description("Planned overhead cost"),
37
+ actualMaterialCost: db.float().description("Actual material cost"),
38
+ actualLaborCost: db.float().description("Actual labor cost"),
39
+ actualMachineCost: db.float().description("Actual machine cost"),
40
+ actualOverheadCost: db.float().description("Actual overhead cost"),
41
+ currencyCode: db.string().description("Currency code (e.g. USD, EUR)"),
42
+ reviewedDate: db
43
+ .datetime({ optional: true })
44
+ .description("Date when cost summary was reviewed"),
45
+ reviewerNotes: db.string({ optional: true }).description("Notes from the reviewer"),
46
+ status: db
47
+ .enum([...BASE_COST_SUMMARY_STATUSES] as [string, ...string[]])
48
+ .description(
49
+ "Lifecycle status: COLLECTING, PENDING_VARIANCE_REVIEW, VARIANCE_REVIEWED, SETTLED",
50
+ ),
51
+ ...db.fields.timestamps(),
52
+ })
53
+ .permission(unsafeAllowAllTypePermission)
54
+ .gqlPermission(unsafeAllowAllGqlPermission);
55
+ }
56
+
57
+ export const manufacturingCostSummary = createManufacturingCostSummaryType({
58
+ productionOrderType: productionOrder,
59
+ });
@@ -0,0 +1,83 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBField,
4
+ type TailorAnyDBType,
5
+ unsafeAllowAllGqlPermission,
6
+ unsafeAllowAllTypePermission,
7
+ } from "@tailor-platform/sdk";
8
+ import { company as companyStub, site as siteStub, item as itemStub } from "../lib/_db_deps";
9
+
10
+ export const BASE_PRODUCTION_ORDER_STATUSES = [
11
+ "DRAFT",
12
+ "RELEASED",
13
+ "IN_PROGRESS",
14
+ "COMPLETED",
15
+ "TECHNICALLY_COMPLETE",
16
+ "CLOSED",
17
+ "CANCELLED",
18
+ ] as const;
19
+
20
+ export interface CreateProductionOrderTypeParams<F extends Record<string, TailorAnyDBField>> {
21
+ fields?: F;
22
+ companyType: TailorAnyDBType;
23
+ siteType: TailorAnyDBType;
24
+ itemType: TailorAnyDBType;
25
+ }
26
+
27
+ export function createProductionOrderType<const F extends Record<string, TailorAnyDBField>>(
28
+ params: CreateProductionOrderTypeParams<F>,
29
+ ) {
30
+ return db
31
+ .type("ProductionOrder", {
32
+ orderedItemId: db
33
+ .uuid()
34
+ .relation({
35
+ type: "n-1",
36
+ toward: { type: params.itemType },
37
+ backward: "productionOrders",
38
+ })
39
+ .description("Foreign key to Item – the item to be manufactured"),
40
+ companyId: db
41
+ .uuid()
42
+ .relation({
43
+ type: "n-1",
44
+ toward: { type: params.companyType },
45
+ backward: "productionOrders",
46
+ })
47
+ .description("Foreign key to Company"),
48
+ siteId: db
49
+ .uuid()
50
+ .relation({
51
+ type: "n-1",
52
+ toward: { type: params.siteType },
53
+ backward: "productionOrders",
54
+ })
55
+ .description("Foreign key to Site"),
56
+ plannedQuantity: db
57
+ .float()
58
+ .description("Planned production quantity, must be greater than 0"),
59
+ plannedStartDate: db.datetime().description("Planned start date for the production order"),
60
+ plannedEndDate: db.datetime().description("Planned end date for the production order"),
61
+ selectedBomVersionId: db
62
+ .uuid({ optional: true })
63
+ .description("Optional reference to BillOfMaterial – resolved at release"),
64
+ selectedRoutingRevisionId: db
65
+ .uuid({ optional: true })
66
+ .description("Optional reference to Routing – resolved at release"),
67
+ status: db
68
+ .enum([...BASE_PRODUCTION_ORDER_STATUSES] as [string, ...string[]])
69
+ .description(
70
+ "Lifecycle status: DRAFT, RELEASED, IN_PROGRESS, COMPLETED, TECHNICALLY_COMPLETE, CLOSED, CANCELLED",
71
+ ),
72
+ ...((params.fields ?? {}) as F),
73
+ ...db.fields.timestamps(),
74
+ })
75
+ .permission(unsafeAllowAllTypePermission)
76
+ .gqlPermission(unsafeAllowAllGqlPermission);
77
+ }
78
+
79
+ export const productionOrder = createProductionOrderType({
80
+ companyType: companyStub,
81
+ siteType: siteStub,
82
+ itemType: itemStub,
83
+ });
@@ -0,0 +1,44 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBField,
4
+ type TailorAnyDBType,
5
+ unsafeAllowAllGqlPermission,
6
+ unsafeAllowAllTypePermission,
7
+ } from "@tailor-platform/sdk";
8
+ import { productionOrder } from "./productionOrder";
9
+
10
+ export interface CreateProductionOrderBomSnapshotTypeParams<
11
+ F extends Record<string, TailorAnyDBField>,
12
+ > {
13
+ fields?: F;
14
+ productionOrderType: TailorAnyDBType;
15
+ }
16
+
17
+ export function createProductionOrderBomSnapshotType<
18
+ const F extends Record<string, TailorAnyDBField>,
19
+ >(params: CreateProductionOrderBomSnapshotTypeParams<F>) {
20
+ return db
21
+ .type("ProductionOrderBomSnapshot", {
22
+ productionOrderId: db
23
+ .uuid()
24
+ .relation({
25
+ type: "1-1",
26
+ toward: { type: params.productionOrderType },
27
+ backward: "bomSnapshot",
28
+ })
29
+ .description("Foreign key to ProductionOrder (one snapshot per order)"),
30
+ parentItemId: db.uuid().description("Item ID of the parent item in the BOM snapshot"),
31
+ bomType: db.string().description("BOM type (e.g., STANDARD, PHANTOM)"),
32
+ snapshotData: db
33
+ .string({ optional: true })
34
+ .description("JSON blob containing the full BOM snapshot"),
35
+ ...((params.fields ?? {}) as F),
36
+ ...db.fields.timestamps(),
37
+ })
38
+ .permission(unsafeAllowAllTypePermission)
39
+ .gqlPermission(unsafeAllowAllGqlPermission);
40
+ }
41
+
42
+ export const productionOrderBomSnapshot = createProductionOrderBomSnapshotType({
43
+ productionOrderType: productionOrder,
44
+ });
@@ -0,0 +1,44 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBField,
4
+ type TailorAnyDBType,
5
+ unsafeAllowAllGqlPermission,
6
+ unsafeAllowAllTypePermission,
7
+ } from "@tailor-platform/sdk";
8
+ import { productionOrder } from "./productionOrder";
9
+
10
+ export interface CreateProductionOrderCostBaselineTypeParams<
11
+ F extends Record<string, TailorAnyDBField>,
12
+ > {
13
+ fields?: F;
14
+ productionOrderType: TailorAnyDBType;
15
+ }
16
+
17
+ export function createProductionOrderCostBaselineType<
18
+ const F extends Record<string, TailorAnyDBField>,
19
+ >(params: CreateProductionOrderCostBaselineTypeParams<F>) {
20
+ return db
21
+ .type("ProductionOrderCostBaseline", {
22
+ productionOrderId: db
23
+ .uuid()
24
+ .relation({
25
+ type: "1-1",
26
+ toward: { type: params.productionOrderType },
27
+ backward: "costBaseline",
28
+ })
29
+ .description("Foreign key to ProductionOrder (one baseline per order)"),
30
+ plannedMaterialCost: db.float().description("Planned total material cost"),
31
+ plannedLaborCost: db.float().description("Planned total labor cost"),
32
+ plannedMachineCost: db.float().description("Planned total machine cost"),
33
+ plannedOverheadCost: db.float().description("Planned total overhead cost"),
34
+ currencyCode: db.string().description("ISO 4217 currency code (e.g., USD)"),
35
+ ...((params.fields ?? {}) as F),
36
+ ...db.fields.timestamps(),
37
+ })
38
+ .permission(unsafeAllowAllTypePermission)
39
+ .gqlPermission(unsafeAllowAllGqlPermission);
40
+ }
41
+
42
+ export const productionOrderCostBaseline = createProductionOrderCostBaselineType({
43
+ productionOrderType: productionOrder,
44
+ });
@@ -0,0 +1,57 @@
1
+ import {
2
+ db,
3
+ type TailorAnyDBField,
4
+ type TailorAnyDBType,
5
+ unsafeAllowAllGqlPermission,
6
+ unsafeAllowAllTypePermission,
7
+ } from "@tailor-platform/sdk";
8
+ import { item as itemStub } from "../lib/_db_deps";
9
+ import { productionOrder } from "./productionOrder";
10
+
11
+ export interface CreateProductionOrderMaterialRequirementTypeParams<
12
+ F extends Record<string, TailorAnyDBField>,
13
+ > {
14
+ fields?: F;
15
+ productionOrderType: TailorAnyDBType;
16
+ itemType: TailorAnyDBType;
17
+ }
18
+
19
+ export function createProductionOrderMaterialRequirementType<
20
+ const F extends Record<string, TailorAnyDBField>,
21
+ >(params: CreateProductionOrderMaterialRequirementTypeParams<F>) {
22
+ return db
23
+ .type("ProductionOrderMaterialRequirement", {
24
+ productionOrderId: db
25
+ .uuid()
26
+ .relation({
27
+ type: "n-1",
28
+ toward: { type: params.productionOrderType },
29
+ backward: "materialRequirements",
30
+ })
31
+ .description("Foreign key to ProductionOrder"),
32
+ itemId: db
33
+ .uuid()
34
+ .relation({
35
+ type: "n-1",
36
+ toward: { type: params.itemType },
37
+ backward: "productionOrderMaterialRequirements",
38
+ })
39
+ .description("Foreign key to Item – the required material"),
40
+ requiredQuantity: db.float().description("Required quantity of the material"),
41
+ unitOfMeasure: db
42
+ .string({ optional: true })
43
+ .description("Unit of measure for the required quantity"),
44
+ plannedUnitCost: db
45
+ .float({ optional: true })
46
+ .description("Planned cost per unit of the material"),
47
+ ...((params.fields ?? {}) as F),
48
+ ...db.fields.timestamps(),
49
+ })
50
+ .permission(unsafeAllowAllTypePermission)
51
+ .gqlPermission(unsafeAllowAllGqlPermission);
52
+ }
53
+
54
+ export const productionOrderMaterialRequirement = createProductionOrderMaterialRequirementType({
55
+ productionOrderType: productionOrder,
56
+ itemType: itemStub,
57
+ });