mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
- Created centralized logger utility using Payload's built-in logger system - Added PAYLOAD_MAILING_LOG_LEVEL environment variable for log level configuration - Replaced all console.log/error/warn calls with structured logger - Added debug logging for immediate processing flow to help troubleshoot issues - Improved logging context with specific prefixes (IMMEDIATE, PROCESSOR, JOB_SCHEDULER, etc.) - Bumped version to 0.4.10 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import type { PayloadRequest, Payload } from 'payload'
|
|
import { processAllEmails } from '../utils/emailProcessor.js'
|
|
import { createContextLogger } from '../utils/logger.js'
|
|
|
|
/**
|
|
* Data passed to the process emails task
|
|
*/
|
|
export interface ProcessEmailsTaskData {
|
|
// Currently no data needed - always processes both pending and failed emails
|
|
}
|
|
|
|
/**
|
|
* Handler function for processing emails
|
|
* Used internally by the task definition
|
|
*/
|
|
export const processEmailsTaskHandler = async (
|
|
job: { data: ProcessEmailsTaskData },
|
|
context: { req: PayloadRequest }
|
|
) => {
|
|
const { req } = context
|
|
const payload = (req as any).payload
|
|
|
|
// Use the shared email processing logic
|
|
await processAllEmails(payload)
|
|
}
|
|
|
|
/**
|
|
* Task definition for processing emails
|
|
* This is what gets registered with Payload's job system
|
|
*/
|
|
export const processEmailsTask = {
|
|
slug: 'process-emails',
|
|
handler: async ({ job, req }: { job: any; req: any }) => {
|
|
// Get mailing context from payload
|
|
const payload = (req as any).payload
|
|
const mailingContext = payload.mailing
|
|
|
|
if (!mailingContext) {
|
|
throw new Error('Mailing plugin not properly initialized')
|
|
}
|
|
|
|
// Use the task handler
|
|
await processEmailsTaskHandler(
|
|
job as { data: ProcessEmailsTaskData },
|
|
{ req }
|
|
)
|
|
|
|
return {
|
|
output: {
|
|
success: true,
|
|
message: 'Email queue processing completed successfully'
|
|
}
|
|
}
|
|
},
|
|
interfaceName: 'ProcessEmailsTask',
|
|
}
|
|
|
|
// For backward compatibility, export as processEmailsJob
|
|
export const processEmailsJob = processEmailsTask
|
|
|
|
/**
|
|
* Helper function to schedule an email processing job
|
|
* Used by the plugin during initialization and can be used by developers
|
|
*/
|
|
export const scheduleEmailsJob = async (
|
|
payload: Payload,
|
|
queueName: string,
|
|
delay?: number
|
|
) => {
|
|
if (!payload.jobs) {
|
|
const logger = createContextLogger(payload, 'SCHEDULER')
|
|
logger.warn('PayloadCMS jobs not configured - emails will not be processed automatically')
|
|
return
|
|
}
|
|
|
|
try {
|
|
await payload.jobs.queue({
|
|
queue: queueName,
|
|
task: 'process-emails',
|
|
input: {},
|
|
waitUntil: delay ? new Date(Date.now() + delay) : undefined,
|
|
} as any)
|
|
} catch (error) {
|
|
const logger = createContextLogger(payload, 'SCHEDULER')
|
|
logger.error('Failed to schedule email processing job:', error)
|
|
}
|
|
}
|