Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/payload/src/collections/operations/utilities/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ export const updateDocument = async <
? unpublishAllLocalesArg === 'true'
: !!unpublishAllLocalesArg
const isSavingDraft =
Boolean(draftArg && hasDraftsEnabled(collectionConfig)) &&
data._status !== 'published' &&
!publishAllLocales
Boolean(draftArg && hasDraftsEnabled(collectionConfig)) && !publishAllLocales
const shouldSavePassword = Boolean(
password &&
collectionConfig.auth &&
Expand All @@ -124,6 +122,13 @@ export const updateDocument = async <

if (isSavingDraft) {
data._status = 'draft'

if (!req.context) {
req.context = {}
}

// Used by storage plugins to persist adapter metadata without updating the published document
req.context.internalSavingDraft = true
}

// /////////////////////////////////////
Expand Down
5 changes: 1 addition & 4 deletions packages/payload/src/globals/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ export const updateOperation = async <
typeof unpublishAllLocalesArg === 'string'
? unpublishAllLocalesArg === 'true'
: !!unpublishAllLocalesArg
const isSavingDraft =
Boolean(draftArg && hasDraftsEnabled(globalConfig)) &&
data._status !== 'published' &&
!publishAllLocales
const isSavingDraft = Boolean(draftArg && hasDraftsEnabled(globalConfig)) && !publishAllLocales

if (isSavingDraft) {
data._status = 'draft'
Expand Down
40 changes: 40 additions & 0 deletions test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,46 @@ describe('Versions', () => {
expect(jsonByID.parent).toBe(autosavePost.id)
})

it('should preserve published main document when saving a draft with _status published', async () => {
const initialDoc = await payload.create({
collection: draftCollectionSlug,
data: {
title: 'Published title',
description: 'Published description',
_status: 'published',
},
})

await payload.update({
id: initialDoc.id,
collection: draftCollectionSlug,
data: {
title: 'Draft title',
_status: 'published',
},
draft: true,
})

const mainDoc = await payload.findByID({
collection: draftCollectionSlug,
id: initialDoc.id,
})

expect(mainDoc.title).toBe('Published title')
expect(mainDoc._status).toBe('published')

const versions = await payload.findVersions({
collection: draftCollectionSlug,
where: {
parent: {
equals: initialDoc.id,
},
},
})

expect(versions.docs.some((doc) => doc.version.title === 'Draft title')).toBe(true)
})

it('should allow query by latest', async () => {
async function createVersion({ title }: { title: string }) {
return payload.create({
Expand Down
Loading