@open-mercato/core 0.11.1-develop.8557.1.e615240a3e → 0.11.1-develop.8572.1.85986dc508

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 (164) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/api_keys/di.js +6 -2
  3. package/dist/modules/api_keys/di.js.map +2 -2
  4. package/dist/modules/api_keys/services/oneTimeIssuerService.js +47 -0
  5. package/dist/modules/api_keys/services/oneTimeIssuerService.js.map +7 -0
  6. package/dist/modules/audit_logs/di.js +2 -1
  7. package/dist/modules/audit_logs/di.js.map +2 -2
  8. package/dist/modules/audit_logs/services/actionLogService.js +65 -1
  9. package/dist/modules/audit_logs/services/actionLogService.js.map +2 -2
  10. package/dist/modules/auth/cli.js +1 -0
  11. package/dist/modules/auth/cli.js.map +2 -2
  12. package/dist/modules/auth/commands/users.js +10 -2
  13. package/dist/modules/auth/commands/users.js.map +2 -2
  14. package/dist/modules/auth/lib/setup-app.js +19 -5
  15. package/dist/modules/auth/lib/setup-app.js.map +2 -2
  16. package/dist/modules/catalog/lib/productScopeReindex.js +1 -1
  17. package/dist/modules/catalog/lib/productScopeReindex.js.map +2 -2
  18. package/dist/modules/configs/cli.js +26 -29
  19. package/dist/modules/configs/cli.js.map +2 -2
  20. package/dist/modules/configs/di.js +3 -1
  21. package/dist/modules/configs/di.js.map +2 -2
  22. package/dist/modules/configs/lib/instance-config-initializer.js +44 -0
  23. package/dist/modules/configs/lib/instance-config-initializer.js.map +7 -0
  24. package/dist/modules/configs/lib/instance-defaults.js +204 -0
  25. package/dist/modules/configs/lib/instance-defaults.js.map +7 -0
  26. package/dist/modules/configs/lib/module-config-service.js +115 -1
  27. package/dist/modules/configs/lib/module-config-service.js.map +2 -2
  28. package/dist/modules/entities/api/definitions.batch.js +55 -25
  29. package/dist/modules/entities/api/definitions.batch.js.map +2 -2
  30. package/dist/modules/entities/api/definitions.js +35 -8
  31. package/dist/modules/entities/api/definitions.js.map +2 -2
  32. package/dist/modules/entities/backend/entities/user/[entityId]/page.js +14 -8
  33. package/dist/modules/entities/backend/entities/user/[entityId]/page.js.map +2 -2
  34. package/dist/modules/notifications/lib/deliveryConfig.js +19 -12
  35. package/dist/modules/notifications/lib/deliveryConfig.js.map +2 -2
  36. package/dist/modules/notifications/setup.js +10 -0
  37. package/dist/modules/notifications/setup.js.map +2 -2
  38. package/dist/modules/payment_gateways/lib/gateway-service.js +1 -1
  39. package/dist/modules/payment_gateways/lib/gateway-service.js.map +2 -2
  40. package/dist/modules/query_index/api/purge.js +1 -1
  41. package/dist/modules/query_index/api/purge.js.map +2 -2
  42. package/dist/modules/query_index/api/reindex.js +1 -1
  43. package/dist/modules/query_index/api/reindex.js.map +2 -2
  44. package/dist/modules/query_index/cli.js +2 -2
  45. package/dist/modules/query_index/cli.js.map +2 -2
  46. package/dist/modules/query_index/lib/engine.js +5 -1
  47. package/dist/modules/query_index/lib/engine.js.map +2 -2
  48. package/dist/modules/query_index/subscribers/purge.js +3 -3
  49. package/dist/modules/query_index/subscribers/purge.js.map +2 -2
  50. package/dist/modules/query_index/subscribers/reindex.js +11 -13
  51. package/dist/modules/query_index/subscribers/reindex.js.map +2 -2
  52. package/dist/modules/translations/components/TranslationManager.js +47 -10
  53. package/dist/modules/translations/components/TranslationManager.js.map +2 -2
  54. package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js +8 -1
  55. package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js.map +2 -2
  56. package/dist/modules/workflows/ai-tools/authoring-pack.js +1 -1
  57. package/dist/modules/workflows/ai-tools/authoring-pack.js.map +2 -2
  58. package/dist/modules/workflows/api/instances/[id]/advance/route.js +1 -0
  59. package/dist/modules/workflows/api/instances/[id]/advance/route.js.map +2 -2
  60. package/dist/modules/workflows/api/instances/route.js +1 -1
  61. package/dist/modules/workflows/api/instances/route.js.map +2 -2
  62. package/dist/modules/workflows/api/instances/validate-start/route.js +3 -1
  63. package/dist/modules/workflows/api/instances/validate-start/route.js.map +2 -2
  64. package/dist/modules/workflows/components/fields/ActivityConfigFields.js +2 -0
  65. package/dist/modules/workflows/components/fields/ActivityConfigFields.js.map +2 -2
  66. package/dist/modules/workflows/di.js +3 -1
  67. package/dist/modules/workflows/di.js.map +2 -2
  68. package/dist/modules/workflows/events.js +13 -0
  69. package/dist/modules/workflows/events.js.map +2 -2
  70. package/dist/modules/workflows/lib/activity-executor.js +48 -15
  71. package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
  72. package/dist/modules/workflows/lib/condition-handler.js +1 -1
  73. package/dist/modules/workflows/lib/condition-handler.js.map +2 -2
  74. package/dist/modules/workflows/lib/error-handler.js +1 -1
  75. package/dist/modules/workflows/lib/error-handler.js.map +2 -2
  76. package/dist/modules/workflows/lib/event-trigger-service.js +1 -1
  77. package/dist/modules/workflows/lib/event-trigger-service.js.map +2 -2
  78. package/dist/modules/workflows/lib/example-definition-authoring.js +90 -0
  79. package/dist/modules/workflows/lib/example-definition-authoring.js.map +7 -0
  80. package/dist/modules/workflows/lib/named-rules.js +42 -0
  81. package/dist/modules/workflows/lib/named-rules.js.map +7 -0
  82. package/dist/modules/workflows/lib/owned-definition.js +2 -0
  83. package/dist/modules/workflows/lib/owned-definition.js.map +2 -2
  84. package/dist/modules/workflows/lib/parallel-handler.js +1 -1
  85. package/dist/modules/workflows/lib/parallel-handler.js.map +2 -2
  86. package/dist/modules/workflows/lib/signal-handler.js +1 -0
  87. package/dist/modules/workflows/lib/signal-handler.js.map +2 -2
  88. package/dist/modules/workflows/lib/start-validator.js +87 -85
  89. package/dist/modules/workflows/lib/start-validator.js.map +2 -2
  90. package/dist/modules/workflows/lib/step-handler.js +1 -1
  91. package/dist/modules/workflows/lib/step-handler.js.map +2 -2
  92. package/dist/modules/workflows/lib/task-handler.js +1 -0
  93. package/dist/modules/workflows/lib/task-handler.js.map +2 -2
  94. package/dist/modules/workflows/lib/timer-handler.js +1 -0
  95. package/dist/modules/workflows/lib/timer-handler.js.map +2 -2
  96. package/dist/modules/workflows/lib/transition-handler.js +48 -166
  97. package/dist/modules/workflows/lib/transition-handler.js.map +3 -3
  98. package/dist/modules/workflows/lib/workflow-executor.js +3 -2
  99. package/dist/modules/workflows/lib/workflow-executor.js.map +2 -2
  100. package/package.json +8 -8
  101. package/src/modules/api_keys/di.ts +7 -2
  102. package/src/modules/api_keys/services/oneTimeIssuerService.ts +61 -0
  103. package/src/modules/audit_logs/di.ts +1 -0
  104. package/src/modules/audit_logs/services/actionLogService.ts +132 -1
  105. package/src/modules/auth/cli.ts +1 -0
  106. package/src/modules/auth/commands/users.ts +10 -2
  107. package/src/modules/auth/i18n/de.json +2 -0
  108. package/src/modules/auth/i18n/en.json +2 -0
  109. package/src/modules/auth/i18n/es.json +2 -0
  110. package/src/modules/auth/i18n/ko.json +2 -0
  111. package/src/modules/auth/i18n/pl.json +2 -0
  112. package/src/modules/auth/lib/setup-app.ts +30 -7
  113. package/src/modules/catalog/lib/productScopeReindex.ts +1 -1
  114. package/src/modules/configs/cli.ts +39 -37
  115. package/src/modules/configs/di.ts +2 -0
  116. package/src/modules/configs/lib/instance-config-initializer.ts +56 -0
  117. package/src/modules/configs/lib/instance-defaults.ts +262 -0
  118. package/src/modules/configs/lib/module-config-service.ts +156 -0
  119. package/src/modules/entities/api/definitions.batch.ts +65 -27
  120. package/src/modules/entities/api/definitions.ts +40 -11
  121. package/src/modules/entities/backend/entities/user/[entityId]/page.tsx +14 -7
  122. package/src/modules/notifications/lib/deliveryConfig.ts +20 -11
  123. package/src/modules/notifications/setup.ts +11 -0
  124. package/src/modules/payment_gateways/lib/gateway-service.ts +2 -2
  125. package/src/modules/query_index/api/purge.ts +1 -1
  126. package/src/modules/query_index/api/reindex.ts +1 -1
  127. package/src/modules/query_index/cli.ts +2 -2
  128. package/src/modules/query_index/lib/engine.ts +5 -1
  129. package/src/modules/query_index/subscribers/purge.ts +5 -4
  130. package/src/modules/query_index/subscribers/reindex.ts +14 -15
  131. package/src/modules/translations/components/TranslationManager.tsx +49 -10
  132. package/src/modules/translations/i18n/de.json +1 -0
  133. package/src/modules/translations/i18n/en.json +1 -0
  134. package/src/modules/translations/i18n/es.json +1 -0
  135. package/src/modules/translations/i18n/ko.json +1 -0
  136. package/src/modules/translations/i18n/pl.json +1 -0
  137. package/src/modules/wms/components/backend/CompletePutawayTaskDialog.tsx +8 -1
  138. package/src/modules/wms/i18n/de.json +1 -0
  139. package/src/modules/wms/i18n/en.json +1 -0
  140. package/src/modules/wms/i18n/es.json +1 -0
  141. package/src/modules/wms/i18n/ko.json +1 -0
  142. package/src/modules/wms/i18n/pl.json +1 -0
  143. package/src/modules/workflows/ai-tools/authoring-pack.ts +3 -1
  144. package/src/modules/workflows/api/instances/[id]/advance/route.ts +1 -0
  145. package/src/modules/workflows/api/instances/route.ts +1 -1
  146. package/src/modules/workflows/api/instances/validate-start/route.ts +3 -1
  147. package/src/modules/workflows/components/fields/ActivityConfigFields.tsx +2 -0
  148. package/src/modules/workflows/di.ts +2 -0
  149. package/src/modules/workflows/events.ts +14 -0
  150. package/src/modules/workflows/lib/activity-executor.ts +64 -40
  151. package/src/modules/workflows/lib/condition-handler.ts +1 -1
  152. package/src/modules/workflows/lib/error-handler.ts +1 -1
  153. package/src/modules/workflows/lib/event-trigger-service.ts +1 -1
  154. package/src/modules/workflows/lib/example-definition-authoring.ts +114 -0
  155. package/src/modules/workflows/lib/named-rules.ts +54 -0
  156. package/src/modules/workflows/lib/owned-definition.ts +4 -1
  157. package/src/modules/workflows/lib/parallel-handler.ts +1 -1
  158. package/src/modules/workflows/lib/signal-handler.ts +1 -0
  159. package/src/modules/workflows/lib/start-validator.ts +96 -94
  160. package/src/modules/workflows/lib/step-handler.ts +1 -1
  161. package/src/modules/workflows/lib/task-handler.ts +1 -0
  162. package/src/modules/workflows/lib/timer-handler.ts +1 -0
  163. package/src/modules/workflows/lib/transition-handler.ts +74 -238
  164. package/src/modules/workflows/lib/workflow-executor.ts +7 -4
