@open-mercato/core 0.4.2-canary-51881f6bf3 → 0.4.2-canary-5f415b8a44

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 (155) hide show
  1. package/dist/generated/entities/workflow_event_trigger/index.js +33 -0
  2. package/dist/generated/entities/workflow_event_trigger/index.js.map +7 -0
  3. package/dist/generated/entities.ids.generated.js +59 -58
  4. package/dist/generated/entities.ids.generated.js.map +2 -2
  5. package/dist/generated/entity-fields-registry.js +2 -0
  6. package/dist/generated/entity-fields-registry.js.map +2 -2
  7. package/dist/modules/auth/events.js +30 -0
  8. package/dist/modules/auth/events.js.map +7 -0
  9. package/dist/modules/business_rules/api/execute/[ruleId]/route.js +145 -0
  10. package/dist/modules/business_rules/api/execute/[ruleId]/route.js.map +7 -0
  11. package/dist/modules/business_rules/data/validators.js +34 -0
  12. package/dist/modules/business_rules/data/validators.js.map +2 -2
  13. package/dist/modules/business_rules/index.js +21 -1
  14. package/dist/modules/business_rules/index.js.map +2 -2
  15. package/dist/modules/business_rules/lib/rule-engine.js +182 -1
  16. package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
  17. package/dist/modules/catalog/events.js +34 -0
  18. package/dist/modules/catalog/events.js.map +7 -0
  19. package/dist/modules/customers/events.js +49 -0
  20. package/dist/modules/customers/events.js.map +7 -0
  21. package/dist/modules/directory/events.js +23 -0
  22. package/dist/modules/directory/events.js.map +7 -0
  23. package/dist/modules/sales/acl.js +1 -0
  24. package/dist/modules/sales/acl.js.map +2 -2
  25. package/dist/modules/sales/backend/sales/documents/[id]/page.js +12 -0
  26. package/dist/modules/sales/backend/sales/documents/[id]/page.js.map +2 -2
  27. package/dist/modules/sales/commands/documents.js +62 -0
  28. package/dist/modules/sales/commands/documents.js.map +2 -2
  29. package/dist/modules/sales/events.js +63 -0
  30. package/dist/modules/sales/events.js.map +7 -0
  31. package/dist/modules/sales/lib/dictionaries.js +3 -0
  32. package/dist/modules/sales/lib/dictionaries.js.map +2 -2
  33. package/dist/modules/sales/lib/frontend/documentDataEvents.js +25 -0
  34. package/dist/modules/sales/lib/frontend/documentDataEvents.js.map +7 -0
  35. package/dist/modules/workflows/acl.js +2 -0
  36. package/dist/modules/workflows/acl.js.map +2 -2
  37. package/dist/modules/workflows/api/instances/route.js +18 -6
  38. package/dist/modules/workflows/api/instances/route.js.map +2 -2
  39. package/dist/modules/workflows/api/tasks/route.js +6 -1
  40. package/dist/modules/workflows/api/tasks/route.js.map +2 -2
  41. package/dist/modules/workflows/backend/definitions/[id]/page.js +9 -1
  42. package/dist/modules/workflows/backend/definitions/[id]/page.js.map +2 -2
  43. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js +1 -1
  44. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js.map +2 -2
  45. package/dist/modules/workflows/backend/definitions/create/page.js +24 -15
  46. package/dist/modules/workflows/backend/definitions/create/page.js.map +2 -2
  47. package/dist/modules/workflows/backend/definitions/create/page.meta.js +1 -1
  48. package/dist/modules/workflows/backend/definitions/create/page.meta.js.map +2 -2
  49. package/dist/modules/workflows/backend/definitions/visual-editor/page.js +150 -132
  50. package/dist/modules/workflows/backend/definitions/visual-editor/page.js.map +2 -2
  51. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js +1 -1
  52. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js.map +2 -2
  53. package/dist/modules/workflows/backend/events/[id]/page.js +1 -1
  54. package/dist/modules/workflows/backend/events/[id]/page.js.map +2 -2
  55. package/dist/modules/workflows/backend/events/[id]/page.meta.js +2 -2
  56. package/dist/modules/workflows/backend/events/[id]/page.meta.js.map +2 -2
  57. package/dist/modules/workflows/backend/instances/[id]/page.meta.js +2 -2
  58. package/dist/modules/workflows/backend/instances/[id]/page.meta.js.map +2 -2
  59. package/dist/modules/workflows/backend/tasks/[id]/page.js +1 -1
  60. package/dist/modules/workflows/backend/tasks/[id]/page.js.map +2 -2
  61. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js +2 -2
  62. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js.map +2 -2
  63. package/dist/modules/workflows/backend/tasks/page.js +5 -6
  64. package/dist/modules/workflows/backend/tasks/page.js.map +2 -2
  65. package/dist/modules/workflows/cli.js +81 -3
  66. package/dist/modules/workflows/cli.js.map +3 -3
  67. package/dist/modules/workflows/components/DefinitionTriggersEditor.js +481 -0
  68. package/dist/modules/workflows/components/DefinitionTriggersEditor.js.map +7 -0
  69. package/dist/modules/workflows/components/EventTriggersEditor.js +553 -0
  70. package/dist/modules/workflows/components/EventTriggersEditor.js.map +7 -0
  71. package/dist/modules/workflows/data/entities.js +64 -1
  72. package/dist/modules/workflows/data/entities.js.map +2 -2
  73. package/dist/modules/workflows/data/validators.js +115 -0
  74. package/dist/modules/workflows/data/validators.js.map +2 -2
  75. package/dist/modules/workflows/events.js +38 -0
  76. package/dist/modules/workflows/events.js.map +7 -0
  77. package/dist/modules/workflows/examples/checkout-demo-definition.json +1 -5
  78. package/dist/modules/workflows/examples/order-approval-definition.json +257 -0
  79. package/dist/modules/workflows/examples/order-approval-guard-rules.json +32 -0
  80. package/dist/modules/workflows/lib/activity-executor.js +75 -13
  81. package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
  82. package/dist/modules/workflows/lib/event-trigger-service.js +308 -0
  83. package/dist/modules/workflows/lib/event-trigger-service.js.map +7 -0
  84. package/dist/modules/workflows/lib/graph-utils.js +71 -2
  85. package/dist/modules/workflows/lib/graph-utils.js.map +2 -2
  86. package/dist/modules/workflows/lib/seeds.js +22 -5
  87. package/dist/modules/workflows/lib/seeds.js.map +2 -2
  88. package/dist/modules/workflows/lib/start-validator.js +33 -23
  89. package/dist/modules/workflows/lib/start-validator.js.map +2 -2
  90. package/dist/modules/workflows/lib/transition-handler.js +157 -45
  91. package/dist/modules/workflows/lib/transition-handler.js.map +3 -3
  92. package/dist/modules/workflows/migrations/Migration20260123143500.js +36 -0
  93. package/dist/modules/workflows/migrations/Migration20260123143500.js.map +7 -0
  94. package/dist/modules/workflows/subscribers/event-trigger.js +78 -0
  95. package/dist/modules/workflows/subscribers/event-trigger.js.map +7 -0
  96. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js +323 -0
  97. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js.map +7 -0
  98. package/dist/modules/workflows/widgets/injection/order-approval/widget.js +17 -0
  99. package/dist/modules/workflows/widgets/injection/order-approval/widget.js.map +7 -0
  100. package/dist/modules/workflows/widgets/injection-table.js +19 -0
  101. package/dist/modules/workflows/widgets/injection-table.js.map +7 -0
  102. package/generated/entities/workflow_event_trigger/index.ts +15 -0
  103. package/generated/entities.ids.generated.ts +59 -58
  104. package/generated/entity-fields-registry.ts +2 -0
  105. package/package.json +3 -5
  106. package/src/modules/auth/events.ts +39 -0
  107. package/src/modules/business_rules/api/execute/[ruleId]/route.ts +163 -0
  108. package/src/modules/business_rules/data/validators.ts +40 -0
  109. package/src/modules/business_rules/index.ts +25 -0
  110. package/src/modules/business_rules/lib/rule-engine.ts +281 -1
  111. package/src/modules/catalog/events.ts +45 -0
  112. package/src/modules/customers/events.ts +63 -0
  113. package/src/modules/directory/events.ts +31 -0
  114. package/src/modules/sales/acl.ts +1 -0
  115. package/src/modules/sales/backend/sales/documents/[id]/page.tsx +16 -0
  116. package/src/modules/sales/commands/documents.ts +74 -1
  117. package/src/modules/sales/events.ts +82 -0
  118. package/src/modules/sales/lib/dictionaries.ts +3 -0
  119. package/src/modules/sales/lib/frontend/documentDataEvents.ts +28 -0
  120. package/src/modules/workflows/acl.ts +2 -0
  121. package/src/modules/workflows/api/instances/route.ts +21 -7
  122. package/src/modules/workflows/api/tasks/route.ts +7 -1
  123. package/src/modules/workflows/backend/definitions/[id]/page.meta.ts +1 -1
  124. package/src/modules/workflows/backend/definitions/[id]/page.tsx +9 -0
  125. package/src/modules/workflows/backend/definitions/create/page.meta.ts +1 -1
  126. package/src/modules/workflows/backend/definitions/create/page.tsx +9 -0
  127. package/src/modules/workflows/backend/definitions/visual-editor/page.meta.ts +1 -1
  128. package/src/modules/workflows/backend/definitions/visual-editor/page.tsx +21 -3
  129. package/src/modules/workflows/backend/events/[id]/page.meta.ts +2 -2
  130. package/src/modules/workflows/backend/events/[id]/page.tsx +1 -1
  131. package/src/modules/workflows/backend/instances/[id]/page.meta.ts +2 -2
  132. package/src/modules/workflows/backend/tasks/[id]/page.meta.ts +2 -2
  133. package/src/modules/workflows/backend/tasks/[id]/page.tsx +1 -1
  134. package/src/modules/workflows/backend/tasks/page.tsx +5 -6
  135. package/src/modules/workflows/cli.ts +111 -0
  136. package/src/modules/workflows/components/DefinitionTriggersEditor.tsx +581 -0
  137. package/src/modules/workflows/components/EventTriggersEditor.tsx +664 -0
  138. package/src/modules/workflows/data/entities.ts +124 -0
  139. package/src/modules/workflows/data/validators.ts +138 -0
  140. package/src/modules/workflows/events.ts +49 -0
  141. package/src/modules/workflows/examples/checkout-demo-definition.json +1 -5
  142. package/src/modules/workflows/examples/order-approval-definition.json +257 -0
  143. package/src/modules/workflows/examples/order-approval-guard-rules.json +32 -0
  144. package/src/modules/workflows/i18n/en.json +71 -0
  145. package/src/modules/workflows/lib/activity-executor.ts +129 -16
  146. package/src/modules/workflows/lib/event-trigger-service.ts +557 -0
  147. package/src/modules/workflows/lib/graph-utils.ts +117 -2
  148. package/src/modules/workflows/lib/seeds.ts +34 -8
  149. package/src/modules/workflows/lib/start-validator.ts +38 -28
  150. package/src/modules/workflows/lib/transition-handler.ts +208 -55
  151. package/src/modules/workflows/migrations/Migration20260123143500.ts +38 -0
  152. package/src/modules/workflows/subscribers/event-trigger.ts +109 -0
  153. package/src/modules/workflows/widgets/injection/order-approval/widget.client.tsx +446 -0
  154. package/src/modules/workflows/widgets/injection/order-approval/widget.ts +16 -0
  155. package/src/modules/workflows/widgets/injection-table.ts +21 -0
