@open-mercato/checkout 0.6.7-develop.6862.1.c11a64ce0a → 0.6.7

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 (63) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/checkout/api/pay/[slug]/submit/route.js +28 -43
  3. package/dist/modules/checkout/api/pay/[slug]/submit/route.js.map +2 -2
  4. package/dist/modules/checkout/api/transactions/route.js +6 -23
  5. package/dist/modules/checkout/api/transactions/route.js.map +2 -2
  6. package/dist/modules/checkout/backend/checkout/pay-links/page.js +1 -2
  7. package/dist/modules/checkout/backend/checkout/pay-links/page.js.map +2 -2
  8. package/dist/modules/checkout/backend/checkout/templates/page.js +1 -2
  9. package/dist/modules/checkout/backend/checkout/templates/page.js.map +2 -2
  10. package/dist/modules/checkout/backend/checkout/transactions/page.js +1 -2
  11. package/dist/modules/checkout/backend/checkout/transactions/page.js.map +2 -2
  12. package/dist/modules/checkout/commands/links.js +1 -1
  13. package/dist/modules/checkout/commands/links.js.map +2 -2
  14. package/dist/modules/checkout/commands/transactions.js +13 -45
  15. package/dist/modules/checkout/commands/transactions.js.map +2 -2
  16. package/dist/modules/checkout/components/PayPage.js +38 -39
  17. package/dist/modules/checkout/components/PayPage.js.map +2 -2
  18. package/dist/modules/checkout/search.js +6 -44
  19. package/dist/modules/checkout/search.js.map +2 -2
  20. package/dist/modules/checkout/setup.js +0 -28
  21. package/dist/modules/checkout/setup.js.map +2 -2
  22. package/dist/modules/checkout/workers/transaction-expiry.worker.js +2 -9
  23. package/dist/modules/checkout/workers/transaction-expiry.worker.js.map +2 -2
  24. package/package.json +9 -10
  25. package/src/modules/checkout/api/pay/[slug]/submit/__tests__/route.test.ts +0 -77
  26. package/src/modules/checkout/api/pay/[slug]/submit/route.ts +28 -58
  27. package/src/modules/checkout/api/transactions/route.ts +6 -30
  28. package/src/modules/checkout/backend/checkout/pay-links/page.tsx +1 -2
  29. package/src/modules/checkout/backend/checkout/templates/page.tsx +1 -2
  30. package/src/modules/checkout/backend/checkout/transactions/page.tsx +1 -2
  31. package/src/modules/checkout/commands/links.ts +1 -5
  32. package/src/modules/checkout/commands/transactions.ts +13 -68
  33. package/src/modules/checkout/components/PayPage.tsx +37 -38
  34. package/src/modules/checkout/i18n/de.json +0 -1
  35. package/src/modules/checkout/i18n/en.json +0 -1
  36. package/src/modules/checkout/i18n/es.json +0 -1
  37. package/src/modules/checkout/i18n/pl.json +0 -1
  38. package/src/modules/checkout/search.ts +6 -44
  39. package/src/modules/checkout/setup.ts +0 -51
  40. package/src/modules/checkout/workers/transaction-expiry.worker.ts +0 -11
  41. package/dist/modules/checkout/__integration__/TC-CHKT-042.spec.js +0 -39
  42. package/dist/modules/checkout/__integration__/TC-CHKT-042.spec.js.map +0 -7
  43. package/dist/modules/checkout/__integration__/TC-CHKT-043-concurrent-status-update.spec.js +0 -62
  44. package/dist/modules/checkout/__integration__/TC-CHKT-043-concurrent-status-update.spec.js.map +0 -7
  45. package/dist/modules/checkout/extension-points.js +0 -68
  46. package/dist/modules/checkout/extension-points.js.map +0 -7
  47. package/dist/modules/checkout/lib/transaction-status-machine.js +0 -31
  48. package/dist/modules/checkout/lib/transaction-status-machine.js.map +0 -7
  49. package/dist/modules/checkout/subscribers/search-reindex-transaction-created.js +0 -32
  50. package/dist/modules/checkout/subscribers/search-reindex-transaction-created.js.map +0 -7
  51. package/src/modules/checkout/__integration__/TC-CHKT-042.spec.ts +0 -41
  52. package/src/modules/checkout/__integration__/TC-CHKT-043-concurrent-status-update.spec.ts +0 -112
  53. package/src/modules/checkout/__tests__/search.test.ts +0 -21
  54. package/src/modules/checkout/__tests__/setup-schedules.test.ts +0 -99
  55. package/src/modules/checkout/api/transactions/__tests__/route.test.ts +0 -113
  56. package/src/modules/checkout/commands/__tests__/update-transaction-status.test.ts +0 -363
  57. package/src/modules/checkout/extension-points.ts +0 -67
  58. package/src/modules/checkout/i18n/ko.json +0 -463
  59. package/src/modules/checkout/lib/__tests__/transaction-status-machine.test.ts +0 -152
  60. package/src/modules/checkout/lib/transaction-status-machine.ts +0 -71
  61. package/src/modules/checkout/subscribers/__tests__/search-reindex-transaction-created.test.ts +0 -29
  62. package/src/modules/checkout/subscribers/search-reindex-transaction-created.ts +0 -39
  63. package/src/modules/checkout/workers/__tests__/transaction-expiry.worker.test.ts +0 -124
