@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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/modules/api_keys/di.js +6 -2
- package/dist/modules/api_keys/di.js.map +2 -2
- package/dist/modules/api_keys/services/oneTimeIssuerService.js +47 -0
- package/dist/modules/api_keys/services/oneTimeIssuerService.js.map +7 -0
- package/dist/modules/audit_logs/di.js +2 -1
- package/dist/modules/audit_logs/di.js.map +2 -2
- package/dist/modules/audit_logs/services/actionLogService.js +65 -1
- package/dist/modules/audit_logs/services/actionLogService.js.map +2 -2
- package/dist/modules/auth/cli.js +1 -0
- package/dist/modules/auth/cli.js.map +2 -2
- package/dist/modules/auth/commands/users.js +10 -2
- package/dist/modules/auth/commands/users.js.map +2 -2
- package/dist/modules/auth/lib/setup-app.js +19 -5
- package/dist/modules/auth/lib/setup-app.js.map +2 -2
- package/dist/modules/catalog/lib/productScopeReindex.js +1 -1
- package/dist/modules/catalog/lib/productScopeReindex.js.map +2 -2
- package/dist/modules/configs/cli.js +26 -29
- package/dist/modules/configs/cli.js.map +2 -2
- package/dist/modules/configs/di.js +3 -1
- package/dist/modules/configs/di.js.map +2 -2
- package/dist/modules/configs/lib/instance-config-initializer.js +44 -0
- package/dist/modules/configs/lib/instance-config-initializer.js.map +7 -0
- package/dist/modules/configs/lib/instance-defaults.js +204 -0
- package/dist/modules/configs/lib/instance-defaults.js.map +7 -0
- package/dist/modules/configs/lib/module-config-service.js +115 -1
- package/dist/modules/configs/lib/module-config-service.js.map +2 -2
- package/dist/modules/entities/api/definitions.batch.js +55 -25
- package/dist/modules/entities/api/definitions.batch.js.map +2 -2
- package/dist/modules/entities/api/definitions.js +35 -8
- package/dist/modules/entities/api/definitions.js.map +2 -2
- package/dist/modules/entities/backend/entities/user/[entityId]/page.js +14 -8
- package/dist/modules/entities/backend/entities/user/[entityId]/page.js.map +2 -2
- package/dist/modules/notifications/lib/deliveryConfig.js +19 -12
- package/dist/modules/notifications/lib/deliveryConfig.js.map +2 -2
- package/dist/modules/notifications/setup.js +10 -0
- package/dist/modules/notifications/setup.js.map +2 -2
- package/dist/modules/payment_gateways/lib/gateway-service.js +1 -1
- package/dist/modules/payment_gateways/lib/gateway-service.js.map +2 -2
- package/dist/modules/query_index/api/purge.js +1 -1
- package/dist/modules/query_index/api/purge.js.map +2 -2
- package/dist/modules/query_index/api/reindex.js +1 -1
- package/dist/modules/query_index/api/reindex.js.map +2 -2
- package/dist/modules/query_index/cli.js +2 -2
- package/dist/modules/query_index/cli.js.map +2 -2
- package/dist/modules/query_index/lib/engine.js +5 -1
- package/dist/modules/query_index/lib/engine.js.map +2 -2
- package/dist/modules/query_index/subscribers/purge.js +3 -3
- package/dist/modules/query_index/subscribers/purge.js.map +2 -2
- package/dist/modules/query_index/subscribers/reindex.js +11 -13
- package/dist/modules/query_index/subscribers/reindex.js.map +2 -2
- package/dist/modules/translations/components/TranslationManager.js +47 -10
- package/dist/modules/translations/components/TranslationManager.js.map +2 -2
- package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js +8 -1
- package/dist/modules/wms/components/backend/CompletePutawayTaskDialog.js.map +2 -2
- package/dist/modules/workflows/ai-tools/authoring-pack.js +1 -1
- package/dist/modules/workflows/ai-tools/authoring-pack.js.map +2 -2
- package/dist/modules/workflows/api/instances/[id]/advance/route.js +1 -0
- package/dist/modules/workflows/api/instances/[id]/advance/route.js.map +2 -2
- package/dist/modules/workflows/api/instances/route.js +1 -1
- package/dist/modules/workflows/api/instances/route.js.map +2 -2
- package/dist/modules/workflows/api/instances/validate-start/route.js +3 -1
- package/dist/modules/workflows/api/instances/validate-start/route.js.map +2 -2
- package/dist/modules/workflows/components/fields/ActivityConfigFields.js +2 -0
- package/dist/modules/workflows/components/fields/ActivityConfigFields.js.map +2 -2
- package/dist/modules/workflows/di.js +3 -1
- package/dist/modules/workflows/di.js.map +2 -2
- package/dist/modules/workflows/events.js +13 -0
- package/dist/modules/workflows/events.js.map +2 -2
- package/dist/modules/workflows/lib/activity-executor.js +48 -15
- package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
- package/dist/modules/workflows/lib/condition-handler.js +1 -1
- package/dist/modules/workflows/lib/condition-handler.js.map +2 -2
- package/dist/modules/workflows/lib/error-handler.js +1 -1
- package/dist/modules/workflows/lib/error-handler.js.map +2 -2
- package/dist/modules/workflows/lib/event-trigger-service.js +1 -1
- package/dist/modules/workflows/lib/event-trigger-service.js.map +2 -2
- package/dist/modules/workflows/lib/example-definition-authoring.js +90 -0
- package/dist/modules/workflows/lib/example-definition-authoring.js.map +7 -0
- package/dist/modules/workflows/lib/named-rules.js +42 -0
- package/dist/modules/workflows/lib/named-rules.js.map +7 -0
- package/dist/modules/workflows/lib/owned-definition.js +2 -0
- package/dist/modules/workflows/lib/owned-definition.js.map +2 -2
- package/dist/modules/workflows/lib/parallel-handler.js +1 -1
- package/dist/modules/workflows/lib/parallel-handler.js.map +2 -2
- package/dist/modules/workflows/lib/signal-handler.js +1 -0
- package/dist/modules/workflows/lib/signal-handler.js.map +2 -2
- package/dist/modules/workflows/lib/start-validator.js +87 -85
- package/dist/modules/workflows/lib/start-validator.js.map +2 -2
- package/dist/modules/workflows/lib/step-handler.js +1 -1
- package/dist/modules/workflows/lib/step-handler.js.map +2 -2
- package/dist/modules/workflows/lib/task-handler.js +1 -0
- package/dist/modules/workflows/lib/task-handler.js.map +2 -2
- package/dist/modules/workflows/lib/timer-handler.js +1 -0
- package/dist/modules/workflows/lib/timer-handler.js.map +2 -2
- package/dist/modules/workflows/lib/transition-handler.js +48 -166
- package/dist/modules/workflows/lib/transition-handler.js.map +3 -3
- package/dist/modules/workflows/lib/workflow-executor.js +3 -2
- package/dist/modules/workflows/lib/workflow-executor.js.map +2 -2
- package/package.json +8 -8
- package/src/modules/api_keys/di.ts +7 -2
- package/src/modules/api_keys/services/oneTimeIssuerService.ts +61 -0
- package/src/modules/audit_logs/di.ts +1 -0
- package/src/modules/audit_logs/services/actionLogService.ts +132 -1
- package/src/modules/auth/cli.ts +1 -0
- package/src/modules/auth/commands/users.ts +10 -2
- package/src/modules/auth/i18n/de.json +2 -0
- package/src/modules/auth/i18n/en.json +2 -0
- package/src/modules/auth/i18n/es.json +2 -0
- package/src/modules/auth/i18n/ko.json +2 -0
- package/src/modules/auth/i18n/pl.json +2 -0
- package/src/modules/auth/lib/setup-app.ts +30 -7
- package/src/modules/catalog/lib/productScopeReindex.ts +1 -1
- package/src/modules/configs/cli.ts +39 -37
- package/src/modules/configs/di.ts +2 -0
- package/src/modules/configs/lib/instance-config-initializer.ts +56 -0
- package/src/modules/configs/lib/instance-defaults.ts +262 -0
- package/src/modules/configs/lib/module-config-service.ts +156 -0
- package/src/modules/entities/api/definitions.batch.ts +65 -27
- package/src/modules/entities/api/definitions.ts +40 -11
- package/src/modules/entities/backend/entities/user/[entityId]/page.tsx +14 -7
- package/src/modules/notifications/lib/deliveryConfig.ts +20 -11
- package/src/modules/notifications/setup.ts +11 -0
- package/src/modules/payment_gateways/lib/gateway-service.ts +2 -2
- package/src/modules/query_index/api/purge.ts +1 -1
- package/src/modules/query_index/api/reindex.ts +1 -1
- package/src/modules/query_index/cli.ts +2 -2
- package/src/modules/query_index/lib/engine.ts +5 -1
- package/src/modules/query_index/subscribers/purge.ts +5 -4
- package/src/modules/query_index/subscribers/reindex.ts +14 -15
- package/src/modules/translations/components/TranslationManager.tsx +49 -10
- package/src/modules/translations/i18n/de.json +1 -0
- package/src/modules/translations/i18n/en.json +1 -0
- package/src/modules/translations/i18n/es.json +1 -0
- package/src/modules/translations/i18n/ko.json +1 -0
- package/src/modules/translations/i18n/pl.json +1 -0
- package/src/modules/wms/components/backend/CompletePutawayTaskDialog.tsx +8 -1
- package/src/modules/wms/i18n/de.json +1 -0
- package/src/modules/wms/i18n/en.json +1 -0
- package/src/modules/wms/i18n/es.json +1 -0
- package/src/modules/wms/i18n/ko.json +1 -0
- package/src/modules/wms/i18n/pl.json +1 -0
- package/src/modules/workflows/ai-tools/authoring-pack.ts +3 -1
- package/src/modules/workflows/api/instances/[id]/advance/route.ts +1 -0
- package/src/modules/workflows/api/instances/route.ts +1 -1
- package/src/modules/workflows/api/instances/validate-start/route.ts +3 -1
- package/src/modules/workflows/components/fields/ActivityConfigFields.tsx +2 -0
- package/src/modules/workflows/di.ts +2 -0
- package/src/modules/workflows/events.ts +14 -0
- package/src/modules/workflows/lib/activity-executor.ts +64 -40
- package/src/modules/workflows/lib/condition-handler.ts +1 -1
- package/src/modules/workflows/lib/error-handler.ts +1 -1
- package/src/modules/workflows/lib/event-trigger-service.ts +1 -1
- package/src/modules/workflows/lib/example-definition-authoring.ts +114 -0
- package/src/modules/workflows/lib/named-rules.ts +54 -0
- package/src/modules/workflows/lib/owned-definition.ts +4 -1
- package/src/modules/workflows/lib/parallel-handler.ts +1 -1
- package/src/modules/workflows/lib/signal-handler.ts +1 -0
- package/src/modules/workflows/lib/start-validator.ts +96 -94
- package/src/modules/workflows/lib/step-handler.ts +1 -1
- package/src/modules/workflows/lib/task-handler.ts +1 -0
- package/src/modules/workflows/lib/timer-handler.ts +1 -0
- package/src/modules/workflows/lib/transition-handler.ts +74 -238
- 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
|
|
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
|
|
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
|
-
|
|
340
|
-
|
|
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
|
|
346
|
-
|
|
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
|
|
453
|
+
const preConditionsResult = await evaluateNamedTransitionRules(
|
|
456
454
|
em,
|
|
457
455
|
instance,
|
|
458
456
|
transition,
|
|
459
457
|
context,
|
|
460
|
-
|
|
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
|
|
770
|
+
const postConditionsResult = await evaluateNamedTransitionRules(
|
|
772
771
|
em,
|
|
773
772
|
instance,
|
|
774
773
|
transition,
|
|
775
774
|
context,
|
|
776
|
-
|
|
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
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
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
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
927
|
+
type NamedTransitionRuleResult = {
|
|
928
|
+
rule: { ruleId: string; ruleName: string }
|
|
929
|
+
conditionResult: boolean
|
|
930
|
+
error?: string
|
|
931
|
+
}
|
|
1035
932
|
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
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:
|
|
942
|
+
transition: NamedTransition,
|
|
1072
943
|
context: TransitionExecutionContext,
|
|
1073
|
-
|
|
1074
|
-
|
|
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
|
-
|
|
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
|
|
1102
|
-
const executedRules: ruleEngine.RuleExecutionResult[] = []
|
|
955
|
+
const executedRules: NamedTransitionRuleResult[] = []
|
|
1103
956
|
const errors: string[] = []
|
|
1104
|
-
|
|
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
|
-
|
|
1107
|
-
|
|
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:
|
|
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,
|
|
994
|
+
await logSuppressedRuleActions(em, instance, condition.ruleId, phase)
|
|
1136
995
|
}
|
|
1137
|
-
|
|
1138
|
-
|
|
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
|
-
|
|
1150
|
-
}
|
|
1151
|
-
executedRules.push(ruleResult)
|
|
1000
|
+
})
|
|
1152
1001
|
|
|
1153
|
-
|
|
1154
|
-
if (result.error) {
|
|
1002
|
+
if (required && result.error) {
|
|
1155
1003
|
errors.push(`Rule '${result.ruleId}': ${result.error}`)
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
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:
|
|
647
|
+
const evalContext: TransitionEvaluationContext = {
|
|
648
|
+
container,
|
|
646
649
|
workflowContext: currentInstance.context,
|
|
647
650
|
userId: executionUserId,
|
|
648
651
|
}
|