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

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 (173) hide show
  1. package/dist/generated/entities/api_key/index.js +2 -0
  2. package/dist/generated/entities/api_key/index.js.map +2 -2
  3. package/dist/generated/entities/workflow_event_trigger/index.js +33 -0
  4. package/dist/generated/entities/workflow_event_trigger/index.js.map +7 -0
  5. package/dist/generated/entities.ids.generated.js +1 -0
  6. package/dist/generated/entities.ids.generated.js.map +2 -2
  7. package/dist/generated/entity-fields-registry.js +2 -0
  8. package/dist/generated/entity-fields-registry.js.map +2 -2
  9. package/dist/modules/api_keys/data/entities.js +3 -0
  10. package/dist/modules/api_keys/data/entities.js.map +2 -2
  11. package/dist/modules/api_keys/migrations/Migration20260125204102.js +13 -0
  12. package/dist/modules/api_keys/migrations/Migration20260125204102.js.map +7 -0
  13. package/dist/modules/api_keys/services/apiKeyService.js +41 -0
  14. package/dist/modules/api_keys/services/apiKeyService.js.map +3 -3
  15. package/dist/modules/auth/events.js +30 -0
  16. package/dist/modules/auth/events.js.map +7 -0
  17. package/dist/modules/auth/services/rbacService.js.map +2 -2
  18. package/dist/modules/business_rules/api/execute/[ruleId]/route.js +145 -0
  19. package/dist/modules/business_rules/api/execute/[ruleId]/route.js.map +7 -0
  20. package/dist/modules/business_rules/data/validators.js +34 -0
  21. package/dist/modules/business_rules/data/validators.js.map +2 -2
  22. package/dist/modules/business_rules/index.js +21 -1
  23. package/dist/modules/business_rules/index.js.map +2 -2
  24. package/dist/modules/business_rules/lib/rule-engine.js +182 -1
  25. package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
  26. package/dist/modules/catalog/events.js +34 -0
  27. package/dist/modules/catalog/events.js.map +7 -0
  28. package/dist/modules/customers/events.js +49 -0
  29. package/dist/modules/customers/events.js.map +7 -0
  30. package/dist/modules/directory/events.js +23 -0
  31. package/dist/modules/directory/events.js.map +7 -0
  32. package/dist/modules/sales/acl.js +1 -0
  33. package/dist/modules/sales/acl.js.map +2 -2
  34. package/dist/modules/sales/backend/sales/documents/[id]/page.js +12 -0
  35. package/dist/modules/sales/backend/sales/documents/[id]/page.js.map +2 -2
  36. package/dist/modules/sales/commands/documents.js +62 -0
  37. package/dist/modules/sales/commands/documents.js.map +2 -2
  38. package/dist/modules/sales/events.js +63 -0
  39. package/dist/modules/sales/events.js.map +7 -0
  40. package/dist/modules/sales/lib/dictionaries.js +3 -0
  41. package/dist/modules/sales/lib/dictionaries.js.map +2 -2
  42. package/dist/modules/sales/lib/frontend/documentDataEvents.js +25 -0
  43. package/dist/modules/sales/lib/frontend/documentDataEvents.js.map +7 -0
  44. package/dist/modules/workflows/acl.js +2 -0
  45. package/dist/modules/workflows/acl.js.map +2 -2
  46. package/dist/modules/workflows/api/instances/route.js +18 -6
  47. package/dist/modules/workflows/api/instances/route.js.map +2 -2
  48. package/dist/modules/workflows/api/tasks/route.js +6 -1
  49. package/dist/modules/workflows/api/tasks/route.js.map +2 -2
  50. package/dist/modules/workflows/backend/definitions/[id]/page.js +9 -1
  51. package/dist/modules/workflows/backend/definitions/[id]/page.js.map +2 -2
  52. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js +1 -1
  53. package/dist/modules/workflows/backend/definitions/[id]/page.meta.js.map +2 -2
  54. package/dist/modules/workflows/backend/definitions/create/page.js +24 -15
  55. package/dist/modules/workflows/backend/definitions/create/page.js.map +2 -2
  56. package/dist/modules/workflows/backend/definitions/create/page.meta.js +1 -1
  57. package/dist/modules/workflows/backend/definitions/create/page.meta.js.map +2 -2
  58. package/dist/modules/workflows/backend/definitions/visual-editor/page.js +150 -132
  59. package/dist/modules/workflows/backend/definitions/visual-editor/page.js.map +2 -2
  60. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js +1 -1
  61. package/dist/modules/workflows/backend/definitions/visual-editor/page.meta.js.map +2 -2
  62. package/dist/modules/workflows/backend/events/[id]/page.js +1 -1
  63. package/dist/modules/workflows/backend/events/[id]/page.js.map +2 -2
  64. package/dist/modules/workflows/backend/events/[id]/page.meta.js +2 -2
  65. package/dist/modules/workflows/backend/events/[id]/page.meta.js.map +2 -2
  66. package/dist/modules/workflows/backend/instances/[id]/page.meta.js +2 -2
  67. package/dist/modules/workflows/backend/instances/[id]/page.meta.js.map +2 -2
  68. package/dist/modules/workflows/backend/tasks/[id]/page.js +1 -1
  69. package/dist/modules/workflows/backend/tasks/[id]/page.js.map +2 -2
  70. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js +2 -2
  71. package/dist/modules/workflows/backend/tasks/[id]/page.meta.js.map +2 -2
  72. package/dist/modules/workflows/backend/tasks/page.js +5 -6
  73. package/dist/modules/workflows/backend/tasks/page.js.map +2 -2
  74. package/dist/modules/workflows/cli.js +81 -3
  75. package/dist/modules/workflows/cli.js.map +3 -3
  76. package/dist/modules/workflows/components/DefinitionTriggersEditor.js +481 -0
  77. package/dist/modules/workflows/components/DefinitionTriggersEditor.js.map +7 -0
  78. package/dist/modules/workflows/components/EventTriggersEditor.js +553 -0
  79. package/dist/modules/workflows/components/EventTriggersEditor.js.map +7 -0
  80. package/dist/modules/workflows/data/entities.js +64 -1
  81. package/dist/modules/workflows/data/entities.js.map +2 -2
  82. package/dist/modules/workflows/data/validators.js +115 -0
  83. package/dist/modules/workflows/data/validators.js.map +2 -2
  84. package/dist/modules/workflows/events.js +38 -0
  85. package/dist/modules/workflows/events.js.map +7 -0
  86. package/dist/modules/workflows/examples/checkout-demo-definition.json +1 -5
  87. package/dist/modules/workflows/examples/order-approval-definition.json +257 -0
  88. package/dist/modules/workflows/examples/order-approval-guard-rules.json +32 -0
  89. package/dist/modules/workflows/lib/activity-executor.js +75 -13
  90. package/dist/modules/workflows/lib/activity-executor.js.map +2 -2
  91. package/dist/modules/workflows/lib/event-trigger-service.js +308 -0
  92. package/dist/modules/workflows/lib/event-trigger-service.js.map +7 -0
  93. package/dist/modules/workflows/lib/graph-utils.js +71 -2
  94. package/dist/modules/workflows/lib/graph-utils.js.map +2 -2
  95. package/dist/modules/workflows/lib/seeds.js +22 -5
  96. package/dist/modules/workflows/lib/seeds.js.map +2 -2
  97. package/dist/modules/workflows/lib/start-validator.js +33 -23
  98. package/dist/modules/workflows/lib/start-validator.js.map +2 -2
  99. package/dist/modules/workflows/lib/transition-handler.js +157 -45
  100. package/dist/modules/workflows/lib/transition-handler.js.map +3 -3
  101. package/dist/modules/workflows/migrations/Migration20260123143500.js +36 -0
  102. package/dist/modules/workflows/migrations/Migration20260123143500.js.map +7 -0
  103. package/dist/modules/workflows/subscribers/event-trigger.js +78 -0
  104. package/dist/modules/workflows/subscribers/event-trigger.js.map +7 -0
  105. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js +323 -0
  106. package/dist/modules/workflows/widgets/injection/order-approval/widget.client.js.map +7 -0
  107. package/dist/modules/workflows/widgets/injection/order-approval/widget.js +17 -0
  108. package/dist/modules/workflows/widgets/injection/order-approval/widget.js.map +7 -0
  109. package/dist/modules/workflows/widgets/injection-table.js +19 -0
  110. package/dist/modules/workflows/widgets/injection-table.js.map +7 -0
  111. package/generated/entities/api_key/index.ts +1 -0
  112. package/generated/entities/workflow_event_trigger/index.ts +15 -0
  113. package/generated/entities.ids.generated.ts +1 -0
  114. package/generated/entity-fields-registry.ts +2 -0
  115. package/package.json +2 -2
  116. package/src/modules/api_keys/data/entities.ts +4 -0
  117. package/src/modules/api_keys/migrations/.snapshot-open-mercato.json +9 -0
  118. package/src/modules/api_keys/migrations/Migration20260125204102.ts +13 -0
  119. package/src/modules/api_keys/services/apiKeyService.ts +85 -0
  120. package/src/modules/auth/events.ts +39 -0
  121. package/src/modules/auth/services/rbacService.ts +1 -1
  122. package/src/modules/business_rules/api/execute/[ruleId]/route.ts +163 -0
  123. package/src/modules/business_rules/data/validators.ts +40 -0
  124. package/src/modules/business_rules/index.ts +25 -0
  125. package/src/modules/business_rules/lib/rule-engine.ts +281 -1
  126. package/src/modules/catalog/events.ts +45 -0
  127. package/src/modules/customers/events.ts +63 -0
  128. package/src/modules/directory/events.ts +31 -0
  129. package/src/modules/sales/acl.ts +1 -0
  130. package/src/modules/sales/backend/sales/documents/[id]/page.tsx +16 -0
  131. package/src/modules/sales/commands/documents.ts +74 -1
  132. package/src/modules/sales/events.ts +82 -0
  133. package/src/modules/sales/lib/dictionaries.ts +3 -0
  134. package/src/modules/sales/lib/frontend/documentDataEvents.ts +28 -0
  135. package/src/modules/workflows/acl.ts +2 -0
  136. package/src/modules/workflows/api/__tests__/instances.route.test.ts +5 -2
  137. package/src/modules/workflows/api/instances/route.ts +21 -7
  138. package/src/modules/workflows/api/tasks/route.ts +7 -1
  139. package/src/modules/workflows/backend/definitions/[id]/page.meta.ts +1 -1
  140. package/src/modules/workflows/backend/definitions/[id]/page.tsx +9 -0
  141. package/src/modules/workflows/backend/definitions/create/page.meta.ts +1 -1
  142. package/src/modules/workflows/backend/definitions/create/page.tsx +9 -0
  143. package/src/modules/workflows/backend/definitions/visual-editor/page.meta.ts +1 -1
  144. package/src/modules/workflows/backend/definitions/visual-editor/page.tsx +21 -3
  145. package/src/modules/workflows/backend/events/[id]/page.meta.ts +2 -2
  146. package/src/modules/workflows/backend/events/[id]/page.tsx +1 -1
  147. package/src/modules/workflows/backend/instances/[id]/page.meta.ts +2 -2
  148. package/src/modules/workflows/backend/tasks/[id]/page.meta.ts +2 -2
  149. package/src/modules/workflows/backend/tasks/[id]/page.tsx +1 -1
  150. package/src/modules/workflows/backend/tasks/page.tsx +5 -6
  151. package/src/modules/workflows/cli.ts +111 -0
  152. package/src/modules/workflows/components/DefinitionTriggersEditor.tsx +581 -0
  153. package/src/modules/workflows/components/EventTriggersEditor.tsx +664 -0
  154. package/src/modules/workflows/data/entities.ts +124 -0
  155. package/src/modules/workflows/data/validators.ts +138 -0
  156. package/src/modules/workflows/events.ts +49 -0
  157. package/src/modules/workflows/examples/checkout-demo-definition.json +1 -5
  158. package/src/modules/workflows/examples/order-approval-definition.json +257 -0
  159. package/src/modules/workflows/examples/order-approval-guard-rules.json +32 -0
  160. package/src/modules/workflows/i18n/en.json +71 -0
  161. package/src/modules/workflows/lib/__tests__/activity-executor.test.ts +43 -36
  162. package/src/modules/workflows/lib/__tests__/transition-handler.test.ts +170 -90
  163. package/src/modules/workflows/lib/activity-executor.ts +129 -16
  164. package/src/modules/workflows/lib/event-trigger-service.ts +557 -0
  165. package/src/modules/workflows/lib/graph-utils.ts +117 -2
  166. package/src/modules/workflows/lib/seeds.ts +34 -8
  167. package/src/modules/workflows/lib/start-validator.ts +38 -28
  168. package/src/modules/workflows/lib/transition-handler.ts +208 -55
  169. package/src/modules/workflows/migrations/Migration20260123143500.ts +38 -0
  170. package/src/modules/workflows/subscribers/event-trigger.ts +109 -0
  171. package/src/modules/workflows/widgets/injection/order-approval/widget.client.tsx +446 -0
  172. package/src/modules/workflows/widgets/injection/order-approval/widget.ts +16 -0
  173. package/src/modules/workflows/widgets/injection-table.ts +21 -0
@@ -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
  /**