fix: v3 update many with drafts (#5900)

Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
This commit is contained in:
Dan Ribbens
2024-04-19 16:32:59 -04:00
committed by GitHub
parent 27297284cf
commit 1383191f15
5 changed files with 85 additions and 6 deletions

View File

@@ -129,6 +129,7 @@ export const updateOperation = async <TSlug extends keyof GeneratedTypes['collec
const query = await payload.db.queryDrafts<GeneratedTypes['collections'][TSlug]>({
collection: collectionConfig.slug,
locale,
pagination: false,
req,
where: versionsWhere,
})
@@ -266,7 +267,8 @@ export const updateOperation = async <TSlug extends keyof GeneratedTypes['collec
global: null,
operation: 'update',
req,
skipValidation: shouldSaveDraft || data._status === 'draft',
skipValidation:
Boolean(collectionConfig.versions?.drafts) && data._status !== 'published',
})
// /////////////////////////////////////
@@ -295,7 +297,6 @@ export const updateOperation = async <TSlug extends keyof GeneratedTypes['collec
...result,
createdAt: doc.createdAt,
},
draft: shouldSaveDraft,
payload,
req,
})

View File

@@ -240,7 +240,7 @@ export const updateByIDOperation = async <TSlug extends keyof GeneratedTypes['co
global: null,
operation: 'update',
req,
skipValidation: shouldSaveDraft || data._status === 'draft',
skipValidation: Boolean(collectionConfig.versions?.drafts) && data._status !== 'published',
})
// /////////////////////////////////////

View File

@@ -220,11 +220,11 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
{collection?.versions?.drafts ? (
<React.Fragment>
<Publish
action={`${serverURL}${apiRoute}/${slug}${getQueryParams()}`}
action={`${serverURL}${apiRoute}/${slug}${getQueryParams()}&draft=true`}
disabled={selected.length === 0}
/>
<SaveDraft
action={`${serverURL}${apiRoute}/${slug}${getQueryParams()}`}
action={`${serverURL}${apiRoute}/${slug}${getQueryParams()}&draft=true`}
disabled={selected.length === 0}
/>
</React.Fragment>

View File

@@ -19,7 +19,6 @@
* - tabs
* - text
* - richtext
* - restore version
* - specify locales to show
*/

View File

@@ -1,5 +1,7 @@
import type { Payload } from 'payload'
import { ValidationError } from 'payload/errors'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import { devUser } from '../credentials.js'
@@ -472,7 +474,84 @@ describe('Versions', () => {
expect(draftPost.title.en).toBe(patchedTitle)
expect(draftPost.title.es).toBe(spanishTitle)
})
it('should validate when publishing with the draft arg', async () => {
// no title (not valid for publishing)
const doc = await payload.create({
collection: draftCollectionSlug,
data: {
description: 'desc',
},
draft: true,
})
await expect(async () => {
// should not be able to publish a doc that fails validation
await payload.update({
id: doc.id,
collection: draftCollectionSlug,
data: { _status: 'published' },
draft: true,
})
}).rejects.toThrow(ValidationError)
// succeeds but returns zero docs updated, with an error
const updateManyResult = await payload.update({
collection: draftCollectionSlug,
data: { _status: 'published' },
draft: true,
where: {
id: { equals: doc.id },
},
})
expect(updateManyResult.docs).toHaveLength(0)
expect(updateManyResult.errors).toStrictEqual([
{ id: doc.id, message: 'The following field is invalid: title' },
])
})
})
describe('Update Many', () => {
it('should update many using drafts', async () => {
const doc = await payload.create({
collection: draftCollectionSlug,
data: {
title: 'initial value',
description: 'description to bulk update',
_status: 'published',
},
})
await payload.update({
collection: draftCollectionSlug,
id: doc.id,
draft: true,
data: {
title: 'updated title',
},
})
const updated = await payload.update({
collection: draftCollectionSlug,
data: {
description: 'updated description',
},
draft: true,
where: {
id: {
in: [doc.id],
},
},
})
const updatedDoc = updated.docs?.[0]
expect(updatedDoc.description).toStrictEqual('updated description')
expect(updatedDoc.title).toStrictEqual('updated title') // probably will fail
})
})
describe('Delete', () => {
let postToDelete
beforeEach(async () => {