@open-mercato/core 0.4.2-canary-ed15f2e753 → 0.4.2-canary-f075c3eb92

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 (241) hide show
  1. package/dist/generated/entities.ids.generated.js +0 -1
  2. package/dist/generated/entities.ids.generated.js.map +2 -2
  3. package/dist/generated/entity-fields-registry.js +0 -2
  4. package/dist/generated/entity-fields-registry.js.map +2 -2
  5. package/dist/modules/api_keys/setup.js +11 -0
  6. package/dist/modules/api_keys/setup.js.map +7 -0
  7. package/dist/modules/attachments/components/AttachmentLibrary.js +1 -1
  8. package/dist/modules/attachments/components/AttachmentLibrary.js.map +2 -2
  9. package/dist/modules/attachments/lib/assignmentDetails.js +31 -17
  10. package/dist/modules/attachments/lib/assignmentDetails.js.map +2 -2
  11. package/dist/modules/attachments/lib/partitions.js +3 -3
  12. package/dist/modules/attachments/lib/partitions.js.map +2 -2
  13. package/dist/modules/attachments/setup.js +11 -0
  14. package/dist/modules/attachments/setup.js.map +7 -0
  15. package/dist/modules/audit_logs/setup.js +12 -0
  16. package/dist/modules/audit_logs/setup.js.map +7 -0
  17. package/dist/modules/auth/lib/setup-app.js +29 -159
  18. package/dist/modules/auth/lib/setup-app.js.map +2 -2
  19. package/dist/modules/auth/setup.js +11 -0
  20. package/dist/modules/auth/setup.js.map +7 -0
  21. package/dist/modules/business_rules/data/validators.js +0 -34
  22. package/dist/modules/business_rules/data/validators.js.map +2 -2
  23. package/dist/modules/business_rules/index.js +1 -21
  24. package/dist/modules/business_rules/index.js.map +2 -2
  25. package/dist/modules/business_rules/lib/rule-engine.js +1 -182
  26. package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
  27. package/dist/modules/business_rules/setup.js +11 -0
  28. package/dist/modules/business_rules/setup.js.map +7 -0
  29. package/dist/modules/catalog/setup.js +22 -0
  30. package/dist/modules/catalog/setup.js.map +7 -0
  31. package/dist/modules/configs/lib/upgrade-actions.js +65 -15
  32. package/dist/modules/configs/lib/upgrade-actions.js.map +2 -2
  33. package/dist/modules/configs/setup.js +16 -0
  34. package/dist/modules/configs/setup.js.map +7 -0
  35. package/dist/modules/currencies/setup.js +16 -0
  36. package/dist/modules/currencies/setup.js.map +7 -0
  37. package/dist/modules/customers/setup.js +36 -0
  38. package/dist/modules/customers/setup.js.map +7 -0
  39. package/dist/modules/dashboards/setup.js +12 -0
  40. package/dist/modules/dashboards/setup.js.map +7 -0
  41. package/dist/modules/dictionaries/setup.js +12 -0
  42. package/dist/modules/dictionaries/setup.js.map +7 -0
  43. package/dist/modules/directory/setup.js +12 -0
  44. package/dist/modules/directory/setup.js.map +7 -0
  45. package/dist/modules/entities/setup.js +11 -0
  46. package/dist/modules/entities/setup.js.map +7 -0
  47. package/dist/modules/feature_toggles/setup.js +11 -0
  48. package/dist/modules/feature_toggles/setup.js.map +7 -0
  49. package/dist/modules/perspectives/setup.js +12 -0
  50. package/dist/modules/perspectives/setup.js.map +7 -0
  51. package/dist/modules/planner/setup.js +21 -0
  52. package/dist/modules/planner/setup.js.map +7 -0
  53. package/dist/modules/query_index/setup.js +11 -0
  54. package/dist/modules/query_index/setup.js.map +7 -0
  55. package/dist/modules/resources/setup.js +21 -0
  56. package/dist/modules/resources/setup.js.map +7 -0
  57. package/dist/modules/sales/acl.js +0 -1
  58. package/dist/modules/sales/acl.js.map +2 -2
  59. package/dist/modules/sales/backend/sales/documents/[id]/page.js +0 -12
  60. package/dist/modules/sales/backend/sales/documents/[id]/page.js.map +2 -2
  61. package/dist/modules/sales/commands/documents.js +0 -62
  62. package/dist/modules/sales/commands/documents.js.map +2 -2
  63. package/dist/modules/sales/lib/dictionaries.js +0 -3
  64. package/dist/modules/sales/lib/dictionaries.js.map +2 -2
  65. package/dist/modules/sales/setup.js +99 -0
  66. package/dist/modules/sales/setup.js.map +7 -0
  67. package/dist/modules/staff/setup.js +27 -0
  68. package/dist/modules/staff/setup.js.map +7 -0
  69. package/dist/modules/workflows/acl.js +0 -2
  70. package/dist/modules/workflows/acl.js.map +2 -2
  71. package/dist/modules/workflows/api/instances/route.js +6 -18
  72. package/dist/modules/workflows/api/instances/route.js.map +2 -2
  73. package/dist/modules/workflows/api/tasks/route.js +1 -6
  74. package/dist/modules/workflows/api/tasks/route.js.map +2 -2
  75. package/dist/modules/workflows/backend/definitions/[id]/page.js +1 -9
  76. package/dist/modules/workflows/backend/definitions/[id]/page.js.map +2 -2
  77. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js +1 -1
  78. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js.map +2 -2
  79. package/dist/modules/workflows/backend/definitions/create/page.js +15 -24
  80. package/dist/modules/workflows/backend/definitions/create/page.js.map +2 -2
  81. package/dist/modules/workflows/backend/definitions/create/page.meta.js +1 -1
  82. package/dist/modules/workflows/backend/definitions/create/page.meta.js.map +2 -2
  83. package/dist/modules/workflows/backend/definitions/visual-editor/page.js +132 -150
  84. package/dist/modules/workflows/backend/definitions/visual-editor/page.js.map +2 -2
  85. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js +1 -1
  86. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js.map +2 -2
  87. package/dist/modules/workflows/backend/events/[id]/page.js +1 -1
  88. package/dist/modules/workflows/backend/events/[id]/page.js.map +2 -2
  89. package/dist/modules/workflows/backend/events/[id]/page.meta.js +2 -2
  90. package/dist/modules/workflows/backend/events/[id]/page.meta.js.map +2 -2
  91. package/dist/modules/workflows/backend/instances/[id]/page.meta.js +2 -2
  92. package/dist/modules/workflows/backend/instances/[id]/page.meta.js.map +2 -2
  93. package/dist/modules/workflows/backend/tasks/[id]/page.js +1 -1
  94. package/dist/modules/workflows/backend/tasks/[id]/page.js.map +2 -2
  95. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js +2 -2
  96. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js.map +2 -2
  97. package/dist/modules/workflows/backend/tasks/page.js +6 -5
  98. package/dist/modules/workflows/backend/tasks/page.js.map +2 -2
  99. package/dist/modules/workflows/cli.js +3 -81
  100. package/dist/modules/workflows/cli.js.map +3 -3
  101. package/dist/modules/workflows/data/entities.js +1 -64
  102. package/dist/modules/workflows/data/entities.js.map +2 -2
  103. package/dist/modules/workflows/data/validators.js +0 -115
  104. package/dist/modules/workflows/data/validators.js.map +2 -2
  105. package/dist/modules/workflows/examples/checkout-demo-definition.json +5 -1
  106. package/dist/modules/workflows/lib/activity-executor.js +13 -75
  107. package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
  108. package/dist/modules/workflows/lib/graph-utils.js +2 -71
  109. package/dist/modules/workflows/lib/graph-utils.js.map +2 -2
  110. package/dist/modules/workflows/lib/seeds.js +7 -36
  111. package/dist/modules/workflows/lib/seeds.js.map +2 -2
  112. package/dist/modules/workflows/lib/start-validator.js +23 -33
  113. package/dist/modules/workflows/lib/start-validator.js.map +2 -2
  114. package/dist/modules/workflows/lib/transition-handler.js +45 -157
  115. package/dist/modules/workflows/lib/transition-handler.js.map +3 -3
  116. package/dist/modules/workflows/migrations/Migration20251207131955.js +76 -72
  117. package/dist/modules/workflows/migrations/Migration20251207131955.js.map +2 -2
  118. package/dist/modules/workflows/setup.js +16 -0
  119. package/dist/modules/workflows/setup.js.map +7 -0
  120. package/generated/entities.ids.generated.ts +0 -1
  121. package/generated/entity-fields-registry.ts +0 -2
  122. package/package.json +2 -2
  123. package/src/__tests__/module-decoupling.test.ts +356 -0
  124. package/src/modules/api_keys/setup.ts +9 -0
  125. package/src/modules/attachments/components/AttachmentLibrary.tsx +2 -2
  126. package/src/modules/attachments/lib/assignmentDetails.ts +32 -16
  127. package/src/modules/attachments/lib/partitions.ts +3 -3
  128. package/src/modules/attachments/setup.ts +9 -0
  129. package/src/modules/audit_logs/setup.ts +10 -0
  130. package/src/modules/auth/__tests__/cli-setup-acl.test.ts +30 -0
  131. package/src/modules/auth/lib/setup-app.ts +40 -177
  132. package/src/modules/auth/setup.ts +9 -0
  133. package/src/modules/business_rules/data/validators.ts +0 -40
  134. package/src/modules/business_rules/index.ts +0 -25
  135. package/src/modules/business_rules/lib/rule-engine.ts +1 -281
  136. package/src/modules/business_rules/setup.ts +9 -0
  137. package/src/modules/catalog/setup.ts +22 -0
  138. package/src/modules/configs/lib/upgrade-actions.ts +78 -17
  139. package/src/modules/configs/setup.ts +14 -0
  140. package/src/modules/currencies/setup.ts +15 -0
  141. package/src/modules/customers/setup.ts +36 -0
  142. package/src/modules/dashboards/setup.ts +10 -0
  143. package/src/modules/dictionaries/setup.ts +10 -0
  144. package/src/modules/directory/setup.ts +10 -0
  145. package/src/modules/entities/setup.ts +9 -0
  146. package/src/modules/feature_toggles/setup.ts +9 -0
  147. package/src/modules/perspectives/setup.ts +10 -0
  148. package/src/modules/planner/setup.ts +21 -0
  149. package/src/modules/query_index/setup.ts +9 -0
  150. package/src/modules/resources/setup.ts +21 -0
  151. package/src/modules/sales/acl.ts +0 -1
  152. package/src/modules/sales/backend/sales/documents/[id]/page.tsx +0 -16
  153. package/src/modules/sales/commands/documents.ts +1 -74
  154. package/src/modules/sales/lib/dictionaries.ts +0 -3
  155. package/src/modules/sales/setup.ts +108 -0
  156. package/src/modules/staff/setup.ts +27 -0
  157. package/src/modules/workflows/acl.ts +0 -2
  158. package/src/modules/workflows/api/__tests__/instances.route.test.ts +2 -5
  159. package/src/modules/workflows/api/instances/route.ts +7 -21
  160. package/src/modules/workflows/api/tasks/route.ts +1 -7
  161. package/src/modules/workflows/backend/definitions/[id]/page.meta.ts +1 -1
  162. package/src/modules/workflows/backend/definitions/[id]/page.tsx +0 -9
  163. package/src/modules/workflows/backend/definitions/create/page.meta.ts +1 -1
  164. package/src/modules/workflows/backend/definitions/create/page.tsx +0 -9
  165. package/src/modules/workflows/backend/definitions/visual-editor/page.meta.ts +1 -1
  166. package/src/modules/workflows/backend/definitions/visual-editor/page.tsx +3 -21
  167. package/src/modules/workflows/backend/events/[id]/page.meta.ts +2 -2
  168. package/src/modules/workflows/backend/events/[id]/page.tsx +1 -1
  169. package/src/modules/workflows/backend/instances/[id]/page.meta.ts +2 -2
  170. package/src/modules/workflows/backend/tasks/[id]/page.meta.ts +2 -2
  171. package/src/modules/workflows/backend/tasks/[id]/page.tsx +1 -1
  172. package/src/modules/workflows/backend/tasks/page.tsx +6 -5
  173. package/src/modules/workflows/cli.ts +0 -111
  174. package/src/modules/workflows/data/entities.ts +0 -124
  175. package/src/modules/workflows/data/validators.ts +0 -138
  176. package/src/modules/workflows/examples/checkout-demo-definition.json +5 -1
  177. package/src/modules/workflows/i18n/en.json +0 -71
  178. package/src/modules/workflows/lib/__tests__/activity-executor.test.ts +36 -43
  179. package/src/modules/workflows/lib/__tests__/transition-handler.test.ts +90 -170
  180. package/src/modules/workflows/lib/activity-executor.ts +16 -129
  181. package/src/modules/workflows/lib/graph-utils.ts +2 -117
  182. package/src/modules/workflows/lib/seeds.ts +12 -50
  183. package/src/modules/workflows/lib/start-validator.ts +28 -38
  184. package/src/modules/workflows/lib/transition-handler.ts +55 -208
  185. package/src/modules/workflows/migrations/Migration20251207131955.ts +77 -143
  186. package/src/modules/workflows/setup.ts +15 -0
  187. package/dist/generated/entities/workflow_event_trigger/index.js +0 -33
  188. package/dist/generated/entities/workflow_event_trigger/index.js.map +0 -7
  189. package/dist/modules/auth/events.js +0 -30
  190. package/dist/modules/auth/events.js.map +0 -7
  191. package/dist/modules/business_rules/api/execute/[ruleId]/route.js +0 -145
  192. package/dist/modules/business_rules/api/execute/[ruleId]/route.js.map +0 -7
  193. package/dist/modules/catalog/events.js +0 -34
  194. package/dist/modules/catalog/events.js.map +0 -7
  195. package/dist/modules/customers/events.js +0 -49
  196. package/dist/modules/customers/events.js.map +0 -7
  197. package/dist/modules/directory/events.js +0 -23
  198. package/dist/modules/directory/events.js.map +0 -7
  199. package/dist/modules/sales/events.js +0 -63
  200. package/dist/modules/sales/events.js.map +0 -7
  201. package/dist/modules/sales/lib/frontend/documentDataEvents.js +0 -25
  202. package/dist/modules/sales/lib/frontend/documentDataEvents.js.map +0 -7
  203. package/dist/modules/workflows/components/DefinitionTriggersEditor.js +0 -481
  204. package/dist/modules/workflows/components/DefinitionTriggersEditor.js.map +0 -7
  205. package/dist/modules/workflows/components/EventTriggersEditor.js +0 -553
  206. package/dist/modules/workflows/components/EventTriggersEditor.js.map +0 -7
  207. package/dist/modules/workflows/events.js +0 -38
  208. package/dist/modules/workflows/events.js.map +0 -7
  209. package/dist/modules/workflows/examples/order-approval-definition.json +0 -257
  210. package/dist/modules/workflows/examples/order-approval-guard-rules.json +0 -32
  211. package/dist/modules/workflows/lib/event-trigger-service.js +0 -308
  212. package/dist/modules/workflows/lib/event-trigger-service.js.map +0 -7
  213. package/dist/modules/workflows/migrations/Migration20260123143500.js +0 -36
  214. package/dist/modules/workflows/migrations/Migration20260123143500.js.map +0 -7
  215. package/dist/modules/workflows/subscribers/event-trigger.js +0 -78
  216. package/dist/modules/workflows/subscribers/event-trigger.js.map +0 -7
  217. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js +0 -323
  218. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js.map +0 -7
  219. package/dist/modules/workflows/widgets/injection/order-approval/widget.js +0 -17
  220. package/dist/modules/workflows/widgets/injection/order-approval/widget.js.map +0 -7
  221. package/dist/modules/workflows/widgets/injection-table.js +0 -19
  222. package/dist/modules/workflows/widgets/injection-table.js.map +0 -7
  223. package/generated/entities/workflow_event_trigger/index.ts +0 -15
  224. package/src/modules/auth/events.ts +0 -39
  225. package/src/modules/business_rules/api/execute/[ruleId]/route.ts +0 -163
  226. package/src/modules/catalog/events.ts +0 -45
  227. package/src/modules/customers/events.ts +0 -63
  228. package/src/modules/directory/events.ts +0 -31
  229. package/src/modules/sales/events.ts +0 -82
  230. package/src/modules/sales/lib/frontend/documentDataEvents.ts +0 -28
  231. package/src/modules/workflows/components/DefinitionTriggersEditor.tsx +0 -581
  232. package/src/modules/workflows/components/EventTriggersEditor.tsx +0 -664
  233. package/src/modules/workflows/events.ts +0 -49
  234. package/src/modules/workflows/examples/order-approval-definition.json +0 -257
  235. package/src/modules/workflows/examples/order-approval-guard-rules.json +0 -32
  236. package/src/modules/workflows/lib/event-trigger-service.ts +0 -557
  237. package/src/modules/workflows/migrations/Migration20260123143500.ts +0 -38
  238. package/src/modules/workflows/subscribers/event-trigger.ts +0 -109
  239. package/src/modules/workflows/widgets/injection/order-approval/widget.client.tsx +0 -446
  240. package/src/modules/workflows/widgets/injection/order-approval/widget.ts +0 -16
  241. package/src/modules/workflows/widgets/injection-table.ts +0 -21
