Skip to content

Commit df17cb1

Browse files
authored
fix: globals not updating updatedAt when saving drafts (#15764)
### What When saving drafts, version documents for globals weren't getting updated `updatedAt` timestamps. ### Why The condition in `saveVersion.ts` only checked `collection?.timestamps`, which excluded globals even though they always have timestamps. Broke stale data detection which relies on timestamp comparison to detect concurrent edits. ### How Added `|| global` check in `saveVersion.ts` since globals always have timestamps but were excluded from the original `collection?.timestamps` condition.
1 parent ad4c0f6 commit df17cb1

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

packages/payload/src/versions/saveVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function saveVersion<TData extends JsonObject = JsonObject>({
5858
updatedAt?: string
5959
} & TData = deepCopyObjectSimple(docWithLocales)
6060

61-
if (collection?.timestamps && draft) {
61+
if ((collection?.timestamps || global) && draft) {
6262
versionData.updatedAt = now
6363
}
6464

test/versions/int.spec.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from
1111
import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js'
1212
import type { AutosaveMultiSelectPost, DraftPost } from './payload-types.js'
1313

14-
import { devUser } from '../credentials.js'
1514
import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js'
15+
import { devUser } from '../credentials.js'
1616
import {
1717
cleanupDocuments,
1818
cleanupGlobal,
@@ -1012,15 +1012,14 @@ describe('Versions', () => {
10121012
draft: true,
10131013
})
10141014

1015-
await expect(async () => {
1016-
// should not be able to publish a doc that fails validation
1017-
await payload.update({
1015+
await expect(
1016+
payload.update({
10181017
id: doc.id,
10191018
collection: draftCollectionSlug,
10201019
data: { _status: 'published' },
10211020
draft: true,
1022-
})
1023-
}).rejects.toThrow(ValidationError)
1021+
}),
1022+
).rejects.toThrow(ValidationError)
10241023

10251024
// succeeds but returns zero docs updated, with an error
10261025
const updateManyResult = await payload.update({
@@ -2324,6 +2323,57 @@ describe('Versions', () => {
23242323
expect(versions.docs[0].version.title.en).toStrictEqual(newEnglishTitle)
23252324
expect(versions.docs[0].version.title.es).toStrictEqual(spanishTitle)
23262325
})
2326+
2327+
it('should have correct updatedAt timestamps for globals when saving drafts', async () => {
2328+
const created = await payload.updateGlobal({
2329+
slug: draftGlobalSlug,
2330+
data: {
2331+
title: 'title',
2332+
},
2333+
draft: true,
2334+
})
2335+
2336+
await wait(10)
2337+
2338+
const updated = await payload.updateGlobal({
2339+
slug: draftGlobalSlug,
2340+
data: {
2341+
title: 'updated title',
2342+
},
2343+
draft: true,
2344+
})
2345+
2346+
const createdUpdatedAt = new Date(created.updatedAt)
2347+
const updatedUpdatedAt = new Date(updated.updatedAt)
2348+
2349+
expect(Number(updatedUpdatedAt)).toBeGreaterThan(Number(createdUpdatedAt))
2350+
})
2351+
2352+
it('should have correct updatedAt timestamps for globals when saving drafts with autosave', async () => {
2353+
const created = await payload.updateGlobal({
2354+
slug: draftGlobalSlug,
2355+
data: {
2356+
title: 'title',
2357+
},
2358+
draft: true,
2359+
})
2360+
2361+
await wait(10)
2362+
2363+
const updated = await payload.updateGlobal({
2364+
slug: draftGlobalSlug,
2365+
data: {
2366+
title: 'updated title',
2367+
},
2368+
draft: true,
2369+
autosave: true,
2370+
})
2371+
2372+
const createdUpdatedAt = new Date(created.updatedAt)
2373+
const updatedUpdatedAt = new Date(updated.updatedAt)
2374+
2375+
expect(Number(updatedUpdatedAt)).toBeGreaterThan(Number(createdUpdatedAt))
2376+
})
23272377
})
23282378

23292379
describe('Restore', () => {

0 commit comments

Comments
 (0)