Files
payload/packages/ui/src/utilities/schedulePublishHandler.ts
Dan Ribbens f95d6ba94a feat: delete scheduled published events (#10504)
### What?

Allows a user to delete a scheduled publish event after it has been
added:

![image](https://github.com/user-attachments/assets/79b1a206-c8a7-4ffa-a9bf-d0f84f86b8f9)

### Why?

Previously a user had no control over making changes once scheduled.

### How?

Extends the `scheduledPublishHandler` server action to accept a
`deleteID` for the event that should be removed and exposes this to the
user via the admin UI in a new column in the Upcoming Events table.
2025-01-13 19:41:38 +00:00

87 lines
1.9 KiB
TypeScript

import type { PayloadRequest, SchedulePublishTaskInput } from 'payload'
export type SchedulePublishHandlerArgs = {
date?: Date
/**
* The job id to delete to remove a scheduled publish event
*/
deleteID?: number | string
req: PayloadRequest
} & SchedulePublishTaskInput
export const schedulePublishHandler = async ({
type,
date,
deleteID,
doc,
global,
locale,
req,
}: SchedulePublishHandlerArgs) => {
const { i18n, payload, user } = req
const incomingUserSlug = user?.collection
const adminUserSlug = payload.config.admin.user
if (!incomingUserSlug) {
throw new Error('Unauthorized')
}
const adminAccessFunction = payload.collections[incomingUserSlug].config.access?.admin
// Run the admin access function from the config if it exists
if (adminAccessFunction) {
const canAccessAdmin = await adminAccessFunction({ req })
if (!canAccessAdmin) {
throw new Error('Unauthorized')
}
// Match the user collection to the global admin config
} else if (adminUserSlug !== incomingUserSlug) {
throw new Error('Unauthorized')
}
try {
if (deleteID) {
await payload.delete({
collection: 'payload-jobs',
req,
where: { id: { equals: deleteID } },
})
}
await payload.jobs.queue({
input: {
type,
doc,
global,
locale,
user: user.id,
},
task: 'schedulePublish',
waitUntil: date,
})
} catch (err) {
let error
if (deleteID) {
error = `Error deleting scheduled publish event with ID ${deleteID}`
} else {
error = `Error scheduling ${type} for `
if (doc) {
error += `document with ID ${doc.value} in collection ${doc.relationTo}`
}
}
payload.logger.error(error)
payload.logger.error(err)
return {
error,
}
}
return { message: i18n.t('general:success') }
}