@tailor-platform/erp-kit 0.1.1 → 0.2.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 (342) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +158 -62
  3. package/dist/cli.js +1010 -270
  4. package/package.json +11 -8
  5. package/schemas/module/command.yml +1 -0
  6. package/schemas/module/model.yml +14 -0
  7. package/schemas/module/query.yml +53 -0
  8. package/skills/{app-compose-1-requirement-analysis → erp-kit-app-1-requirements}/SKILL.md +2 -2
  9. package/skills/{app-compose-2-requirements-breakdown → erp-kit-app-2-breakdown}/SKILL.md +3 -3
  10. package/skills/{app-compose-3-doc-review → erp-kit-app-3-doc-review}/SKILL.md +2 -2
  11. package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/SKILL.md +3 -3
  12. package/skills/{app-compose-5-design-mock-review → erp-kit-app-5-design-review}/SKILL.md +4 -4
  13. package/skills/{app-compose-6-implementation-spec → erp-kit-app-6-impl-spec}/SKILL.md +3 -3
  14. package/skills/{mock-scenario → erp-kit-mock-scenario}/SKILL.md +1 -1
  15. package/skills/{1-module-docs → erp-kit-module-1-docs}/SKILL.md +2 -2
  16. package/skills/{2-module-feature-breakdown → erp-kit-module-2-feature-breakdown}/SKILL.md +13 -9
  17. package/skills/erp-kit-module-2-feature-breakdown/references/naming.md +59 -0
  18. package/skills/{3-module-doc-review → erp-kit-module-3-doc-review}/SKILL.md +83 -25
  19. package/skills/erp-kit-module-4-tdd/SKILL.md +94 -0
  20. package/skills/erp-kit-module-4-tdd/references/cross-module-dependency.md +133 -0
  21. package/skills/{4-module-tdd-implementation → erp-kit-module-4-tdd}/references/db-relations.md +5 -1
  22. package/skills/{4-module-tdd-implementation → erp-kit-module-4-tdd}/references/exports.md +1 -1
  23. package/skills/erp-kit-module-4-tdd/references/generated-code.md +32 -0
  24. package/skills/{5-module-implementation-review → erp-kit-module-5-impl-review}/SKILL.md +46 -44
  25. package/skills/erp-kit-module-5-impl-review/references/commands.md +62 -0
  26. package/skills/erp-kit-module-5-impl-review/references/errors.md +10 -0
  27. package/skills/{5-module-implementation-review → erp-kit-module-5-impl-review}/references/testing.md +1 -1
  28. package/skills/erp-kit-module-shared/SKILL.md +16 -0
  29. package/skills/erp-kit-module-shared/references/commands.md +203 -0
  30. package/skills/erp-kit-module-shared/references/errors.md +35 -0
  31. package/skills/erp-kit-module-shared/references/queries.md +168 -0
  32. package/skills/erp-kit-module-shared/references/structure.md +36 -0
  33. package/skills/{3-module-doc-review → erp-kit-module-shared}/references/testing.md +4 -3
  34. package/skills/erp-kit-update/SKILL.md +64 -0
  35. package/src/cli.doc.test.ts +65 -0
  36. package/src/cli.ts +3 -117
  37. package/src/commands/app/index.ts +74 -0
  38. package/src/commands/check.test.ts +3 -2
  39. package/src/commands/check.ts +3 -2
  40. package/src/commands/index.ts +73 -0
  41. package/src/commands/init.test.ts +22 -5
  42. package/src/commands/init.ts +25 -16
  43. package/src/commands/license.ts +193 -0
  44. package/src/commands/mock/index.ts +2 -2
  45. package/src/commands/mock/start.ts +1 -1
  46. package/src/commands/mock/validate.test.ts +1 -1
  47. package/src/commands/module/generate.ts +35 -0
  48. package/src/commands/module/index.ts +87 -0
  49. package/src/commands/module/list.test.ts +57 -0
  50. package/src/commands/module/list.ts +64 -0
  51. package/src/commands/scaffold-templates.ts +65 -0
  52. package/src/commands/scaffold.test.ts +97 -2
  53. package/src/commands/scaffold.ts +24 -3
  54. package/src/commands/sync-check.test.ts +88 -1
  55. package/src/commands/sync-check.ts +41 -2
  56. package/src/generator/generate-code.test.ts +200 -0
  57. package/src/generator/generate-code.ts +260 -0
  58. package/src/generator/parse-command-doc.test.ts +159 -0
  59. package/src/generator/parse-command-doc.ts +116 -0
  60. package/src/integration.test.ts +6 -8
  61. package/src/module.ts +10 -9
  62. package/src/modules/item-management/README.md +38 -0
  63. package/src/modules/item-management/command/activateItem.generated.ts +6 -0
  64. package/src/modules/item-management/command/activateItem.test.ts +76 -0
  65. package/src/modules/item-management/command/activateItem.ts +42 -0
  66. package/src/modules/item-management/command/assignItemToTaxonomy.generated.ts +6 -0
  67. package/src/modules/item-management/command/assignItemToTaxonomy.test.ts +88 -0
  68. package/src/modules/item-management/command/assignItemToTaxonomy.ts +63 -0
  69. package/src/modules/item-management/command/createItem.generated.ts +6 -0
  70. package/src/modules/item-management/command/createItem.test.ts +152 -0
  71. package/src/modules/item-management/command/createItem.ts +72 -0
  72. package/src/modules/item-management/command/createTaxonomyNode.generated.ts +6 -0
  73. package/src/modules/item-management/command/createTaxonomyNode.test.ts +126 -0
  74. package/src/modules/item-management/command/createTaxonomyNode.ts +70 -0
  75. package/src/modules/item-management/command/deactivateItem.generated.ts +6 -0
  76. package/src/modules/item-management/command/deactivateItem.test.ts +76 -0
  77. package/src/modules/item-management/command/deactivateItem.ts +42 -0
  78. package/src/modules/item-management/command/deleteItem.generated.ts +6 -0
  79. package/src/modules/item-management/command/deleteItem.test.ts +61 -0
  80. package/src/modules/item-management/command/deleteItem.ts +38 -0
  81. package/src/modules/item-management/command/deleteTaxonomyNode.generated.ts +6 -0
  82. package/src/modules/item-management/command/deleteTaxonomyNode.test.ts +73 -0
  83. package/src/modules/item-management/command/deleteTaxonomyNode.ts +50 -0
  84. package/src/modules/item-management/command/moveTaxonomyNode.generated.ts +6 -0
  85. package/src/modules/item-management/command/moveTaxonomyNode.test.ts +136 -0
  86. package/src/modules/item-management/command/moveTaxonomyNode.ts +85 -0
  87. package/src/modules/item-management/command/reactivateItem.generated.ts +6 -0
  88. package/src/modules/item-management/command/reactivateItem.test.ts +76 -0
  89. package/src/modules/item-management/command/reactivateItem.ts +42 -0
  90. package/src/modules/item-management/command/removeItemFromTaxonomy.generated.ts +6 -0
  91. package/src/modules/item-management/command/removeItemFromTaxonomy.test.ts +43 -0
  92. package/src/modules/item-management/command/removeItemFromTaxonomy.ts +30 -0
  93. package/src/modules/item-management/command/updateItem.generated.ts +6 -0
  94. package/src/modules/item-management/command/updateItem.test.ts +178 -0
  95. package/src/modules/item-management/command/updateItem.ts +103 -0
  96. package/src/modules/item-management/command/updateTaxonomyNode.generated.ts +6 -0
  97. package/src/modules/item-management/command/updateTaxonomyNode.test.ts +88 -0
  98. package/src/modules/item-management/command/updateTaxonomyNode.ts +62 -0
  99. package/src/modules/item-management/db/item.ts +47 -0
  100. package/src/modules/item-management/db/itemTaxonomyAssignment.ts +49 -0
  101. package/src/modules/item-management/db/taxonomyNode.ts +34 -0
  102. package/src/modules/item-management/docs/commands/ActivateItem.md +32 -0
  103. package/src/modules/item-management/docs/commands/AssignItemToTaxonomy.md +38 -0
  104. package/src/modules/item-management/docs/commands/CreateItem.md +44 -0
  105. package/src/modules/item-management/docs/commands/CreateTaxonomyNode.md +44 -0
  106. package/src/modules/item-management/docs/commands/DeactivateItem.md +34 -0
  107. package/src/modules/item-management/docs/commands/DeleteItem.md +35 -0
  108. package/src/modules/item-management/docs/commands/DeleteTaxonomyNode.md +39 -0
  109. package/src/modules/item-management/docs/commands/MoveTaxonomyNode.md +45 -0
  110. package/src/modules/item-management/docs/commands/ReactivateItem.md +34 -0
  111. package/src/modules/item-management/docs/commands/RemoveItemFromTaxonomy.md +30 -0
  112. package/src/modules/item-management/docs/commands/UpdateItem.md +55 -0
  113. package/src/modules/item-management/docs/commands/UpdateTaxonomyNode.md +36 -0
  114. package/src/modules/item-management/docs/features/item-lifecycle.md +60 -0
  115. package/src/modules/item-management/docs/features/item-taxonomy.md +65 -0
  116. package/src/modules/item-management/docs/models/ItemTaxonomyAssignment.md +36 -0
  117. package/src/modules/item-management/docs/models/TaxonomyNode.md +47 -0
  118. package/src/modules/item-management/docs/models/item.md +59 -0
  119. package/src/modules/item-management/docs/queries/CalculateNodeDepth.md +36 -0
  120. package/src/modules/item-management/docs/queries/CalculateSubtreeDepth.md +40 -0
  121. package/src/modules/item-management/docs/queries/DetectCircularReference.md +41 -0
  122. package/src/modules/item-management/docs/queries/GetItem.md +38 -0
  123. package/src/modules/item-management/docs/queries/GetItemTaxonomyAssignment.md +29 -0
  124. package/src/modules/item-management/docs/queries/GetTaxonomyNode.md +35 -0
  125. package/src/modules/item-management/docs/queries/GetTaxonomyNodeAssignments.md +29 -0
  126. package/src/modules/item-management/docs/queries/GetTaxonomyNodeChildren.md +29 -0
  127. package/src/modules/item-management/generated/enums.ts +9 -0
  128. package/src/modules/item-management/generated/kysely-tailordb.ts +62 -0
  129. package/src/modules/item-management/index.ts +53 -0
  130. package/src/modules/item-management/lib/_db_deps.ts +13 -0
  131. package/src/modules/item-management/lib/errors.generated.ts +117 -0
  132. package/src/modules/item-management/lib/permissions.generated.ts +17 -0
  133. package/src/modules/item-management/lib/types.ts +19 -0
  134. package/src/modules/item-management/module.ts +97 -0
  135. package/src/modules/item-management/query/calculateNodeDepth.generated.ts +5 -0
  136. package/src/modules/item-management/query/calculateNodeDepth.test.ts +56 -0
  137. package/src/modules/item-management/query/calculateNodeDepth.ts +28 -0
  138. package/src/modules/item-management/query/calculateSubtreeDepth.generated.ts +5 -0
  139. package/src/modules/item-management/query/calculateSubtreeDepth.test.ts +75 -0
  140. package/src/modules/item-management/query/calculateSubtreeDepth.ts +29 -0
  141. package/src/modules/item-management/query/detectCircularReference.generated.ts +5 -0
  142. package/src/modules/item-management/query/detectCircularReference.test.ts +61 -0
  143. package/src/modules/item-management/query/detectCircularReference.ts +32 -0
  144. package/src/modules/item-management/query/getItem.generated.ts +5 -0
  145. package/src/modules/item-management/query/getItem.test.ts +67 -0
  146. package/src/modules/item-management/query/getItem.ts +20 -0
  147. package/src/modules/item-management/query/getItemTaxonomyAssignment.generated.ts +5 -0
  148. package/src/modules/item-management/query/getItemTaxonomyAssignment.test.ts +25 -0
  149. package/src/modules/item-management/query/getItemTaxonomyAssignment.ts +18 -0
  150. package/src/modules/item-management/query/getTaxonomyNode.generated.ts +5 -0
  151. package/src/modules/item-management/query/getTaxonomyNode.test.ts +47 -0
  152. package/src/modules/item-management/query/getTaxonomyNode.ts +18 -0
  153. package/src/modules/item-management/query/getTaxonomyNodeAssignments.generated.ts +5 -0
  154. package/src/modules/item-management/query/getTaxonomyNodeAssignments.test.ts +25 -0
  155. package/src/modules/item-management/query/getTaxonomyNodeAssignments.ts +16 -0
  156. package/src/modules/item-management/query/getTaxonomyNodeChildren.generated.ts +5 -0
  157. package/src/modules/item-management/query/getTaxonomyNodeChildren.test.ts +34 -0
  158. package/src/modules/item-management/query/getTaxonomyNodeChildren.ts +16 -0
  159. package/src/modules/item-management/tailor.config.ts +11 -0
  160. package/src/modules/item-management/testing/fixtures.ts +81 -0
  161. package/src/modules/primitives/command/activateCategory.generated.ts +6 -0
  162. package/src/modules/primitives/command/activateCategory.test.ts +11 -29
  163. package/src/modules/primitives/command/activateCategory.ts +27 -34
  164. package/src/modules/primitives/command/activateCurrency.generated.ts +6 -0
  165. package/src/modules/primitives/command/activateCurrency.test.ts +11 -29
  166. package/src/modules/primitives/command/activateCurrency.ts +27 -34
  167. package/src/modules/primitives/command/activateUnit.generated.ts +6 -0
  168. package/src/modules/primitives/command/activateUnit.test.ts +11 -15
  169. package/src/modules/primitives/command/activateUnit.ts +27 -34
  170. package/src/modules/primitives/command/createCategory.generated.ts +6 -0
  171. package/src/modules/primitives/command/createCategory.test.ts +27 -39
  172. package/src/modules/primitives/command/createCategory.ts +53 -62
  173. package/src/modules/primitives/command/createCurrency.generated.ts +6 -0
  174. package/src/modules/primitives/command/createCurrency.test.ts +78 -71
  175. package/src/modules/primitives/command/createCurrency.ts +43 -48
  176. package/src/modules/primitives/command/createExchangeRate.generated.ts +6 -0
  177. package/src/modules/primitives/command/createExchangeRate.test.ts +101 -100
  178. package/src/modules/primitives/command/createExchangeRate.ts +50 -59
  179. package/src/modules/primitives/command/createUnit.generated.ts +6 -0
  180. package/src/modules/primitives/command/createUnit.test.ts +92 -95
  181. package/src/modules/primitives/command/createUnit.ts +54 -57
  182. package/src/modules/primitives/command/deactivateCategory.generated.ts +6 -0
  183. package/src/modules/primitives/command/deactivateCategory.test.ts +27 -28
  184. package/src/modules/primitives/command/deactivateCategory.ts +43 -50
  185. package/src/modules/primitives/command/deactivateCurrency.generated.ts +6 -0
  186. package/src/modules/primitives/command/deactivateCurrency.test.ts +23 -38
  187. package/src/modules/primitives/command/deactivateCurrency.ts +31 -38
  188. package/src/modules/primitives/command/deactivateUnit.generated.ts +6 -0
  189. package/src/modules/primitives/command/deactivateUnit.test.ts +27 -23
  190. package/src/modules/primitives/command/deactivateUnit.ts +39 -49
  191. package/src/modules/primitives/command/setBaseCurrency.generated.ts +6 -0
  192. package/src/modules/primitives/command/setBaseCurrency.test.ts +40 -33
  193. package/src/modules/primitives/command/setBaseCurrency.ts +43 -50
  194. package/src/modules/primitives/command/setReferenceUnit.generated.ts +6 -0
  195. package/src/modules/primitives/command/setReferenceUnit.test.ts +39 -35
  196. package/src/modules/primitives/command/setReferenceUnit.ts +46 -59
  197. package/src/modules/primitives/db/unit.ts +13 -3
  198. package/src/modules/primitives/docs/commands/ActivateCategory.md +1 -2
  199. package/src/modules/primitives/docs/commands/ActivateCurrency.md +1 -2
  200. package/src/modules/primitives/docs/commands/ActivateUnit.md +1 -2
  201. package/src/modules/primitives/docs/commands/CreateCategory.md +1 -4
  202. package/src/modules/primitives/docs/commands/CreateCurrency.md +3 -4
  203. package/src/modules/primitives/docs/commands/CreateExchangeRate.md +4 -5
  204. package/src/modules/primitives/docs/commands/CreateUnit.md +5 -5
  205. package/src/modules/primitives/docs/commands/DeactivateCategory.md +2 -3
  206. package/src/modules/primitives/docs/commands/DeactivateCurrency.md +2 -3
  207. package/src/modules/primitives/docs/commands/DeactivateUnit.md +2 -3
  208. package/src/modules/primitives/docs/commands/SetBaseCurrency.md +2 -3
  209. package/src/modules/primitives/docs/commands/SetReferenceUnit.md +2 -3
  210. package/src/modules/primitives/docs/models/Currency.md +4 -0
  211. package/src/modules/primitives/docs/models/ExchangeRate.md +4 -1
  212. package/src/modules/primitives/docs/models/Unit.md +4 -1
  213. package/src/modules/primitives/docs/models/UoMCategory.md +2 -0
  214. package/src/modules/primitives/docs/{commands → queries}/ConvertAmount.md +3 -5
  215. package/src/modules/primitives/docs/{commands → queries}/ConvertQuantity.md +3 -5
  216. package/src/modules/primitives/docs/queries/GetBaseCurrency.md +32 -0
  217. package/src/modules/primitives/docs/queries/GetCurrency.md +36 -0
  218. package/src/modules/primitives/docs/queries/GetUnit.md +36 -0
  219. package/src/modules/primitives/docs/queries/GetUoMCategory.md +36 -0
  220. package/src/modules/primitives/docs/queries/ListUnitsByCategory.md +26 -0
  221. package/src/modules/primitives/generated/kysely-tailordb.ts +24 -45
  222. package/src/modules/primitives/index.ts +17 -6
  223. package/src/modules/primitives/lib/errors.generated.ts +112 -0
  224. package/src/modules/primitives/{permissions.ts → lib/permissions.generated.ts} +9 -10
  225. package/src/modules/primitives/module.ts +39 -27
  226. package/src/modules/primitives/query/convertAmount.generated.ts +5 -0
  227. package/src/modules/primitives/{command → query}/convertAmount.test.ts +4 -21
  228. package/src/modules/primitives/query/convertAmount.ts +121 -0
  229. package/src/modules/primitives/query/convertQuantity.generated.ts +5 -0
  230. package/src/modules/primitives/{command → query}/convertQuantity.test.ts +8 -15
  231. package/src/modules/primitives/query/convertQuantity.ts +63 -0
  232. package/src/modules/primitives/query/getBaseCurrency.generated.ts +5 -0
  233. package/src/modules/primitives/query/getBaseCurrency.test.ts +28 -0
  234. package/src/modules/primitives/query/getBaseCurrency.ts +16 -0
  235. package/src/modules/primitives/query/getCurrency.generated.ts +5 -0
  236. package/src/modules/primitives/query/getCurrency.test.ts +47 -0
  237. package/src/modules/primitives/query/getCurrency.ts +18 -0
  238. package/src/modules/primitives/query/getUnit.generated.ts +5 -0
  239. package/src/modules/primitives/query/getUnit.test.ts +47 -0
  240. package/src/modules/primitives/query/getUnit.ts +18 -0
  241. package/src/modules/primitives/query/getUoMCategory.generated.ts +5 -0
  242. package/src/modules/primitives/query/getUoMCategory.test.ts +47 -0
  243. package/src/modules/primitives/query/getUoMCategory.ts +18 -0
  244. package/src/modules/primitives/query/listUnitsByCategory.generated.ts +5 -0
  245. package/src/modules/primitives/query/listUnitsByCategory.ts +16 -0
  246. package/src/modules/primitives/tailor.config.ts +3 -3
  247. package/src/modules/shared/defineCommand.test.ts +23 -10
  248. package/src/modules/shared/defineCommand.ts +23 -10
  249. package/src/modules/shared/defineQuery.test.ts +28 -0
  250. package/src/modules/shared/defineQuery.ts +16 -0
  251. package/src/modules/shared/internal.ts +3 -1
  252. package/src/modules/shared/requirePermission.test.ts +22 -21
  253. package/src/modules/shared/requirePermission.ts +8 -2
  254. package/src/modules/shared/result.ts +12 -0
  255. package/src/modules/shared/types.ts +8 -0
  256. package/src/modules/testing/index.ts +36 -11
  257. package/src/modules/user-management/command/activateUser.generated.ts +6 -0
  258. package/src/modules/user-management/command/activateUser.test.ts +27 -27
  259. package/src/modules/user-management/command/activateUser.ts +40 -48
  260. package/src/modules/user-management/command/assignPermissionToRole.generated.ts +6 -0
  261. package/src/modules/user-management/command/assignPermissionToRole.test.ts +42 -43
  262. package/src/modules/user-management/command/assignPermissionToRole.ts +59 -62
  263. package/src/modules/user-management/command/assignRoleToUser.generated.ts +6 -0
  264. package/src/modules/user-management/command/assignRoleToUser.test.ts +70 -63
  265. package/src/modules/user-management/command/assignRoleToUser.ts +63 -66
  266. package/src/modules/user-management/command/createPermission.generated.ts +6 -0
  267. package/src/modules/user-management/command/createPermission.test.ts +45 -38
  268. package/src/modules/user-management/command/createPermission.ts +42 -46
  269. package/src/modules/user-management/command/createRole.generated.ts +6 -0
  270. package/src/modules/user-management/command/createRole.test.ts +30 -29
  271. package/src/modules/user-management/command/createRole.ts +33 -33
  272. package/src/modules/user-management/command/createUser.generated.ts +6 -0
  273. package/src/modules/user-management/command/createUser.test.ts +64 -42
  274. package/src/modules/user-management/command/createUser.ts +54 -56
  275. package/src/modules/user-management/command/deactivateUser.generated.ts +6 -0
  276. package/src/modules/user-management/command/deactivateUser.test.ts +27 -27
  277. package/src/modules/user-management/command/deactivateUser.ts +40 -48
  278. package/src/modules/user-management/command/logAuditEvent.generated.ts +6 -0
  279. package/src/modules/user-management/command/logAuditEvent.test.ts +50 -42
  280. package/src/modules/user-management/command/logAuditEvent.ts +25 -28
  281. package/src/modules/user-management/command/reactivateUser.generated.ts +6 -0
  282. package/src/modules/user-management/command/reactivateUser.test.ts +31 -27
  283. package/src/modules/user-management/command/reactivateUser.ts +40 -48
  284. package/src/modules/user-management/command/revokePermissionFromRole.generated.ts +6 -0
  285. package/src/modules/user-management/command/revokePermissionFromRole.test.ts +52 -51
  286. package/src/modules/user-management/command/revokePermissionFromRole.ts +60 -57
  287. package/src/modules/user-management/command/revokeRoleFromUser.generated.ts +6 -0
  288. package/src/modules/user-management/command/revokeRoleFromUser.test.ts +53 -48
  289. package/src/modules/user-management/command/revokeRoleFromUser.ts +58 -57
  290. package/src/modules/user-management/docs/commands/CreatePermission.md +2 -2
  291. package/src/modules/user-management/docs/commands/CreateRole.md +1 -1
  292. package/src/modules/user-management/docs/models/AuditEvent.md +2 -0
  293. package/src/modules/user-management/docs/models/Permission.md +2 -0
  294. package/src/modules/user-management/docs/models/Role.md +2 -0
  295. package/src/modules/user-management/docs/models/RolePermission.md +2 -0
  296. package/src/modules/user-management/docs/models/User.md +2 -0
  297. package/src/modules/user-management/docs/models/UserRole.md +2 -0
  298. package/src/modules/user-management/generated/enums.ts +11 -11
  299. package/src/modules/user-management/generated/kysely-tailordb.ts +27 -56
  300. package/src/modules/user-management/index.ts +2 -2
  301. package/src/modules/user-management/lib/errors.generated.ts +67 -0
  302. package/src/modules/user-management/{permissions.ts → lib/permissions.generated.ts} +8 -7
  303. package/src/modules/user-management/module.ts +22 -22
  304. package/src/modules/user-management/tailor.config.ts +3 -3
  305. package/src/schemas.ts +2 -1
  306. package/skills/1-module-docs/references/structure.md +0 -22
  307. package/skills/2-module-feature-breakdown/references/commands.md +0 -48
  308. package/skills/2-module-feature-breakdown/references/structure.md +0 -22
  309. package/skills/3-module-doc-review/references/commands.md +0 -54
  310. package/skills/3-module-doc-review/references/models.md +0 -29
  311. package/skills/4-module-tdd-implementation/SKILL.md +0 -74
  312. package/skills/4-module-tdd-implementation/references/commands.md +0 -45
  313. package/skills/4-module-tdd-implementation/references/errors.md +0 -7
  314. package/skills/4-module-tdd-implementation/references/models.md +0 -30
  315. package/skills/4-module-tdd-implementation/references/structure.md +0 -22
  316. package/skills/4-module-tdd-implementation/references/testing.md +0 -37
  317. package/skills/5-module-implementation-review/references/commands.md +0 -45
  318. package/skills/5-module-implementation-review/references/errors.md +0 -7
  319. package/skills/5-module-implementation-review/references/exports.md +0 -8
  320. package/skills/5-module-implementation-review/references/models.md +0 -30
  321. package/src/modules/primitives/command/convertAmount.ts +0 -126
  322. package/src/modules/primitives/command/convertQuantity.ts +0 -73
  323. package/src/modules/primitives/lib/errors.ts +0 -138
  324. package/src/modules/user-management/lib/errors.ts +0 -81
  325. /package/skills/{app-compose-1-requirement-analysis → erp-kit-app-1-requirements}/references/structure.md +0 -0
  326. /package/skills/{app-compose-2-requirements-breakdown → erp-kit-app-2-breakdown}/references/screen-detailview.md +0 -0
  327. /package/skills/{app-compose-2-requirements-breakdown → erp-kit-app-2-breakdown}/references/screen-form.md +0 -0
  328. /package/skills/{app-compose-2-requirements-breakdown → erp-kit-app-2-breakdown}/references/screen-listview.md +0 -0
  329. /package/skills/{app-compose-2-requirements-breakdown → erp-kit-app-2-breakdown}/references/structure.md +0 -0
  330. /package/skills/{app-compose-3-doc-review → erp-kit-app-3-doc-review}/references/structure.md +0 -0
  331. /package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/references/component.md +0 -0
  332. /package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/references/screen-detailview.md +0 -0
  333. /package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/references/screen-form.md +0 -0
  334. /package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/references/screen-listview.md +0 -0
  335. /package/skills/{app-compose-4-design-mock → erp-kit-app-4-design}/references/structure.md +0 -0
  336. /package/skills/{app-compose-5-design-mock-review → erp-kit-app-5-design-review}/references/component.md +0 -0
  337. /package/skills/{app-compose-5-design-mock-review → erp-kit-app-5-design-review}/references/screen-detailview.md +0 -0
  338. /package/skills/{app-compose-5-design-mock-review → erp-kit-app-5-design-review}/references/screen-form.md +0 -0
  339. /package/skills/{app-compose-5-design-mock-review → erp-kit-app-5-design-review}/references/screen-listview.md +0 -0
  340. /package/skills/{app-compose-6-implementation-spec → erp-kit-app-6-impl-spec}/references/auth.md +0 -0
  341. /package/skills/{app-compose-6-implementation-spec → erp-kit-app-6-impl-spec}/references/structure.md +0 -0
  342. /package/skills/{2-module-feature-breakdown → erp-kit-module-4-tdd}/references/models.md +0 -0
