mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
- Replace inline plugin task with jobs directory system - Move sendTemplateEmailTask to jobs/sendEmailTask.ts and integrate with createMailingJobs() - Simplify processEmailsJob to always process both pending and failed emails in one task - Remove separate 'retry-failed' task type - retry logic now runs automatically - Update MailingService to support lazy initialization for job context - Update exports to include consolidated job system 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import type { PayloadRequest } from 'payload'
|
|
import { MailingService } from '../services/MailingService.js'
|
|
|
|
export interface ProcessEmailsJobData {
|
|
// No type needed - always processes both pending and failed emails
|
|
}
|
|
|
|
export const processEmailsJob = async (
|
|
job: { data: ProcessEmailsJobData },
|
|
context: { req: PayloadRequest; mailingService: MailingService }
|
|
) => {
|
|
const { mailingService } = context
|
|
|
|
try {
|
|
console.log('🔄 Processing email queue (pending + failed emails)...')
|
|
|
|
// Process pending emails first
|
|
await mailingService.processEmails()
|
|
|
|
// Then retry failed emails
|
|
await mailingService.retryFailedEmails()
|
|
|
|
console.log('✅ Email queue processing completed successfully (pending and failed emails)')
|
|
} catch (error) {
|
|
console.error('❌ Email queue processing failed:', error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export const scheduleEmailsJob = async (
|
|
payload: any,
|
|
queueName: string,
|
|
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: 'processEmails',
|
|
input: {},
|
|
waitUntil: delay ? new Date(Date.now() + delay) : undefined,
|
|
})
|
|
} catch (error) {
|
|
console.error('Failed to schedule email processing job:', error)
|
|
}
|
|
} |