@@ -12,13 +12,13 @@
12
12
 
13
13
  import { EntityManager } from '@mikro-orm/core'
14
14
  import type { AwilixContainer } from 'awilix'
15
- import type { EventBus } from '@open-mercato/events'
15
+ import { reportError } from '@open-mercato/telemetry'
16
16
  import {
17
17
  WorkflowInstance,
18
18
  WorkflowEvent,
19
19
  } from '../data/entities'
20
20
  import * as ruleEvaluator from '../../business_rules/lib/rule-evaluator'
21
- import * as ruleEngine from '../../business_rules/lib/rule-engine'
21
+ import { createNamedWorkflowRuleScope, type WorkflowRuleContainer } from './named-rules'
22
22
  import * as activityExecutor from './activity-executor'
23
23
  import type { ActivityDefinition } from './activity-executor'
24
24
  import * as stepHandler from './step-handler'
@@ -102,6 +102,7 @@ async function logSuppressedRuleActions(
102
102
  // ============================================================================
103
103
 
104
104
  export interface TransitionEvaluationContext {
105
+ container?: WorkflowRuleContainer | null
105
106
  workflowContext: Record<string, any>
106
107
  userId?: string
107
108
  triggerData?: any
@@ -301,6 +302,7 @@ export async function findValidTransitions(
301
302
  fromStepId: string,
302
303
  context: TransitionEvaluationContext
303
304
  ): Promise<TransitionEvaluationResult[]> {
305
+ const namedRules = createNamedWorkflowRuleScope(context.container)
304
306
  try {
305
307
  // Load workflow definition
306
308
  const definition = await findDefinitionForInstance(em, instance)
@@ -336,14 +338,14 @@ export async function findValidTransitions(
336
338
  }
337
339
 
338
340
  // Also evaluate preConditions if they exist
339
- const preConditions = transition.preConditions || []
340
- if (preConditions.length > 0) {
341
- const preConditionsResult = await evaluatePreConditions(
341
+ if (transition.preConditions?.length) {
342
+ const preConditionsResult = await evaluateNamedTransitionRules(
342
343
  em,
343
344
  instance,
344
345
  transition,
345
- context as TransitionExecutionContext,
346
- null
346
+ context,
347
+ namedRules.resolve,
348
+ 'pre_transition',
347
349
  )
348
350
 
349
351
  if (!preConditionsResult.allowed) {
@@ -373,6 +375,8 @@ export async function findValidTransitions(
373
375
  } catch (error) {
374
376
  logger.error('Error finding valid transitions', { err: error })
375
377
  return []
378
+ } finally {
379
+ await namedRules.dispose()
376
380
  }
377
381
  }
378
382
 
@@ -425,14 +429,8 @@ export async function executeTransitionForToken(
425
429
  const instance = token.instance
426
430
  const branch = token.kind === 'branch' ? token.branch : null
427
431
  const branchInstanceId = tokenBranchInstanceId(token)
432
+ const namedRules = createNamedWorkflowRuleScope(container)
428
433
  try {
429
- let eventBus: Pick<EventBus, 'emitEvent'> | null = null
430
- try {
431
- eventBus = container.resolve('eventBus') as EventBus
432
- } catch {
433
- eventBus = null
434
- }
435
-
436
434
  // First, evaluate if transition is valid
437
435
  const evaluation = await evaluateTransition(
438
436
  em,
@@ -452,12 +450,13 @@ export async function executeTransitionForToken(
452
450
  const transition = evaluation.transition!
453
451
 
454
452
  // Evaluate pre-conditions (business rules)
455
- const preConditionsResult = await evaluatePreConditions(
453
+ const preConditionsResult = await evaluateNamedTransitionRules(
456
454
  em,
457
455
  instance,
458
456
  transition,
459
457
  context,
460
- eventBus
458
+ namedRules.resolve,
459
+ 'pre_transition',
461
460
  )
462
461
 
463
462
  if (!preConditionsResult.allowed) {
@@ -768,12 +767,13 @@ export async function executeTransitionForToken(
768
767
  stepExecutionResult.status === 'WAITING' && stepExecutionResult.waitReason !== 'FORK'
769
768
 
770
769
  // Evaluate post-conditions (business rules)
771
- const postConditionsResult = await evaluatePostConditions(
770
+ const postConditionsResult = await evaluateNamedTransitionRules(
772
771
  em,
773
772
  instance,
774
773
  transition,
775
774
  context,
776
- eventBus
775
+ namedRules.resolve,
776
+ 'post_transition',
777
777
  )
778
778
 
779
779
  if (!postConditionsResult.allowed) {
@@ -848,6 +848,8 @@ export async function executeTransitionForToken(
848
848
  success: false,
849
849
  error: `Transition execution failed: ${errorMessage}`,
850
850
  }
851
+ } finally {
852
+ await namedRules.dispose()
851
853
  }
852
854
  }
853
855
 
@@ -912,200 +914,62 @@ async function evaluateTransitionConditions(
912
914
  }
913
915
  }
914
916
 
915
- /**
916
- * Evaluate pre-conditions using business rules engine
917
- *
918
- * Pre-conditions are GUARD rules that must pass before transition can execute.
919
- * If any GUARD rule fails, the transition is blocked.
920
- *
921
- * If the transition defines specific preConditions with ruleIds, those are
922
- * executed directly via executeRuleByRuleId. Otherwise, falls back to
923
- * discovery-based execution via executeRules.
924
- *
925
- * @param em - Entity manager
926
- * @param instance - Workflow instance
927
- * @param transition - Transition definition
928
- * @param context - Execution context
929
- * @returns Rule engine result
930
- */
931
- async function evaluatePreConditions(
932
- em: EntityManager,
933
- instance: WorkflowInstance,
934
- transition: any,
935
- context: TransitionExecutionContext,
936
- eventBus: Pick<EventBus, 'emitEvent'> | null
937
- ): Promise<ruleEngine.RuleEngineResult> {
938
- try {
939
- // Load workflow definition to get workflow ID
940
- const definition = await findDefinitionForInstance(em, instance)
941
-
942
- if (!definition) {
943
- return {
944
- allowed: true,
945
- executedRules: [],
946
- totalExecutionTime: 0,
947
- }
948
- }
949
-
950
- // Check if transition has specific preConditions defined
951
- const preConditions = transition.preConditions || []
952
-
953
- // If no pre-conditions defined, allow transition
954
- if (preConditions.length === 0) {
955
- return {
956
- allowed: true,
957
- executedRules: [],
958
- totalExecutionTime: 0,
959
- }
960
- }
917
+ type NamedTransitionCondition = { ruleId: string; required?: boolean }
961
918
 
962
- // Execute each pre-condition rule directly by ruleId
963
- const dryRun = isDryRunInstance(instance)
964
- const startTime = Date.now()
965
- const executedRules: ruleEngine.RuleExecutionResult[] = []
966
- const errors: string[] = []
967
- let allowed = true
968
-
969
- for (const condition of preConditions) {
970
- const result = await ruleEngine.executeRuleByRuleId(em, {
971
- ruleId: condition.ruleId, // String identifier
972
- data: {
973
- workflowInstanceId: instance.id,
974
- workflowId: definition.workflowId,
975
- fromStepId: transition.fromStepId,
976
- toStepId: transition.toStepId,
977
- workflowContext: {
978
- ...instance.context,
979
- ...context.workflowContext,
980
- },
981
- triggerData: context.triggerData,
982
- },
983
- user: context.userId ? { id: context.userId } : undefined,
984
- tenantId: instance.tenantId,
985
- organizationId: instance.organizationId,
986
- executedBy: context.userId,
987
- entityType: `workflow:${definition.workflowId}:transition`,
988
- entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
989
- eventType: 'pre_transition',
990
- // Dry run (spec section 8.2): the rule engine's own `dryRun` only
991
- // suppresses its execution LOG — a rule's success/failure ACTIONS run
992
- // regardless — so a side-effect-free run has to ask for `skipActions`.
993
- // The conditions still evaluate, so the run takes the routes it really
994
- // would; only the ACTION arm is withheld, and it is reported.
995
- skipActions: dryRun,
996
- })
997
- if (dryRun && result.actionsExecuted === null) {
998
- await logSuppressedRuleActions(em, instance, condition.ruleId, 'pre_transition')
999
- }
1000
-
1001
- // Create a compatible RuleExecutionResult for tracking
1002
- // We don't have the full BusinessRule entity, but we can create a partial result
1003
- const ruleResult: ruleEngine.RuleExecutionResult = {
1004
- rule: {
1005
- ruleId: result.ruleId,
1006
- ruleName: result.ruleName,
1007
- ruleType: 'GUARD',
1008
- } as any,
1009
- conditionResult: result.conditionResult,
1010
- actionsExecuted: result.actionsExecuted,
1011
- executionTime: result.executionTime,
1012
- error: result.error,
1013
- logId: result.logId,
1014
- }
1015
- executedRules.push(ruleResult)
1016
-
1017
- // Handle rule errors
1018
- if (result.error) {
1019
- // Rule not found, disabled, or other errors
1020
- const isRequired = condition.required !== false // Default to required
1021
- if (isRequired) {
1022
- allowed = false
1023
- errors.push(`Rule '${result.ruleId}': ${result.error}`)
1024
- }
1025
- continue
1026
- }
919
+ type NamedTransition = {
920
+ transitionId?: string
921
+ fromStepId: string
922
+ toStepId: string
923
+ preConditions?: NamedTransitionCondition[]
924
+ postConditions?: NamedTransitionCondition[]
925
+ }
1027
926
 
1028
- // If required and condition failed, block transition
1029
- const isRequired = condition.required !== false // Default to required
1030
- if (isRequired && !result.conditionResult) {
1031
- allowed = false
1032
- errors.push(`Pre-condition '${result.ruleName || result.ruleId}' failed`)
1033
- }
1034
- }
927
+ type NamedTransitionRuleResult = {
928
+ rule: { ruleId: string; ruleName: string }
929
+ conditionResult: boolean
930
+ error?: string
931
+ }
1035
932
 
1036
- return {
1037
- allowed,
1038
- executedRules,
1039
- totalExecutionTime: Date.now() - startTime,
1040
- errors: errors.length > 0 ? errors : undefined,
1041
- }
1042
- } catch (error) {
1043
- logger.error('Error evaluating pre-conditions', { err: error })
1044
- return {
1045
- allowed: false,
1046
- executedRules: [],
1047
- totalExecutionTime: 0,
1048
- errors: [error instanceof Error ? error.message : String(error)],
1049
- }
1050
- }
933
+ type NamedTransitionRulesResult = {
934
+ allowed: boolean
935
+ executedRules: NamedTransitionRuleResult[]
936
+ errors?: string[]
1051
937
  }
1052
938
 
1053
- /**
1054
- * Evaluate post-conditions using business rules engine
1055
- *
1056
- * Post-conditions are GUARD rules that should pass after transition executes.
1057
- * Unlike pre-conditions, post-condition failures are logged but don't block the transition.
1058
- *
1059
- * If the transition defines specific postConditions with ruleIds, those are
1060
- * executed directly via executeRuleByRuleId. Otherwise, returns allowed: true.
1061
- *
1062
- * @param em - Entity manager
1063
- * @param instance - Workflow instance
1064
- * @param transition - Transition definition
1065
- * @param context - Execution context
1066
- * @returns Rule engine result
1067
- */
1068
- async function evaluatePostConditions(
939
+ async function evaluateNamedTransitionRules(
1069
940
  em: EntityManager,
1070
941
  instance: WorkflowInstance,
1071
- transition: any,
942
+ transition: NamedTransition,
1072
943
  context: TransitionExecutionContext,
1073
- eventBus: Pick<EventBus, 'emitEvent'> | null
1074
- ): Promise<ruleEngine.RuleEngineResult> {
944
+ resolveRunner: ReturnType<typeof createNamedWorkflowRuleScope>['resolve'],
945
+ phase: 'pre_transition' | 'post_transition',
946
+ ): Promise<NamedTransitionRulesResult> {
947
+ const isPreCondition = phase === 'pre_transition'
948
+ const conditions = isPreCondition ? transition.preConditions : transition.postConditions
1075
949
  try {
1076
- // Load workflow definition to get workflow ID
1077
950
  const definition = await findDefinitionForInstance(em, instance)
951
+ if (!definition || !conditions?.length) return { allowed: true, executedRules: [] }
1078
952
 
1079
- if (!definition) {
1080
- return {
1081
- allowed: true,
1082
- executedRules: [],
1083
- totalExecutionTime: 0,
1084
- }
1085
- }
1086
-
1087
- // Check if transition has specific postConditions defined
1088
- const postConditions = transition.postConditions || []
1089
-
1090
- // If no post-conditions defined, allow
1091
- if (postConditions.length === 0) {
1092
- return {
1093
- allowed: true,
1094
- executedRules: [],
1095
- totalExecutionTime: 0,
1096
- }
1097
- }
1098
-
1099
- // Execute each post-condition rule directly by ruleId
953
+ const runner = await resolveRunner()
1100
954
  const dryRun = isDryRunInstance(instance)
1101
- const startTime = Date.now()
1102
- const executedRules: ruleEngine.RuleExecutionResult[] = []
955
+ const executedRules: NamedTransitionRuleResult[] = []
1103
956
  const errors: string[] = []
1104
- let allowed = true
957
+ for (const condition of conditions) {
958
+ const required = !isPreCondition || condition.required !== false
959
+ if (!runner) {
960
+ if (!required) continue
961
+ const error = 'Business rules engine unavailable'
962
+ executedRules.push({
963
+ rule: { ruleId: condition.ruleId, ruleName: condition.ruleId },
964
+ conditionResult: false,
965
+ error,
966
+ })
967
+ errors.push(`Rule '${condition.ruleId}': ${error}`)
968
+ continue
969
+ }
1105
970
 
1106
- for (const condition of postConditions) {
1107
- const result = await ruleEngine.executeRuleByRuleId(em, {
1108
- ruleId: condition.ruleId, // String identifier
971
+ const result = await runner.executeRuleByRuleId(em, {
972
+ ruleId: condition.ruleId,
1109
973
  data: {
1110
974
  workflowInstanceId: instance.id,
1111
975
  workflowId: definition.workflowId,
@@ -1123,60 +987,32 @@ async function evaluatePostConditions(
1123
987
  executedBy: context.userId,
1124
988
  entityType: `workflow:${definition.workflowId}:transition`,
1125
989
  entityId: transition.transitionId || `${transition.fromStepId}->${transition.toStepId}`,
1126
- eventType: 'post_transition',
1127
- // Dry run (spec section 8.2): the rule engine's own `dryRun` only
1128
- // suppresses its execution LOG — a rule's success/failure ACTIONS run
1129
- // regardless — so a side-effect-free run has to ask for `skipActions`.
1130
- // The conditions still evaluate, so the run takes the routes it really
1131
- // would; only the ACTION arm is withheld, and it is reported.
990
+ eventType: phase,
1132
991
  skipActions: dryRun,
1133
992
  })
1134
993
  if (dryRun && result.actionsExecuted === null) {
1135
- await logSuppressedRuleActions(em, instance, condition.ruleId, 'post_transition')
994
+ await logSuppressedRuleActions(em, instance, condition.ruleId, phase)
1136
995
  }
1137
-
1138
- // Create a compatible RuleExecutionResult for tracking
1139
- const ruleResult: ruleEngine.RuleExecutionResult = {
1140
- rule: {
1141
- ruleId: result.ruleId,
1142
- ruleName: result.ruleName,
1143
- ruleType: 'GUARD',
1144
- } as any,
996
+ executedRules.push({
997
+ rule: { ruleId: result.ruleId, ruleName: result.ruleName },
1145
998
  conditionResult: result.conditionResult,
1146
- actionsExecuted: result.actionsExecuted,
1147
- executionTime: result.executionTime,
1148
999
  error: result.error,
1149
- logId: result.logId,
1150
- }
1151
- executedRules.push(ruleResult)
1000
+ })
1152
1001
 
1153
- // Handle rule errors
1154
- if (result.error) {
1002
+ if (required && result.error) {
1155
1003
  errors.push(`Rule '${result.ruleId}': ${result.error}`)
1156
- // Post-conditions don't block, but track the failure
1157
- allowed = false
1158
- continue
1159
- }
1160
-
1161
- // Track condition failures (post-conditions are warnings, not blockers)
1162
- if (!result.conditionResult) {
1163
- allowed = false
1164
- errors.push(`Post-condition '${result.ruleName || result.ruleId}' failed`)
1004
+ } else if (required && !result.conditionResult) {
1005
+ const label = isPreCondition ? 'Pre-condition' : 'Post-condition'
1006
+ errors.push(`${label} '${result.ruleName || result.ruleId}' failed`)
1165
1007
  }
1166
1008
  }
1167
-
1168
- return {
1169
- allowed,
1170
- executedRules,
1171
- totalExecutionTime: Date.now() - startTime,
1172
- errors: errors.length > 0 ? errors : undefined,
1173
- }
1009
+ return { allowed: errors.length === 0, executedRules, errors: errors.length ? errors : undefined }
1174
1010
  } catch (error) {
1175
- logger.error('Error evaluating post-conditions', { err: error })
1011
+ logger.error(isPreCondition ? 'Error evaluating pre-conditions' : 'Error evaluating post-conditions', { err: error })
1012
+ reportError(error, { module: 'workflows', code: 'workflows.named_transition_rule_failed' })
1176
1013
  return {
1177
1014
  allowed: false,
1178
1015
  executedRules: [],
1179
- totalExecutionTime: 0,
1180
1016
  errors: [error instanceof Error ? error.message : String(error)],
1181
1017
  }
1182
1018
  }
@@ -62,7 +62,8 @@ import type {
62
62
  WorkflowActivityJob,
63
63
  WorkflowActivityJobResumeSubWorkflowParent,
64
64
  } from './activity-queue-types'
65
- import type { TransitionExecutionResult } from './transition-handler'
65
+ import type { TransitionEvaluationContext, TransitionExecutionResult } from './transition-handler'
66
+ import type { WorkflowRuleContainer } from './named-rules'
66
67
 
67
68
  import { assertTrustedEventTriggerIdentity } from './event-trigger-identity'
68
69
 
@@ -195,7 +196,8 @@ export class WorkflowExecutionError extends Error {
195
196
  */
196
197
  export async function startWorkflow(
197
198
  em: EntityManager,
198
- options: StartWorkflowOptions
199
+ options: StartWorkflowOptions,
200
+ container?: WorkflowRuleContainer | null,
199
201
  ): Promise<WorkflowInstance> {
200
202
  const {
201
203
  workflowId,
@@ -282,7 +284,7 @@ export async function startWorkflow(
282
284
  context: initialContext,
283
285
  tenantId,
284
286
  organizationId,
285
- })
287
+ }, { container, skipActions: isDryRun })
286
288
 
287
289
  if (!validationResult.canStart) {
288
290
  throw new WorkflowExecutionError(
@@ -642,7 +644,8 @@ export async function executeWorkflow(
642
644
  }
643
645
 
644
646
  const transitionHandler = await import('./transition-handler')
645
- const evalContext: any = {
647
+ const evalContext: TransitionEvaluationContext = {
648
+ container,
646
649
  workflowContext: currentInstance.context,
647
650
  userId: executionUserId,
648
651
  }