@open-mercato/checkout 0.6.5-develop.4534.1.b459babe6d → 0.6.5-develop.4559.1.839e136509

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 (22) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/checkout/__integration__/TC-CHKT-039-null-gateway-edit.spec.js +118 -0
  3. package/dist/modules/checkout/__integration__/TC-CHKT-039-null-gateway-edit.spec.js.map +7 -0
  4. package/dist/modules/checkout/__integration__/TC-CHKT-039-stale-delete-lock.spec.js +66 -0
  5. package/dist/modules/checkout/__integration__/TC-CHKT-039-stale-delete-lock.spec.js.map +7 -0
  6. package/dist/modules/checkout/commands/links.js +13 -0
  7. package/dist/modules/checkout/commands/links.js.map +2 -2
  8. package/dist/modules/checkout/commands/templates.js +13 -0
  9. package/dist/modules/checkout/commands/templates.js.map +2 -2
  10. package/dist/modules/checkout/components/LinkTemplateForm.js +27 -7
  11. package/dist/modules/checkout/components/LinkTemplateForm.js.map +2 -2
  12. package/dist/modules/checkout/data/validators.js +11 -2
  13. package/dist/modules/checkout/data/validators.js.map +2 -2
  14. package/package.json +5 -5
  15. package/src/modules/checkout/__integration__/TC-CHKT-039-null-gateway-edit.spec.ts +134 -0
  16. package/src/modules/checkout/__integration__/TC-CHKT-039-stale-delete-lock.spec.ts +98 -0
  17. package/src/modules/checkout/commands/__tests__/optimistic-lock.test.ts +261 -0
  18. package/src/modules/checkout/commands/links.ts +13 -0
  19. package/src/modules/checkout/commands/templates.ts +13 -0
  20. package/src/modules/checkout/components/LinkTemplateForm.tsx +27 -7
  21. package/src/modules/checkout/data/__tests__/validators.test.ts +59 -1
  22. package/src/modules/checkout/data/validators.ts +11 -2
@@ -5,6 +5,7 @@ import { registerCommand } from '@open-mercato/shared/lib/commands'
5
5
  import { buildCustomFieldResetMap, loadCustomFieldSnapshot } from '@open-mercato/shared/lib/commands/customFieldSnapshots'
6
6
  import { setCustomFieldsIfAny } from '@open-mercato/shared/lib/commands/helpers'
