diff --git a/package.json b/package.json index e5426a8..0a25527 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-mailing", - "version": "0.1.3", + "version": "0.1.4", "description": "Template-based email system with scheduling and job processing for PayloadCMS", "type": "module", "main": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index 31f8d06..3bed52c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ export { default as EmailTemplates, createEmailTemplatesCollection } from './col export { default as Emails } from './collections/Emails.js' // Jobs (includes the send email task) -export { createMailingJobs, sendEmailJob } from './jobs/index.js' +export { mailingJobs, sendEmailJob } from './jobs/index.js' export type { SendEmailTaskInput } from './jobs/sendEmailTask.js' // Utility functions for developers diff --git a/src/jobs/index.ts b/src/jobs/index.ts index f16e2c1..86f5007 100644 --- a/src/jobs/index.ts +++ b/src/jobs/index.ts @@ -2,21 +2,34 @@ import { processEmailsJob, ProcessEmailsJobData } from './processEmailsJob.js' import { sendEmailJob } from './sendEmailTask.js' import { MailingService } from '../services/MailingService.js' -export const createMailingJobs = (mailingService: MailingService): any[] => { - return [ - { - slug: 'processEmails', - handler: async ({ job, req }: { job: any; req: any }) => { - return processEmailsJob( - job as { data: ProcessEmailsJobData }, - { req, mailingService } - ) - }, - interfaceName: 'ProcessEmailsJob', +export const mailingJobs = [ + { + slug: 'processEmails', + 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 existing mailing service from context + await processEmailsJob( + job as { data: ProcessEmailsJobData }, + { req, mailingService: mailingContext.service } + ) + + return { + output: { + success: true, + message: 'Email queue processing completed successfully' + } + } }, - sendEmailJob, - ] -} + interfaceName: 'ProcessEmailsJob', + }, + sendEmailJob, +] export * from './processEmailsJob.js' export * from './sendEmailTask.js' \ No newline at end of file diff --git a/src/jobs/sendEmailTask.ts b/src/jobs/sendEmailTask.ts index 66d3be6..c965d6e 100644 --- a/src/jobs/sendEmailTask.ts +++ b/src/jobs/sendEmailTask.ts @@ -208,25 +208,22 @@ export const sendEmailJob = { }) return { - success: true, - emailId: email.id, - message: `Email queued successfully with ID: ${email.id}`, - mode: taskInput.templateSlug ? 'template' : 'direct', - templateSlug: taskInput.templateSlug || null, - subject: subject, - recipients: emailData.to?.length || 0, - scheduledAt: emailData.scheduledAt || null + output: { + success: true, + emailId: email.id, + message: `Email queued successfully with ID: ${email.id}`, + mode: taskInput.templateSlug ? 'template' : 'direct', + templateSlug: taskInput.templateSlug || null, + subject: subject, + recipients: emailData.to?.length || 0, + scheduledAt: emailData.scheduledAt || null + } } } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error' - return { - success: false, - error: errorMessage, - templateSlug: taskInput.templateSlug, - message: `Failed to queue email: ${errorMessage}` - } + throw new Error(`Failed to queue email: ${errorMessage}`) } } } diff --git a/src/plugin.ts b/src/plugin.ts index 942c6b1..a447526 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,7 +3,7 @@ import { MailingPluginConfig, MailingContext } from './types/index.js' import { MailingService } from './services/MailingService.js' import { createEmailTemplatesCollection } from './collections/EmailTemplates.js' import Emails from './collections/Emails.js' -import { createMailingJobs, scheduleEmailsJob } from './jobs/index.js' +import { mailingJobs, scheduleEmailsJob } from './jobs/index.js' export const mailingPlugin = (pluginConfig: MailingPluginConfig) => (config: Config): Config => { @@ -14,14 +14,6 @@ export const mailingPlugin = (pluginConfig: MailingPluginConfig) => (config: Con throw new Error('Invalid queue configuration: queue must be a non-empty string') } - // Create a factory function that will provide the mailing service once initialized - const getMailingService = () => { - if (!mailingService) { - throw new Error('MailingService not yet initialized - this should only be called after plugin initialization') - } - return mailingService - } - let mailingService: MailingService // Handle templates collection configuration const templatesConfig = pluginConfig.collections?.templates @@ -93,7 +85,7 @@ export const mailingPlugin = (pluginConfig: MailingPluginConfig) => (config: Con ...(config.jobs || {}), tasks: [ ...(config.jobs?.tasks || []), - // Jobs will be properly added after initialization + ...mailingJobs, ], }, onInit: async (payload: any) => { @@ -102,13 +94,7 @@ export const mailingPlugin = (pluginConfig: MailingPluginConfig) => (config: Con } // Initialize mailing service with proper payload instance - mailingService = new MailingService(payload, pluginConfig) - - // Add mailing jobs to payload's job system - const mailingJobs = createMailingJobs(mailingService) - mailingJobs.forEach(job => { - payload.jobs.tasks.push(job) - }) + const mailingService = new MailingService(payload, pluginConfig) // Add mailing context to payload for developer access ;(payload as any).mailing = {