@open-mercato/cli 0.6.6-develop.6381.1.042df302c3 → 0.6.6-develop.6382.1.4b9b9091ab

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 (31) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/dist/agentic/guides/core.md +3 -38
  3. package/dist/agentic/guides/module-system.md +130 -0
  4. package/dist/agentic/shared/AGENTS.md.template +4 -12
  5. package/dist/lib/generators/index.js +2 -0
  6. package/dist/lib/generators/index.js.map +2 -2
  7. package/dist/lib/generators/module-facts-generate.js +52 -0
  8. package/dist/lib/generators/module-facts-generate.js.map +7 -0
  9. package/dist/lib/generators/module-facts.js +734 -0
  10. package/dist/lib/generators/module-facts.js.map +7 -0
  11. package/dist/mercato.js +3 -1
  12. package/dist/mercato.js.map +2 -2
  13. package/package.json +5 -5
  14. package/src/__tests__/mercato.test.ts +5 -0
  15. package/src/lib/generators/__tests__/module-facts.auth-source.test.ts +83 -0
  16. package/src/lib/generators/__tests__/module-facts.bc-guard.test.ts +56 -0
  17. package/src/lib/generators/__tests__/module-facts.customers.fixture.test.ts +69 -0
  18. package/src/lib/generators/__tests__/module-facts.malformed.test.ts +76 -0
  19. package/src/lib/generators/index.ts +1 -0
  20. package/src/lib/generators/module-facts-generate.ts +68 -0
  21. package/src/lib/generators/module-facts.ts +958 -0
  22. package/src/mercato.ts +2 -0
  23. package/dist/agentic/guides/core.auth.md +0 -101
  24. package/dist/agentic/guides/core.catalog.md +0 -79
  25. package/dist/agentic/guides/core.currencies.md +0 -43
  26. package/dist/agentic/guides/core.customer_accounts.md +0 -129
  27. package/dist/agentic/guides/core.customers.md +0 -138
  28. package/dist/agentic/guides/core.data_sync.md +0 -107
  29. package/dist/agentic/guides/core.integrations.md +0 -113
  30. package/dist/agentic/guides/core.sales.md +0 -104
  31. package/dist/agentic/guides/core.workflows.md +0 -152
package/src/mercato.ts CHANGED
@@ -752,6 +752,7 @@ async function runGeneratorSuite(quiet: boolean): Promise<void> {
752
752
  generateModuleDi,
753
753
  generateModulePackageSources,
754
754
  generateOpenApi,
755
+ generateModuleFacts,
755
756
  } = await import('./lib/generators')
756
757
  const resolver = createResolver()
757
758
  await generateEntityIds({ resolver, quiet })
@@ -762,6 +763,7 @@ async function runGeneratorSuite(quiet: boolean): Promise<void> {
762
763
  await generateModuleDi({ resolver, quiet })
763
764
  await generateModulePackageSources({ resolver, quiet })
764
765
  await generateOpenApi({ resolver, quiet })
766
+ await generateModuleFacts({ resolver, quiet })
765
767
  }
766
768
 
