Add mailing plugin with templates, outbox, and job processing

This commit is contained in:
2025-09-12 19:18:14 +02:00
parent ebaed4fd07
commit ed9d979d3e
33 changed files with 13904 additions and 0 deletions

20
src/jobs/index.ts Normal file
View File

@@ -0,0 +1,20 @@
import { Job } from 'payload/jobs'
import { processOutboxJob, ProcessOutboxJobData } from './processOutboxJob'
import { MailingService } from '../services/MailingService'
export const createMailingJobs = (mailingService: MailingService): Job[] => {
return [
{
slug: 'processOutbox',
handler: async ({ job, req }) => {
return processOutboxJob(
job as { data: ProcessOutboxJobData },
{ req, mailingService }
)
},
interfaceName: 'ProcessOutboxJob',
},
]
}
export * from './processOutboxJob'

View File

@@ -0,0 +1,50 @@
import { PayloadRequest } from 'payload/types'
import { MailingService } from '../services/MailingService'
export interface ProcessOutboxJobData {
type: 'process-outbox' | 'retry-failed'
}
export const processOutboxJob = async (
job: { data: ProcessOutboxJobData },
context: { req: PayloadRequest; mailingService: MailingService }
) => {
const { mailingService } = context
const { type } = job.data
try {
if (type === 'process-outbox') {
await mailingService.processOutbox()
console.log('Outbox processing completed successfully')
} else if (type === 'retry-failed') {
await mailingService.retryFailedEmails()
console.log('Failed email retry completed successfully')
}
} catch (error) {
console.error(`${type} job failed:`, error)
throw error
}
}
export const scheduleOutboxJob = async (
payload: any,
queueName: string,
jobType: 'process-outbox' | 'retry-failed',
delay?: number
) => {
if (!payload.jobs) {
console.warn('PayloadCMS jobs not configured - emails will not be processed automatically')
return
}
try {
await payload.jobs.queue({
queue: queueName,
task: 'processOutbox',
input: { type: jobType },
waitUntil: delay ? new Date(Date.now() + delay) : undefined,
})
} catch (error) {
console.error(`Failed to schedule ${jobType} job:`, error)
}
}