mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 00:03:23 +00:00
Merge pull request #20 from xtr-dev/dev
Simplify job system architecture
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 [
|
||||
export const mailingJobs = [
|
||||
{
|
||||
slug: 'processEmails',
|
||||
handler: async ({ job, req }: { job: any; req: any }) => {
|
||||
return processEmailsJob(
|
||||
// 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 }
|
||||
{ req, mailingService: mailingContext.service }
|
||||
)
|
||||
|
||||
return {
|
||||
output: {
|
||||
success: true,
|
||||
message: 'Email queue processing completed successfully'
|
||||
}
|
||||
}
|
||||
},
|
||||
interfaceName: 'ProcessEmailsJob',
|
||||
},
|
||||
sendEmailJob,
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
export * from './processEmailsJob.js'
|
||||
export * from './sendEmailTask.js'
|
||||
@@ -208,6 +208,7 @@ export const sendEmailJob = {
|
||||
})
|
||||
|
||||
return {
|
||||
output: {
|
||||
success: true,
|
||||
emailId: email.id,
|
||||
message: `Email queued successfully with ID: ${email.id}`,
|
||||
@@ -217,16 +218,12 @@ export const sendEmailJob = {
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user