@@ -161,6 +161,7 @@
161
161
  "title": "Workflow Instances",
162
162
  "singular": "Workflow Instance",
163
163
  "plural": "Workflow Instances",
164
+ "duration": "Duration",
164
165
  "fields": {
165
166
  "instanceId": "Instance ID",
166
167
  "workflowId": "Workflow ID",
@@ -563,6 +564,55 @@
563
564
  "CompensationCompleted": "Compensation Completed"
564
565
  }
565
566
  },
567
+ "triggers": {
568
+ "title": "Event Triggers",
569
+ "add": "Add Trigger",
570
+ "description": "Configure events that automatically start this workflow. When a matching event occurs in the system, a new workflow instance will be created with the mapped context.",
571
+ "empty": {
572
+ "title": "No triggers configured",
573
+ "description": "Click \"Add Trigger\" to create an event trigger that automatically starts this workflow."
574
+ },
575
+ "dialog": {
576
+ "create": {
577
+ "title": "Create Event Trigger"
578
+ },
579
+ "edit": {
580
+ "title": "Edit Event Trigger"
581
+ },
582
+ "description": "Configure when this workflow should be automatically started based on system events."
583
+ },
584
+ "delete": {
585
+ "title": "Delete Event Trigger?",
586
+ "description": "This will remove the event trigger. The change will take effect when you save the workflow definition."
587
+ },
588
+ "fields": {
589
+ "name": "Name",
590
+ "description": "Description",
591
+ "eventPattern": "Event Pattern",
592
+ "enabled": "Enabled",
593
+ "priority": "Priority",
594
+ "filterConditions": "Filter Conditions",
595
+ "contextMapping": "Context Mapping",
596
+ "debounceMs": "Debounce (ms)",
597
+ "maxConcurrent": "Max Concurrent Instances"
598
+ },
599
+ "placeholders": {
600
+ "name": "Order Created Trigger",
601
+ "description": "Describe when this trigger should fire...",
602
+ "quickSelect": "Quick select...",
603
+ "unlimited": "Unlimited"
604
+ },
605
+ "hints": {
606
+ "priority": "Higher priority triggers execute first",
607
+ "eventPattern": "Use * as wildcard: \"sales.orders.*\" matches any order event",
608
+ "filterConditions": "Only trigger when the event payload matches these conditions (all must match)",
609
+ "contextMapping": "Map values from the event payload to the workflow's initial context",
610
+ "debounce": "Delay to prevent rapid re-triggers",
611
+ "maxConcurrent": "Limit simultaneous workflow instances"
612
+ },
613
+ "addCondition": "Add Condition",
614
+ "addMapping": "Add Mapping"
615
+ },
566
616
  "common": {
567
617
  "save": "Save",
568
618
  "cancel": "Cancel",
@@ -629,6 +679,27 @@
629
679
  "duplicateNotYetImplemented": "Duplicate functionality is not yet implemented"
630
680
  }
