fix: ensure scheduled publish restriction (#10317)

This commit is contained in:
Sasha
2025-01-03 15:26:21 +02:00
committed by GitHub
parent d6d9edc304
commit 4e57054bb7
7 changed files with 113 additions and 1 deletions

View File

@@ -5,6 +5,13 @@ import { draftCollectionSlug } from '../slugs.js'
const DraftPosts: CollectionConfig = {
slug: draftCollectionSlug,
access: {
update: () => {
return {
restrictedToUpdate: {
not_equals: true,
},
}
},
read: ({ req: { user } }) => {
if (user) {
return true
@@ -111,6 +118,10 @@ const DraftPosts: CollectionConfig = {
type: 'relationship',
relationTo: draftCollectionSlug,
},
{
name: 'restrictedToUpdate',
type: 'checkbox',
},
],
versions: {
drafts: {

View File

@@ -1792,6 +1792,51 @@ describe('Versions', () => {
expect(retrieved._status).toStrictEqual('published')
})
it('should restrict scheduled publish based on user', async () => {
const draft = await payload.create({
collection: draftCollectionSlug,
data: {
title: 'my doc to publish in the future',
description: 'hello',
restrictedToUpdate: true,
},
draft: true,
})
expect(draft._status).toStrictEqual('draft')
const currentDate = new Date()
const user = (
await payload.find({ collection: 'users', where: { email: { equals: devUser.email } } })
).docs[0]
await payload.jobs.queue({
task: 'schedulePublish',
waitUntil: new Date(currentDate.getTime() + 3000),
input: {
doc: {
relationTo: draftCollectionSlug,
value: draft.id,
},
user: user.id,
},
})
await wait(4000)
const res = await payload.jobs.run()
expect(res.jobStatus[Object.keys(res.jobStatus)[0]].status).toBe('error-reached-max-retries')
const retrieved = await payload.findByID({
collection: draftCollectionSlug,
id: draft.id,
})
expect(retrieved._status).toStrictEqual('draft')
})
it('should allow collection scheduled unpublish', async () => {
const published = await payload.create({
collection: draftCollectionSlug,

View File

@@ -157,6 +157,7 @@ export interface DraftPost {
}[]
| null;
relation?: (string | null) | DraftPost;
restrictedToUpdate?: boolean | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
@@ -459,6 +460,7 @@ export interface DraftPostsSelect<T extends boolean = true> {
};
};
relation?: T;
restrictedToUpdate?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;
@@ -723,6 +725,7 @@ export interface TaskSchedulePublish {
value: string | DraftPost;
} | null;
global?: 'draft-global' | null;
user?: (string | null) | User;
};
output?: unknown;
}