@@ -1,14 +1,11 @@
1
1
  import type { EntityManager } from '@mikro-orm/postgresql'
2
2
  import * as fs from 'fs'
3
3
  import * as path from 'path'
4
+ import { fileURLToPath } from 'node:url'
4
5
  import { WorkflowDefinition, type WorkflowDefinitionData } from '../data/entities'
5
6
  import { BusinessRule, type RuleType } from '@open-mercato/core/modules/business_rules/data/entities'
6
- import checkoutDemoDefinition from '../examples/checkout-demo-definition.json'
7
- import guardRulesExample from '../examples/guard-rules-example.json'
8
- import salesPipelineDefinition from '../examples/sales-pipeline-definition.json'
9
- import simpleApprovalDefinition from '../examples/simple-approval-definition.json'
10
- import orderApprovalDefinition from '../examples/order-approval-definition.json'
11
- import orderApprovalGuardRules from '../examples/order-approval-guard-rules.json'
7
+
8
+ const __esmDirname = path.dirname(fileURLToPath(import.meta.url))
12
9
 
13
10
  export type WorkflowSeedScope = { tenantId: string; organizationId: string }
14
11
 
@@ -48,22 +45,9 @@ type GuardRuleSeed = {
48
45
  labelsJson?: Record<string, string>
49
46
  }
