@open-mercato/checkout 0.6.5-develop.5187.1.82e5532561 → 0.6.5-develop.5212.1.b47932beef

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-UNDO-001-no-undo-affordance.spec.js +40 -0
  3. package/dist/modules/checkout/__integration__/TC-UNDO-001-no-undo-affordance.spec.js.map +7 -0
  4. package/dist/modules/checkout/__integration__/TC-UNDO-001-pay-link.spec.js +205 -0
  5. package/dist/modules/checkout/__integration__/TC-UNDO-001-pay-link.spec.js.map +7 -0
  6. package/dist/modules/checkout/__integration__/TC-UNDO-001-template.spec.js +171 -0
  7. package/dist/modules/checkout/__integration__/TC-UNDO-001-template.spec.js.map +7 -0
  8. package/dist/modules/checkout/components/LinkTemplateForm.js +30 -2
  9. package/dist/modules/checkout/components/LinkTemplateForm.js.map +2 -2
  10. package/dist/modules/checkout/lib/recordNotFound.js +20 -0
  11. package/dist/modules/checkout/lib/recordNotFound.js.map +7 -0
  12. package/package.json +5 -5
  13. package/src/modules/checkout/__integration__/TC-UNDO-001-no-undo-affordance.spec.ts +51 -0
  14. package/src/modules/checkout/__integration__/TC-UNDO-001-pay-link.spec.ts +266 -0
  15. package/src/modules/checkout/__integration__/TC-UNDO-001-template.spec.ts +228 -0
  16. package/src/modules/checkout/components/LinkTemplateForm.tsx +39 -2
  17. package/src/modules/checkout/i18n/de.json +4 -0
  18. package/src/modules/checkout/i18n/en.json +4 -0
  19. package/src/modules/checkout/i18n/es.json +4 -0
  20. package/src/modules/checkout/i18n/pl.json +4 -0
  21. package/src/modules/checkout/lib/__tests__/recordNotFound.test.ts +30 -0
  22. package/src/modules/checkout/lib/recordNotFound.ts +17 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/checkout",
3
- "version": "0.6.5-develop.5187.1.82e5532561",
3
+ "version": "0.6.5-develop.5212.1.b47932beef",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -61,18 +61,18 @@
61
61
  }
62
62
  },
63
63
  "dependencies": {
64
- "@open-mercato/core": "0.6.5-develop.5187.1.82e5532561",
65
- "@open-mercato/ui": "0.6.5-develop.5187.1.82e5532561",
64
+ "@open-mercato/core": "0.6.5-develop.5212.1.b47932beef",
65
+ "@open-mercato/ui": "0.6.5-develop.5212.1.b47932beef",
66
66
  "bcryptjs": "^3.0.3"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "@mikro-orm/postgresql": "^7.0.14",
70
- "@open-mercato/shared": "0.6.5-develop.5187.1.82e5532561",
70
+ "@open-mercato/shared": "0.6.5-develop.5212.1.b47932beef",
71
71
  "react": "^19.0.0",
72
72
  "react-dom": "^19.0.0"
73
73
  },
