Files
payloadcms/test/queues/runners/updatePost.ts
Alessio Gravili 84cb2b5819 refactor: simplify job type (#12816)
Previously, there were multiple ways to type a running job:
- `GeneratedTypes['payload-jobs']` - only works in an installed project
- is `any` in monorepo
- `BaseJob` - works everywhere, but does not incorporate generated types
which may include type for custom fields added to the jobs collection
- `RunningJob<>` - more accurate version of `BaseJob`, but same problem

This PR deprecated all those types in favor of a new `Job` type.
Benefits:
- Works in both monorepo and installed projects. If no generated types
exist, it will automatically fall back to `BaseJob`
- Comes with an optional generic that can be used to narrow down
`job.input` based on the task / workflow slug. No need to use a separate
type helper like `RunningJob<>`

With this new type, I was able to replace every usage of
`GeneratedTypes['payload-jobs']`, `BaseJob` and `RunningJob<>` with the
simple `Job` type.

Additionally, this PR simplifies some of the logic used to run jobs
2025-06-16 16:15:56 -04:00

56 lines
1.1 KiB
TypeScript

import type { TaskHandler } from 'payload'
export const updatePostStep1: TaskHandler<'UpdatePost'> = async ({ req, input }) => {
const postID =
typeof input.post === 'string' || typeof input.post === 'number' ? input.post : input.post.id
if (!postID) {
return {
state: 'failed',
output: null,
}
}
await req.payload.update({
collection: 'posts',
id: postID,
req,
data: {
jobStep1Ran: input.message,
},
})
return {
state: 'succeeded',
output: {
messageTwice: input.message + input.message,
},
}
}
export const updatePostStep2: TaskHandler<'UpdatePostStep2'> = async ({ req, input, job }) => {
const postID =
typeof input.post === 'string' || typeof input.post === 'number' ? input.post : input.post.id
if (!postID) {
return {
state: 'failed',
output: null,
}
}
await req.payload.update({
collection: 'posts',
id: postID,
req,
data: {
jobStep2Ran: input.messageTwice + job.taskStatus.UpdatePost?.['1']?.output?.messageTwice,
},
})
return {
state: 'succeeded',
output: null,
}
}