767
769
  /**
@@ -1,101 +0,0 @@
1
- # Auth Module — Standalone App Guide
2
-
3
- The auth module handles staff authentication, authorization, users, roles, and RBAC. For customer portal authentication, see the `customer_accounts` module guide.
4
-
5
- ## RBAC Implementation
6
-
7
- ### Two-Layer Model
8
-
9
- 1. **Role ACLs** — features assigned to roles (admin, employee, etc.)
10
- 2. **User ACLs** — per-user overrides (additional features or restrictions)
11
-
12
- Effective permissions = Role features + User-specific features.
13
-
14
- ### Declaring Features
15
-
16
- Every module MUST declare features in `acl.ts` and wire them in `setup.ts`:
17
-
18
- ```typescript
19
- // src/modules/<your_module>/acl.ts
20
- export const features = [
21
- 'your_module.view',
22
- 'your_module.create',
23
- 'your_module.update',
24
- 'your_module.delete',
25
- ]
26
-
27
- // src/modules/<your_module>/setup.ts
28
- import type { ModuleSetupConfig } from '@open-mercato/shared/modules/setup'
29
-
30
- export const setup: ModuleSetupConfig = {
31
- defaultRoleFeatures: {
32
- superadmin: ['your_module.*'],
33
- admin: ['your_module.*'],
34
- user: ['your_module.view'],
35
- },
36
- }
37
- ```
38
-
39
- ### Feature Naming Convention
40
-
41
- Features follow the `<module>.<action>` pattern (e.g., `users.view`, `users.edit`).
42
-
43
- ### Declarative Guards
44
-
45
- Prefer declarative guards in page and API metadata:
46
-
47
- ```typescript
48
- export const metadata = {
49
- requireAuth: true,
50
- requireRoles: ['admin'],
51
- requireFeatures: ['users.manage'],
52
- }
53
- ```
54
-
55
- ### Server-Side Checks
56
-
57
- ```typescript
58
- const rbacService = container.resolve('rbacService')
59
- const hasAccess = await rbacService.userHasAllFeatures(
60
- userId,
61
- ['your_module.view'],
62
- { tenantId, organizationId }
63
- )
64
- ```
65
-
66
- ### Wildcards
67
-
68
- Wildcards are first-class ACL grants: `module.*` and `*` satisfy matching concrete features. When your code inspects raw granted feature arrays (instead of calling `rbacService`), use the shared wildcard-aware matchers (`matchFeature`, `hasFeature`, `hasAllFeatures`) — never use `includes(...)`.
69
-
70
- ### Special Flags
71
-
72
- - `isSuperAdmin` — bypasses all feature checks
73
- - Organization visibility list — restricts which organizations a user can access
74
-
75
- ## Security Rules
76
-
77
- - Hash passwords with `bcryptjs` (cost >= 10)
78
- - Never log credentials
79
- - Return minimal auth error messages — never reveal whether an email exists
80
- - Use `findWithDecryption` / `findOneWithDecryption` for user queries
81
-
82
- ## Authentication Flow
83
-
84
- 1. User submits credentials via `POST /api/auth/session`
85
- 2. Password verified with bcryptjs
86
- 3. JWT session token issued
87
- 4. Session attached to requests via middleware
88
-
89
- ## Subscribing to Auth Events
90
-
91
- ```typescript
92
- export const metadata = {
93
- event: 'auth.user.created',
94
- persistent: true,
95
- id: 'your-module-user-created',
96
- }
97
-
98
- export default async function handler(payload, ctx) {
99
- // React to new user registration
100
- }
101
- ```
@@ -1,79 +0,0 @@
1
- # Catalog Module — Standalone App Guide
2
-
3
- Use the catalog module for products, categories, pricing, variants, and offers.
4
-
5
- ## Pricing System
6
-
7
- Never reimplement pricing logic. Use the catalog pricing service via DI:
8
-
9
- ```typescript
10
- const pricingService = container.resolve('catalogPricingService')
11
- ```
12
-
13
- - `selectBestPrice` — finds the best price for a given context (customer, channel, quantity)
14
- - `resolvePriceVariantId` — resolves variant-level prices
15
- - Register custom pricing resolvers with priority (higher = checked first):
16
-
17
- ```typescript
18
- import { registerCatalogPricingResolver } from '@open-mercato/core/modules/catalog/lib/pricing'
19
- registerCatalogPricingResolver(myResolver, { priority: 10 })
20
- ```
21
-
22
- Price layers compose in order: base price → channel override → customer-specific → promotional.
23
-
24
- The pipeline emits `catalog.pricing.resolve.before` and `catalog.pricing.resolve.after` events that your module can subscribe to.
25
-
26
- ## Data Model
27
-
28
- | Entity | Purpose | Key Constraints |
29
- |--------|---------|----------------|
30
- | **Products** | Core items with media and descriptions | MUST have at least a name |
31
- | **Categories** | Hierarchical product grouping | No circular parent-child references |
32
- | **Variants** | Product variations (size, color) | MUST reference valid option schemas |
33
- | **Prices** | Multi-tier with channel scoping | Use `selectBestPrice` for resolution |
34
- | **Offers** | Time-limited promotions | MUST have valid date ranges |
35
- | **Option Schemas** | Variant option type definitions | Cannot delete while variants reference them |
36
-
37
- ## Subscribing to Catalog Events
38
-
39
- React to product lifecycle events in your module:
40
-
41
- ```typescript
42
- // src/modules/<your_module>/subscribers/product-updated.ts
43
- export const metadata = {
44
- event: 'catalog.product.updated',
45
- persistent: true,
46
- id: 'your-module-product-updated',
47
- }
48
-
49
- export default async function handler(payload, ctx) {
50
- // payload.resourceId = product ID
51
- }
52
- ```
53
-
54
- Key events:
55
- - `catalog.product.created` / `updated` / `deleted`
56
- - `catalog.pricing.resolve.before` / `after` (excluded from workflow triggers)
57
-
58
- ## Extending Catalog UI
59
-
60
- Use widget injection to add your module's UI into catalog pages:
61
-
62
- ```typescript
63
- // src/modules/<your_module>/widgets/injection-table.ts
64
- export const widgetInjections = {
65
- 'crud-form:catalog.catalog_product:fields': {
66
- widgetId: 'your-module-product-fields',
67
- priority: 100,
68
- },
69
- }
70
- ```
71
-
72
- Common injection spots:
73
- - `crud-form:catalog.catalog_product:fields` — product edit form
74
- - `data-table:catalog.products:columns` — product list columns
75
- - `data-table:catalog.products:row-actions` — product row actions
76
-
77
- ## Using Catalog in Sales
78
-
79
- When building sales-related features, use the catalog pricing service to resolve prices rather than reading price entities directly. This ensures channel scoping, customer-specific pricing, and promotional offers are applied correctly.
@@ -1,43 +0,0 @@
1
- # Currencies Module — Standalone App Guide
2
-
3
- Use the currencies module for multi-currency support, exchange rates, and currency conversion.
4
-
5
- ## Key Rules
6
-
7
- 1. **Store amounts with 4 decimal precision** — never truncate to 2 decimals internally
8
- 2. **Use date-based exchange rates** — always resolve rates for the transaction date, not the "current" rate
9
- 3. **Record both currencies** — dual recording (transaction currency + base currency) is mandatory for reporting
10
- 4. **Calculate realized gains/losses** on payment: `(payment rate - invoice rate) × foreign amount`
11
- 5. **Never hard-delete exchange rates** — they are historical reference data
12
-
13
- ## Multi-Currency Transaction Pattern
14
-
15
- When processing multi-currency transactions (e.g., sales invoice in EUR with USD base):
16
-
17
- 1. Retrieve the exchange rate for the transaction date
18
- 2. Generate the document in the transaction currency
19
- 3. Calculate the base currency equivalent: `foreign amount × rate`
20
- 4. Store both amounts on the document
21
- 5. On payment: calculate realized gain/loss from rate difference
22
- 6. Report in both transaction and base currencies
23
-
24
- ## Data Model
25
-
26
- | Entity | Table | Purpose |
27
- |--------|-------|---------|
28
- | **Currency** | `currency` | Currency master data (code, name, symbol) |
29
- | **Exchange Rate** | `exchange_rate` | Daily exchange rates per currency pair |
30
-
31
- ## Adding a New Currency
32
-
33
- 1. Add the currency record via the admin UI or `seedDefaults` hook in your `setup.ts`
34
- 2. Ensure exchange rates exist for the currency pair at required dates
35
- 3. Verify all sales/pricing logic resolves the new currency correctly
36
-
37
- ## Using Currencies in Your Module
38
-
39
- When your module deals with monetary amounts:
40
- - Store the currency code alongside the amount
41
- - Reference the currencies module for exchange rate lookups
42
- - Use the transaction date for rate resolution, not the current date
43
- - Store both foreign and base amounts for reporting
@@ -1,129 +0,0 @@
1
- # Customer Accounts Module — Standalone App Guide
2
-
3
- Customer-facing identity and portal authentication. This module manages customer user accounts, sessions, roles, and the authentication flow for the customer portal. It is separate from the staff `auth` module.
4
-
5
- ## Portal Authentication
6
-
7
- ### Login Flow
8
- 1. Customer submits credentials via `POST /api/login`
9
- 2. Password verified with bcryptjs, lockout checked (5 attempts → 15 min lock)
10
- 3. JWT issued with customer claims (`type: 'customer'`, features, CRM links)
11
- 4. Two cookies set: `customer_auth_token` (JWT, 8h) + `customer_session_token` (raw, 30d)
12
-
13
- ### Other Auth Methods
14
- - **Signup**: `POST /api/signup` — self-registration with email verification
15
- - **Magic Link**: `POST /api/magic-link/request` + `/verify` — passwordless login (15 min TTL)
16
- - **Password Reset**: `POST /api/password/reset-request` + `/reset-confirm` (60 min TTL)
17
- - **Invitation**: Admin invites user → `POST /api/invitations/accept` (72h TTL)
18
-
19
- ## Customer RBAC
20
-
21
- ### Two-Layer Model (mirrors staff RBAC)
22
- 1. **Role ACLs** — features assigned to roles
23
- 2. **User ACLs** — per-user overrides (takes precedence if present)
24
-
25
- ### Default Roles (seeded on tenant creation)
26
- | Role | Features | Portal Admin |
27
- |------|----------|-------------|
28
- | Portal Admin | `portal.*` | Yes |
29
- | Buyer | Orders, quotes, catalog, account | No |
30
- | Viewer | Read-only orders, invoices, catalog | No |
31
-
32
- ### Feature Convention
33
- Portal features use `portal.<area>.<action>` naming (e.g., `portal.orders.view`, `portal.catalog.view`).
34
-
35
- ### Cross-Module Feature Merging
36
- Your module can declare `defaultCustomerRoleFeatures` in `setup.ts`. During tenant setup, these are merged into the corresponding customer role ACLs:
37
-
38
- ```typescript
39
- // src/modules/<your_module>/setup.ts
40
- export const setup: ModuleSetupConfig = {
41
- defaultCustomerRoleFeatures: {
42
- portal_admin: ['portal.your_feature.*'],
43
- buyer: ['portal.your_feature.view'],
44
- },
45
- }
46
- ```
47
-
48
- ## Using Customer Auth in Your Module
49
-
50
- ### Server Components (pages)
51
- ```typescript
52
- import { getCustomerAuthFromCookies } from '@open-mercato/core/modules/customer_accounts/lib/customerAuthServer'
53
-
54
- const auth = await getCustomerAuthFromCookies()
55
- if (!auth) redirect('/login')
56
- ```
57
-
58
- ### API Routes
59
- ```typescript
60
- import { requireCustomerAuth, requireCustomerFeature } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'
61
- import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
62
- import type { CustomerRbacService } from '@open-mercato/core/modules/customer_accounts/services/customerRbacService'
63
-
64
- // In your API handler:
65
- const auth = await requireCustomerAuth(request) // throws 401 if not authenticated
66
- const container = await createRequestContainer()
67
- const customerRbacService = container.resolve('customerRbacService') as CustomerRbacService
68
- // Re-resolves ACL on every request so role/feature revocation is immediate
69
- await requireCustomerFeature(auth, ['portal.orders.view'], customerRbacService) // throws 403 if missing
70
- ```
71
-
72
- ### RBAC Service
73
- ```typescript
74
- const rbacService = container.resolve('customerRbacService')
75
- const hasAccess = await rbacService.userHasAllFeatures(
76
- userId, ['portal.orders.view'], { tenantId, organizationId }
77
- )
78
- ```
79
-
80
- ## Portal Page Guards
81
-
82
- Use declarative metadata for portal pages:
83
-
84
- ```typescript
85
- export const metadata = {
86
- requireCustomerAuth: true,
87
- requireCustomerFeatures: ['portal.orders.view'],
88
- }
89
- ```
90
-
91
- ## Subscribing to Customer Events
92
-
93
- | Event | When |
94
- |-------|------|
95
- | `customer_accounts.user.created` | New customer signup |
96
- | `customer_accounts.user.updated` | Profile updated |
97
- | `customer_accounts.user.locked` | Account locked after failed logins |
98
- | `customer_accounts.login.success` | Successful login |
99
- | `customer_accounts.invitation.accepted` | Invitation accepted |
100
-
101
- ```typescript
102
- export const metadata = {
103
- event: 'customer_accounts.user.created',
104
- persistent: true,
105
- id: 'your-module-customer-signup',
106
- }
107
-
108
- export default async function handler(payload, ctx) {
109
- // React to customer signup — e.g., create default preferences
110
- }
111
- ```
112
-
113
- ## CRM Auto-Linking
114
-
115
- When a customer signs up, the module automatically searches for a matching CRM person by email and links them (`personEntityId`). The reverse also works — creating a CRM person auto-links to an existing customer user.
116
-
117
- ## Widget Injection Spots
118
-
119
- | Spot | Widget | Purpose |
120
- |------|--------|---------|
121
- | `crud-form:customers:customer_person_profile:fields` | Account status | Shows portal account status on CRM person detail |
122
- | `crud-form:customers:customer_company_profile:fields` | Company users | Shows portal users linked to a CRM company |
123
-
124
- ## Security Notes
125
-
126
- - All public endpoints are rate-limited (per-email + per-IP)
127
- - Tokens stored as SHA-256 hashes — raw tokens never persisted
128
- - Emails use deterministic hash for lookups (`hashForLookup`)
129
- - Error messages never confirm whether an email is registered
@@ -1,138 +0,0 @@
1
- # Customers Module — Reference CRUD Patterns
2
-
3
- This is the **reference CRUD module**. When building new modules in your standalone app, follow these patterns.
4
-
5
- ## CRUD API Pattern
6
-
7
- Use `makeCrudRoute` with `indexer: { entityType }` for query index coverage:
8
-
9
- ```typescript
10
- // src/modules/<your_module>/api/get/<entities>.ts
11
- import { makeCrudRoute } from '@open-mercato/shared/lib/crud/make-crud-route'
12
- import { YourEntity } from '../../entities/YourEntity'
13
-
14
- const handler = makeCrudRoute({
15
- entity: YourEntity,
16
- entityId: 'your_module.your_entity',
17
- operations: ['list', 'detail'],
18
- indexer: { entityType: 'your_module.your_entity' },
19
- })
20
-
21
- export default handler
22
- export const openApi = { summary: 'List and retrieve entities', tags: ['Your Module'] }
23
- ```
24
-
25
- Key points:
26
- - Always set `indexer: { entityType }` — keeps custom entities indexed
27
- - Wire custom field helpers for create/update if your module supports custom fields
28
- - Export `openApi` on every API route file
29
-
30
- ## Undoable Commands Pattern
31
-
32
- All write operations should use the Command pattern with undo support:
33
-
34
- ```typescript
35
- import { registerCommand } from '@open-mercato/shared/lib/commands'
36
- import { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'
37
-
38
- registerCommand('your_module.entity.create', {
39
- async execute(payload, ctx) {
40
- // 1. Create entity
41
- // 2. Capture snapshot for undo: extractUndoPayload(entity)
42
- // 3. Side effects: emitCrudSideEffects({ indexer: { entityType, cacheAliases } })
43
- },
44
- async undo(payload, ctx) {
45
- // 1. Restore from snapshot
46
- // 2. Side effects: emitCrudUndoSideEffects({ indexer: { entityType, cacheAliases } })
47
- },
48
- })
49
- ```
50
-
51
- Key points:
52
- - Include `indexer: { entityType, cacheAliases }` in both `emitCrudSideEffects` and `emitCrudUndoSideEffects`
53
- - Capture custom field snapshots in `before`/`after` payloads (`snapshot.custom`)
54
- - Restore custom fields via `buildCustomFieldResetMap(before.custom, after.custom)` in undo
55
-
56
- ## Custom Field Integration
57
-
58
- ```typescript
59
- import { collectCustomFieldValues } from '@open-mercato/ui/backend/utils/customFieldValues'
60
- ```
61
-
62
- - Pass `{ transform }` to normalize values (e.g., `normalizeCustomFieldSubmitValue`)
63
- - Works for both `cf_` and `cf:` prefixed keys
64
- - Pass `entityIds` to form helpers so correct custom-field sets are loaded
65
- - If your module ships default custom fields, declare them in `ce.ts` via `entities[].fields`
66
-
67
- ## Search Configuration
68
-
69
- Declare in `search.ts` with all three strategies:
70
-
71
- ```typescript
72
- import type { SearchModuleConfig } from '@open-mercato/shared/modules/search'
73
-
74
- export const searchConfig: SearchModuleConfig = {
75
- entities: {
76
- 'your_module.your_entity': {
77
- fields: ['name', 'description'], // Fulltext indexing
78
- // fieldPolicy for sensitive field handling
79
- // buildSource for vector embeddings
80
- // formatResult for search result display
81
- },
82
- },
83
- }
84
- ```
85
-
86
- Key points:
87
- - Use `fieldPolicy.excluded` for sensitive fields (passwords, tokens)
88
- - Use `fieldPolicy.hashOnly` for PII needing exact-match only (email, phone)
89
- - Always define `formatResult` for human-friendly search results
90
-
91
- ## Backend Page Structure
92
-
93
- Follow this pattern for each page type:
94
-
95
- | Page | Pattern | Key Features |
96
- |------|---------|-------------|
97
- | **List** | `DataTable` | Filters, search, export, row actions, pagination |
98
- | **Create** | `CrudForm` mode=create | Fields, groups, custom fields, back link |
99
- | **Detail/Edit** | `CrudForm` mode=edit or tabbed layout | Entity data, related entities, activities |
100
-
101
- ## Module Files Checklist
102
-
103
- When scaffolding a new CRUD module, ensure all these files are present:
104
-
105
- | File | Purpose |
106
- |------|---------|
107
- | `index.ts` | Module metadata |
108
- | `acl.ts` | Feature-based permissions |
109
- | `setup.ts` | Tenant init, default role features |
110
- | `di.ts` | Awilix DI registrations |
111
- | `events.ts` | Typed event declarations |
112
- | `data/entities.ts` | MikroORM entity classes |
113
- | `data/validators.ts` | Zod validation schemas |
114
- | `search.ts` | Search indexing configuration |
115
- | `ce.ts` | Custom entities / custom field sets |
116
-
117
- Optional:
118
- - `translations.ts` — translatable fields per entity
119
- - `notifications.ts` — notification type definitions
120
- - `cli.ts` — module CLI commands
121
-
122
- ## Entity Update Safety
123
-
124
- When mutating entities across multiple phases that include queries:
125
-
126
- ```typescript
127
- import { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'
128
-
129
- await withAtomicFlush(em, [
130
- () => { record.name = 'New'; record.status = 'active' },
131
- () => syncEntityTags(em, record, tags),
132
- ], { transaction: true })
133
-
134
- // Side effects AFTER the atomic flush
135
- await emitCrudSideEffects({ ... })
136
- ```
137
-
138
- Never run `em.find`/`em.findOne` between scalar mutations and `em.flush()` without `withAtomicFlush` — changes will be silently lost. Cache invalidation must also stay outside `withAtomicFlush` and fire after commit, so the opt-in `OM_CACHE_SAFETY_ALWAYS_CONSISTENT` mode (default OFF) never serves stale or partially-committed reads.
@@ -1,107 +0,0 @@
1
- # Data Sync Module — Standalone App Guide
2
-
3
- The data sync module provides a streaming synchronization hub for import/export operations with external systems. Provider modules register `DataSyncAdapter` implementations.
4
-
5
- ## Creating a Sync Adapter
6
-
7
- Implement the `DataSyncAdapter` interface in your provider module:
8
-
9
- ```typescript
10
- import type { DataSyncAdapter } from '@open-mercato/core/modules/data_sync/lib/adapter'
11
-
12
- const myAdapter: DataSyncAdapter = {
13
- providerKey: 'my_provider',
14
- direction: 'import', // 'import' | 'export' | 'bidirectional'
15
- supportedEntities: ['catalog.product', 'customers.person'],
16
-
17
- async *streamImport(entityType, cursor, config) {
18
- // Yield ImportBatch objects with records
19
- yield { records: [...], cursor: 'next-page-token' }
20
- },
21
-
22
- async validateConnection(credentials) {
23
- // Verify external system is reachable
24
- return { valid: true }
25
- },
26
-
27
- async getInitialCursor(entityType) {
28
- return null // Start from beginning
29
- },
30
- }
31
- ```
32
-
33
- Register in your module's `di.ts`:
34
- ```typescript
35
- import { registerDataSyncAdapter } from '@open-mercato/core/modules/data_sync/lib/adapter-registry'
36
- registerDataSyncAdapter(myAdapter)
37
- ```
38
-
39
- ## Run Lifecycle
40
-
41
- ```
42
- pending → running → completed | failed | cancelled
43
- ```
44
-
45
- - **Cursor persistence**: After each batch, cursor is saved — enables resume on failure
46
- - **Progress**: Linked to `ProgressJob` for live progress display via `ProgressTopBar`
47
- - **Cancellation**: Via `progressService.isCancellationRequested()`
48
- - **Overlap protection**: Only one sync per integration + entityType + direction at a time
49
-
50
- ## Key Services (DI)
51
-
52
- | Service | Purpose |
53
- |---------|---------|
54
- | `dataSyncRunService` | CRUD for sync runs, cursor management, overlap detection |
55
- | `dataSyncEngine` | Orchestrates streaming import/export with batch processing and progress |
56
- | `externalIdMappingService` | Maps local entity IDs to/from external system IDs |
57
-
58
- ## Starting a Sync
59
-
60
- Via API:
61
- ```
62
- POST /api/data_sync/run
63
- { "integrationId": "my_provider", "entityType": "catalog.product", "direction": "import" }
64
- ```
65
-
66
- Syncs run asynchronously via the queue system — never run inline in API handlers.
67
-
68
- ## Queue Workers
69
-
70
- | Queue | Worker | Concurrency |
71
- |-------|--------|-------------|
72
- | `data-sync-import` | Import handler | 5 |
73
- | `data-sync-export` | Export handler | 5 |
74
- | `data-sync-scheduled` | Scheduled sync dispatch | 3 |
75
-
76
- ## Events
77
-
78
- | Event | When |
79
- |-------|------|
80
- | `data_sync.run.started` | Sync begins processing |
81
- | `data_sync.run.completed` | Sync finishes successfully |
82
- | `data_sync.run.failed` | Sync fails |
83
- | `data_sync.run.cancelled` | Sync is cancelled |
84
-
85
- Subscribe to these events to trigger post-sync side effects in your module.
86
-
87
- ## UMES Extension Points
88
-
89
- Sync providers can extend the platform UI:
90
-
91
- | Extension | Use Case |
92
- |-----------|----------|
93
- | **Widget Injection** | Sync status badges, mapping previews on entity pages |
94
- | **Event Subscribers** | React to sync lifecycle events |
95
- | **Entity Extensions** | Link sync metadata to core entities |
96
- | **Response Enrichers** | Attach external ID data to API responses |
97
- | **Notifications** | Alerts on sync completion/failure |
98
- | **DOM Event Bridge** | Real-time sync progress via SSE |
99
- | **Menu Injection** | Provider-specific sync dashboards in sidebar |
100
-
101
- ## Key Rules
102
-
103
- - Always scope queries by `organizationId` + `tenantId`
104
- - Use the queue system — never run syncs inline
105
- - Persist cursor after each batch — enables resume on failure
106
- - Log item-level errors — don't stop the sync for individual failures
107
- - Check for overlap before starting a new run