74
74
  "devDependencies": {
75
- "@open-mercato/shared": "0.6.5-develop.5187.1.82e5532561",
75
+ "@open-mercato/shared": "0.6.5-develop.5212.1.b47932beef",
76
76
  "@types/jest": "^30.0.0",
77
77
  "@types/react": "^19.2.17",
78
78
  "@types/react-dom": "^19.2.3",
@@ -0,0 +1,51 @@
1
+ import { expect, test } from '@playwright/test'
2
+ import { getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { extractOperation, skipIfUndoTestsDisabled } from '@open-mercato/core/helpers/integration/undoHarness'
4
+ import {
5
+ createCustomerData,
6
+ createFixedTemplateInput,
7
+ createLinkFixture,
8
+ createTemplateFixture,
9
+ deleteCheckoutEntityIfExists,
10
+ submitPayLink,
11
+ } from './helpers/fixtures'
12
+
13
+ /**
14
+ * TC-UNDO-001 (§4 checkout) — non-undoable commands expose no undo affordance.
15
+ *
16
+ * Public checkout submissions create a CheckoutTransaction via the `checkout.transaction.*`
17
+ * commands, which are intentionally NOT undoable (financial events). The mutating response
18
+ * must therefore carry no `x-om-operation` undo envelope: `extractOperation(res) === null`.
19
+ */
20
+
21
+ test.describe('TC-UNDO-001 checkout §4 — non-undoable commands expose no undo token', () => {
22
+ test.beforeAll(() => {
23
+ skipIfUndoTestsDisabled()
24
+ })
25
+
26
+ test('public checkout submit creates a transaction with no undo token', async ({ request }) => {
27
+ const token = await getAuthToken(request, 'admin')
28
+ let templateId: string | null = null
29
+ let linkId: string | null = null
30
+ try {
31
+ templateId = await createTemplateFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
32
+ const link = await createLinkFixture(request, token, createFixedTemplateInput({ status: 'active', templateId }))
33
+ linkId = link.id
34
+
35
+ const submitRes = await submitPayLink(request, link.slug, {
36
+ customerData: createCustomerData(),
37
+ acceptedLegalConsents: {},
38
+ amount: 49.99,
39
+ })
40
+ expect(submitRes.status(), `submit status ${submitRes.status()}`).toBe(201)
41
+
42
+ expect(
43
+ extractOperation(submitRes),
44
+ 'a financial checkout submit must expose no undo token (§4)',
45
+ ).toBeNull()
46
+ } finally {
47
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
48
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
49
+ }
50
+ })
51
+ })
@@ -0,0 +1,266 @@
1
+ import { expect, test, type APIRequestContext } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+ import {
5
+ expectOperation,
6
+ undoOk,
7
+ redoOk,
8
+ expectTokenConsumed,
9
+ skipIfUndoTestsDisabled,
10
+ } from '@open-mercato/core/helpers/integration/undoHarness'
11
+ import {
12
+ createFixedTemplateInput,
13
+ createLinkFixture,
14
+ deleteCheckoutEntityIfExists,
15
+ deleteLink,
16
+ updateLink,
17
+ } from './helpers/fixtures'
18
+
19
+ /**
20
+ * TC-UNDO-001 (§3 checkout.pay-link) — Undo/Redo correctness for the checkout pay-link
21
+ * command bus, driven through the real `/api/checkout/links` routes plus the
22
+ * `/api/audit_logs/audit-logs/actions/undo|redo` endpoints.
23
+ *
24
+ * Invariants asserted: I1 (update→undo restores), I2 (delete→undo re-materializes),
25
+ * I3 (create→undo soft-deletes), I4 (custom fields restore), I5 (token consumed),
26
+ * I6 (redo reproduces post-state). checkout.link.create ships an id-preserving redo
27
+ * handler, so the create→undo→redo SAME-id leg is asserted as corrected behaviour.
28
+ *
29
+ * Also covers the #2540 fix: a draft pay-link may persist a null gatewayProviderKey,
30
+ * and editing it to null then undoing must restore the prior gateway.
31
+ */
32
+
33
+ const LINKS = '/api/checkout/links'
34
+
35
+ type LinkRecord = {
36
+ id?: string
37
+ name?: string
38
+ slug?: string
39
+ updatedAt?: string
40
+ gatewayProviderKey?: string | null
41
+ customFields?: Record<string, unknown>
42
+ }
43
+
44
+ // The undo endpoint resolves the *latest* undoable log for a resource ordered by
45
+ // millisecond-precision created_at, so consecutive mutations issued within the same
46
+ // millisecond can tie. A short settle keeps each round-trip deterministic.
47
+ async function settle(): Promise<void> {
48
+ await new Promise((resolve) => setTimeout(resolve, 50))
49
+ }
50
+
51
+ async function getLink(
52
+ request: APIRequestContext,
53
+ token: string,
54
+ id: string,
55
+ ): Promise<{ status: number; body: LinkRecord | null }> {
56
+ const res = await apiRequest(request, 'GET', `${LINKS}/${encodeURIComponent(id)}`, { token })
57
+ return { status: res.status(), body: await readJsonSafe<LinkRecord>(res) }
58
+ }
59
+
60
+ test.describe('TC-UNDO-001 checkout.pay-link undo/redo', () => {
61
+ test.beforeAll(() => {
62
+ skipIfUndoTestsDisabled()
63
+ })
64
+
65
+ test('create → undo soft-deletes (I3) + token consumed (I5)', async ({ request }) => {
66
+ const token = await getAuthToken(request, 'admin')
67
+ let linkId: string | null = null
68
+ try {
69
+ const createRes = await apiRequest(request, 'POST', LINKS, {
70
+ token,
71
+ data: createFixedTemplateInput({ status: 'draft' }),
72
+ })
73
+ expect(createRes.status(), `create status ${createRes.status()}`).toBe(201)
74
+ const createOp = expectOperation(createRes, 'checkout.link.create')
75
+ linkId = createOp.resourceId
76
+ expect(linkId, 'create returns a resource id').toBeTruthy()
77
+
78
+ expect((await getLink(request, token, linkId as string)).status, 'pay-link exists after create').toBe(200)
79
+
80
+ await undoOk(request, token, createOp.undoToken, 'undo create pay-link')
81
+ expect(
82
+ (await getLink(request, token, linkId as string)).status,
83
+ 'pay-link is gone after undoing create (I3 — soft-deleted, not readable)',
84
+ ).not.toBe(200)
85
+
86
+ await expectTokenConsumed(request, token, createOp.undoToken, 'checkout.link.create double-undo (I5)')
87
+ } finally {
88
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
89
+ }
90
+ })
91
+
92
+ test('update → undo restores scalars (I1) → redo re-applies (I6)', async ({ request }) => {
93
+ const token = await getAuthToken(request, 'admin')
94
+ let linkId: string | null = null
95
+ try {
96
+ const link = await createLinkFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
97
+ linkId = link.id
98
+ const before = await getLink(request, token, linkId)
99
+ const beforeName = before.body?.name
100
+ expect(beforeName, 'pay-link name readable before update').toBeTruthy()
101
+
102
+ await settle()
103
+ const changedName = `Renamed by undo test ${Date.now()}`
104
+ const updateRes = await updateLink(request, token, linkId, { name: changedName })
105
+ expect(updateRes.status(), `update status ${updateRes.status()}`).toBe(200)
106
+ const updateOp = expectOperation(updateRes, 'checkout.link.update')
107
+ expect((await getLink(request, token, linkId)).body?.name, 'update changed the name').toBe(changedName)
108
+
109
+ await settle()
110
+ await undoOk(request, token, updateOp.undoToken, 'undo update pay-link')
111
+ const afterUndo = await getLink(request, token, linkId)
112
+ expect(afterUndo.body?.name, 'update→undo restores the prior name (I1)').toBe(beforeName)
113
+ expect(typeof afterUndo.body?.updatedAt, 'pay-link surfaces updatedAt').toBe('string')
114
+ expect(afterUndo.body?.updatedAt, 'undo bumps updatedAt (I1)').not.toBe(before.body?.updatedAt)
115
+
116
+ await redoOk(request, token, updateOp.logId, 'redo update pay-link')
117
+ expect((await getLink(request, token, linkId)).body?.name, 'redo re-applies the renamed value (I6)').toBe(changedName)
118
+ } finally {
119
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
120
+ }
121
+ })
122
+
123
+ test('delete → undo re-materializes (I2) → redo re-deletes (I6)', async ({ request }) => {
124
+ const token = await getAuthToken(request, 'admin')
125
+ let linkId: string | null = null
126
+ try {
127
+ const link = await createLinkFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
128
+ linkId = link.id
129
+ const beforeName = (await getLink(request, token, linkId)).body?.name
130
+
131
+ await settle()
132
+ const deleteRes = await deleteLink(request, token, linkId)
133
+ expect(deleteRes.ok(), `delete status ${deleteRes.status()}`).toBeTruthy()
134
+ const deleteOp = expectOperation(deleteRes, 'checkout.link.delete')
135
+ expect((await getLink(request, token, linkId)).status, 'gone after delete').not.toBe(200)
136
+
137
+ await undoOk(request, token, deleteOp.undoToken, 'undo delete pay-link')
138
+ const afterUndo = await getLink(request, token, linkId)
139
+ expect(afterUndo.status, 'delete→undo re-materializes the row (I2)').toBe(200)
140
+ expect(afterUndo.body?.name, 're-materialized record keeps its scalars (I2)').toBe(beforeName)
141
+
142
+ await redoOk(request, token, deleteOp.logId, 'redo delete pay-link')
143
+ expect((await getLink(request, token, linkId)).status, 'gone again after redo delete (I6)').not.toBe(200)
144
+ } finally {
145
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
146
+ }
147
+ })
148
+
149
+ test('create → undo → redo restores the SAME record (I6)', async ({ request }) => {
150
+ // checkout.link.create registers an id-preserving redo handler, so redo must restore
151
+ // the original soft-deleted row — not mint a new id (the #2468 customers.people defect).
152
+ const token = await getAuthToken(request, 'admin')
153
+ let linkId: string | null = null
154
+ try {
155
+ const createRes = await apiRequest(request, 'POST', LINKS, {
156
+ token,
157
+ data: createFixedTemplateInput({ status: 'draft' }),
158
+ })
159
+ const createOp = expectOperation(createRes, 'checkout.link.create')
160
+ linkId = createOp.resourceId
161
+
162
+ await settle()
163
+ const undoLogId = await undoOk(request, token, createOp.undoToken, 'undo create pay-link')
164
+ expect((await getLink(request, token, linkId as string)).status, 'gone after undo create').not.toBe(200)
165
+
166
+ await redoOk(request, token, undoLogId, 'redo create pay-link')
167
+ const afterRedo = await getLink(request, token, linkId as string)
168
+ expect(afterRedo.status, 'same pay-link restored after redo (I6)').toBe(200)
169
+ expect(afterRedo.body?.id, 'redo preserves the original id (I6)').toBe(linkId)
170
+ } finally {
171
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
172
+ }
173
+ })
174
+
175
+ test('custom fields revert on undo and re-apply on redo (I4)', async ({ request }) => {
176
+ const token = await getAuthToken(request, 'admin')
177
+ let linkId: string | null = null
178
+ try {
179
+ const beforeValue = `before ${Date.now()}`
180
+ const link = await createLinkFixture(
181
+ request,
182
+ token,
183
+ createFixedTemplateInput({
184
+ status: 'draft',
185
+ customFieldsetCode: 'service_package',
186
+ customFields: { delivery_timeline: beforeValue },
187
+ }),
188
+ )
189
+ linkId = link.id
190
+ expect(
191
+ (await getLink(request, token, linkId)).body?.customFields?.delivery_timeline,
192
+ 'custom field persisted on create',
193
+ ).toBe(beforeValue)
194
+
195
+ await settle()
196
+ const afterValue = `after ${Date.now()}`
197
+ const updateRes = await updateLink(request, token, linkId, { customFields: { delivery_timeline: afterValue } })
198
+ const updateOp = expectOperation(updateRes, 'checkout.link.update')
199
+ expect(
200
+ (await getLink(request, token, linkId)).body?.customFields?.delivery_timeline,
201
+ 'update changed the custom field',
202
+ ).toBe(afterValue)
203
+
204
+ await settle()
205
+ await undoOk(request, token, updateOp.undoToken, 'undo update custom field')
206
+ expect(
207
+ (await getLink(request, token, linkId)).body?.customFields?.delivery_timeline,
208
+ 'custom field reverts on undo (I4)',
209
+ ).toBe(beforeValue)
210
+
211
+ await redoOk(request, token, updateOp.logId, 'redo update custom field')
212
+ expect(
213
+ (await getLink(request, token, linkId)).body?.customFields?.delivery_timeline,
214
+ 'custom field re-applies on redo (I4)',
215
+ ).toBe(afterValue)
216
+ } finally {
217
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
218
+ }
219
+ })
220
+
221
+ test('null gatewayProviderKey edit → undo restores the prior gateway (#2540, I1)', async ({ request }) => {
222
+ // #2540: a draft pay-link may persist a null gatewayProviderKey. Editing the gateway to
223
+ // null must round-trip, and undoing the edit must restore the prior gateway value.
224
+ const token = await getAuthToken(request, 'admin')
225
+ let linkId: string | null = null
226
+ try {
227
+ const link = await createLinkFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
228
+ linkId = link.id
229
+ const before = await getLink(request, token, linkId)
230
+ expect(before.body?.gatewayProviderKey, 'pay-link starts with the mock gateway').toBe('mock')
231
+
232
+ await settle()
233
+ const updateRes = await updateLink(request, token, linkId, {
234
+ name: 'Pay link (no gateway)',
235
+ pricingMode: 'fixed',
236
+ fixedPriceAmount: 49.99,
237
+ fixedPriceCurrencyCode: 'USD',
238
+ status: 'draft',
239
+ gatewayProviderKey: null,
240
+ })
241
+ expect(
242
+ updateRes.ok(),
243
+ `clearing the gateway on a draft pay-link should succeed: ${updateRes.status()}`,
244
+ ).toBeTruthy()
245
+ const updateOp = expectOperation(updateRes, 'checkout.link.update (null gateway)')
246
+
247
+ const afterUpdate = await getLink(request, token, linkId)
248
+ expect(afterUpdate.body?.gatewayProviderKey ?? null, 'gateway cleared to null after edit').toBeNull()
249
+ expect(afterUpdate.body?.name, 'name changed by edit').toBe('Pay link (no gateway)')
250
+
251
+ await settle()
252
+ await undoOk(request, token, updateOp.undoToken, 'undo null-gateway edit')
253
+ const afterUndo = await getLink(request, token, linkId)
254
+ expect(afterUndo.body?.gatewayProviderKey, 'undo restores the prior gateway (#2540, I1)').toBe('mock')
255
+ expect(afterUndo.body?.name, 'undo restores the prior name (I1)').toBe(before.body?.name)
256
+
257
+ await redoOk(request, token, updateOp.logId, 'redo null-gateway edit')
258
+ expect(
259
+ (await getLink(request, token, linkId)).body?.gatewayProviderKey ?? null,
260
+ 'redo re-clears the gateway to null (I6)',
261
+ ).toBeNull()
262
+ } finally {
263
+ await deleteCheckoutEntityIfExists(request, token, 'links', linkId)
264
+ }
265
+ })
266
+ })
@@ -0,0 +1,228 @@
1
+ import { expect, test, type APIRequestContext } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+ import {
5
+ expectOperation,
6
+ undoOk,
7
+ redoOk,
8
+ expectTokenConsumed,
9
+ skipIfUndoTestsDisabled,
10
+ } from '@open-mercato/core/helpers/integration/undoHarness'
11
+ import {
12
+ createFixedTemplateInput,
13
+ createTemplateFixture,
14
+ deleteCheckoutEntityIfExists,
15
+ deleteTemplate,
16
+ updateTemplate,
17
+ } from './helpers/fixtures'
18
+
19
+ /**
20
+ * TC-UNDO-001 (§3 checkout.template) — Undo/Redo correctness for the checkout template
21
+ * command bus, driven through the real `/api/checkout/templates` routes plus the
22
+ * `/api/audit_logs/audit-logs/actions/undo|redo` endpoints.
23
+ *
24
+ * Invariants asserted (per the #2468 tracking issue):
25
+ * I1 update→undo restores scalars (and bumps updatedAt)
26
+ * I2 delete→undo re-materializes the soft-deleted row
27
+ * I3 create→undo soft-deletes (never hard-deletes)
28
+ * I4 custom fields revert on undo and re-apply on redo
29
+ * I5 a consumed undo token is rejected on a second undo
30
+ * I6 redo reproduces the command's post-state
31
+ *
32
+ * Unlike the customers.people pilot (where create→undo→redo mints a new id, #2468),
33
+ * checkout.template.create ships an id-preserving redo handler, so the SAME-id redo
34
+ * leg is asserted as corrected behaviour rather than quarantined.
35
+ */
36
+
37
+ const TEMPLATES = '/api/checkout/templates'
38
+
39
+ type TemplateRecord = {
40
+ id?: string
41
+ name?: string
42
+ updatedAt?: string
43
+ gatewayProviderKey?: string | null
44
+ customFields?: Record<string, unknown>
45
+ }
46
+
47
+ // The undo endpoint resolves the *latest* undoable log for a resource ordered by
48
+ // millisecond-precision created_at, so consecutive mutations issued within the same
49
+ // millisecond can tie. A short settle keeps each round-trip deterministic.
50
+ async function settle(): Promise<void> {
51
+ await new Promise((resolve) => setTimeout(resolve, 50))
52
+ }
53
+
54
+ async function getTemplate(
55
+ request: APIRequestContext,
56
+ token: string,
57
+ id: string,
58
+ ): Promise<{ status: number; body: TemplateRecord | null }> {
59
+ const res = await apiRequest(request, 'GET', `${TEMPLATES}/${encodeURIComponent(id)}`, { token })
60
+ return { status: res.status(), body: await readJsonSafe<TemplateRecord>(res) }
61
+ }
62
+
63
+ test.describe('TC-UNDO-001 checkout.template undo/redo', () => {
64
+ test.beforeAll(() => {
65
+ skipIfUndoTestsDisabled()
66
+ })
67
+
68
+ test('create → undo soft-deletes (I3) + token consumed (I5)', async ({ request }) => {
69
+ const token = await getAuthToken(request, 'admin')
70
+ let templateId: string | null = null
71
+ try {
72
+ const createRes = await apiRequest(request, 'POST', TEMPLATES, {
73
+ token,
74
+ data: createFixedTemplateInput({ status: 'draft' }),
75
+ })
76
+ expect(createRes.status(), `create status ${createRes.status()}`).toBe(201)
77
+ const createOp = expectOperation(createRes, 'checkout.template.create')
78
+ templateId = createOp.resourceId
79
+ expect(templateId, 'create returns a resource id').toBeTruthy()
80
+
81
+ expect((await getTemplate(request, token, templateId as string)).status, 'template exists after create').toBe(200)
82
+
83
+ await undoOk(request, token, createOp.undoToken, 'undo create template')
84
+ expect(
85
+ (await getTemplate(request, token, templateId as string)).status,
86
+ 'template is gone after undoing create (I3 — soft-deleted, not readable)',
87
+ ).not.toBe(200)
88
+
89
+ await expectTokenConsumed(request, token, createOp.undoToken, 'checkout.template.create double-undo (I5)')
90
+ } finally {
91
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
92
+ }
93
+ })
94
+
95
+ test('update → undo restores scalars (I1) → redo re-applies (I6)', async ({ request }) => {
96
+ const token = await getAuthToken(request, 'admin')
97
+ let templateId: string | null = null
98
+ try {
99
+ templateId = await createTemplateFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
100
+ const before = await getTemplate(request, token, templateId)
101
+ const beforeName = before.body?.name
102
+ expect(beforeName, 'template name readable before update').toBeTruthy()
103
+
104
+ await settle()
105
+ const changedName = `Renamed by undo test ${Date.now()}`
106
+ const updateRes = await updateTemplate(request, token, templateId, { name: changedName })
107
+ expect(updateRes.status(), `update status ${updateRes.status()}`).toBe(200)
108
+ const updateOp = expectOperation(updateRes, 'checkout.template.update')
109
+ expect((await getTemplate(request, token, templateId)).body?.name, 'update changed the name').toBe(changedName)
110
+
111
+ await settle()
112
+ await undoOk(request, token, updateOp.undoToken, 'undo update template')
113
+ const afterUndo = await getTemplate(request, token, templateId)
114
+ expect(afterUndo.body?.name, 'update→undo restores the prior name (I1)').toBe(beforeName)
115
+ expect(typeof afterUndo.body?.updatedAt, 'template surfaces updatedAt').toBe('string')
116
+ expect(afterUndo.body?.updatedAt, 'undo bumps updatedAt (I1)').not.toBe(before.body?.updatedAt)
117
+
118
+ await redoOk(request, token, updateOp.logId, 'redo update template')
119
+ expect(
120
+ (await getTemplate(request, token, templateId)).body?.name,
121
+ 'redo re-applies the renamed value (I6)',
122
+ ).toBe(changedName)
123
+ } finally {
124
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
125
+ }
126
+ })
127
+
128
+ test('delete → undo re-materializes (I2) → redo re-deletes (I6)', async ({ request }) => {
129
+ const token = await getAuthToken(request, 'admin')
130
+ let templateId: string | null = null
131
+ try {
132
+ templateId = await createTemplateFixture(request, token, createFixedTemplateInput({ status: 'draft' }))
133
+ const beforeName = (await getTemplate(request, token, templateId)).body?.name
134
+
135
+ await settle()
136
+ const deleteRes = await deleteTemplate(request, token, templateId)
137
+ expect(deleteRes.ok(), `delete status ${deleteRes.status()}`).toBeTruthy()
138
+ const deleteOp = expectOperation(deleteRes, 'checkout.template.delete')
139
+ expect((await getTemplate(request, token, templateId)).status, 'gone after delete').not.toBe(200)
140
+
141
+ await undoOk(request, token, deleteOp.undoToken, 'undo delete template')
142
+ const afterUndo = await getTemplate(request, token, templateId)
143
+ expect(afterUndo.status, 'delete→undo re-materializes the row (I2)').toBe(200)
144
+ expect(afterUndo.body?.name, 're-materialized record keeps its scalars (I2)').toBe(beforeName)
145
+
146
+ await redoOk(request, token, deleteOp.logId, 'redo delete template')
147
+ expect(
148
+ (await getTemplate(request, token, templateId)).status,
149
+ 'gone again after redo delete (I6)',
150
+ ).not.toBe(200)
151
+ } finally {
152
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
153
+ }
154
+ })
155
+
156
+ test('create → undo → redo restores the SAME record (I6)', async ({ request }) => {
157
+ // checkout.template.create registers an id-preserving redo handler, so redo must restore
158
+ // the original soft-deleted row — not mint a new id (the #2468 customers.people defect).
159
+ const token = await getAuthToken(request, 'admin')
160
+ let templateId: string | null = null
161
+ try {
162
+ const createRes = await apiRequest(request, 'POST', TEMPLATES, {
163
+ token,
164
+ data: createFixedTemplateInput({ status: 'draft' }),
165
+ })
166
+ const createOp = expectOperation(createRes, 'checkout.template.create')
167
+ templateId = createOp.resourceId
168
+
169
+ await settle()
170
+ const undoLogId = await undoOk(request, token, createOp.undoToken, 'undo create template')
171
+ expect((await getTemplate(request, token, templateId as string)).status, 'gone after undo create').not.toBe(200)
172
+
173
+ await redoOk(request, token, undoLogId, 'redo create template')
174
+ const afterRedo = await getTemplate(request, token, templateId as string)
175
+ expect(afterRedo.status, 'same template restored after redo (I6)').toBe(200)
176
+ expect(afterRedo.body?.id, 'redo preserves the original id (I6)').toBe(templateId)
177
+ } finally {
178
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
179
+ }
180
+ })
181
+
182
+ test('custom fields revert on undo and re-apply on redo (I4)', async ({ request }) => {
183
+ const token = await getAuthToken(request, 'admin')
184
+ let templateId: string | null = null
185
+ try {
186
+ const beforeValue = `before ${Date.now()}`
187
+ templateId = await createTemplateFixture(
188
+ request,
189
+ token,
190
+ createFixedTemplateInput({
191
+ status: 'draft',
192
+ customFieldsetCode: 'service_package',
193
+ customFields: { delivery_timeline: beforeValue },
194
+ }),
195
+ )
196
+ expect(
197
+ (await getTemplate(request, token, templateId)).body?.customFields?.delivery_timeline,
198
+ 'custom field persisted on create',
199
+ ).toBe(beforeValue)
200
+
201
+ await settle()
202
+ const afterValue = `after ${Date.now()}`
203
+ const updateRes = await updateTemplate(request, token, templateId, {
204
+ customFields: { delivery_timeline: afterValue },
205
+ })
206
+ const updateOp = expectOperation(updateRes, 'checkout.template.update')
207
+ expect(
208
+ (await getTemplate(request, token, templateId)).body?.customFields?.delivery_timeline,
209
+ 'update changed the custom field',
210
+ ).toBe(afterValue)
211
+
212
+ await settle()
213
+ await undoOk(request, token, updateOp.undoToken, 'undo update custom field')
214
+ expect(
215
+ (await getTemplate(request, token, templateId)).body?.customFields?.delivery_timeline,
216
+ 'custom field reverts on undo (I4)',
217
+ ).toBe(beforeValue)
218
+
219
+ await redoOk(request, token, updateOp.logId, 'redo update custom field')
220
+ expect(
221
+ (await getTemplate(request, token, templateId)).body?.customFields?.delivery_timeline,
222
+ 'custom field re-applies on redo (I4)',
223
+ ).toBe(afterValue)
224
+ } finally {
225
+ await deleteCheckoutEntityIfExists(request, token, 'templates', templateId)
226
+ }
227
+ })
228
+ })
@@ -23,6 +23,7 @@ import { slugify } from '@open-mercato/shared/lib/slugify'
23
23
  import { CrudForm, type CrudField, type CrudFormGroup, type CrudFormGroupComponentProps } from '@open-mercato/ui/backend/CrudForm'
24
24
  import { ComboboxInput, type ComboboxOption } from '@open-mercato/ui/backend/inputs'
25
25
  import { Page, PageBody } from '@open-mercato/ui/backend/Page'
26
+ import { RecordNotFoundState } from '@open-mercato/ui/backend/detail'
26
27
  import { SwitchableMarkdownInput } from '@open-mercato/ui/backend/inputs'
27
28
  import { apiCall, apiCallOrThrow, readApiResultOrThrow, withScopedApiRequestHeaders } from '@open-mercato/ui/backend/utils/apiCall'
28
29
  import { buildOptimisticLockHeader } from '@open-mercato/ui/backend/utils/optimisticLock'
@@ -40,6 +41,7 @@ import { getLocalizedDefaultCheckoutCustomerFields } from '../lib/defaults'
40
41
  import type { CustomerFieldDefinitionInput, PriceListItemInput } from '../data/validators'
41
42
  import { getGatewayProviderConfigurationMessageKey } from '../lib/gatewayProviderAvailability'
42
43
  import { readCustomerFieldsSectionError } from '../lib/customerFieldErrors'
44
+ import { isRecordNotFoundError } from '../lib/recordNotFound'
43
45
  import { CheckoutCurrencySelect } from './CheckoutCurrencySelect'
44
46
  import { CustomerFieldsEditor } from './CustomerFieldsEditor'
45
47
  import { GatewaySettingsFields } from './GatewaySettingsFields'
@@ -1332,6 +1334,7 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1332
1334
  const [initialValues, setInitialValues] = React.useState<FormValues | null>(
1333
1335
  recordId ? null : normalizeFormValues(createDefaultValues(t), t),
1334
1336
  )
1337
+ const [notFound, setNotFound] = React.useState(false)
1335
1338
 
1336
1339
  const replaceInitialValues = React.useCallback((nextValues: FormValues) => {
1337
1340
  setInitialValues(nextValues)
@@ -1425,13 +1428,19 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1425
1428
  React.useEffect(() => {
1426
1429
  if (!recordId) return
1427
1430
  let active = true
1431
+ setNotFound(false)
1428
1432
  void readApiResultOrThrow<FormValues>(`/api/checkout/${mode === 'link' ? 'links' : 'templates'}/${encodeURIComponent(recordId)}`)
1429
1433
  .then((result) => {
1430
1434
  if (!active) return
1431
1435
  replaceInitialValues(normalizeFormValues(result, t))
1432
1436
  })
1433
- .catch(() => {
1434
- if (active) replaceInitialValues(normalizeFormValues({}, t))
1437
+ .catch((error) => {
1438
+ if (!active) return
1439
+ if (isRecordNotFoundError(error)) {
1440
+ setNotFound(true)
1441
+ return
1442
+ }
1443
+ replaceInitialValues(normalizeFormValues({}, t))
1435
1444
  })
1436
1445
  return () => {
1437
1446
  active = false
@@ -1567,6 +1576,26 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1567
1576
  </div>
1568
1577
  ) : undefined
1569
1578
 
1579
+ const listHref = mode === 'link' ? '/backend/checkout/pay-links' : '/backend/checkout/templates'
1580
+
1581
+ if (notFound) {
1582
+ return (
1583
+ <Page>
1584
+ <PageBody>
1585
+ <RecordNotFoundState
1586
+ label={t(mode === 'link'
1587
+ ? 'checkout.linkTemplateForm.notFound.link.title'
1588
+ : 'checkout.linkTemplateForm.notFound.template.title')}
1589
+ description={t(mode === 'link'
1590
+ ? 'checkout.linkTemplateForm.notFound.link.description'
1591
+ : 'checkout.linkTemplateForm.notFound.template.description')}
1592
+ backHref={listHref}
1593
+ />
1594
+ </PageBody>
1595
+ </Page>
1596
+ )
1597
+ }
1598
+
1570
1599
  return (
1571
1600
  <Page>
1572
1601
  <PageBody>
@@ -1653,6 +1682,10 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1653
1682
  )
1654
1683
  } catch (error) {
1655
1684
  if (surfaceRecordConflict(error, t)) return
1685
+ if (recordId && isRecordNotFoundError(error)) {
1686
+ setNotFound(true)
1687
+ return
1688
+ }
1656
1689
  throw error
1657
1690
  }
1658
1691
  const targetId = recordId ?? (typeof response?.id === 'string' ? response.id : null)
@@ -1706,6 +1739,10 @@ export function LinkTemplateForm({ mode, recordId }: Props) {
1706
1739
  )
1707
1740
  } catch (error) {
1708
1741
  if (surfaceRecordConflict(error, t)) return
1742
+ if (isRecordNotFoundError(error)) {
1743
+ setNotFound(true)
1744
+ return
1745
+ }
1709
1746
  throw error
1710
1747
  }
1711
1748
  window.location.href = mode === 'link'