@open-mercato/core 0.6.6-develop.6381.1.042df302c3 → 0.6.6-develop.6383.1.27fcc83455
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/agentic/standalone-guide.md +3 -38
- package/dist/modules/sales/commands/returns.js +3 -2
- package/dist/modules/sales/commands/returns.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/sales/commands/returns.ts +10 -2
- package/src/modules/auth/agentic/standalone-guide.md +0 -101
- package/src/modules/catalog/agentic/standalone-guide.md +0 -79
- package/src/modules/currencies/agentic/standalone-guide.md +0 -43
- package/src/modules/customer_accounts/agentic/standalone-guide.md +0 -129
- package/src/modules/customers/agentic/standalone-guide.md +0 -138
- package/src/modules/data_sync/agentic/standalone-guide.md +0 -107
- package/src/modules/integrations/agentic/standalone-guide.md +0 -113
- package/src/modules/sales/agentic/standalone-guide.md +0 -104
- package/src/modules/workflows/agentic/standalone-guide.md +0 -152
|
@@ -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
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
# Integrations Module — Standalone App Guide
|
|
2
|
-
|
|
3
|
-
The integrations module provides the foundation for all external connectors (payment gateways, shipping carriers, data sync providers, etc.). It offers three shared mechanisms: **Integration Registry**, **Credentials API**, and **Operation Logs**.
|
|
4
|
-
|
|
5
|
-
## Creating an Integration Provider
|
|
6
|
-
|
|
7
|
-
Create a new module in your app for each provider:
|
|
8
|
-
|
|
9
|
-
1. Create `src/modules/<provider_id>/` with standard module files
|
|
10
|
-
2. Add `integration.ts` at the module root exporting an `IntegrationDefinition`:
|
|
11
|
-
|
|
12
|
-
```typescript
|
|
13
|
-
import type { IntegrationDefinition } from '@open-mercato/shared/modules/integrations/types'
|
|
14
|
-
|
|
15
|
-
export const integration: IntegrationDefinition = {
|
|
16
|
-
id: 'my_provider',
|
|
17
|
-
name: 'My Provider',
|
|
18
|
-
description: 'Integration with My Provider',
|
|
19
|
-
category: 'payment',
|
|
20
|
-
credentials: {
|
|
21
|
-
fields: [
|
|
22
|
-
{ key: 'apiKey', label: 'API Key', type: 'password', required: true },
|
|
23
|
-
{ key: 'environment', label: 'Environment', type: 'select', options: ['sandbox', 'production'] },
|
|
24
|
-
],
|
|
25
|
-
},
|
|
26
|
-
healthCheck: { service: 'myProviderHealthCheck' }, // optional
|
|
27
|
-
apiVersions: ['v1', 'v2'], // optional
|
|
28
|
-
}
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
3. Register the health check service in `di.ts` (if declared)
|
|
32
|
-
4. Run `yarn generate` to auto-discover the integration
|
|
33
|
-
|
|
34
|
-
## Key Services (DI)
|
|
35
|
-
|
|
36
|
-
| Service | Purpose |
|
|
37
|
-
|---------|---------|
|
|
38
|
-
| `integrationCredentialsService` | Encrypted credential CRUD with bundle fallthrough |
|
|
39
|
-
| `integrationStateService` | Enable/disable, API version, reauth, health state |
|
|
40
|
-
| `integrationLogService` | Structured logging with scoped loggers |
|
|
41
|
-
| `integrationHealthService` | Resolves and runs provider health checks |
|
|
42
|
-
|
|
43
|
-
## Credential Resolution
|
|
44
|
-
|
|
45
|
-
1. Direct credentials for the integration ID
|
|
46
|
-
2. If `bundleId` is set, fallback to bundle's credentials
|
|
47
|
-
3. Returns `null` if neither exists
|
|
48
|
-
|
|
49
|
-
## Bundle Integrations
|
|
50
|
-
|
|
51
|
-
For platform connectors with multiple integrations (e.g., an ERP with products + orders sync):
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
export const bundle: IntegrationBundle = {
|
|
55
|
-
id: 'my_erp',
|
|
56
|
-
name: 'My ERP',
|
|
57
|
-
integrations: ['my_erp_products', 'my_erp_orders'],
|
|
58
|
-
}
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
- Set `bundleId` on each child integration
|
|
62
|
-
- Bundle credentials are shared via fallthrough
|
|
63
|
-
|
|
64
|
-
## Events
|
|
65
|
-
|
|
66
|
-
| Event | When |
|
|
67
|
-
|-------|------|
|
|
68
|
-
| `integrations.credentials.updated` | Credentials saved |
|
|
69
|
-
| `integrations.state.updated` | Integration enabled/disabled |
|
|
70
|
-
| `integrations.version.changed` | API version changed |
|
|
71
|
-
| `integrations.log.created` | Log entry written |
|
|
72
|
-
|
|
73
|
-
## Extending the Integration Detail Page
|
|
74
|
-
|
|
75
|
-
Provider modules can add tabs, cards, or sections to the integration detail page:
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
// integration.ts
|
|
79
|
-
import { buildIntegrationDetailWidgetSpotId } from '@open-mercato/shared/modules/integrations/types'
|
|
80
|
-
|
|
81
|
-
export const integration = {
|
|
82
|
-
id: 'my_provider',
|
|
83
|
-
detailPage: {
|
|
84
|
-
widgetSpotId: buildIntegrationDetailWidgetSpotId('my_provider'),
|
|
85
|
-
},
|
|
86
|
-
} satisfies IntegrationDefinition
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
Register widgets for that spot in `widgets/injection-table.ts`. Use `placement.kind: 'tab'` for additional tabs, `'group'` for card panels, `'stack'` for inline sections.
|
|
90
|
-
|
|
91
|
-
## UMES Extension Points
|
|
92
|
-
|
|
93
|
-
Integration providers can leverage the full extension system:
|
|
94
|
-
|
|
95
|
-
| Extension | Use Case |
|
|
96
|
-
|-----------|----------|
|
|
97
|
-
| **Widget Injection** | Inject status badges, config panels into other modules |
|
|
98
|
-
| **Event Subscribers** | React to integration events for side-effects |
|
|
99
|
-
| **Entity Extensions** | Link provider data to core entities (e.g., external IDs) |
|
|
100
|
-
| **Response Enrichers** | Attach provider data to API responses |
|
|
101
|
-
| **API Interceptors** | Intercept routes with before/after hooks |
|
|
102
|
-
| **Notifications** | In-app alerts on integration events |
|
|
103
|
-
| **DOM Event Bridge** | Real-time updates via SSE (`clientBroadcast: true`) |
|
|
104
|
-
|
|
105
|
-
## Provider-Owned Env Preconfiguration
|
|
106
|
-
|
|
107
|
-
If your provider needs credentials or settings after a fresh install:
|
|
108
|
-
|
|
109
|
-
- Read env vars in a provider-local helper (e.g., `lib/preset.ts`)
|
|
110
|
-
- Apply from your module's `setup.ts` for automatic tenant bootstrap
|
|
111
|
-
- Expose a CLI command for rerunning the bootstrap
|
|
112
|
-
- Use provider-prefixed env names (e.g., `OM_INTEGRATION_MYPROVIDER_*`)
|
|
113
|
-
- Persist through normal integration services — never special-case in core
|