50
47
 
51
- const embeddedSeeds: Record<string, unknown> = {
52
- 'checkout-demo-definition.json': checkoutDemoDefinition,
53
- 'guard-rules-example.json': guardRulesExample,
54
- 'sales-pipeline-definition.json': salesPipelineDefinition,
55
- 'simple-approval-definition.json': simpleApprovalDefinition,
56
- 'order-approval-definition.json': orderApprovalDefinition,
57
- 'order-approval-guard-rules.json': orderApprovalGuardRules,
58
- }
59
-
60
48
  function readExampleJson<T>(fileName: string): T {
61
- const embedded = embeddedSeeds[fileName]
62
- if (embedded) {
63
- return embedded as T
64
- }
65
49
  const candidates = [
66
- path.join(__dirname, '..', 'examples', fileName),
50
+ path.join(__esmDirname, '..', 'examples', fileName),
67
51
  path.join(process.cwd(), 'packages', 'core', 'src', 'modules', 'workflows', 'examples', fileName),
68
52
  path.join(process.cwd(), 'src', 'modules', 'workflows', 'examples', fileName),
69
53
  ]
@@ -94,35 +78,16 @@ async function seedWorkflowDefinition(
94
78
  })
95
79
 
96
80
  if (existing) {
97
- // Check if the definition needs to be updated by comparing steps and transitions
98
- const seedStepCount = seed.definition.steps.length
99
- const existingStepCount = existing.definition.steps.length
100
- const seedTransitionCount = seed.definition.transitions.length
101
- const existingTransitionCount = existing.definition.transitions.length
102
-
103
- // Check for preConditions on transitions
104
- const seedHasTransitionPreConditions = seed.definition.transitions.some(
105
- (t: any) => t.preConditions && t.preConditions.length > 0
106
- )
107
- const existingHasTransitionPreConditions = existing.definition.transitions.some(
108
- (t: any) => t.preConditions && t.preConditions.length > 0
109
- )
110
-
111
- // Check for preConditions on START step
81
+ // Check if the definition needs to be updated (e.g., missing preConditions on START step)
112
82
  const seedStartStep = seed.definition.steps.find((s: any) => s.stepType === 'START')
113
83
  const existingStartStep = existing.definition.steps.find((s: any) => s.stepType === 'START')
114
- const seedHasStartPreConditions = seedStartStep?.preConditions && seedStartStep.preConditions.length > 0
115
- const existingHasStartPreConditions = existingStartStep?.preConditions && existingStartStep.preConditions.length > 0
116
-
117
- // Update if structure has changed
118
- const needsUpdate =
119
- seedStepCount !== existingStepCount ||
120
- seedTransitionCount !== existingTransitionCount ||
121
- (seedHasStartPreConditions && !existingHasStartPreConditions) ||
122
- (seedHasTransitionPreConditions && !existingHasTransitionPreConditions)
123
-
124
- if (needsUpdate) {
125
- console.log(`[seed] Updating workflow ${workflowId} (steps: ${existingStepCount}→${seedStepCount}, transitions: ${existingTransitionCount}→${seedTransitionCount})`)
84
+
85
+ const seedHasPreConditions = seedStartStep?.preConditions && seedStartStep.preConditions.length > 0
86
+ const existingHasPreConditions = existingStartStep?.preConditions && existingStartStep.preConditions.length > 0
87
+
88
+ // Update if seed has preConditions but existing doesn't
89
+ if (seedHasPreConditions && !existingHasPreConditions) {
90
+ console.log(`[seed] Updating workflow ${workflowId} with preConditions`)
126
91
  existing.definition = seed.definition
127
92
  await em.flush()
128
93
  return true
@@ -195,7 +160,4 @@ export async function seedExampleWorkflows(em: EntityManager, scope: WorkflowSee
195
160
  await seedGuardRules(em, scope, 'guard-rules-example.json')
196
161
  await seedWorkflowDefinition(em, scope, 'sales-pipeline-definition.json')
197
162
  await seedWorkflowDefinition(em, scope, 'simple-approval-definition.json')
198
- // Seed order approval guard rules before the workflow definition
199
- await seedGuardRules(em, scope, 'order-approval-guard-rules.json')
200
- await seedWorkflowDefinition(em, scope, 'order-approval-definition.json')
201
163
  }
@@ -128,69 +128,59 @@ export async function validateWorkflowStart(
128
128
  const validatedRules: ValidatedRule[] = []
129
129
 
130
130
  for (const condition of preConditions) {
131
- // Execute rule directly by string rule_id
132
- const result = await ruleEngine.executeRuleByRuleId(em, {
133
- ruleId: condition.ruleId, // String identifier like "workflow_checkout_inventory_available"
131
+ const ruleContext: ruleEngine.RuleEngineContext = {
132
+ entityType: `workflow:${workflowId}:start`,
133
+ entityId: 'pre_start_validation',
134
+ eventType: 'validate_start',
134
135
  data: {
135
136
  workflowId,
136
137
  workflowContext: context,
137
138
  },
138
139
  tenantId,
139
140
  organizationId,
140
- entityType: `workflow:${workflowId}:start`,
141
- entityId: 'pre_start_validation',
142
- eventType: 'validate_start',
143
141
  dryRun: true, // Don't log execution during validation
144
- })
142
+ }
145
143
 
146
- validatedRules.push({
147
- ruleId: condition.ruleId,
148
- passed: result.conditionResult,
149
- executionTime: result.executionTime,
144
+ // Find applicable rules for this context
145
+ const rules = await ruleEngine.findApplicableRules(em, {
146
+ entityType: ruleContext.entityType,
147
+ eventType: ruleContext.eventType,
148
+ tenantId,
149
+ organizationId,
150
+ ruleType: 'GUARD',
150
151
  })
151
152
 
152
- // Handle rule not found
153
- if (result.error === 'Rule not found') {
153
+ const rule = rules.find(r => r.ruleId === condition.ruleId)
154
+
155
+ if (!rule) {
156
+ // Rule not found - if required, this is an error
154
157
  if (condition.required) {
155
158
  errors.push({
156
159
  ruleId: condition.ruleId,
157
- message: getLocalizedMessage(condition, null, locale, `Business rule not found: ${condition.ruleId}`),
160
+ message: getLocalizedMessage(condition, null, locale, `Business rule '${condition.ruleId}' not found`),
158
161
  code: 'RULE_NOT_FOUND',
159
162
  })
163
+ validatedRules.push({ ruleId: condition.ruleId, passed: false })
160
164
  }
161
165
  continue
162
166
  }
163
167
 
164
- // Handle disabled rule
165
- if (result.error === 'Rule is disabled') {
166
- if (condition.required) {
167
- errors.push({
168
- ruleId: condition.ruleId,
169
- message: getLocalizedMessage(condition, null, locale, `Business rule is disabled: ${result.ruleName}`),
170
- code: 'RULE_DISABLED',
171
- })
172
- }
173
- continue
174
- }
168
+ // Execute the single rule
169
+ const result = await ruleEngine.executeSingleRule(em, rule, ruleContext)
175
170
 
176
- // Handle other errors (not yet effective, expired, etc.)
177
- if (result.error && condition.required) {
178
- errors.push({
179
- ruleId: condition.ruleId,
180
- message: getLocalizedMessage(condition, null, locale, `Rule error: ${result.error}`),
181
- code: 'RULE_ERROR',
182
- })
183
- continue
184
- }
171
+ validatedRules.push({
172
+ ruleId: condition.ruleId,
173
+ passed: result.conditionResult,
174
+ executionTime: result.executionTime,
175
+ })
185
176
 
186
- // Handle condition failure
187
177
  if (!result.conditionResult && condition.required) {
188
- // Get localized message from condition or use default with rule name
178
+ // Get localized message from condition, rule failure actions, or default
189
179
  const message = getLocalizedMessage(
190
180
  condition,
191
- null,
181
+ rule,
192
182
  locale,
193
- `Pre-condition '${result.ruleName || condition.ruleId}' failed`
183
+ `Pre-condition '${rule.ruleName || condition.ruleId}' failed`
194
184
  )
195
185
  errors.push({
196
186
  ruleId: condition.ruleId,
@@ -19,6 +19,7 @@ import {
19
19
  } from '../data/entities'
20
20
  import * as ruleEvaluator from '../../business_rules/lib/rule-evaluator'
21
21
  import * as ruleEngine from '../../business_rules/lib/rule-engine'
22
+ import type { RuleEngineContext } from '../../business_rules/lib/rule-engine'
22
23
  import * as activityExecutor from './activity-executor'
23
24
  import type { ActivityDefinition } from './activity-executor'
24
25
  import * as stepHandler from './step-handler'
@@ -193,15 +194,11 @@ export async function evaluateTransition(
193
194
  /**
194
195
  * Find all valid transitions from current step
195
196
  *
196
- * This function evaluates both inline conditions AND preConditions (business rules)
197
- * to determine which transitions are truly valid. This is important for decision
198
- * branching where multiple transitions exist with different preConditions.
199
- *
200
197
  * @param em - Entity manager
201
198
  * @param instance - Workflow instance
202
199
  * @param fromStepId - Current step ID
203
200
  * @param context - Evaluation context
204
- * @returns Array of evaluation results for all transitions, sorted by priority (desc)
201
+ * @returns Array of evaluation results for all transitions
205
202
  */
206
203
  export async function findValidTransitions(
207
204
  em: EntityManager,
@@ -219,17 +216,16 @@ export async function findValidTransitions(
219
216
  return []
220
217
  }
221
218
 
222
- // Find all transitions from current step, sorted by priority (highest first)
223
- const transitions = (definition.definition.transitions || [])
224
- .filter((t: any) => t.fromStepId === fromStepId)
225
- .sort((a: any, b: any) => (b.priority || 0) - (a.priority || 0))
219
+ // Find all transitions from current step
220
+ const transitions = (definition.definition.transitions || []).filter(
221
+ (t: any) => t.fromStepId === fromStepId
222
+ )
226
223
 
227
- // Evaluate each transition including preConditions
224
+ // Evaluate each transition
228
225
  const results: TransitionEvaluationResult[] = []
229
226
 
230
227
  for (const transition of transitions) {
231
- // First check inline condition
232
- const conditionResult = await evaluateTransition(
228
+ const result = await evaluateTransition(
233
229
  em,
234
230
  instance,
235
231
  fromStepId,
@@ -237,42 +233,7 @@ export async function findValidTransitions(
237
233
  context
238
234
  )
239
235
 
240
- if (!conditionResult.isValid) {
241
- results.push(conditionResult)
242
- continue
243
- }
244
-
245
- // Also evaluate preConditions if they exist
246
- const preConditions = transition.preConditions || []
247
- if (preConditions.length > 0) {
248
- const preConditionsResult = await evaluatePreConditions(
249
- em,
250
- instance,
251
- transition,
252
- context as TransitionExecutionContext
253
- )
254
-
255
- if (!preConditionsResult.allowed) {
256
- // Transition is invalid due to preConditions
257
- const failedRules = preConditionsResult.executedRules
258
- .filter((r) => !r.conditionResult)
259
- .map((r) => r.rule.ruleId || r.rule.ruleName)
260
-
261
- results.push({
262
- isValid: false,
263
- transition,
264
- reason: `Pre-conditions failed: ${failedRules.join(', ')}`,
265
- failedConditions: failedRules,
266
- })
267
- continue
268
- }
269
- }
270
-
271
- // Transition is valid (both condition and preConditions passed)
272
- results.push({
273
- ...conditionResult,
274
- transition,
275
- })
236
+ results.push(result)
276
237
  }
277
238
 
278
239
  return results
@@ -688,10 +649,6 @@ async function evaluateTransitionConditions(
688
649
  * Pre-conditions are GUARD rules that must pass before transition can execute.
689
650
  * If any GUARD rule fails, the transition is blocked.
690
651
  *
691
- * If the transition defines specific preConditions with ruleIds, those are
692
- * executed directly via executeRuleByRuleId. Otherwise, falls back to
693
- * discovery-based execution via executeRules.
694
- *
695
652
  * @param em - Entity manager
696
653
  * @param instance - Workflow instance
697
654
  * @param transition - Transition definition
@@ -718,88 +675,32 @@ async function evaluatePreConditions(
718
675
  }
719
676
  }
720
677
 
721
- // Check if transition has specific preConditions defined
722
- const preConditions = transition.preConditions || []
723
-
724
- // If no pre-conditions defined, allow transition
725
- if (preConditions.length === 0) {
726
- return {
727
- allowed: true,
728
- executedRules: [],
729
- totalExecutionTime: 0,
730
- }
731
- }
732
-
733
- // Execute each pre-condition rule directly by ruleId
734
- const startTime = Date.now()
735
- const executedRules: ruleEngine.RuleExecutionResult[] = []
736
- const errors: string[] = []
737
- let allowed = true
738
-
739
- for (const condition of preConditions) {
740
- const result = await ruleEngine.executeRuleByRuleId(em, {
741
- ruleId: condition.ruleId, // String identifier
742
- data: {
743
- workflowInstanceId: instance.id,
744
- workflowId: definition.workflowId,
745
- fromStepId: transition.fromStepId,
746
- toStepId: transition.toStepId,
747
- workflowContext: {
748
- ...instance.context,
749
- ...context.workflowContext,
750
- },
751
- triggerData: context.triggerData,
678
+ // Build rule engine context
679
+ const ruleContext: RuleEngineContext = {
680
+ entityType: `workflow:${definition.workflowId}:transition`,
681
+ entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
682
+ eventType: 'pre_transition',
683
+ data: {
684
+ workflowInstanceId: instance.id,
685
+ workflowId: definition.workflowId,
686
+ fromStepId: transition.fromStepId,
687
+ toStepId: transition.toStepId,
688
+ workflowContext: {
689
+ ...instance.context,
690
+ ...context.workflowContext,
752
691
  },
753
- user: context.userId ? { id: context.userId } : undefined,
754
- tenantId: instance.tenantId,
755
- organizationId: instance.organizationId,
756
- executedBy: context.userId,
757
- entityType: `workflow:${definition.workflowId}:transition`,
758
- entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
759
- eventType: 'pre_transition',
760
- })
761
-
762
- // Create a compatible RuleExecutionResult for tracking
763
- // We don't have the full BusinessRule entity, but we can create a partial result
764
- const ruleResult: ruleEngine.RuleExecutionResult = {
765
- rule: {
766
- ruleId: result.ruleId,
767
- ruleName: result.ruleName,
768
- ruleType: 'GUARD',
769
- } as any,
770
- conditionResult: result.conditionResult,
771
- actionsExecuted: result.actionsExecuted,
772
- executionTime: result.executionTime,
773
- error: result.error,
774
- logId: result.logId,
775
- }
776
- executedRules.push(ruleResult)
777
-
778
- // Handle rule errors
779
- if (result.error) {
780
- // Rule not found, disabled, or other errors
781
- const isRequired = condition.required !== false // Default to required
782
- if (isRequired) {
783
- allowed = false
784
- errors.push(`Rule '${result.ruleId}': ${result.error}`)
785
- }
786
- continue
787
- }
788
-
789
- // If required and condition failed, block transition
790
- const isRequired = condition.required !== false // Default to required
791
- if (isRequired && !result.conditionResult) {
792
- allowed = false
793
- errors.push(`Pre-condition '${result.ruleName || result.ruleId}' failed`)
794
- }
692
+ triggerData: context.triggerData,
693
+ },
694
+ user: context.userId ? { id: context.userId } : undefined,
695
+ tenantId: instance.tenantId,
696
+ organizationId: instance.organizationId,
697
+ executedBy: context.userId,
795
698
  }
796
699
 
797
- return {
798
- allowed,
799
- executedRules,
800
- totalExecutionTime: Date.now() - startTime,
801
- errors: errors.length > 0 ? errors : undefined,
802
- }
700
+ // Execute rules - only GUARD rules will affect the 'allowed' status
701
+ const result = await ruleEngine.executeRules(em, ruleContext)
702
+
703
+ return result
803
704
  } catch (error) {
804
705
  console.error('Error evaluating pre-conditions:', error)
805
706
  return {
@@ -817,9 +718,6 @@ async function evaluatePreConditions(
817
718
  * Post-conditions are GUARD rules that should pass after transition executes.
818
719
  * Unlike pre-conditions, post-condition failures are logged but don't block the transition.
819
720
  *
820
- * If the transition defines specific postConditions with ruleIds, those are
821
- * executed directly via executeRuleByRuleId. Otherwise, returns allowed: true.
822
- *
823
721
  * @param em - Entity manager
824
722
  * @param instance - Workflow instance
825
723
  * @param transition - Transition definition
@@ -846,83 +744,32 @@ async function evaluatePostConditions(
846
744
  }
847
745
  }
848
746
 
849
- // Check if transition has specific postConditions defined
850
- const postConditions = transition.postConditions || []
851
-
852
- // If no post-conditions defined, allow
853
- if (postConditions.length === 0) {
854
- return {
855
- allowed: true,
856
- executedRules: [],
857
- totalExecutionTime: 0,
858
- }
859
- }
860
-
861
- // Execute each post-condition rule directly by ruleId
862
- const startTime = Date.now()
863
- const executedRules: ruleEngine.RuleExecutionResult[] = []
864
- const errors: string[] = []
865
- let allowed = true
866
-
867
- for (const condition of postConditions) {
868
- const result = await ruleEngine.executeRuleByRuleId(em, {
869
- ruleId: condition.ruleId, // String identifier
870
- data: {
871
- workflowInstanceId: instance.id,
872
- workflowId: definition.workflowId,
873
- fromStepId: transition.fromStepId,
874
- toStepId: transition.toStepId,
875
- workflowContext: {
876
- ...instance.context,
877
- ...context.workflowContext,
878
- },
879
- triggerData: context.triggerData,
747
+ // Build rule engine context
748
+ const ruleContext: RuleEngineContext = {
749
+ entityType: `workflow:${definition.workflowId}:transition`,
750
+ entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
751
+ eventType: 'post_transition',
752
+ data: {
753
+ workflowInstanceId: instance.id,
754
+ workflowId: definition.workflowId,
755
+ fromStepId: transition.fromStepId,
756
+ toStepId: transition.toStepId,
757
+ workflowContext: {
758
+ ...instance.context,
759
+ ...context.workflowContext,
880
760
  },
881
- user: context.userId ? { id: context.userId } : undefined,
882
- tenantId: instance.tenantId,
883
- organizationId: instance.organizationId,
884
- executedBy: context.userId,
885
- entityType: `workflow:${definition.workflowId}:transition`,
886
- entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
887
- eventType: 'post_transition',
888
- })
889
-
890
- // Create a compatible RuleExecutionResult for tracking
891
- const ruleResult: ruleEngine.RuleExecutionResult = {
892
- rule: {
893
- ruleId: result.ruleId,
894
- ruleName: result.ruleName,
895
- ruleType: 'GUARD',
896
- } as any,
897
- conditionResult: result.conditionResult,
898
- actionsExecuted: result.actionsExecuted,
899
- executionTime: result.executionTime,
900
- error: result.error,
901
- logId: result.logId,
902
- }
903
- executedRules.push(ruleResult)
904
-
905
- // Handle rule errors
906
- if (result.error) {
907
- errors.push(`Rule '${result.ruleId}': ${result.error}`)
908
- // Post-conditions don't block, but track the failure
909
- allowed = false
910
- continue
911
- }
912
-
913
- // Track condition failures (post-conditions are warnings, not blockers)
914
- if (!result.conditionResult) {
915
- allowed = false
916
- errors.push(`Post-condition '${result.ruleName || result.ruleId}' failed`)
917
- }
761
+ triggerData: context.triggerData,
762
+ },
763
+ user: context.userId ? { id: context.userId } : undefined,
764
+ tenantId: instance.tenantId,
765
+ organizationId: instance.organizationId,
766
+ executedBy: context.userId,
918
767
  }
919
768
 
920
- return {
921
- allowed,
922
- executedRules,
923
- totalExecutionTime: Date.now() - startTime,
924
- errors: errors.length > 0 ? errors : undefined,
925
- }
769
+ // Execute rules
770
+ const result = await ruleEngine.executeRules(em, ruleContext)
771
+
772
+ return result
926
773
  } catch (error) {
927
774
  console.error('Error evaluating post-conditions:', error)
928
775
  return {