631
681
  },
682
+ "orderApproval": {
683
+ "groupLabel": "Order Approval",
684
+ "groupDescription": "Review and approve or reject this order",
685
+ "pendingTitle": "Pending Approval",
686
+ "pendingDescription": "This order requires approval before processing",
687
+ "decisionLabel": "Decision",
688
+ "approveButton": "Approve",
689
+ "rejectButton": "Reject",
690
+ "commentsLabel": "Comments",
691
+ "commentsPlaceholder": "Add optional comments...",
692
+ "approvedStatus": "Approved",
693
+ "rejectedStatus": "Rejected",
694
+ "requestApproval": "Request Approval",
695
+ "submitDecision": "Submit Decision",
696
+ "noWorkflowActive": "No approval workflow is active for this order.",
697
+ "processing": "Workflow is processing...",
698
+ "completed": "Approval workflow completed.",
699
+ "startError": "Failed to start approval workflow",
700
+ "completeError": "Failed to complete approval task",
701
+ "missingStatuses": "Missing order status entries. Please ensure pending_approval, approved, and rejected statuses exist in the sales.order_status dictionary."
702
+ },
632
703
  "common": {
633
704
  "all": "All",
634
705
  "enabled": "Enabled",
@@ -448,7 +448,30 @@ export async function executeEmitEvent(
448
448
  /**
449
449
  * UPDATE_ENTITY activity handler
450
450
  *
451
- * Updates an entity via query engine
451
+ * Updates an entity via CommandBus for proper audit logging, undo support, and side effects.
452
+ *
453
+ * Config format:
454
+ * ```json
455
+ * {
456
+ * "commandId": "sales.documents.update",
457
+ * "input": {
458
+ * "id": "{{context.orderId}}",
459
+ * "statusEntryId": "{{context.approvedStatusId}}"
460
+ * }
461
+ * }
462
+ * ```
463
+ *
464
+ * Alternative format with statusValue (auto-resolves to statusEntryId):
465
+ * ```json
466
+ * {
467
+ * "commandId": "sales.orders.update",
468
+ * "statusDictionary": "sales.order_status",
469
+ * "input": {
470
+ * "id": "{{context.id}}",
471
+ * "statusValue": "pending_approval"
472
+ * }
473
+ * }
474
+ * ```
452
475
  */
453
476
  export async function executeUpdateEntity(
454
477
  em: EntityManager,
@@ -456,29 +479,119 @@ export async function executeUpdateEntity(
456
479
  context: ActivityContext,
457
480
  container: AwilixContainer
458
481
  ): Promise<any> {
459
- const { entityType, entityId, updates } = config
482
+ const { commandId, input, statusDictionary } = config
483
+
484
+ if (!commandId) {
485
+ throw new Error('UPDATE_ENTITY requires "commandId" field (e.g., "sales.documents.update")')
486
+ }
460
487
 
461
- if (!entityType || !entityId || !updates) {
462
- throw new Error('UPDATE_ENTITY requires "entityType", "entityId", and "updates" fields')
488
+ if (!input || typeof input !== 'object') {
489
+ throw new Error('UPDATE_ENTITY requires "input" object with entity data')
463
490
  }
464
491
 
465
- // Get query engine from container
466
- const queryEngine = container.resolve('queryEngine')
492
+ // Resolve CommandBus from container
493
+ const commandBus = container.resolve('commandBus') as any
467
494
 
468
- if (!queryEngine || typeof queryEngine.update !== 'function') {
469
- throw new Error('Query engine not available in container')
495
+ if (!commandBus || typeof commandBus.execute !== 'function') {
496
+ throw new Error('CommandBus not available in container')
470
497
  }
471
498
 
472
- // Execute update with tenant scoping
473
- await queryEngine.update({
474
- entity: entityType,
475
- where: { id: entityId },
476
- data: updates,
477
- tenantId: context.workflowInstance.tenantId,
478
- organizationId: context.workflowInstance.organizationId,
499
+ // Prepare final input, resolving statusValue if provided
500
+ let finalInput = { ...input }
501
+
502
+ // If statusValue is provided with a statusDictionary, resolve it to statusEntryId
503
+ if (finalInput.statusValue && statusDictionary) {
504
+ const statusEntryId = await resolveDictionaryEntryId(
505
+ em,
506
+ statusDictionary,
507
+ finalInput.statusValue,
508
+ context.workflowInstance.tenantId,
509
+ context.workflowInstance.organizationId
510
+ )
511
+ if (statusEntryId) {
512
+ finalInput.statusEntryId = statusEntryId
513
+ }
514
+ delete finalInput.statusValue
515
+ }
516
+
517
+ // Build synthetic CommandRuntimeContext for workflow execution
518
+ // Use nil UUID for system actions when no user context is available
519
+ const SYSTEM_USER_ID = '00000000-0000-0000-0000-000000000000'
520
+ const ctx = {
521
+ container,
522
+ auth: {
523
+ sub: context.userId || SYSTEM_USER_ID,
524
+ tenantId: context.workflowInstance.tenantId,
525
+ orgId: context.workflowInstance.organizationId,
526
+ isSuperAdmin: false,
527
+ },
528
+ organizationScope: null,
529
+ selectedOrganizationId: context.workflowInstance.organizationId,
530
+ organizationIds: context.workflowInstance.organizationId
531
+ ? [context.workflowInstance.organizationId]
532
+ : null,
533
+ }
534
+
535
+ // Execute the command
536
+ const { result, logEntry } = await commandBus.execute(commandId, {
537
+ input: finalInput,
538
+ ctx,
479
539
  })
480
540
 
481
- return { updated: true, entityType, entityId, updates }
541
+ return {
542
+ executed: true,
543
+ commandId,
544
+ result,
545
+ logEntryId: logEntry?.id,
546
+ }
547
+ }
548
+
549
+ /**
550
+ * Helper to resolve dictionary entry ID by value
551
+ */
552
+ async function resolveDictionaryEntryId(
553
+ em: EntityManager,
554
+ dictionaryKey: string,
555
+ value: string,
556
+ tenantId: string,
557
+ organizationId: string
558
+ ): Promise<string | null> {
559
+ try {
560
+ // Import here to avoid circular dependencies
561
+ const { Dictionary, DictionaryEntry } = await import('@open-mercato/core/modules/dictionaries/data/entities')
562
+
563
+ // Find the dictionary
564
+ const dictionary = await em.findOne(Dictionary, {
565
+ key: dictionaryKey,
566
+ tenantId,
567
+ organizationId,
568
+ deletedAt: null,
569
+ })
570
+
571
+ if (!dictionary) {
572
+ console.warn(`[UPDATE_ENTITY] Dictionary not found: ${dictionaryKey}`)
573
+ return null
574
+ }
575
+
576
+ // Find the entry by normalized value
577
+ const normalizedValue = value.toLowerCase().trim()
578
+ const entry = await em.findOne(DictionaryEntry, {
579
+ dictionary: dictionary.id,
580
+ tenantId,
581
+ organizationId,
582
+ normalizedValue,
583
+ })
584
+
585
+ if (!entry) {
586
+ console.warn(`[UPDATE_ENTITY] Dictionary entry not found: ${dictionaryKey}/${value}`)
587
+ return null
588
+ }
589
+
590
+ return entry.id
591
+ } catch (error) {
592
+ console.error(`[UPDATE_ENTITY] Error resolving dictionary entry:`, error)
593
+ return null
594
+ }
482
595
  }
483
596
 
484
597
  /**