mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-11 08:43:24 +00:00
Add mailing plugin with templates, outbox, and job processing
This commit is contained in:
20
src/jobs/index.ts
Normal file
20
src/jobs/index.ts
Normal 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'
|
||||
50
src/jobs/processOutboxJob.ts
Normal file
50
src/jobs/processOutboxJob.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user