create-mercato-app 0.6.8-develop.6942.1.dd382caa0f → 0.6.8-develop.6943.1.ed2e2cf6f6
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/dist/agentic/guides/module-facts.json +112 -112
- package/dist/agentic/guides/module-facts.v2.json +112 -112
- package/dist/agentic/guides/modules/ai_assistant.md +1 -1
- package/dist/agentic/guides/modules/api_docs.md +1 -1
- package/dist/agentic/guides/modules/api_keys.md +1 -1
- package/dist/agentic/guides/modules/attachments.md +1 -1
- package/dist/agentic/guides/modules/audit_logs.md +1 -1
- package/dist/agentic/guides/modules/auth.md +1 -1
- package/dist/agentic/guides/modules/business_rules.md +1 -1
- package/dist/agentic/guides/modules/catalog.md +1 -1
- package/dist/agentic/guides/modules/channel_gmail.md +1 -1
- package/dist/agentic/guides/modules/channel_imap.md +1 -1
- package/dist/agentic/guides/modules/checkout.md +1 -1
- package/dist/agentic/guides/modules/communication_channels.md +1 -1
- package/dist/agentic/guides/modules/configs.md +1 -1
- package/dist/agentic/guides/modules/content.md +1 -1
- package/dist/agentic/guides/modules/currencies.md +1 -1
- package/dist/agentic/guides/modules/customer_accounts.md +1 -1
- package/dist/agentic/guides/modules/customers.md +1 -1
- package/dist/agentic/guides/modules/dashboards.md +1 -1
- package/dist/agentic/guides/modules/data_sync.md +1 -1
- package/dist/agentic/guides/modules/design_system.md +1 -1
- package/dist/agentic/guides/modules/dictionaries.md +1 -1
- package/dist/agentic/guides/modules/directory.md +1 -1
- package/dist/agentic/guides/modules/entities.md +1 -1
- package/dist/agentic/guides/modules/eudr.md +1 -1
- package/dist/agentic/guides/modules/events.md +1 -1
- package/dist/agentic/guides/modules/feature_toggles.md +1 -1
- package/dist/agentic/guides/modules/gateway_stripe.md +1 -1
- package/dist/agentic/guides/modules/generators.md +1 -1
- package/dist/agentic/guides/modules/inbox_ops.md +1 -1
- package/dist/agentic/guides/modules/integrations.md +1 -1
- package/dist/agentic/guides/modules/messages.md +1 -1
- package/dist/agentic/guides/modules/notifications.md +1 -1
- package/dist/agentic/guides/modules/onboarding.md +1 -1
- package/dist/agentic/guides/modules/payment_gateways.md +1 -1
- package/dist/agentic/guides/modules/perspectives.md +1 -1
- package/dist/agentic/guides/modules/planner.md +1 -1
- package/dist/agentic/guides/modules/portal.md +1 -1
- package/dist/agentic/guides/modules/progress.md +1 -1
- package/dist/agentic/guides/modules/query_index.md +1 -1
- package/dist/agentic/guides/modules/record_locks.md +1 -1
- package/dist/agentic/guides/modules/resources.md +1 -1
- package/dist/agentic/guides/modules/sales.md +1 -1
- package/dist/agentic/guides/modules/scheduler.md +1 -1
- package/dist/agentic/guides/modules/search.md +1 -1
- package/dist/agentic/guides/modules/security.md +1 -1
- package/dist/agentic/guides/modules/shipping_carriers.md +1 -1
- package/dist/agentic/guides/modules/sso.md +1 -1
- package/dist/agentic/guides/modules/staff.md +1 -1
- package/dist/agentic/guides/modules/storage_s3.md +1 -1
- package/dist/agentic/guides/modules/sync_akeneo.md +1 -1
- package/dist/agentic/guides/modules/sync_excel.md +1 -1
- package/dist/agentic/guides/modules/system_status_overlays.md +1 -1
- package/dist/agentic/guides/modules/translations.md +1 -1
- package/dist/agentic/guides/modules/webhooks.md +1 -1
- package/dist/agentic/guides/modules/wms.md +1 -1
- package/dist/agentic/guides/modules/workflows.md +1 -1
- package/dist/agentic/guides/reference-module-facts.json +2 -2
- package/dist/agentic/guides/reference-modules/example.md +1 -1
- package/dist/agentic/guides/upstream/manifest.json +1 -1
- package/package.json +3 -3
- package/template/src/modules/example/__integration__/TC-UMES-003.spec.ts +15 -22
- package/template/src/modules/example/api/assignees/__tests__/route.test.ts +126 -0
- package/template/src/modules/example/api/assignees/route.ts +50 -6
- package/template/src/modules/example_customers_sync/lib/__tests__/inbound-subscriber.scope.test.ts +82 -0
- package/template/src/modules/example_customers_sync/lib/__tests__/sync-scope.test.ts +87 -0
- package/template/src/modules/example_customers_sync/lib/inbound-subscriber.ts +7 -3
- package/template/src/modules/example_customers_sync/lib/sync.ts +5 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** @jest-environment node */
|
|
2
|
+
|
|
3
|
+
const emitEvent = jest.fn(async () => undefined)
|
|
4
|
+
const getAuthFromRequest = jest.fn()
|
|
5
|
+
|
|
6
|
+
jest.mock('@open-mercato/shared/lib/auth/server', () => ({
|
|
7
|
+
getAuthFromCookies: jest.fn(),
|
|
8
|
+
getAuthFromRequest: (...args: unknown[]) => getAuthFromRequest(...(args as [])),
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
jest.mock('@open-mercato/shared/lib/di/container', () => ({
|
|
12
|
+
createRequestContainer: jest.fn(async () => ({
|
|
13
|
+
resolve: (name: string) => {
|
|
14
|
+
if (name === 'eventBus') return { emitEvent }
|
|
15
|
+
throw new Error(`Unexpected container resolve: ${name}`)
|
|
16
|
+
},
|
|
17
|
+
})),
|
|
18
|
+
}))
|
|
19
|
+
|
|
20
|
+
import { POST } from '../route'
|
|
21
|
+
|
|
22
|
+
const tenantId = '11111111-1111-4111-8111-111111111111'
|
|
23
|
+
const organizationId = '22222222-2222-4222-8222-222222222222'
|
|
24
|
+
const foreignOrganizationId = '33333333-3333-4333-8333-333333333333'
|
|
25
|
+
const userId = '44444444-4444-4444-8444-444444444444'
|
|
26
|
+
const foreignUserId = '55555555-5555-4555-8555-555555555555'
|
|
27
|
+
|
|
28
|
+
function makeRequest(data: Record<string, unknown>): Request {
|
|
29
|
+
return new Request('http://localhost/api/example/assignees', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: { 'content-type': 'application/json' },
|
|
32
|
+
body: JSON.stringify(data),
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('POST /api/example/assignees event probe scope', () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
jest.clearAllMocks()
|
|
39
|
+
getAuthFromRequest.mockResolvedValue({
|
|
40
|
+
sub: userId,
|
|
41
|
+
tenantId,
|
|
42
|
+
orgId: organizationId,
|
|
43
|
+
roles: ['admin'],
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it.each([
|
|
48
|
+
['foreign organization', { organizationId: foreignOrganizationId }],
|
|
49
|
+
['foreign organization list', { organizationIds: [organizationId, foreignOrganizationId] }],
|
|
50
|
+
['foreign recipient user', { recipientUserId: foreignUserId }],
|
|
51
|
+
['foreign recipient user list', { recipientUserIds: [userId, foreignUserId] }],
|
|
52
|
+
['foreign recipient role', { recipientRoleId: 'employee' }],
|
|
53
|
+
['foreign recipient role list', { recipientRoleIds: ['admin', 'employee'] }],
|
|
54
|
+
])('rejects %s targeting without emitting', async (_label, targeting) => {
|
|
55
|
+
const response = await POST(makeRequest({
|
|
56
|
+
eventId: 'example.todo.updated',
|
|
57
|
+
payload: { probeId: 'scope-probe' },
|
|
58
|
+
...targeting,
|
|
59
|
+
}))
|
|
60
|
+
|
|
61
|
+
expect(response.status).toBe(403)
|
|
62
|
+
expect(emitEvent).not.toHaveBeenCalled()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it.each([
|
|
66
|
+
['tenant scope', { tenantId: foreignOrganizationId }],
|
|
67
|
+
['organization scope', { organizationId: foreignOrganizationId }],
|
|
68
|
+
['organization list', { organizationIds: [foreignOrganizationId] }],
|
|
69
|
+
['recipient user', { recipientUserId: foreignUserId }],
|
|
70
|
+
['recipient user list', { recipientUserIds: [foreignUserId] }],
|
|
71
|
+
['recipient role', { recipientRoleId: 'employee' }],
|
|
72
|
+
['recipient role list', { recipientRoleIds: ['employee'] }],
|
|
73
|
+
['todo identity', { id: 'forged-todo-id' }],
|
|
74
|
+
])('rejects nested payload-authored %s', async (_label, reservedPayload) => {
|
|
75
|
+
const response = await POST(makeRequest({
|
|
76
|
+
eventId: 'example.todo.deleted',
|
|
77
|
+
payload: { probeId: 'nested-scope-probe', ...reservedPayload },
|
|
78
|
+
}))
|
|
79
|
+
|
|
80
|
+
expect(response.status).toBe(403)
|
|
81
|
+
expect(emitEvent).not.toHaveBeenCalled()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('emits valid same-scope probes with trusted scope and no persistent inline subscribers', async () => {
|
|
85
|
+
const response = await POST(makeRequest({
|
|
86
|
+
eventId: 'example.todo.updated',
|
|
87
|
+
organizationId,
|
|
88
|
+
organizationIds: [organizationId],
|
|
89
|
+
recipientUserId: userId,
|
|
90
|
+
recipientUserIds: [userId],
|
|
91
|
+
recipientRoleId: 'admin',
|
|
92
|
+
recipientRoleIds: ['admin'],
|
|
93
|
+
payload: { probeId: 'valid-scope-probe' },
|
|
94
|
+
}))
|
|
95
|
+
|
|
96
|
+
expect(response.status).toBe(200)
|
|
97
|
+
expect(emitEvent).toHaveBeenCalledWith(
|
|
98
|
+
'example.todo.updated',
|
|
99
|
+
{
|
|
100
|
+
probeId: 'valid-scope-probe',
|
|
101
|
+
tenantId,
|
|
102
|
+
organizationId,
|
|
103
|
+
organizationIds: [organizationId],
|
|
104
|
+
recipientUserId: userId,
|
|
105
|
+
recipientUserIds: [userId],
|
|
106
|
+
recipientRoleId: 'admin',
|
|
107
|
+
recipientRoleIds: ['admin'],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
persistent: false,
|
|
111
|
+
skipPersistentSubscribersInline: true,
|
|
112
|
+
tenantId,
|
|
113
|
+
organizationId,
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('requires an authenticated organization scope', async () => {
|
|
119
|
+
getAuthFromRequest.mockResolvedValue({ sub: userId, tenantId, orgId: null, roles: ['admin'] })
|
|
120
|
+
|
|
121
|
+
const response = await POST(makeRequest({ eventId: 'example.todo.updated' }))
|
|
122
|
+
|
|
123
|
+
expect(response.status).toBe(403)
|
|
124
|
+
expect(emitEvent).not.toHaveBeenCalled()
|
|
125
|
+
})
|
|
126
|
+
})
|
|
@@ -3,6 +3,7 @@ import { getAuthFromCookies, getAuthFromRequest } from '@open-mercato/shared/lib
|
|
|
3
3
|
import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
|
|
4
4
|
import { readJsonSafe } from '@open-mercato/shared/lib/http/readJsonSafe'
|
|
5
5
|
import type { OpenApiMethodDoc, OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
6
|
+
import type { EventBus } from '@open-mercato/events/types'
|
|
6
7
|
import {
|
|
7
8
|
assigneeQuerySchema,
|
|
8
9
|
exampleErrorSchema,
|
|
@@ -64,6 +65,25 @@ const emitSseTestSchema = z.object({
|
|
|
64
65
|
recipientRoleIds: z.array(z.string().min(1)).optional(),
|
|
65
66
|
})
|
|
66
67
|
|
|
68
|
+
const reservedProbePayloadKeys = new Set([
|
|
69
|
+
'id',
|
|
70
|
+
'tenantId',
|
|
71
|
+
'organizationId',
|
|
72
|
+
'organizationIds',
|
|
73
|
+
'recipientUserId',
|
|
74
|
+
'recipientUserIds',
|
|
75
|
+
'recipientRoleId',
|
|
76
|
+
'recipientRoleIds',
|
|
77
|
+
'syncOrigin',
|
|
78
|
+
])
|
|
79
|
+
|
|
80
|
+
function forbiddenResponse(): Response {
|
|
81
|
+
return new Response(JSON.stringify({ error: 'Forbidden' }), {
|
|
82
|
+
status: 403,
|
|
83
|
+
headers: { 'content-type': 'application/json' },
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
67
87
|
export async function POST(req: Request) {
|
|
68
88
|
const auth = await getAuthFromRequest(req)
|
|
69
89
|
if (!auth?.sub || !auth.tenantId) {
|
|
@@ -72,6 +92,7 @@ export async function POST(req: Request) {
|
|
|
72
92
|
headers: { 'content-type': 'application/json' },
|
|
73
93
|
})
|
|
74
94
|
}
|
|
95
|
+
if (!auth.orgId) return forbiddenResponse()
|
|
75
96
|
|
|
76
97
|
const rawBody = await readJsonSafe<unknown>(req, {})
|
|
77
98
|
const parsed = emitSseTestSchema.safeParse(rawBody)
|
|
@@ -82,15 +103,32 @@ export async function POST(req: Request) {
|
|
|
82
103
|
})
|
|
83
104
|
}
|
|
84
105
|
|
|
106
|
+
const payload = parsed.data.payload ?? {}
|
|
107
|
+
if (Object.keys(payload).some((key) => reservedProbePayloadKeys.has(key))) {
|
|
108
|
+
return forbiddenResponse()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const actorRoles = new Set(
|
|
112
|
+
Array.isArray(auth.roles)
|
|
113
|
+
? auth.roles.filter((role): role is string => typeof role === 'string')
|
|
114
|
+
: [],
|
|
115
|
+
)
|
|
116
|
+
const hasForbiddenTarget = (
|
|
117
|
+
typeof parsed.data.organizationId === 'string' && parsed.data.organizationId !== auth.orgId
|
|
118
|
+
) || parsed.data.organizationIds?.some((organizationId) => organizationId !== auth.orgId)
|
|
119
|
+
|| (typeof parsed.data.recipientUserId === 'string' && parsed.data.recipientUserId !== auth.sub)
|
|
120
|
+
|| parsed.data.recipientUserIds?.some((userId) => userId !== auth.sub)
|
|
121
|
+
|| (typeof parsed.data.recipientRoleId === 'string' && !actorRoles.has(parsed.data.recipientRoleId))
|
|
122
|
+
|| parsed.data.recipientRoleIds?.some((roleId) => !actorRoles.has(roleId))
|
|
123
|
+
if (hasForbiddenTarget) return forbiddenResponse()
|
|
124
|
+
|
|
85
125
|
const container = await createRequestContainer()
|
|
86
|
-
const eventBus = container.resolve<
|
|
87
|
-
emitEvent: (event: string, payload: Record<string, unknown>, options?: { persistent?: boolean }) => Promise<void>
|
|
88
|
-
}>('eventBus')
|
|
126
|
+
const eventBus = container.resolve<EventBus>('eventBus')
|
|
89
127
|
|
|
90
128
|
const eventPayload: Record<string, unknown> = {
|
|
91
|
-
...
|
|
129
|
+
...payload,
|
|
92
130
|
tenantId: auth.tenantId,
|
|
93
|
-
organizationId:
|
|
131
|
+
organizationId: auth.orgId,
|
|
94
132
|
}
|
|
95
133
|
|
|
96
134
|
if (Array.isArray(parsed.data.organizationIds) && parsed.data.organizationIds.length > 0) {
|
|
@@ -109,7 +147,12 @@ export async function POST(req: Request) {
|
|
|
109
147
|
eventPayload.recipientRoleIds = parsed.data.recipientRoleIds
|
|
110
148
|
}
|
|
111
149
|
|
|
112
|
-
await eventBus.emitEvent(parsed.data.eventId, eventPayload, {
|
|
150
|
+
await eventBus.emitEvent(parsed.data.eventId, eventPayload, {
|
|
151
|
+
persistent: false,
|
|
152
|
+
skipPersistentSubscribersInline: true,
|
|
153
|
+
tenantId: auth.tenantId,
|
|
154
|
+
organizationId: auth.orgId,
|
|
155
|
+
})
|
|
113
156
|
|
|
114
157
|
return new Response(JSON.stringify({ ok: true, eventId: parsed.data.eventId, payload: eventPayload }), {
|
|
115
158
|
headers: { 'content-type': 'application/json' },
|
|
@@ -148,6 +191,7 @@ const assigneesPostDoc: OpenApiMethodDoc = {
|
|
|
148
191
|
errors: [
|
|
149
192
|
{ status: 400, description: 'Invalid payload', schema: exampleErrorSchema },
|
|
150
193
|
{ status: 401, description: 'Authentication required', schema: exampleErrorSchema },
|
|
194
|
+
{ status: 403, description: 'Targeting outside the authenticated scope is forbidden', schema: exampleErrorSchema },
|
|
151
195
|
],
|
|
152
196
|
}
|
|
153
197
|
|
package/template/src/modules/example_customers_sync/lib/__tests__/inbound-subscriber.scope.test.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const enqueue = jest.fn(async () => undefined)
|
|
2
|
+
const resolveExampleCustomersSyncFlags = jest.fn(async () => ({ enabled: true, bidirectional: true }))
|
|
3
|
+
|
|
4
|
+
jest.mock('../toggles', () => ({
|
|
5
|
+
resolveExampleCustomersSyncFlags: (...args: unknown[]) => resolveExampleCustomersSyncFlags(...args),
|
|
6
|
+
}))
|
|
7
|
+
|
|
8
|
+
jest.mock('../queue', () => ({
|
|
9
|
+
EXAMPLE_CUSTOMERS_SYNC_INBOUND_QUEUE: 'example-customers-sync-inbound',
|
|
10
|
+
getExampleCustomersSyncQueue: () => ({ enqueue }),
|
|
11
|
+
}))
|
|
12
|
+
|
|
13
|
+
import { createInboundSubscriber } from '../inbound-subscriber'
|
|
14
|
+
|
|
15
|
+
describe('example customers sync inbound subscriber scope', () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
jest.clearAllMocks()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('rejects payload scope that disagrees with trusted subscriber context', async () => {
|
|
21
|
+
const handler = createInboundSubscriber('example.todo.deleted')
|
|
22
|
+
|
|
23
|
+
await handler(
|
|
24
|
+
{
|
|
25
|
+
id: 'todo-1',
|
|
26
|
+
tenantId: 'payload-tenant',
|
|
27
|
+
organizationId: 'payload-organization',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
resolve: jest.fn(),
|
|
31
|
+
tenantId: 'trusted-tenant',
|
|
32
|
+
organizationId: 'trusted-organization',
|
|
33
|
+
},
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
expect(resolveExampleCustomersSyncFlags).not.toHaveBeenCalled()
|
|
37
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('queues only the matching trusted subscriber scope', async () => {
|
|
41
|
+
const handler = createInboundSubscriber('example.todo.deleted')
|
|
42
|
+
|
|
43
|
+
await handler(
|
|
44
|
+
{
|
|
45
|
+
id: 'todo-1',
|
|
46
|
+
tenantId: 'trusted-tenant',
|
|
47
|
+
organizationId: 'trusted-organization',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
resolve: jest.fn(),
|
|
51
|
+
tenantId: 'trusted-tenant',
|
|
52
|
+
organizationId: 'trusted-organization',
|
|
53
|
+
},
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
expect(resolveExampleCustomersSyncFlags).toHaveBeenCalledWith(
|
|
57
|
+
expect.objectContaining({ tenantId: 'trusted-tenant', organizationId: 'trusted-organization' }),
|
|
58
|
+
'trusted-tenant',
|
|
59
|
+
)
|
|
60
|
+
expect(enqueue).toHaveBeenCalledWith({
|
|
61
|
+
eventId: 'example.todo.deleted',
|
|
62
|
+
todoId: 'todo-1',
|
|
63
|
+
tenantId: 'trusted-tenant',
|
|
64
|
+
organizationId: 'trusted-organization',
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('rejects events without trusted subscriber scope', async () => {
|
|
69
|
+
const handler = createInboundSubscriber('example.todo.deleted')
|
|
70
|
+
|
|
71
|
+
await handler(
|
|
72
|
+
{
|
|
73
|
+
id: 'todo-1',
|
|
74
|
+
tenantId: 'payload-tenant',
|
|
75
|
+
organizationId: 'payload-organization',
|
|
76
|
+
},
|
|
77
|
+
{ resolve: jest.fn() },
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
expect(enqueue).not.toHaveBeenCalled()
|
|
81
|
+
})
|
|
82
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const execute = jest.fn(async () => ({ interactionId: 'interaction-1' }))
|
|
2
|
+
const findMappingByTodoId = jest.fn()
|
|
3
|
+
const deleteExampleCustomerInteractionMapping = jest.fn(async () => true)
|
|
4
|
+
|
|
5
|
+
jest.mock('../toggles', () => ({
|
|
6
|
+
resolveExampleCustomersSyncFlags: jest.fn(async () => ({ enabled: true, bidirectional: true })),
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
jest.mock('../mappings', () => ({
|
|
10
|
+
findMappingByTodoId: (...args: unknown[]) => findMappingByTodoId(...args),
|
|
11
|
+
deleteExampleCustomerInteractionMapping: (...args: unknown[]) =>
|
|
12
|
+
deleteExampleCustomerInteractionMapping(...args),
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
jest.mock('../runtime', () => ({
|
|
16
|
+
buildExampleCustomersSyncCommandContext: jest.fn(() => ({
|
|
17
|
+
tenantId: 'tenant-1',
|
|
18
|
+
organizationId: 'organization-1',
|
|
19
|
+
})),
|
|
20
|
+
EXAMPLE_CUSTOMERS_SYNC_INBOUND_ORIGIN: 'example-customers-sync-inbound',
|
|
21
|
+
EXAMPLE_CUSTOMERS_SYNC_OUTBOUND_ORIGIN: 'example-customers-sync-outbound',
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
jest.mock('../../events', () => ({
|
|
25
|
+
emitExampleCustomersSyncEvent: jest.fn(async () => undefined),
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
import { syncExampleTodoToCanonicalInteraction } from '../sync'
|
|
29
|
+
|
|
30
|
+
function makeContainer() {
|
|
31
|
+
const entityManager = { fork: jest.fn(() => entityManager) }
|
|
32
|
+
return {
|
|
33
|
+
resolve: (name: string) => {
|
|
34
|
+
if (name === 'em') return entityManager
|
|
35
|
+
if (name === 'commandBus') return { execute }
|
|
36
|
+
if (name === 'eventBus') return { emitEvent: jest.fn(async () => undefined) }
|
|
37
|
+
throw new Error(`Unexpected container resolve: ${name}`)
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('example customers sync worker scope', () => {
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
jest.clearAllMocks()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('rejects a mapping whose scope does not match the queued delete job', async () => {
|
|
48
|
+
findMappingByTodoId.mockResolvedValue({
|
|
49
|
+
interactionId: 'interaction-1',
|
|
50
|
+
todoId: 'todo-1',
|
|
51
|
+
tenantId: 'tenant-1',
|
|
52
|
+
organizationId: 'foreign-organization',
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
await syncExampleTodoToCanonicalInteraction(makeContainer(), {
|
|
56
|
+
eventId: 'example.todo.deleted',
|
|
57
|
+
todoId: 'todo-1',
|
|
58
|
+
tenantId: 'tenant-1',
|
|
59
|
+
organizationId: 'organization-1',
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
expect(execute).not.toHaveBeenCalled()
|
|
63
|
+
expect(deleteExampleCustomerInteractionMapping).not.toHaveBeenCalled()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('preserves valid same-scope mapped deletion', async () => {
|
|
67
|
+
const mapping = {
|
|
68
|
+
interactionId: 'interaction-1',
|
|
69
|
+
todoId: 'todo-1',
|
|
70
|
+
tenantId: 'tenant-1',
|
|
71
|
+
organizationId: 'organization-1',
|
|
72
|
+
}
|
|
73
|
+
findMappingByTodoId.mockResolvedValue(mapping)
|
|
74
|
+
|
|
75
|
+
await syncExampleTodoToCanonicalInteraction(makeContainer(), {
|
|
76
|
+
eventId: 'example.todo.deleted',
|
|
77
|
+
todoId: 'todo-1',
|
|
78
|
+
tenantId: 'tenant-1',
|
|
79
|
+
organizationId: 'organization-1',
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
expect(execute).toHaveBeenCalledWith('customers.interactions.delete', expect.objectContaining({
|
|
83
|
+
input: { body: { id: 'interaction-1' } },
|
|
84
|
+
}))
|
|
85
|
+
expect(deleteExampleCustomerInteractionMapping).toHaveBeenCalledWith(expect.anything(), mapping)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
@@ -4,6 +4,8 @@ import { resolveExampleCustomersSyncFlags } from '../lib/toggles'
|
|
|
4
4
|
|
|
5
5
|
type ResolverContext = {
|
|
6
6
|
resolve: <T = unknown>(name: string) => T
|
|
7
|
+
tenantId?: string | null
|
|
8
|
+
organizationId?: string | null
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
type InboundPayload = {
|
|
@@ -16,14 +18,16 @@ type InboundPayload = {
|
|
|
16
18
|
export function createInboundSubscriber(eventName: string) {
|
|
17
19
|
return async function handle(payload: InboundPayload, ctx: ResolverContext): Promise<void> {
|
|
18
20
|
if (!shouldEnqueueInboundSync(payload)) return
|
|
19
|
-
|
|
21
|
+
if (typeof ctx.tenantId !== 'string' || typeof ctx.organizationId !== 'string') return
|
|
22
|
+
if (payload.tenantId !== ctx.tenantId || payload.organizationId !== ctx.organizationId) return
|
|
23
|
+
const flags = await resolveExampleCustomersSyncFlags(ctx, ctx.tenantId)
|
|
20
24
|
if (!flags.enabled || !flags.bidirectional) return
|
|
21
25
|
const queue = getExampleCustomersSyncQueue(EXAMPLE_CUSTOMERS_SYNC_INBOUND_QUEUE)
|
|
22
26
|
await queue.enqueue({
|
|
23
27
|
eventId: eventName,
|
|
24
28
|
todoId: payload.id,
|
|
25
|
-
tenantId:
|
|
26
|
-
organizationId:
|
|
29
|
+
tenantId: ctx.tenantId,
|
|
30
|
+
organizationId: ctx.organizationId,
|
|
27
31
|
})
|
|
28
32
|
}
|
|
29
33
|
}
|
|
@@ -621,6 +621,11 @@ export async function syncExampleTodoToCanonicalInteraction(
|
|
|
621
621
|
mapping = await ensureMappingForLegacyExampleTodo(container, scope, payload.todoId)
|
|
622
622
|
}
|
|
623
623
|
if (!mapping) return
|
|
624
|
+
if (
|
|
625
|
+
mapping.tenantId !== scope.tenantId
|
|
626
|
+
|| mapping.organizationId !== scope.organizationId
|
|
627
|
+
|| mapping.todoId !== payload.todoId
|
|
628
|
+
) return
|
|
624
629
|
|
|
625
630
|
try {
|
|
626
631
|
const commandBus = container.resolve('commandBus') as CommandBus
|