@@ -1,363 +0,0 @@
1
- /** @jest-environment node */
2
-
3
- /**
4
- * Tests for the state-machine guard added to updateTransactionStatusCommand.
5
- *
6
- * Exercises:
7
- * 1. A terminal → non-terminal transition is rejected with a 409 before the
8
- * nativeUpdate is even called.
9
- * 2. A non-terminal → terminal transition succeeds (nativeUpdate called with
10
- * the correct WHERE clause locking the current status).
11
- * 3. When nativeUpdate returns 0 (concurrent write won the race), the command
12
- * throws a 409 (concurrent_status_update) and does NOT proceed to emit
13
- * terminal events.
14
- * 4. When nativeUpdate returns 1, the command succeeds and emits the correct
15
- * terminal event.
16
- */
17
-
18
- import { commandRegistry } from '@open-mercato/shared/lib/commands/registry'
19
- import type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands/types'
20
- import { CheckoutTransaction } from '../../data/entities'
21
-
22
- // Register the transaction commands
23
- import '../transactions'
24
-
25
- jest.mock('../../events', () => ({
26
- emitCheckoutEvent: jest.fn(async () => undefined),
27
- }))
28
-
29
- const { emitCheckoutEvent } = jest.requireMock('../../events') as {
30
- emitCheckoutEvent: jest.Mock
31
- }
32
-
33
- const ORG_ID = '123e4567-e89b-12d3-a456-426614174000'
34
- const TENANT_ID = '123e4567-e89b-12d3-a456-426614174001'
35
- const LINK_ID = '123e4567-e89b-12d3-a456-426614174010'
36
- const TX_ID = '123e4567-e89b-12d3-a456-426614174020'
37
-
38
- function makeTransaction(status: CheckoutTransaction['status']) {
39
- return {
40
- id: TX_ID,
41
- linkId: LINK_ID,
42
- organizationId: ORG_ID,
43
- tenantId: TENANT_ID,
44
- status,
45
- paymentStatus: null,
46
- gatewayTransactionId: null,
47
- amount: '100.00',
48
- currencyCode: 'USD',
49
- createdAt: new Date(),
50
- updatedAt: new Date(),
51
- }
52
- }
53
-
54
- function makeLink() {
55
- return {
56
- id: LINK_ID,
57
- organizationId: ORG_ID,
58
- tenantId: TENANT_ID,
59
- slug: 'test-link',
60
- templateId: null,
61
- gatewayProviderKey: 'stripe',
62
- activeReservationCount: 1,
63
- completionCount: 0,
64
- maxCompletions: null,
65
- isLocked: false,
66
- deletedAt: null,
67
- }
68
- }
69
-
70
- type MockEm = {
71
- findOne: jest.Mock
72
- nativeUpdate: jest.Mock
73
- flush: jest.Mock
74
- refresh: jest.Mock
75
- transactional: (fn: (tx: MockEm) => Promise<unknown>) => Promise<unknown>
76
- }
77
-
78
- function makeMockEm(
79
- transaction: ReturnType<typeof makeTransaction>,
80
- nativeUpdateResult = 1,
81
- // When nativeUpdate returns 0, the post-CAS findOne should return a
82
- // different status to simulate the winning writer's value.
83
- postCasStatus?: CheckoutTransaction['status'],
84
- ): MockEm {
85
- let findOneCallCount = 0
86
- const mockTx: MockEm = {
87
- findOne: jest.fn(async (_entity: unknown, filter: Record<string, unknown>) => {
88
- findOneCallCount++
89
- if (filter.id === TX_ID) {
90
- // After a CAS miss (nativeUpdate=0) the command re-reads the row with
91
- // refresh:true to fetch the winning writer's status. Return the
92
- // post-CAS status on subsequent calls when one is provided.
93
- if (findOneCallCount > 1 && postCasStatus !== undefined) {
94
- return { ...transaction, status: postCasStatus }
95
- }
96
- return transaction
97
- }
98
- if (filter.id === LINK_ID) return makeLink()
99
- return null
100
- }),
101
- nativeUpdate: jest.fn(async () => nativeUpdateResult),
102
- flush: jest.fn(async () => undefined),
103
- refresh: jest.fn(async () => undefined),
104
- transactional: (fn) => fn(mockTx),
105
- }
106
- return mockTx
107
- }
108
-
109
- function makeContext(em: MockEm): CommandRuntimeContext {
110
- return {
111
- container: {
112
- resolve: (token: string) => {
113
- if (token === 'em') return em
114
- return null
115
- },
116
- } as unknown as CommandRuntimeContext['container'],
117
- auth: null,
118
- organizationScope: null,
119
- selectedOrganizationId: ORG_ID,
120
- organizationIds: [ORG_ID],
121
- }
122
- }
123
-
124
- function baseInput(status: string) {
125
- return {
126
- id: TX_ID,
127
- status,
128
- paymentStatus: null,
129
- gatewayTransactionId: null,
130
- organizationId: ORG_ID,
131
- tenantId: TENANT_ID,
132
- }
133
- }
134
-
135
- async function runUpdateStatus(em: MockEm, status: string) {
136
- const handler = commandRegistry.get('checkout.transaction.updateStatus')
137
- if (!handler) throw new Error('checkout.transaction.updateStatus not registered')
138
- return handler.execute(baseInput(status), makeContext(em))
139
- }
140
-
141
- describe('updateTransactionStatusCommand — state-machine guard', () => {
142
- beforeEach(() => {
143
- jest.clearAllMocks()
144
- })
145
-
146
- describe('valid transitions', () => {
147
- it('processing → completed: calls nativeUpdate with status=processing in the WHERE clause', async () => {
148
- const transaction = makeTransaction('processing')
149
- const em = makeMockEm(transaction, 1)
150
-
151
- await runUpdateStatus(em, 'completed')
152
-
153
- expect(em.nativeUpdate).toHaveBeenCalledWith(
154
- expect.anything(),
155
- expect.objectContaining({
156
- id: TX_ID,
157
- organizationId: ORG_ID,
158
- tenantId: TENANT_ID,
159
- status: 'processing', // pins the current status for atomicity
160
- }),
161
- expect.objectContaining({ status: 'completed' }),
162
- )
163
- })
164
-
165
- it('processing → completed: emits checkout.transaction.completed event', async () => {
166
- const transaction = makeTransaction('processing')
167
- const em = makeMockEm(transaction, 1)
168
-
169
- await runUpdateStatus(em, 'completed')
170
-
171
- expect(emitCheckoutEvent).toHaveBeenCalledWith(
172
- 'checkout.transaction.completed',
173
- expect.any(Object),
174
- )
175
- })
176
-
177
- it('pending → processing: nativeUpdate is called with correct WHERE', async () => {
178
- const transaction = makeTransaction('pending')
179
- const em = makeMockEm(transaction, 1)
180
-
181
- await runUpdateStatus(em, 'processing')
182
-
183
- expect(em.nativeUpdate).toHaveBeenCalledWith(
184
- expect.anything(),
185
- expect.objectContaining({ status: 'pending' }),
186
- expect.objectContaining({ status: 'processing' }),
187
- )
188
- })
189
-
190
- it('pending → processing: does NOT emit a terminal event', async () => {
191
- const transaction = makeTransaction('pending')
192
- const em = makeMockEm(transaction, 1)
193
-
194
- await runUpdateStatus(em, 'processing')
195
-
196
- expect(emitCheckoutEvent).not.toHaveBeenCalledWith(
197
- 'checkout.transaction.completed',
198
- expect.any(Object),
199
- )
200
- expect(emitCheckoutEvent).not.toHaveBeenCalledWith(
201
- 'checkout.transaction.failed',
202
- expect.any(Object),
203
- )
204
- })
205
- })
206
-
207
- describe('regression guard — terminal → non-terminal (the race condition)', () => {
208
- it('completed → processing: throws 409 with code invalid_status_transition', async () => {
209
- expect.assertions(3)
210
- const transaction = makeTransaction('completed')
211
- const em = makeMockEm(transaction, 1)
212
-
213
- try {
214
- await runUpdateStatus(em, 'processing')
215
- } catch (err) {
216
- const error = err as { status?: number; body?: Record<string, unknown> }
217
- expect(error.status).toBe(409)
218
- expect(error.body?.code).toBe('invalid_status_transition')
219
- expect(em.nativeUpdate).not.toHaveBeenCalled()
220
- }
221
- })
222
-
223
- it('completed → failed: throws 409', async () => {
224
- expect.assertions(2)
225
- const transaction = makeTransaction('completed')
226
- const em = makeMockEm(transaction, 1)
227
-
228
- try {
229
- await runUpdateStatus(em, 'failed')
230
- } catch (err) {
231
- const error = err as { status?: number; body?: Record<string, unknown> }
232
- expect(error.status).toBe(409)
233
- expect(em.nativeUpdate).not.toHaveBeenCalled()
234
- }
235
- })
236
-
237
- it('expired → processing: throws 409', async () => {
238
- expect.assertions(2)
239
- const transaction = makeTransaction('expired')
240
- const em = makeMockEm(transaction, 1)
241
-
242
- try {
243
- await runUpdateStatus(em, 'processing')
244
- } catch (err) {
245
- const error = err as { status?: number; body?: Record<string, unknown> }
246
- expect(error.status).toBe(409)
247
- expect(em.nativeUpdate).not.toHaveBeenCalled()
248
- }
249
- })
250
-
251
- it('failed → completed: throws 409', async () => {
252
- expect.assertions(2)
253
- const transaction = makeTransaction('failed')
254
- const em = makeMockEm(transaction, 1)
255
-
256
- try {
257
- await runUpdateStatus(em, 'completed')
258
- } catch (err) {
259
- const error = err as { status?: number; body?: Record<string, unknown> }
260
- expect(error.status).toBe(409)
261
- expect(em.nativeUpdate).not.toHaveBeenCalled()
262
- }
263
- })
264
- })
265
-
266
- describe('TOCTOU guard — nativeUpdate returns 0 (concurrent write wins)', () => {
267
- it('nativeUpdate=0 throws 409 with code concurrent_status_update', async () => {
268
- expect.assertions(5)
269
- // Transaction is still processing from our read, but another writer won
270
- // the race and set status to completed. The post-CAS re-read (refresh:true)
271
- // should return the winner's status, not the stale identity-mapped value.
272
- const transaction = makeTransaction('processing')
273
- const em = makeMockEm(transaction, 0, 'completed')
274
-
275
- try {
276
- await runUpdateStatus(em, 'completed')
277
- } catch (err) {
278
- const error = err as { status?: number; body?: Record<string, unknown> }
279
- expect(error.status).toBe(409)
280
- expect(error.body?.code).toBe('concurrent_status_update')
281
- expect(error.body?.expectedStatus).toBe('processing')
282
- // currentStatus must reflect the winning writer's value, not the stale snapshot
283
- expect(error.body?.currentStatus).toBe('completed')
284
- // The terminal event must NOT be emitted when the update was a no-op
285
- expect(emitCheckoutEvent).not.toHaveBeenCalledWith(
286
- 'checkout.transaction.completed',
287
- expect.any(Object),
288
- )
289
- }
290
- })
291
-
292
- it('nativeUpdate=0 does NOT emit any event', async () => {
293
- expect.assertions(1)
294
- const transaction = makeTransaction('processing')
295
- const em = makeMockEm(transaction, 0)
296
-
297
- try {
298
- await runUpdateStatus(em, 'completed')
299
- } catch {
300
- // Expected 409 — assert no events were emitted
301
- expect(emitCheckoutEvent).not.toHaveBeenCalled()
302
- }
303
- })
304
- })
305
-
306
- describe('same-state transitions (idempotency support)', () => {
307
- it('processing → processing: succeeds and calls nativeUpdate', async () => {
308
- const transaction = makeTransaction('processing')
309
- const em = makeMockEm(transaction, 1)
310
-
311
- await runUpdateStatus(em, 'processing')
312
-
313
- expect(em.nativeUpdate).toHaveBeenCalledWith(
314
- expect.anything(),
315
- expect.objectContaining({ status: 'processing' }),
316
- expect.objectContaining({ status: 'processing' }),
317
- )
318
- })
319
-
320
- it('completed → completed: succeeds and calls nativeUpdate', async () => {
321
- const transaction = makeTransaction('completed')
322
- const em = makeMockEm(transaction, 1)
323
-
324
- await runUpdateStatus(em, 'completed')
325
-
326
- expect(em.nativeUpdate).toHaveBeenCalledWith(
327
- expect.anything(),
328
- expect.objectContaining({ status: 'completed' }),
329
- expect.objectContaining({ status: 'completed' }),
330
- )
331
- })
332
-
333
- it('completed → completed: does NOT re-emit checkout.transaction.completed (idempotent redelivery guard)', async () => {
334
- const transaction = makeTransaction('completed')
335
- const em = makeMockEm(transaction, 1)
336
-
337
- await runUpdateStatus(em, 'completed')
338
-
339
- expect(emitCheckoutEvent).not.toHaveBeenCalledWith(
340
- 'checkout.transaction.completed',
341
- expect.any(Object),
342
- )
343
- })
344
-
345
- it('authorized → captured (both → completed): CAS succeeds but terminal event is NOT re-emitted', async () => {
346
- // Both payment_gateways.payment.authorized and .captured map to 'completed'.
347
- // The second delivery (captured) finds the transaction already completed;
348
- // the CAS still succeeds (same-state), but checkout.transaction.completed
349
- // must NOT fire a second time so the customer does not receive a duplicate notification.
350
- const transaction = makeTransaction('completed')
351
- const em = makeMockEm(transaction, 1)
352
-
353
- await runUpdateStatus(em, 'completed')
354
-
355
- expect(em.nativeUpdate).toHaveBeenCalledTimes(1)
356
- expect(emitCheckoutEvent).not.toHaveBeenCalledWith(
357
- 'checkout.transaction.completed',
358
- expect.any(Object),
359
- )
360
- })
361
- })
362
- })
363
-
@@ -1,67 +0,0 @@
1
- import {
2
- componentExtensionHost,
3
- dataTableExtensionHost,
4
- defineModuleExtensionPoints,
5
- injectionExtensionHost,
6
- } from '@open-mercato/shared/modules/widgets/extension-points'
7
-
8
- const payPageSpot = (spotId: string) => injectionExtensionHost({
9
- family: 'detail',
10
- spotId,
11
- supported: ['render-widget'],
12
- contextContract: 'checkout.pay-page.v1',
13
- source: 'components/PayPage.tsx',
14
- })
15
-
16
- const payPageComponent = (componentId: string) => componentExtensionHost({
17
- componentId,
18
- propsContract: 'checkout.pay-page.v1',
19
- source: 'components/PayPage.tsx',
20
- })
21
-
22
- export const extensionPoints = defineModuleExtensionPoints({
23
- moduleId: 'checkout',
24
- hosts: {
25
- linksTable: dataTableExtensionHost({ tableId: 'checkout-links', source: 'backend/checkout/pay-links/page.tsx' }),
26
- templatesTable: dataTableExtensionHost({ tableId: 'checkout-templates', source: 'backend/checkout/templates/page.tsx' }),
27
- transactionsTable: dataTableExtensionHost({ tableId: 'checkout-transactions', source: 'backend/checkout/transactions/page.tsx' }),
28
- gatewayWidgetBefore: payPageSpot('checkout.pay-page:gateway-widget:before'),
29
- gatewayRendererBefore: payPageSpot('checkout.pay-page:gateway-widget:renderer:before'),
30
- gatewayRendererAfter: payPageSpot('checkout.pay-page:gateway-widget:renderer:after'),
31
- gatewayActionsBefore: payPageSpot('checkout.pay-page:gateway-widget:actions:before'),
32
- gatewayActionsAfter: payPageSpot('checkout.pay-page:gateway-widget:actions:after'),
33
- gatewayWidgetAfter: payPageSpot('checkout.pay-page:gateway-widget:after'),
34
- headerBefore: payPageSpot('checkout.pay-page:header:before'),
35
- headerAfter: payPageSpot('checkout.pay-page:header:after'),
36
- descriptionAfter: payPageSpot('checkout.pay-page:description:after'),
37
- customerFieldsBefore: payPageSpot('checkout.pay-page:customer-fields:before'),
38
- customerFieldsAfter: payPageSpot('checkout.pay-page:customer-fields:after'),
39
- pricingBefore: payPageSpot('checkout.pay-page:pricing:before'),
40
- pricingAfter: payPageSpot('checkout.pay-page:pricing:after'),
41
- summaryBefore: payPageSpot('checkout.pay-page:summary:before'),
42
- summaryAfter: payPageSpot('checkout.pay-page:summary:after'),
43
- legalConsentBefore: payPageSpot('checkout.pay-page:legal-consent:before'),
44
- legalConsentAfter: payPageSpot('checkout.pay-page:legal-consent:after'),
45
- submitBefore: payPageSpot('checkout.pay-page:submit:before'),
46
- submitAfter: payPageSpot('checkout.pay-page:submit:after'),
47
- helpBefore: payPageSpot('checkout.pay-page:help:before'),
48
- helpAfter: payPageSpot('checkout.pay-page:help:after'),
49
- paymentBefore: payPageSpot('checkout.pay-page:payment:before'),
50
- paymentAfter: payPageSpot('checkout.pay-page:payment:after'),
51
- footerBefore: payPageSpot('checkout.pay-page:footer:before'),
52
- footerAfter: payPageSpot('checkout.pay-page:footer:after'),
53
- payPage: payPageComponent('page:checkout.pay-page'),
54
- header: payPageComponent('section:checkout.pay-page.header'),
55
- description: payPageComponent('section:checkout.pay-page.description'),
56
- summary: payPageComponent('section:checkout.pay-page.summary'),
57
- pricing: payPageComponent('section:checkout.pay-page.pricing'),
58
- payment: payPageComponent('section:checkout.pay-page.payment'),
59
- customerForm: payPageComponent('section:checkout.pay-page.customer-form'),
60
- legalConsent: payPageComponent('section:checkout.pay-page.legal-consent'),
61
- gatewayForm: payPageComponent('section:checkout.pay-page.gateway-form'),
62
- help: payPageComponent('section:checkout.pay-page.help'),
63
- footer: payPageComponent('section:checkout.pay-page.footer'),
64
- },
65
- })
66
-
67
- export default extensionPoints