@open-mercato/webhooks 0.9.1-develop.7459.1.99e3accd30 → 0.9.1-develop.7464.1.9a392fca41
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/modules/webhooks/workers/inbound-dispatch.js +5 -4
- package/dist/modules/webhooks/workers/inbound-dispatch.js.map +2 -2
- package/dist/modules/webhooks/workers/webhook-delivery.js +1 -0
- package/dist/modules/webhooks/workers/webhook-delivery.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/webhooks/workers/__tests__/inbound-dispatch.test.ts +142 -0
- package/src/modules/webhooks/workers/__tests__/webhook-delivery.test.ts +107 -14
- package/src/modules/webhooks/workers/inbound-dispatch.ts +13 -7
- package/src/modules/webhooks/workers/webhook-delivery.ts +9 -3
|
@@ -9,14 +9,15 @@ const metadata = {
|
|
|
9
9
|
async function handler(job, ctx) {
|
|
10
10
|
const em = ctx.resolve("em").fork();
|
|
11
11
|
try {
|
|
12
|
-
await processInboundDispatchJob(em, job.
|
|
12
|
+
await processInboundDispatchJob(em, job.payload, {
|
|
13
13
|
resolve: (name) => ctx.resolve(name)
|
|
14
14
|
});
|
|
15
15
|
} catch (error) {
|
|
16
16
|
logger.error("Inbound dispatch job processing failed", {
|
|
17
|
-
ingestionId: job.
|
|
18
|
-
sourceKey: job.
|
|
19
|
-
tenantId: job.
|
|
17
|
+
ingestionId: job.payload?.ingestionId,
|
|
18
|
+
sourceKey: job.payload?.sourceKey,
|
|
19
|
+
tenantId: job.payload?.tenantId,
|
|
20
|
+
organizationId: job.payload?.organizationId,
|
|
20
21
|
err: error
|
|
21
22
|
});
|
|
22
23
|
throw error;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/webhooks/workers/inbound-dispatch.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { processInboundDispatchJob, type InboundDispatchJob } from '../lib/inbound-dispatch'\n\nconst logger = createLogger('webhooks')\n\nexport const metadata = {\n queue: 'webhook-inbound-dispatch',\n id: 'webhooks:inbound-dispatch-worker',\n concurrency: 5,\n}\n\
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { processInboundDispatchJob, type InboundDispatchJob } from '../lib/inbound-dispatch'\n\nconst logger = createLogger('webhooks')\n\nexport const metadata: WorkerMeta = {\n queue: 'webhook-inbound-dispatch',\n id: 'webhooks:inbound-dispatch-worker',\n concurrency: 5,\n}\n\ntype HandlerContext = JobContext & {\n resolve: <T = unknown>(name: string) => T\n}\n\nexport default async function handler(\n job: QueuedJob<InboundDispatchJob>,\n ctx: HandlerContext,\n) {\n const em = (ctx.resolve('em') as EntityManager).fork()\n try {\n await processInboundDispatchJob(em, job.payload, {\n resolve: <T,>(name: string) => ctx.resolve(name) as T,\n })\n } catch (error) {\n logger.error('Inbound dispatch job processing failed', {\n ingestionId: job.payload?.ingestionId,\n sourceKey: job.payload?.sourceKey,\n tenantId: job.payload?.tenantId,\n organizationId: job.payload?.organizationId,\n err: error,\n })\n throw error\n }\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,oBAAoB;AAC7B,SAAS,iCAA0D;AAEnE,MAAM,SAAS,aAAa,UAAU;AAE/B,MAAM,WAAuB;AAAA,EAClC,OAAO;AAAA,EACP,IAAI;AAAA,EACJ,aAAa;AACf;AAMA,eAAO,QACL,KACA,KACA;AACA,QAAM,KAAM,IAAI,QAAQ,IAAI,EAAoB,KAAK;AACrD,MAAI;AACF,UAAM,0BAA0B,IAAI,IAAI,SAAS;AAAA,MAC/C,SAAS,CAAK,SAAiB,IAAI,QAAQ,IAAI;AAAA,IACjD,CAAC;AAAA,EACH,SAAS,OAAO;AACd,WAAO,MAAM,0CAA0C;AAAA,MACrD,aAAa,IAAI,SAAS;AAAA,MAC1B,WAAW,IAAI,SAAS;AAAA,MACxB,UAAU,IAAI,SAAS;AAAA,MACvB,gBAAgB,IAAI,SAAS;AAAA,MAC7B,KAAK;AAAA,IACP,CAAC;AACD,UAAM;AAAA,EACR;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/webhooks/workers/webhook-delivery.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { processWebhookDeliveryJob, type WebhookDeliveryJob } from '../lib/delivery'\n\nconst logger = createLogger('webhooks').child({ component: 'delivery' })\n\nexport const metadata = {\n queue: 'webhook-deliveries',\n id: 'webhooks:delivery-worker',\n concurrency: 10,\n}\n\
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { processWebhookDeliveryJob, type WebhookDeliveryJob } from '../lib/delivery'\n\nconst logger = createLogger('webhooks').child({ component: 'delivery' })\n\nexport const metadata: WorkerMeta = {\n queue: 'webhook-deliveries',\n id: 'webhooks:delivery-worker',\n concurrency: 10,\n}\n\ntype HandlerContext = JobContext & {\n resolve: <T = unknown>(name: string) => T\n}\n\nexport default async function handler(\n job: QueuedJob<WebhookDeliveryJob>,\n ctx: HandlerContext,\n) {\n const em = (ctx.resolve('em') as EntityManager).fork()\n try {\n await processWebhookDeliveryJob(em, job.payload, { resolver: ctx.resolve })\n } catch (error) {\n logger.error('Job processing failed', {\n deliveryId: job.payload?.deliveryId,\n tenantId: job.payload?.tenantId,\n organizationId: job.payload?.organizationId,\n err: error,\n })\n throw error\n }\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,oBAAoB;AAC7B,SAAS,iCAA0D;AAEnE,MAAM,SAAS,aAAa,UAAU,EAAE,MAAM,EAAE,WAAW,WAAW,CAAC;AAEhE,MAAM,WAAuB;AAAA,EAClC,OAAO;AAAA,EACP,IAAI;AAAA,EACJ,aAAa;AACf;AAMA,eAAO,QACL,KACA,KACA;AACA,QAAM,KAAM,IAAI,QAAQ,IAAI,EAAoB,KAAK;AACrD,MAAI;AACF,UAAM,0BAA0B,IAAI,IAAI,SAAS,EAAE,UAAU,IAAI,QAAQ,CAAC;AAAA,EAC5E,SAAS,OAAO;AACd,WAAO,MAAM,yBAAyB;AAAA,MACpC,YAAY,IAAI,SAAS;AAAA,MACzB,UAAU,IAAI,SAAS;AAAA,MACvB,gBAAgB,IAAI,SAAS;AAAA,MAC7B,KAAK;AAAA,IACP,CAAC;AACD,UAAM;AAAA,EACR;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/webhooks",
|
|
3
|
-
"version": "0.9.1-develop.
|
|
3
|
+
"version": "0.9.1-develop.7464.1.9a392fca41",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Webhooks module for Open Mercato — Standard Webhooks compliant outbound/inbound delivery",
|
|
6
6
|
"type": "module",
|
|
@@ -70,20 +70,20 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@open-mercato/cache": "0.9.1-develop.
|
|
74
|
-
"@open-mercato/core": "0.9.1-develop.
|
|
75
|
-
"@open-mercato/queue": "0.9.1-develop.
|
|
76
|
-
"@open-mercato/ui": "0.9.1-develop.
|
|
73
|
+
"@open-mercato/cache": "0.9.1-develop.7464.1.9a392fca41",
|
|
74
|
+
"@open-mercato/core": "0.9.1-develop.7464.1.9a392fca41",
|
|
75
|
+
"@open-mercato/queue": "0.9.1-develop.7464.1.9a392fca41",
|
|
76
|
+
"@open-mercato/ui": "0.9.1-develop.7464.1.9a392fca41",
|
|
77
77
|
"svix": "^1.99.1"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
80
|
"@mikro-orm/postgresql": "^7.0.14",
|
|
81
|
-
"@open-mercato/shared": "0.9.1-develop.
|
|
81
|
+
"@open-mercato/shared": "0.9.1-develop.7464.1.9a392fca41",
|
|
82
82
|
"react": "^19.0.0",
|
|
83
83
|
"react-dom": "^19.0.0"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
|
-
"@open-mercato/shared": "0.9.1-develop.
|
|
86
|
+
"@open-mercato/shared": "0.9.1-develop.7464.1.9a392fca41",
|
|
87
87
|
"@types/jest": "^30.0.0",
|
|
88
88
|
"@types/react": "^19.2.18",
|
|
89
89
|
"@types/react-dom": "^19.2.5",
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
+
import type { JobContext, QueuedJob } from '@open-mercato/queue'
|
|
3
|
+
import handler, { metadata } from '../inbound-dispatch'
|
|
4
|
+
import { WEBHOOK_INBOUND_DISPATCH_QUEUE } from '../../lib/queue'
|
|
5
|
+
import type { InboundDispatchJob } from '../../lib/inbound-dispatch'
|
|
6
|
+
|
|
7
|
+
const mockProcessInboundDispatchJob = jest.fn()
|
|
8
|
+
const mockLoggerError = jest.fn()
|
|
9
|
+
|
|
10
|
+
jest.mock('../../lib/inbound-dispatch', () => ({
|
|
11
|
+
processInboundDispatchJob: (...args: unknown[]) => mockProcessInboundDispatchJob(...args),
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
15
|
+
const mocked = {
|
|
16
|
+
debug: jest.fn(),
|
|
17
|
+
info: jest.fn(),
|
|
18
|
+
warn: jest.fn(),
|
|
19
|
+
error: (...args: unknown[]) => mockLoggerError(...args),
|
|
20
|
+
child: () => mocked,
|
|
21
|
+
}
|
|
22
|
+
return { createLogger: () => mocked }
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
type DispatchDeps = { resolve: <T = unknown>(name: string) => T }
|
|
26
|
+
|
|
27
|
+
function makeEm() {
|
|
28
|
+
const forked = { id: 'forked-em' } as unknown as EntityManager
|
|
29
|
+
const em = { fork: jest.fn(() => forked) } as unknown as EntityManager
|
|
30
|
+
return { em, forked }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeCtx(em: EntityManager) {
|
|
34
|
+
const resolved: string[] = []
|
|
35
|
+
const ctx: JobContext & DispatchDeps & { resolved: string[] } = {
|
|
36
|
+
jobId: 'job-1',
|
|
37
|
+
attemptNumber: 1,
|
|
38
|
+
queueName: WEBHOOK_INBOUND_DISPATCH_QUEUE,
|
|
39
|
+
resolved,
|
|
40
|
+
resolve: <T,>(name: string): T => {
|
|
41
|
+
resolved.push(name)
|
|
42
|
+
return (name === 'em' ? em : `resolved:${name}`) as T
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
return ctx
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function depsFromLastCall(): DispatchDeps {
|
|
49
|
+
const call = mockProcessInboundDispatchJob.mock.calls[0] as [EntityManager, InboundDispatchJob, DispatchDeps]
|
|
50
|
+
return call[2]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('webhooks inbound dispatch worker', () => {
|
|
54
|
+
const payload: InboundDispatchJob = {
|
|
55
|
+
ingestionId: 'ingestion-1',
|
|
56
|
+
sourceKey: 'stripe',
|
|
57
|
+
eventType: 'payment.succeeded',
|
|
58
|
+
tenantId: 'tenant-1',
|
|
59
|
+
organizationId: 'org-1',
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const job: QueuedJob<InboundDispatchJob> = {
|
|
63
|
+
id: 'job-1',
|
|
64
|
+
payload,
|
|
65
|
+
createdAt: '2026-10-07T00:00:00.000Z',
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
jest.clearAllMocks()
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('consumes the queue the dispatcher publishes to', () => {
|
|
73
|
+
expect(metadata.queue).toBe(WEBHOOK_INBOUND_DISPATCH_QUEUE)
|
|
74
|
+
expect(metadata.id).toBe('webhooks:inbound-dispatch-worker')
|
|
75
|
+
expect(metadata.concurrency).toBe(5)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('reads the job payload off the queue envelope and forks the em', async () => {
|
|
79
|
+
const { em, forked } = makeEm()
|
|
80
|
+
mockProcessInboundDispatchJob.mockResolvedValue(null)
|
|
81
|
+
|
|
82
|
+
await handler(job, makeCtx(em))
|
|
83
|
+
|
|
84
|
+
expect(em.fork).toHaveBeenCalledTimes(1)
|
|
85
|
+
expect(mockProcessInboundDispatchJob).toHaveBeenCalledWith(
|
|
86
|
+
forked,
|
|
87
|
+
payload,
|
|
88
|
+
expect.objectContaining({ resolve: expect.any(Function) }),
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('hands the handler a resolver that delegates to the worker context', async () => {
|
|
93
|
+
const { em } = makeEm()
|
|
94
|
+
const ctx = makeCtx(em)
|
|
95
|
+
mockProcessInboundDispatchJob.mockResolvedValue(null)
|
|
96
|
+
|
|
97
|
+
await handler(job, ctx)
|
|
98
|
+
|
|
99
|
+
expect(depsFromLastCall().resolve('eventBus')).toBe('resolved:eventBus')
|
|
100
|
+
expect(ctx.resolved).toContain('eventBus')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('re-throws the original error so the queue can retry', async () => {
|
|
104
|
+
const { em } = makeEm()
|
|
105
|
+
mockProcessInboundDispatchJob.mockRejectedValue(new Error('DB connection lost'))
|
|
106
|
+
|
|
107
|
+
await expect(handler(job, makeCtx(em))).rejects.toThrow('DB connection lost')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('logs the failing job identifiers instead of masking the error', async () => {
|
|
111
|
+
const { em } = makeEm()
|
|
112
|
+
const cause = new Error('handler blew up')
|
|
113
|
+
mockProcessInboundDispatchJob.mockRejectedValue(cause)
|
|
114
|
+
|
|
115
|
+
await expect(handler(job, makeCtx(em))).rejects.toBe(cause)
|
|
116
|
+
|
|
117
|
+
expect(mockLoggerError).toHaveBeenCalledWith(
|
|
118
|
+
'Inbound dispatch job processing failed',
|
|
119
|
+
expect.objectContaining({
|
|
120
|
+
ingestionId: 'ingestion-1',
|
|
121
|
+
sourceKey: 'stripe',
|
|
122
|
+
tenantId: 'tenant-1',
|
|
123
|
+
organizationId: 'org-1',
|
|
124
|
+
err: cause,
|
|
125
|
+
}),
|
|
126
|
+
)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('surfaces the original error even when the envelope carries no payload', async () => {
|
|
130
|
+
const { em } = makeEm()
|
|
131
|
+
const cause = new Error('handler blew up')
|
|
132
|
+
mockProcessInboundDispatchJob.mockRejectedValue(cause)
|
|
133
|
+
const payloadless = { id: 'job-1', createdAt: '2026-10-07T00:00:00.000Z' } as QueuedJob<InboundDispatchJob>
|
|
134
|
+
|
|
135
|
+
await expect(handler(payloadless, makeCtx(em))).rejects.toBe(cause)
|
|
136
|
+
|
|
137
|
+
expect(mockLoggerError).toHaveBeenCalledWith(
|
|
138
|
+
'Inbound dispatch job processing failed',
|
|
139
|
+
expect.objectContaining({ ingestionId: undefined, err: cause }),
|
|
140
|
+
)
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -1,46 +1,139 @@
|
|
|
1
1
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
-
import
|
|
2
|
+
import type { JobContext, QueuedJob } from '@open-mercato/queue'
|
|
3
|
+
import handler, { metadata } from '../webhook-delivery'
|
|
4
|
+
import { WEBHOOK_DELIVERIES_QUEUE } from '../../lib/queue'
|
|
5
|
+
import type { WebhookDeliveryJob } from '../../lib/delivery'
|
|
3
6
|
|
|
4
7
|
const mockProcessWebhookDeliveryJob = jest.fn()
|
|
8
|
+
const mockLoggerError = jest.fn()
|
|
5
9
|
|
|
6
10
|
jest.mock('../../lib/delivery', () => ({
|
|
7
11
|
processWebhookDeliveryJob: (...args: unknown[]) => mockProcessWebhookDeliveryJob(...args),
|
|
8
12
|
}))
|
|
9
13
|
|
|
14
|
+
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
15
|
+
const mocked = {
|
|
16
|
+
debug: jest.fn(),
|
|
17
|
+
info: jest.fn(),
|
|
18
|
+
warn: jest.fn(),
|
|
19
|
+
error: (...args: unknown[]) => mockLoggerError(...args),
|
|
20
|
+
child: () => mocked,
|
|
21
|
+
}
|
|
22
|
+
return { createLogger: () => mocked }
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
type DeliveryDeps = { resolver: <T = unknown>(name: string) => T }
|
|
26
|
+
|
|
27
|
+
function makeEm() {
|
|
28
|
+
const forked = { id: 'forked-em' } as unknown as EntityManager
|
|
29
|
+
const em = { fork: jest.fn(() => forked) } as unknown as EntityManager
|
|
30
|
+
return { em, forked }
|
|
31
|
+
}
|
|
32
|
+
|
|
10
33
|
function makeCtx(em: EntityManager) {
|
|
11
|
-
|
|
34
|
+
const resolved: string[] = []
|
|
35
|
+
const ctx: JobContext & { resolve: <T = unknown>(name: string) => T; resolved: string[] } = {
|
|
36
|
+
jobId: 'job-1',
|
|
37
|
+
attemptNumber: 1,
|
|
38
|
+
queueName: WEBHOOK_DELIVERIES_QUEUE,
|
|
39
|
+
resolved,
|
|
12
40
|
resolve: <T,>(name: string): T => {
|
|
13
|
-
|
|
14
|
-
|
|
41
|
+
resolved.push(name)
|
|
42
|
+
return (name === 'em' ? em : `resolved:${name}`) as T
|
|
15
43
|
},
|
|
16
44
|
}
|
|
45
|
+
return ctx
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function depsFromLastCall(): DeliveryDeps {
|
|
49
|
+
const call = mockProcessWebhookDeliveryJob.mock.calls[0] as [EntityManager, WebhookDeliveryJob, DeliveryDeps]
|
|
50
|
+
return call[2]
|
|
17
51
|
}
|
|
18
52
|
|
|
19
53
|
describe('webhooks delivery worker', () => {
|
|
20
|
-
const
|
|
54
|
+
const payload: WebhookDeliveryJob = {
|
|
55
|
+
deliveryId: 'delivery-1',
|
|
56
|
+
tenantId: 'tenant-1',
|
|
57
|
+
organizationId: 'org-1',
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const job: QueuedJob<WebhookDeliveryJob> = {
|
|
61
|
+
id: 'job-1',
|
|
62
|
+
payload,
|
|
63
|
+
createdAt: '2026-10-07T00:00:00.000Z',
|
|
64
|
+
}
|
|
21
65
|
|
|
22
66
|
afterEach(() => {
|
|
23
67
|
jest.clearAllMocks()
|
|
24
68
|
})
|
|
25
69
|
|
|
26
|
-
it('
|
|
27
|
-
|
|
70
|
+
it('consumes the queue the dispatcher publishes to', () => {
|
|
71
|
+
expect(metadata.queue).toBe(WEBHOOK_DELIVERIES_QUEUE)
|
|
72
|
+
expect(metadata.id).toBe('webhooks:delivery-worker')
|
|
73
|
+
expect(metadata.concurrency).toBe(10)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('reads the job payload off the queue envelope and forks the em', async () => {
|
|
77
|
+
const { em, forked } = makeEm()
|
|
28
78
|
mockProcessWebhookDeliveryJob.mockResolvedValue(null)
|
|
29
79
|
|
|
30
|
-
await handler(
|
|
80
|
+
await handler(job, makeCtx(em))
|
|
31
81
|
|
|
82
|
+
expect(em.fork).toHaveBeenCalledTimes(1)
|
|
32
83
|
expect(mockProcessWebhookDeliveryJob).toHaveBeenCalledWith(
|
|
33
|
-
|
|
34
|
-
|
|
84
|
+
forked,
|
|
85
|
+
payload,
|
|
35
86
|
expect.objectContaining({ resolver: expect.any(Function) }),
|
|
36
87
|
)
|
|
37
88
|
})
|
|
38
89
|
|
|
39
|
-
it('
|
|
40
|
-
const em =
|
|
41
|
-
const
|
|
90
|
+
it('hands the delivery a resolver that delegates to the worker context', async () => {
|
|
91
|
+
const { em } = makeEm()
|
|
92
|
+
const ctx = makeCtx(em)
|
|
93
|
+
mockProcessWebhookDeliveryJob.mockResolvedValue(null)
|
|
94
|
+
|
|
95
|
+
await handler(job, ctx)
|
|
96
|
+
|
|
97
|
+
expect(depsFromLastCall().resolver('eventBus')).toBe('resolved:eventBus')
|
|
98
|
+
expect(ctx.resolved).toContain('eventBus')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('re-throws the original error so the queue can retry', async () => {
|
|
102
|
+
const { em } = makeEm()
|
|
103
|
+
mockProcessWebhookDeliveryJob.mockRejectedValue(new Error('DB connection lost'))
|
|
104
|
+
|
|
105
|
+
await expect(handler(job, makeCtx(em))).rejects.toThrow('DB connection lost')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('logs the failing job identifiers instead of masking the error', async () => {
|
|
109
|
+
const { em } = makeEm()
|
|
110
|
+
const cause = new Error('delivery blew up')
|
|
111
|
+
mockProcessWebhookDeliveryJob.mockRejectedValue(cause)
|
|
112
|
+
|
|
113
|
+
await expect(handler(job, makeCtx(em))).rejects.toBe(cause)
|
|
114
|
+
|
|
115
|
+
expect(mockLoggerError).toHaveBeenCalledWith(
|
|
116
|
+
'Job processing failed',
|
|
117
|
+
expect.objectContaining({
|
|
118
|
+
deliveryId: 'delivery-1',
|
|
119
|
+
tenantId: 'tenant-1',
|
|
120
|
+
organizationId: 'org-1',
|
|
121
|
+
err: cause,
|
|
122
|
+
}),
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('surfaces the original error even when the envelope carries no payload', async () => {
|
|
127
|
+
const { em } = makeEm()
|
|
128
|
+
const cause = new Error('delivery blew up')
|
|
42
129
|
mockProcessWebhookDeliveryJob.mockRejectedValue(cause)
|
|
130
|
+
const payloadless = { id: 'job-1', createdAt: '2026-10-07T00:00:00.000Z' } as QueuedJob<WebhookDeliveryJob>
|
|
131
|
+
|
|
132
|
+
await expect(handler(payloadless, makeCtx(em))).rejects.toBe(cause)
|
|
43
133
|
|
|
44
|
-
|
|
134
|
+
expect(mockLoggerError).toHaveBeenCalledWith(
|
|
135
|
+
'Job processing failed',
|
|
136
|
+
expect.objectContaining({ deliveryId: undefined, err: cause }),
|
|
137
|
+
)
|
|
45
138
|
})
|
|
46
139
|
})
|
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
+
import type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'
|
|
2
3
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
3
4
|
import { processInboundDispatchJob, type InboundDispatchJob } from '../lib/inbound-dispatch'
|
|
4
5
|
|
|
5
6
|
const logger = createLogger('webhooks')
|
|
6
7
|
|
|
7
|
-
export const metadata = {
|
|
8
|
+
export const metadata: WorkerMeta = {
|
|
8
9
|
queue: 'webhook-inbound-dispatch',
|
|
9
10
|
id: 'webhooks:inbound-dispatch-worker',
|
|
10
11
|
concurrency: 5,
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
type HandlerContext = JobContext & {
|
|
15
|
+
resolve: <T = unknown>(name: string) => T
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
export default async function handler(
|
|
14
|
-
job:
|
|
15
|
-
ctx:
|
|
19
|
+
job: QueuedJob<InboundDispatchJob>,
|
|
20
|
+
ctx: HandlerContext,
|
|
16
21
|
) {
|
|
17
22
|
const em = (ctx.resolve('em') as EntityManager).fork()
|
|
18
23
|
try {
|
|
19
|
-
await processInboundDispatchJob(em, job.
|
|
24
|
+
await processInboundDispatchJob(em, job.payload, {
|
|
20
25
|
resolve: <T,>(name: string) => ctx.resolve(name) as T,
|
|
21
26
|
})
|
|
22
27
|
} catch (error) {
|
|
23
28
|
logger.error('Inbound dispatch job processing failed', {
|
|
24
|
-
ingestionId: job.
|
|
25
|
-
sourceKey: job.
|
|
26
|
-
tenantId: job.
|
|
29
|
+
ingestionId: job.payload?.ingestionId,
|
|
30
|
+
sourceKey: job.payload?.sourceKey,
|
|
31
|
+
tenantId: job.payload?.tenantId,
|
|
32
|
+
organizationId: job.payload?.organizationId,
|
|
27
33
|
err: error,
|
|
28
34
|
})
|
|
29
35
|
throw error
|
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
+
import type { JobContext, QueuedJob, WorkerMeta } from '@open-mercato/queue'
|
|
2
3
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
3
4
|
import { processWebhookDeliveryJob, type WebhookDeliveryJob } from '../lib/delivery'
|
|
4
5
|
|
|
5
6
|
const logger = createLogger('webhooks').child({ component: 'delivery' })
|
|
6
7
|
|
|
7
|
-
export const metadata = {
|
|
8
|
+
export const metadata: WorkerMeta = {
|
|
8
9
|
queue: 'webhook-deliveries',
|
|
9
10
|
id: 'webhooks:delivery-worker',
|
|
10
11
|
concurrency: 10,
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
type HandlerContext = JobContext & {
|
|
15
|
+
resolve: <T = unknown>(name: string) => T
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
export default async function handler(
|
|
14
|
-
job:
|
|
15
|
-
ctx:
|
|
19
|
+
job: QueuedJob<WebhookDeliveryJob>,
|
|
20
|
+
ctx: HandlerContext,
|
|
16
21
|
) {
|
|
17
22
|
const em = (ctx.resolve('em') as EntityManager).fork()
|
|
18
23
|
try {
|
|
@@ -21,6 +26,7 @@ export default async function handler(
|
|
|
21
26
|
logger.error('Job processing failed', {
|
|
22
27
|
deliveryId: job.payload?.deliveryId,
|
|
23
28
|
tenantId: job.payload?.tenantId,
|
|
29
|
+
organizationId: job.payload?.organizationId,
|
|
24
30
|
err: error,
|
|
25
31
|
})
|
|
26
32
|
throw error
|