@@ -0,0 +1,94 @@
1
+ ---
2
+ name: erp-kit-module-4-tdd
3
+ description: Use when implementing features in any module under modules/. Triggers on TDD workflow, domain command implementation, adding new database models, creating test fixtures, or implementing business logic commands.
4
+ ---
5
+
6
+ # TDD Implementation for Module Development
7
+
8
+ Guide for implementing features in modules using Test-Driven Development.
9
+
10
+ ## When to Use
11
+
12
+ - Implementing new domain commands in any module
13
+ - Implementing read-only queries in any module
14
+ - Adding database models with `@tailor-platform/sdk`
15
+ - Creating tests with `@tailor-platform/erp-kit` testing utilities
16
+ - Implementing business logic (CRUD, state transitions, relationships)
17
+
18
+ ## Workflow
19
+
20
+ ```
21
+ DOCS → MODELS → GENERATE → FIXTURES → TESTS → IMPLEMENT → EXPORT → VERIFY
22
+ ```
23
+
24
+ ## Step-by-Step
25
+
26
+ ### 1. Write Command/Query Docs
27
+
28
+ Write markdown specs under `docs/commands/*.md` and `docs/queries/*.md`. These are the source of truth for error scenarios, permissions, and generated code. See erp-kit-module-1-docs, erp-kit-module-2-feature-breakdown, and erp-kit-module-3-doc-review for documentation workflow.
29
+
30
+ ### 2. Database Models (`db/`)
31
+
32
+ **Read [models rules](references/models.md) and [db-relations rules](references/db-relations.md) before writing any model.**
33
+
34
+ Key patterns: factory function, `db.fields.timestamps()`, `.description()` on fields, `.relation()` for foreign keys.
35
+
36
+ ### 3. Generate Code
37
+
38
+ **Read [generated code rules](references/generated-code.md) for what gets generated and what you write by hand.**
39
+
40
+ ```bash
41
+ # Generate kysely types from models
42
+ pnpm generate
43
+
44
+ # Generate errors, permissions, and command/query shells from docs
45
+ erp-kit module generate code --root <root> <module>
46
+ ```
47
+
48
+ Generated `.generated.ts` files provide: error classes, permission definitions, and command/query shells. Never edit these files.
49
+
50
+ ### 4. Test Fixtures (`testing/fixtures.ts`)
51
+
52
+ **Read [testing rules](../erp-kit-module-shared/references/testing.md) before writing fixtures or tests.**
53
+
54
+ ### 5. Write Tests First (`command/*.test.ts`)
55
+
56
+ Same reference: [testing rules](../erp-kit-module-shared/references/testing.md).
57
+
58
+ Key patterns: `createMockDb<DB>()`, fixed IDs `"entity-1"`, cover all doc branches.
59
+
60
+ ### 6. Implement Commands (`command/*.ts`)
61
+
62
+ **Read [commands rules](../erp-kit-module-shared/references/commands.md) before implementing.**
63
+
64
+ Key patterns: export `run` function, generated shell wraps with `defineCommand(permissions.xxx, run)`, generic `CF` for custom fields, validate → query → mutate.
65
+
66
+ ### 7. Implement Queries (`query/*.ts`)
67
+
68
+ **Read [queries rules](../erp-kit-module-shared/references/queries.md) before implementing.**
69
+
70
+ Key patterns: `defineQuery`, `ReadonlyDB<DB>`, `QueryContext`. Generated queries handle simple lookups; write custom queries for joins, aggregations, complex filters.
71
+
72
+ ### 8. Export (`index.ts`)
73
+
74
+ **Read [exports rules](references/exports.md) for export order.**
75
+
76
+ ### 9. Verify
77
+
78
+ ```bash
79
+ pnpm generate # Regenerate types if models changed
80
+ pnpm lint # Check code style
81
+ pnpm typecheck # Verify TypeScript types
82
+ pnpm test # Run all tests
83
+ ```
84
+
85
+ ## References
86
+
87
+ - [Module structure](../erp-kit-module-shared/references/structure.md)
88
+ - [Model patterns](references/models.md)
89
+ - [Database relations](references/db-relations.md)
90
+ - [Command patterns](../erp-kit-module-shared/references/commands.md)
91
+ - [Query patterns](../erp-kit-module-shared/references/queries.md)
92
+ - [Generated code](references/generated-code.md)
93
+ - [Testing patterns](../erp-kit-module-shared/references/testing.md)
94
+ - [Export order](references/exports.md)
@@ -0,0 +1,133 @@
1
+ # Cross-Module Dependency
2
+
3
+ ## Overview
4
+
5
+ Each module's `defineModule()` returns `{ db, commands, queries }`. Other modules can reference any of these through the return type. Use `import type` and relative paths — all modules live in the same package.
6
+
7
+ ```typescript
8
+ import type { defineModule as definePrimitivesModule } from "../primitives";
9
+
10
+ type PrimitivesModule = ReturnType<typeof definePrimitivesModule>;
11
+ type PrimitivesDB = PrimitivesModule["db"];
12
+ type PrimitivesCommands = PrimitivesModule["commands"];
13
+ type PrimitivesQueries = PrimitivesModule["queries"];
14
+ ```
15
+
16
+ ## DB Type Injection (`db/*.ts`)
17
+
18
+ Inject another module's DB type to create a foreign-key relation:
19
+
20
+ - Accept external type via `CreateTypeParams` as required param (e.g., `unitType: TailorAnyDBType`)
21
+ - Use it directly in `.relation()`:
22
+ - `db.uuid().relation({ type: "n-1", toward: { type: param }, backward: "..." })`
23
+ - Define stub types for cross-module dependencies in `lib/_db_deps.ts` (codegen only — replaced at runtime by `defineModule()` params):
24
+ ```typescript
25
+ // lib/_db_deps.ts
26
+ import {
27
+ db,
28
+ unsafeAllowAllGqlPermission,
29
+ unsafeAllowAllTypePermission,
30
+ } from "@tailor-platform/sdk";
31
+ export const unit = db
32
+ .type("Unit", {})
33
+ .permission(unsafeAllowAllTypePermission)
34
+ .gqlPermission(unsafeAllowAllGqlPermission);
35
+ ```
36
+ - Import the stub and export a default instance at file bottom:
37
+ ```typescript
38
+ // db/item.ts
39
+ import { unit as unitStub } from "../lib/_db_deps";
40
+ export const item = createItemType({ unitType: unitStub });
41
+ ```
42
+ - Add `lib/_db_deps.ts` to `tailor.config.ts` files so codegen can resolve the type:
43
+ ```typescript
44
+ db: { "main-db": { files: [`./db/*.ts`, `./lib/_db_deps.ts`] } }
45
+ ```
46
+
47
+ ## Command References
48
+
49
+ A module can accept another module's commands as a dependency and call them internally:
50
+
51
+ - Accept commands via `DefineModuleParams` as dependencies
52
+ - Derive the type from the source module's return type
53
+
54
+ ```typescript
55
+ // Types
56
+ type ConvertQuantity = PrimitivesCommands["convertQuantity"];
57
+
58
+ // DefineModuleParams
59
+ export interface DefineModuleParams {
60
+ primitives: {
61
+ commands: {
62
+ convertQuantity: ConvertQuantity;
63
+ };
64
+ };
65
+ }
66
+
67
+ // Usage in a command
68
+ const convertQuantity = params.primitives.commands.convertQuantity;
69
+ const result = await convertQuantity(db, input, ctx);
70
+ ```
71
+
72
+ ## Query References
73
+
74
+ Same pattern as commands — accept queries via `DefineModuleParams`. See [CQRS read rule](../../erp-kit-module-shared/references/commands.md#command-side-reads-cqrs-separation) for when commands use injected queries vs inline reads.
75
+
76
+ ```typescript
77
+ type ConvertAmount = PrimitivesQueries["convertAmount"];
78
+
79
+ export interface DefineModuleParams {
80
+ primitives: {
81
+ queries: {
82
+ convertAmount: ConvertAmount;
83
+ };
84
+ };
85
+ }
86
+ ```
87
+
88
+ ## Module Wiring (`module.ts`)
89
+
90
+ Group all external dependencies (db, commands, queries) under a named key in `DefineModuleParams`:
91
+
92
+ ```typescript
93
+ export interface DefineModuleParams<IF extends Record<string, TailorAnyDBField>> {
94
+ item?: CreateItemTypeParams<IF>;
95
+ primitives: {
96
+ db: { unit: PrimitivesDB["unit"] };
97
+ commands: { convertQuantity: PrimitivesCommands["convertQuantity"] };
98
+ queries: { convertAmount: PrimitivesQueries["convertAmount"] };
99
+ };
100
+ }
101
+
102
+ export const defineModule = <const IF extends Record<string, TailorAnyDBField> = EmptyFields>(
103
+ params: DefineModuleParams<IF>,
104
+ ) => {
105
+ const item = createItemType({
106
+ ...params.item,
107
+ unitType: params.primitives.db.unit,
108
+ });
109
+
110
+ return {
111
+ db: { item },
112
+ commands: {
113
+ createItem: makeCreateItem({
114
+ convertQuantity: params.primitives.commands.convertQuantity,
115
+ }),
116
+ },
117
+ };
118
+ };
119
+ ```
120
+
121
+ ## Permission Dependencies
122
+
123
+ Declare cross-module permission dependencies via `definePermissions`:
124
+
125
+ ```typescript
126
+ export const { permissions, own, all } = definePermissions(
127
+ "itemManagement",
128
+ ["createItem", "updateItem", ...] as const,
129
+ { deps: primitivesPermissions.all },
130
+ );
131
+ ```
132
+
133
+ The `all` export merges own permissions with dependency permissions, so consumers get a complete permission set.
@@ -14,7 +14,7 @@ userId: db.uuid().relation({
14
14
 
15
15
  ### Parameters
16
16
 
17
- - `type`: `"n-1"` (many-to-one), `"1-1"` (one-to-one), or `"keyOnly"` (FK only, no navigation)
17
+ - `type`: `"n-1"` (many-to-one), `"1-1"` (one-to-one), or `"keyOnly"` (FK only, no navigation),`"self"` ( self-reference )
18
18
  - `toward.type`: The related type definition (import it)
19
19
  - `backward`: Field name for reverse navigation from the related type
20
20
 
@@ -25,6 +25,10 @@ userId: db.uuid().relation({
25
25
  - Backward navigation: `User.userRoles`
26
26
  - GraphQL relational queries
27
27
 
28
+ ## Cross-Module Foreign Keys
29
+
30
+ When a FK references a type from another module, use the DB type injection pattern from [cross-module-dependency.md](cross-module-dependency.md#db-type-injection-srcdbts):
31
+
28
32
  ## Junction Tables (Many-to-Many)
29
33
 
30
34
  Junction tables need relations on both foreign keys plus a composite unique index:
@@ -4,5 +4,5 @@ Export order:
4
4
 
5
5
  1. `defineModule` from module.ts
6
6
  2. Generated types (enum values + types separately)
7
- 3. Error classes (for typed catch blocks)
7
+ 3. Error classes from `lib/errors.generated` (for typed catch blocks)
8
8
  4. Domain functions with input types
@@ -0,0 +1,32 @@
1
+ # Generated Code
2
+
3
+ ## Overview
4
+
5
+ Run `erp-kit module generate code --root <modules-dir> <module-name>` to generate deterministic `.generated.ts` files from command and query documentation.
6
+
7
+ ## What gets generated
8
+
9
+ - `lib/errors.generated.ts` — Domain error classes from Error Scenarios sections
10
+ - `lib/permissions.generated.ts` — Permission definitions from command doc filenames
11
+ - `command/<name>.generated.ts` — Command shells wiring permissions + `run` function
12
+ - `query/<name>.generated.ts` — Query shells wrapping `run` function
13
+
14
+ ## File convention
15
+
16
+ - `.generated.ts` files are always overwritten — never edit them
17
+ - Custom logic goes in `.ts` files (same base name) that import from `.generated.ts`
18
+
19
+ ## Workflow
20
+
21
+ 1. Write command/query docs (skills 1-3)
22
+ 2. Write db models (skill 4, step 2)
23
+ 3. Run `pnpm generate` for kysely types
24
+ 4. Run `erp-kit module generate code --root <root> <module>`
25
+ 5. Implement business logic in `.ts` files importing from `.generated.ts`
26
+
27
+ ## What you still write by hand
28
+
29
+ - Input interface field definitions (in `.generated.ts` they're stubs)
30
+ - Command handler business logic (validation, mutations, state transitions)
31
+ - Test assertions
32
+ - Complex queries beyond simple lookups
@@ -1,5 +1,5 @@
1
1
  ---
2
- name: 5-module-implementation-review
2
+ name: erp-kit-module-5-impl-review
3
3
  description: Review implementation parity between documentation and code. Use when validating that TDD implementation (step 4) matches model docs, command docs, and test coverage requirements.
4
4
  ---
5
5
 
@@ -12,19 +12,19 @@ Review **implementation consistency** between documentation and actual code.
12
12
  Verify that TDD implementation (models, commands, tests) properly implements what's documented in model docs and command docs.
13
13
 
14
14
  ```
15
- Model Docs (Schema) → src/db/*.ts (Implementation)
15
+ Model Docs (Schema) →db/*.ts (Implementation)
16
16
  ↓ ↓
17
17
  Fields createEntityType()
18
18
  Relationships Foreign keys
19
19
  State Transitions Status enums
20
20
 
21
- Command Docs (Spec) → src/command/*.ts (Implementation)
21
+ Command Docs (Spec) →command/*.ts (Implementation)
22
22
  ↓ ↓
23
23
  Business Rules Validation logic
24
24
  Error Scenarios Error classes
25
25
  Process Flows Branching logic
26
26
 
27
- Command Docs (Spec) → src/command/*.test.ts (Tests)
27
+ Command Docs (Spec) →command/*.test.ts (Tests)
28
28
  ↓ ↓
29
29
  Process Flow branches Test cases
30
30
  Error scenarios Error assertions
@@ -66,7 +66,7 @@ Extract for each model:
66
66
  Read ALL model files:
67
67
 
68
68
  ```
69
- modules/<module-name>/src/db/*.ts
69
+ modules/<module-name>/db/*.ts
70
70
  ```
71
71
 
72
72
  Extract for each model:
@@ -96,28 +96,28 @@ Extract for each command:
96
96
  Read ALL command files:
97
97
 
98
98
  ```
99
- modules/<module-name>/src/command/*.ts
99
+ modules/<module-name>/command/*.ts
100
100
  ```
101
101
 
102
102
  Extract for each command:
103
103
 
104
104
  - Validation logic implemented
105
- - Error classes thrown
105
+ - Error classes returned via Result
106
106
  - Branching paths (if/else, early returns)
107
107
  - Input interface fields
108
108
  - Return type structure
109
109
 
110
110
  ### 5. Read Error Implementation
111
111
 
112
- Read error definitions:
112
+ Read generated error definitions:
113
113
 
114
114
  ```
115
- modules/<module-name>/src/lib/errors.ts
115
+ modules/<module-name>/lib/errors.generated.ts
116
116
  ```
117
117
 
118
118
  Extract:
119
119
 
120
- - Error class names
120
+ - Error class names (generated from doc error codes)
121
121
  - Error codes
122
122
  - Error message patterns
123
123
 
@@ -126,7 +126,7 @@ Extract:
126
126
  Read ALL test files:
127
127
 
128
128
  ```
129
- modules/<module-name>/src/command/*.test.ts
129
+ modules/<module-name>/command/*.test.ts
130
130
  ```
131
131
 
132
132
  Extract for each command:
@@ -149,15 +149,15 @@ Extract for each command:
149
149
 
150
150
  ### 8. Command Doc → Command Code Parity Check
151
151
 
152
- | Check Item | Question |
153
- | ----------------------- | ---------------------------------------------------------- |
154
- | Business rule impl | Is each documented business rule implemented in code? |
155
- | Error scenario coverage | Does code throw errors for all documented error scenarios? |
156
- | Error code accuracy | Do error codes in code match documented codes? |
157
- | Process flow alignment | Do code branches match documented process flow? |
158
- | Input type coverage | Does input interface include all documented inputs? |
159
- | Return type accuracy | Does return type match documented output? |
160
- | Dual function pattern | Does command follow `_fn` internal / `fn` public pattern? |
152
+ | Check Item | Question |
153
+ | ----------------------- | ----------------------------------------------------------- |
154
+ | Business rule impl | Is each documented business rule implemented in code? |
155
+ | Error scenario coverage | Does code return errors for all documented error scenarios? |
156
+ | Error code accuracy | Do error codes in code match documented codes? |
157
+ | Process flow alignment | Do code branches match documented process flow? |
158
+ | Input type coverage | Does input interface include all documented inputs? |
159
+ | Return type accuracy | Does return type match documented output? |
160
+ | Generated shell pattern | Does command export `run` with a `.generated.ts` shell? |
161
161
 
162
162
  ### 9. Command Doc → Test Coverage Check
163
163
 
@@ -206,11 +206,11 @@ Extract for each command:
206
206
 
207
207
  ### 4. Error Scenario Implementation
208
208
 
209
- | Command Doc | Error Code | Implemented in errors.ts | Thrown in command | Test assertion |
210
- | ----------- | ---------------- | ------------------------ | ----------------- | -------------- |
211
- | <cmd-a> | ENTITY_NOT_FOUND | ✅ EntityNotFoundError | ✅ | ✅ |
212
- | <cmd-a> | INVALID_STATE | ❌ Not defined | ❌ | ❌ |
213
- | <cmd-b> | DUPLICATE_ENTRY | ✅ DuplicateEntryError | ✅ | ❌ No test |
209
+ | Command Doc | Error Code | Generated in errors.generated.ts | Returned in command | Test assertion |
210
+ | ----------- | ---------------- | -------------------------------- | ------------------- | -------------- |
211
+ | <cmd-a> | ENTITY_NOT_FOUND | ✅ EntityNotFoundError | ✅ | ✅ |
212
+ | <cmd-a> | INVALID_STATE | ❌ Not defined | ❌ | ❌ |
213
+ | <cmd-b> | DUPLICATE_ENTRY | ✅ DuplicateEntryError | ✅ | ❌ No test |
214
214
 
215
215
  ---
216
216
 
@@ -225,10 +225,10 @@ Extract for each command:
225
225
 
226
226
  ### 6. Implementation Pattern Compliance
227
227
 
228
- | Command | Dual Function Pattern | Input Interface Exported | JSDoc Present |
229
- | ------- | --------------------- | ------------------------ | ------------- |
230
- | <cmd-a> | ✅ `_cmdA` + `cmdA` | ✅ | ✅ |
231
- | <cmd-b> | ❌ Only `cmdB` | ✅ | ❌ |
228
+ | Command | Generated Shell | Input Interface Exported | JSDoc Present |
229
+ | ------- | ------------------------- | ------------------------ | ------------- |
230
+ | <cmd-a> | ✅ `.generated.ts` exists | ✅ | ✅ |
231
+ | <cmd-b> | ❌ Missing shell | ✅ | ❌ |
232
232
 
233
233
  ---
234
234
 
@@ -258,7 +258,7 @@ Extract for each command:
258
258
  | Command Doc | Missing Test Case | Doc Reference |
259
259
  | ----------- | -------------------------------- | --------------------- |
260
260
  | <cmd-b> | "entity already exists → return" | Process flow branch 2 |
261
- | <cmd-b> | "throws DUPLICATE_ENTRY" | Error scenario 3 |
261
+ | <cmd-b> | "returns DUPLICATE_ENTRY" | Error scenario 3 |
262
262
 
263
263
  ---
264
264
 
@@ -278,9 +278,9 @@ Extract for each command:
278
278
  | ----------------------------- | ------ | --------------------------------- |
279
279
  | Model Doc → Code Coverage | ⚠️ | X/Y models fully implemented |
280
280
  | Command Doc → Code Coverage | ⚠️ | X/Y commands fully implemented |
281
- | Error Scenario Implementation | ❌ | X/Y errors defined and thrown |
281
+ | Error Scenario Implementation | ❌ | X/Y errors defined and returned |
282
282
  | Test Coverage | ⚠️ | X/Y process branches have tests |
283
- | Pattern Compliance | ✅ | All commands follow dual function |
283
+ | Pattern Compliance | ✅ | All commands use generated shells |
284
284
 
285
285
  ### 10. Recommendations
286
286
 
@@ -294,7 +294,7 @@ Extract for each command:
294
294
  - cmd-b: add validation for "X must be unique"
295
295
 
296
296
  4. **Define missing error classes:**
297
- - Add `InvalidStateError` in errors.ts
297
+ - Add `INVALID_STATE` error scenario to command doc + re-run `erp-kit module generate code`
298
298
 
299
299
  5. **Add missing test cases:**
300
300
  - cmd-b: test "entity already exists → return existing"
@@ -317,10 +317,10 @@ Extract for each command:
317
317
  ### Command Doc → Command Code
318
318
 
319
319
  - **Unimplemented business rules**: Rule in doc but no validation code
320
- - **Missing error throws**: Error scenario documented but not thrown
320
+ - **Missing error returns**: Error scenario documented but not returned
321
321
  - **Wrong error codes**: Code and doc use different error codes
322
322
  - **Missing process branches**: Flowchart branch not in code logic
323
- - **Missing dual function**: Only public function, no internal `_fn`
323
+ - **Missing generated shell**: No `.generated.ts` file for command
324
324
 
325
325
  ### Command Doc → Tests
326
326
 
@@ -360,9 +360,9 @@ PENDING → ACTIVE → REMOVED
360
360
  ## Error Scenarios
361
361
 
362
362
  | Code | Condition |
363
- | -------------- | -------------- | ----------------------------------- |
364
- | NOT_FOUND | Entity missing | → Check: error class + throw + test |
365
- | INVALID_STATUS | Wrong status | → Check: error class + throw + test |
363
+ | -------------- | -------------- | ------------------------------------------ |
364
+ | NOT_FOUND | Entity missing | → Check: error class + return err() + test |
365
+ | INVALID_STATUS | Wrong status | → Check: error class + return err() + test |
366
366
 
367
367
  ## Process Flow (mermaid)
368
368
 
@@ -384,25 +384,27 @@ export function createEntityType(params) {
384
384
  };
385
385
  }
386
386
 
387
- // Command: check business rules
388
- export async function _myCommand(db, input) {
387
+ // Command: check business rules (run function pattern)
388
+ export async function run(db, input, ctx) {
389
389
  // Check: validation for each business rule
390
- if (!entity) throw new EntityNotFoundError(); // Check: matches doc error
390
+ if (!entity) return err(new EntityNotFoundError()); // Check: matches doc error
391
391
  // Check: each branch matches process flow
392
392
  }
393
393
 
394
+ // Generated shell: check it exists
395
+ // myCommand.generated.ts
396
+ export const myCommand = defineCommand(permissions.myCommand, run);
397
+
394
398
  // Test: check coverage
395
399
  describe("myCommand", () => {
396
400
  it("creates entity when valid"); // Check: happy path
397
401
  it("returns existing if found"); // Check: idempotent path
398
- it("throws NOT_FOUND when missing"); // Check: error scenario
402
+ it("returns NOT_FOUND when missing"); // Check: error scenario
399
403
  });
400
404
  ```
401
405
 
402
406
  ## References
403
407
 
404
- - [Model patterns](references/models.md)
405
408
  - [Command patterns](references/commands.md)
406
409
  - [Error patterns](references/errors.md)
407
410
  - [Testing patterns](references/testing.md)
408
- - [Export order](references/exports.md)
@@ -0,0 +1,62 @@
1
+ # Command Implementation
2
+
3
+ ## Unified Pattern: `run` + generated shell
4
+
5
+ All commands follow the same pattern — export a `run` function, and the generated shell wraps it with `defineCommand`:
6
+
7
+ **Implementation file** (`command/myCommand.ts`):
8
+
9
+ ```typescript
10
+ export async function run(db: DB, input: MyCommandInput, ctx: CommandContext) {
11
+ // validate → query → mutate
12
+ return ok({ entity });
13
+ }
14
+ ```
15
+
16
+ **Generated shell** (`command/myCommand.generated.ts`):
17
+
18
+ ```typescript
19
+ export const myCommand = defineCommand(permissions.myCommand, run);
20
+ ```
21
+
22
+ ## Custom Fields (generic CF)
23
+
24
+ Commands that **write** (insert or update) into a table with user-extensible fields make `run` generic:
25
+
26
+ - Generic `CF extends Record<string, unknown>` on the `run` function
27
+ - Input type: `CreateXInput & CF` or `UpdateXInput & Partial<CF>`
28
+ - Destructure known fields, rest-spread custom fields
29
+ - Cast custom fields: `...(customFields as Record<string, unknown>)`
30
+ - module.ts locks CF via instantiation expression: `const createXTyped = createX<FieldsToInsertable<F>>;`
31
+
32
+ ### Rule: when to use generic CF
33
+
34
+ > If a model uses `fields?: F` for custom fields, **every command that writes those fields** must have a generic `CF`. This includes create, update, and any other write command. The determining factor is whether the command writes to a table with custom fields, not just whether it inserts new rows.
35
+
36
+ ## Implementation Considerations
37
+
38
+ - **Validation**: Check referenced entities exist before operating
39
+ - **Idempotency**: For assign/revoke, return existing instead of throwing
40
+ - **Return format**: Wrap in object `{ entity }` not just `entity`
41
+
42
+ ## Conventions
43
+
44
+ - Input types: exported interfaces (`export interface MyFunctionInput`)
45
+ - Use `.executeTakeFirst()` for single results
46
+ - Include JSDoc: `/** Function: name \n Description */`
47
+
48
+ ## State Transitions
49
+
50
+ For commands that transition between statuses, accept `from?: string[]` with a default:
51
+
52
+ ```typescript
53
+ from?: string[]; // Default: ["ACTIVE"]
54
+
55
+ const validFromStatuses = input.from ?? ["ACTIVE"];
56
+ if (!validFromStatuses.includes(user.status)) {
57
+ return err(new InvalidStatusTransitionError(user.status, targetStatus));
58
+ }
59
+ ```
60
+
61
+ - Default `from` contains the base valid source status
62
+ - Parent modules can override to allow transitions from additional statuses
@@ -0,0 +1,10 @@
1
+ # Error Classes
2
+
3
+ Errors are **generated** from command/query documentation into `lib/errors.generated.ts`.
4
+
5
+ ## What to review
6
+
7
+ - Every error code in `docs/commands/*.md` has a corresponding class in `lib/errors.generated.ts`
8
+ - Command implementations import errors from `../lib/errors.generated`
9
+ - Error codes in code match documented codes exactly
10
+ - If errors are missing, re-run `erp-kit module generate code`
@@ -20,7 +20,7 @@ spies.select.mockReturnValue(entity);
20
20
  spies.select.mockReturnValueOnce(first).mockReturnValueOnce(second);
21
21
  ```
22
22
 
23
- ## Fixtures (`src/testing/fixtures.ts`)
23
+ ## Fixtures (`testing/fixtures.ts`)
24
24
 
25
25
  - Import `Schema` from `lib/types` (not `Namespace` from generated code)
26
26
  - Pattern: `export const baseEntity = { ... } as const satisfies Entity<Schema>`
@@ -0,0 +1,16 @@
1
+ ---
2
+ name: erp-kit-module-shared
3
+ description: Shared references for erp-kit module skills. Not invoked directly — referenced by erp-kit-module-1 through erp-kit-module-5.
4
+ ---
5
+
6
+ # erp-kit-module-shared
7
+
8
+ Canonical reference files shared across module skills. Individual skills reference these instead of maintaining their own copies.
9
+
10
+ ## References
11
+
12
+ - [Module structure](references/structure.md)
13
+ - [Command patterns](references/commands.md)
14
+ - [Query patterns](references/queries.md)
15
+ - [Error patterns](references/errors.md)
16
+ - [Testing patterns](references/testing.md)