7
7
  import { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'
8
+ import { enforceCommandOptimisticLock } from '@open-mercato/shared/lib/crud/optimistic-lock-command'
8
9
  import { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
9
10
  import { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'
10
11
  import { CheckoutLink, CheckoutLinkTemplate } from '../data/entities'
@@ -226,6 +227,12 @@ const updateTemplateCommand: CommandHandler<Record<string, unknown>, { ok: true
226
227
  deletedAt: null,
227
228
  }, undefined, scope)
228
229
  if (!template) throw new CrudHttpError(404, { error: 'Template not found' })
230
+ enforceCommandOptimisticLock({
231
+ resourceKind: 'checkout.template',
232
+ resourceId: template.id,
233
+ current: template.updatedAt ?? null,
234
+ request: ctx.request ?? null,
235
+ })
229
236
  const beforeCustom = await loadCustomFieldSnapshot(em, {
230
237
  entityId: CHECKOUT_ENTITY_IDS.template,
231
238
  recordId: template.id,
@@ -376,6 +383,12 @@ const deleteTemplateCommand: CommandHandler<Record<string, unknown>, { ok: true
376
383
  deletedAt: null,
377
384
  }, undefined, scope)
378
385
  if (!template) throw new CrudHttpError(404, { error: 'Template not found' })
386
+ enforceCommandOptimisticLock({
387
+ resourceKind: 'checkout.template',
388
+ resourceId: template.id,
389
+ current: template.updatedAt ?? null,
390
+ request: ctx.request ?? null,
391
+ })
379
392
  template.deletedAt = new Date()
380
393
  await em.flush()
381
394
  await emitCheckoutEvent('checkout.template.deleted', {
@@ -24,7 +24,9 @@ import { CrudForm, type CrudField, type CrudFormGroup, type CrudFormGroupCompone
24
24
  import { ComboboxInput, type ComboboxOption } from '@open-mercato/ui/backend/inputs'
25
25
  import { Page, PageBody } from '@open-mercato/ui/backend/Page'
26
26
  import { SwitchableMarkdownInput } from '@open-mercato/ui/backend/inputs'
27
- import { apiCall, apiCallOrThrow, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'
27
+ import { apiCall, apiCallOrThrow, readApiResultOrThrow, withScopedApiRequestHeaders } from '@open-mercato/ui/backend/utils/apiCall'
28
+ import { buildOptimisticLockHeader } from '@open-mercato/ui/backend/utils/optimisticLock'
29
+ import { surfaceRecordConflict } from '@open-mercato/ui/backend/conflicts'
28
30
  import { collectCustomFieldValues } from '@open-mercato/ui/backend/utils/customFieldValues'
29
31
  import { Button } from '@open-mercato/ui/primitives/button'
30
32
  import { ColorPicker } from '@open-mercato/ui/primitives/color-picker'
@@ -260,6 +262,7 @@ function normalizeFormValues(value: FormValues | null | undefined, t?: Translate
260
262
  primaryColor: readString(source.primaryColor).trim() || defaults.primaryColor,
261
263
  secondaryColor: readString(source.secondaryColor).trim() || defaults.secondaryColor,
262
264
  backgroundColor: readString(source.backgroundColor).trim() || defaults.backgroundColor,
265
+ gatewayProviderKey: readString(source.gatewayProviderKey).trim(),
263
266
  gatewaySettings: isRecord(source.gatewaySettings) ? source.gatewaySettings : {},
264
267
  customFieldsetCode: readString(source.customFieldsetCode).trim() || null,
265
268
  collectCustomerDetails: readBoolean(source.collectCustomerDetails, true),
@@ -1638,11 +1641,20 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1638
1641
  customFields: collectCustomFieldValues(values),
1639
1642
  }
1640
1643
  const endpoint = `/api/checkout/${mode === 'link' ? 'links' : 'templates'}${recordId ? `/${encodeURIComponent(recordId)}` : ''}`
1641
- const response = await readApiResultOrThrow<{ id?: string; slug?: string; ok?: boolean }>(endpoint, {
1642
- method: recordId ? 'PUT' : 'POST',
1643
- headers: { 'Content-Type': 'application/json' },
1644
- body: JSON.stringify(payload),
1645
- })
1644
+ let response: { id?: string; slug?: string; ok?: boolean }
1645
+ try {
1646
+ response = await withScopedApiRequestHeaders(
1647
+ recordId ? buildOptimisticLockHeader(readString(initialValues?.updatedAt) || null) : {},
1648
+ () => readApiResultOrThrow<{ id?: string; slug?: string; ok?: boolean }>(endpoint, {
1649
+ method: recordId ? 'PUT' : 'POST',
1650
+ headers: { 'Content-Type': 'application/json' },
1651
+ body: JSON.stringify(payload),
1652
+ }),
1653
+ )
1654
+ } catch (error) {
1655
+ if (surfaceRecordConflict(error, t)) return
1656
+ throw error
1657
+ }
1646
1658
  const targetId = recordId ?? (typeof response?.id === 'string' ? response.id : null)
1647
1659
  const logoAttachmentId = readString(values.logoAttachmentId)
1648
1660
  if (
@@ -1687,7 +1699,15 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1687
1699
  : `/backend/checkout/templates?flash=${encodeURIComponent(t('checkout.common.flash.saved'))}&type=success`
1688
1700
  }}
1689
1701
  onDelete={recordId ? async () => {
1690
- await apiCallOrThrow(`/api/checkout/${mode === 'link' ? 'links' : 'templates'}/${encodeURIComponent(recordId)}`, { method: 'DELETE' })
1702
+ try {
1703
+ await withScopedApiRequestHeaders(
1704
+ buildOptimisticLockHeader(readString(initialValues?.updatedAt) || null),
1705
+ () => apiCallOrThrow(`/api/checkout/${mode === 'link' ? 'links' : 'templates'}/${encodeURIComponent(recordId)}`, { method: 'DELETE' }),
1706
+ )
1707
+ } catch (error) {
1708
+ if (surfaceRecordConflict(error, t)) return
1709
+ throw error
1710
+ }
1691
1711
  window.location.href = mode === 'link'
1692
1712
  ? `/backend/checkout/pay-links?flash=${encodeURIComponent(t('checkout.common.flash.deleted'))}&type=success`
1693
1713
  : `/backend/checkout/templates?flash=${encodeURIComponent(t('checkout.common.flash.deleted'))}&type=success`
@@ -1,4 +1,11 @@
1
- import { createLinkSchema, createTemplateSchema } from '../validators'
1
+ import {
2
+ createLinkSchema,
3
+ createTemplateSchema,
4
+ updateLinkSchema,
5
+ updateTemplateSchema,
6
+ } from '../validators'
7
+
8
+ const TEMPLATE_ID = '020b29c5-db01-4ee3-8080-1d9b185c5e29'
2
9
 
3
10
  describe('checkout validators', () => {
4
11
  test('createTemplateSchema accepts payloads that omit optional normalized fields', () => {
@@ -44,4 +51,55 @@ describe('checkout validators', () => {
44
51
  expect(result.slug).toBeNull()
45
52
  expect(result.password).toBeNull()
46
53
  })
54
+
55
+ test('updateTemplateSchema accepts a null gatewayProviderKey (issue #2505)', () => {
56
+ const result = updateTemplateSchema.parse({
57
+ id: TEMPLATE_ID,
58
+ name: 'Consulting Fee',
59
+ pricingMode: 'fixed',
60
+ fixedPriceAmount: 49.99,
61
+ fixedPriceCurrencyCode: 'USD',
62
+ gatewayProviderKey: null,
63
+ })
64
+
65
+ expect(result.gatewayProviderKey).toBeNull()
66
+ })
67
+
68
+ test('updateTemplateSchema normalizes a blank gatewayProviderKey to null (issue #2505)', () => {
69
+ const result = updateTemplateSchema.parse({
70
+ id: TEMPLATE_ID,
71
+ name: 'Consulting Fee',
72
+ pricingMode: 'fixed',
73
+ fixedPriceAmount: 49.99,
74
+ fixedPriceCurrencyCode: 'USD',
75
+ gatewayProviderKey: ' ',
76
+ })
77
+
78
+ expect(result.gatewayProviderKey).toBeNull()
79
+ })
80
+
81
+ test('updateLinkSchema accepts a null gatewayProviderKey (issue #2505)', () => {
82
+ const result = updateLinkSchema.parse({
83
+ id: TEMPLATE_ID,
84
+ name: 'Consulting Fee link',
85
+ pricingMode: 'fixed',
86
+ fixedPriceAmount: 49.99,
87
+ fixedPriceCurrencyCode: 'USD',
88
+ gatewayProviderKey: null,
89
+ })
90
+
91
+ expect(result.gatewayProviderKey).toBeNull()
92
+ })
93
+
94
+ test('createTemplateSchema still rejects a null gatewayProviderKey', () => {
95
+ expect(() =>
96
+ createTemplateSchema.parse({
97
+ name: 'QA template',
98
+ pricingMode: 'fixed',
99
+ fixedPriceAmount: 49.99,
100
+ fixedPriceCurrencyCode: 'USD',
101
+ gatewayProviderKey: null,
102
+ }),
103
+ ).toThrow()
104
+ })
47
105
  })
@@ -109,7 +109,7 @@ const checkoutContentSchema = z.object({
109
109
  customAmountMax: positiveMoneySchema.optional().nullable(),
110
110
  customAmountCurrencyCode: currencyCodeSchema.optional().nullable(),
111
111
  priceListItems: z.array(priceListItemSchema).optional().nullable(),
112
- gatewayProviderKey: requiredTrimmedString('checkout.validation.gatewayProviderKey.required'),
112
+ gatewayProviderKey: optionalTrimmedString,
113
113
  gatewaySettings: gatewaySettingsSchema,
114
114
  customFieldsetCode: optionalFieldsetCodeSchema,
115
115
  collectCustomerDetails: z.boolean().default(true),
@@ -174,7 +174,16 @@ function validatePricingConsistency<T extends z.infer<typeof checkoutContentSche
174
174
  }
175
175
  }
176
176
 
177
- export const createTemplateSchema = checkoutContentSchema.superRefine(validatePricingConsistency)
177
+ export const createTemplateSchema = checkoutContentSchema.superRefine((value, ctx) => {
178
+ validatePricingConsistency(value, ctx)
179
+ if (!value.gatewayProviderKey) {
180
+ ctx.addIssue({
181
+ code: z.ZodIssueCode.custom,
182
+ message: 'checkout.validation.gatewayProviderKey.required',
183
+ path: ['gatewayProviderKey'],
184
+ })
185
+ }
186
+ })
178
187
 
179
188
  function applyPartialPricingConsistency(value: Record<string, unknown>, ctx: z.RefinementCtx) {
180
189
  if (!value.pricingMode) return