mirror of
https://github.com/xtr-dev/payload-mailing.git
synced 2025-12-10 16:23:23 +00:00
Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e1128f1e8 | ||
|
|
50ce181893 | ||
|
|
2c0f202518 | ||
|
|
aa5a03b5b0 | ||
|
|
2220d83288 | ||
|
|
de1ae636de | ||
|
|
5e0ed0a03a | ||
|
|
060b1914b6 | ||
|
|
d82b3f2276 | ||
|
|
05f4cd0d7c | ||
|
|
bba223410d | ||
|
|
a40d87c63c | ||
|
|
fde8eb538d | ||
|
|
ff94d72d49 | ||
|
|
0083e8e1fa | ||
|
|
6cf055178b | ||
|
|
556d910e30 | ||
|
|
efdfaf5889 | ||
|
|
f12ac8172e | ||
|
|
672ab3236a | ||
|
|
7f04275d39 | ||
|
|
ea87f14308 | ||
|
|
ff788c1ecf | ||
|
|
72f3d7f66d | ||
|
|
5905f732de | ||
|
|
685875d1b9 | ||
|
|
768b70a003 | ||
|
|
21b22a033a | ||
|
|
03f1f62fbf | ||
|
|
e38b63d814 | ||
|
|
c78a8c2480 | ||
|
|
0c4d894f51 | ||
|
|
6d4e020133 | ||
|
|
b3de54b953 | ||
|
|
ed058c0721 | ||
|
|
273dea5a73 | ||
|
|
c81ef7f8a8 | ||
|
|
cddcfb1e4c | ||
|
|
80d32674a9 | ||
|
|
c7af628beb | ||
|
|
048fa33747 | ||
|
|
cb62874500 | ||
|
|
9efea193b1 | ||
|
|
c09d7d4fc5 |
@@ -123,6 +123,123 @@ export default buildConfig({
|
|||||||
retryDelay: 60000, // 1 minute for dev
|
retryDelay: 60000, // 1 minute for dev
|
||||||
queue: 'default',
|
queue: 'default',
|
||||||
|
|
||||||
|
// Example: Collection overrides for customization
|
||||||
|
// Uncomment and modify as needed for your use case
|
||||||
|
/*
|
||||||
|
collections: {
|
||||||
|
templates: {
|
||||||
|
// Custom access controls - restrict who can manage templates
|
||||||
|
access: {
|
||||||
|
read: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:read')
|
||||||
|
},
|
||||||
|
create: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:create')
|
||||||
|
},
|
||||||
|
update: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:update')
|
||||||
|
},
|
||||||
|
delete: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Custom admin UI settings
|
||||||
|
admin: {
|
||||||
|
group: 'Marketing',
|
||||||
|
description: 'Email templates with enhanced security and categorization'
|
||||||
|
},
|
||||||
|
// Add custom fields to templates
|
||||||
|
fields: [
|
||||||
|
// Default plugin fields are automatically included
|
||||||
|
{
|
||||||
|
name: 'category',
|
||||||
|
type: 'select',
|
||||||
|
options: [
|
||||||
|
{ label: 'Marketing', value: 'marketing' },
|
||||||
|
{ label: 'Transactional', value: 'transactional' },
|
||||||
|
{ label: 'System Notifications', value: 'system' }
|
||||||
|
],
|
||||||
|
defaultValue: 'transactional',
|
||||||
|
admin: {
|
||||||
|
position: 'sidebar',
|
||||||
|
description: 'Template category for organization'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tags',
|
||||||
|
type: 'text',
|
||||||
|
hasMany: true,
|
||||||
|
admin: {
|
||||||
|
position: 'sidebar',
|
||||||
|
description: 'Tags for easy template filtering'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'isActive',
|
||||||
|
type: 'checkbox',
|
||||||
|
defaultValue: true,
|
||||||
|
admin: {
|
||||||
|
position: 'sidebar',
|
||||||
|
description: 'Only active templates can be used'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
// Custom validation hooks
|
||||||
|
hooks: {
|
||||||
|
beforeChange: [
|
||||||
|
({ data, req }) => {
|
||||||
|
// Example: Only admins can create system templates
|
||||||
|
if (data.category === 'system' && req.user?.role !== 'admin') {
|
||||||
|
throw new Error('Only administrators can create system notification templates')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example: Auto-generate slug if not provided
|
||||||
|
if (!data.slug && data.name) {
|
||||||
|
data.slug = data.name.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9]/g, '-')
|
||||||
|
.replace(/-+/g, '-')
|
||||||
|
.replace(/^-|-$/g, '')
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
emails: {
|
||||||
|
// Restrict access to emails collection
|
||||||
|
access: {
|
||||||
|
read: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:read')
|
||||||
|
},
|
||||||
|
create: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:create')
|
||||||
|
},
|
||||||
|
update: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin' || user.permissions?.includes('mailing:update')
|
||||||
|
},
|
||||||
|
delete: ({ req: { user } }) => {
|
||||||
|
if (!user) return false
|
||||||
|
return user.role === 'admin'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Custom admin configuration for emails
|
||||||
|
admin: {
|
||||||
|
group: 'Marketing',
|
||||||
|
description: 'Email delivery tracking and management',
|
||||||
|
defaultColumns: ['subject', 'to', 'status', 'priority', 'scheduledAt'],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
|
||||||
// Optional: Custom rich text editor configuration
|
// Optional: Custom rich text editor configuration
|
||||||
// Comment out to use default lexical editor
|
// Comment out to use default lexical editor
|
||||||
richTextEditor: lexicalEditor({
|
richTextEditor: lexicalEditor({
|
||||||
@@ -139,6 +256,12 @@ export default buildConfig({
|
|||||||
// etc.
|
// etc.
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
||||||
|
// Called after mailing plugin is fully initialized
|
||||||
|
onReady: async (payload) => {
|
||||||
|
await seedUser(payload)
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
secret: process.env.PAYLOAD_SECRET || 'test-secret_key',
|
secret: process.env.PAYLOAD_SECRET || 'test-secret_key',
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-mailing",
|
"name": "@xtr-dev/payload-mailing",
|
||||||
"version": "0.4.21",
|
"version": "0.4.13",
|
||||||
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
"description": "Template-based email system with scheduling and job processing for PayloadCMS",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { CollectionConfig } from 'payload'
|
import type { CollectionConfig } from 'payload'
|
||||||
import { findExistingJobs, ensureEmailJob, updateEmailJobRelationship } from '../utils/jobScheduler.js'
|
import { findExistingJobs, ensureEmailJob, updateEmailJobRelationship } from '../utils/jobScheduler.js'
|
||||||
import { createContextLogger } from '../utils/logger.js'
|
import { createContextLogger } from '../utils/logger.js'
|
||||||
import { resolveID } from '../utils/helpers.js'
|
|
||||||
|
|
||||||
const Emails: CollectionConfig = {
|
const Emails: CollectionConfig = {
|
||||||
slug: 'emails',
|
slug: 'emails',
|
||||||
@@ -11,26 +10,6 @@ const Emails: CollectionConfig = {
|
|||||||
group: 'Mailing',
|
group: 'Mailing',
|
||||||
description: 'Email delivery and status tracking',
|
description: 'Email delivery and status tracking',
|
||||||
},
|
},
|
||||||
defaultPopulate: {
|
|
||||||
templateSlug: true,
|
|
||||||
to: true,
|
|
||||||
cc: true,
|
|
||||||
bcc: true,
|
|
||||||
from: true,
|
|
||||||
replyTo: true,
|
|
||||||
jobs: true,
|
|
||||||
status: true,
|
|
||||||
attempts: true,
|
|
||||||
lastAttemptAt: true,
|
|
||||||
error: true,
|
|
||||||
priority: true,
|
|
||||||
scheduledAt: true,
|
|
||||||
sentAt: true,
|
|
||||||
variables: true,
|
|
||||||
html: true,
|
|
||||||
text: true,
|
|
||||||
createdAt: true,
|
|
||||||
},
|
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'template',
|
name: 'template',
|
||||||
@@ -40,14 +19,6 @@ const Emails: CollectionConfig = {
|
|||||||
description: 'Email template used (optional if custom content provided)',
|
description: 'Email template used (optional if custom content provided)',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'templateSlug',
|
|
||||||
type: 'text',
|
|
||||||
admin: {
|
|
||||||
description: 'Slug of the email template (auto-populated from template relationship)',
|
|
||||||
readOnly: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'to',
|
name: 'to',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
@@ -206,37 +177,15 @@ const Emails: CollectionConfig = {
|
|||||||
readOnly: true,
|
readOnly: true,
|
||||||
},
|
},
|
||||||
filterOptions: ({ id }) => {
|
filterOptions: ({ id }) => {
|
||||||
const emailId = resolveID(id)
|
|
||||||
return {
|
return {
|
||||||
'input.emailId': {
|
'input.emailId': {
|
||||||
equals: emailId ? String(emailId) : '',
|
equals: id,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
hooks: {
|
hooks: {
|
||||||
beforeChange: [
|
|
||||||
async ({ data, req }) => {
|
|
||||||
// Auto-populate templateSlug from template relationship
|
|
||||||
if (data.template) {
|
|
||||||
try {
|
|
||||||
const template = await req.payload.findByID({
|
|
||||||
collection: 'email-templates',
|
|
||||||
id: typeof data.template === 'string' ? data.template : data.template.id,
|
|
||||||
})
|
|
||||||
data.templateSlug = template.slug
|
|
||||||
} catch (error) {
|
|
||||||
// If template lookup fails, clear the slug
|
|
||||||
data.templateSlug = undefined
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Clear templateSlug if template is removed
|
|
||||||
data.templateSlug = undefined
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
],
|
|
||||||
// Simple approach: Only use afterChange hook for job management
|
// Simple approach: Only use afterChange hook for job management
|
||||||
// This avoids complex interaction between hooks and ensures document ID is always available
|
// This avoids complex interaction between hooks and ensures document ID is always available
|
||||||
afterChange: [
|
afterChange: [
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { Payload } from 'payload'
|
import { Payload } from 'payload'
|
||||||
import { getMailing, renderTemplateWithId, parseAndValidateEmails, sanitizeFromName } from './utils/helpers.js'
|
import { getMailing, renderTemplate, parseAndValidateEmails, sanitizeFromName } from './utils/helpers.js'
|
||||||
import { BaseEmailDocument } from './types/index.js'
|
import { BaseEmailDocument } from './types/index.js'
|
||||||
import { processJobById } from './utils/emailProcessor.js'
|
import { processJobById } from './utils/emailProcessor.js'
|
||||||
import { createContextLogger } from './utils/logger.js'
|
import { createContextLogger } from './utils/logger.js'
|
||||||
import { pollForJobId } from './utils/jobPolling.js'
|
|
||||||
|
|
||||||
// Options for sending emails
|
// Options for sending emails
|
||||||
export interface SendEmailOptions<T extends BaseEmailDocument = BaseEmailDocument> {
|
export interface SendEmailOptions<T extends BaseEmailDocument = BaseEmailDocument> {
|
||||||
@@ -49,17 +48,17 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
|
|
||||||
let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail>
|
let emailData: Partial<TEmail> = { ...options.data } as Partial<TEmail>
|
||||||
|
|
||||||
|
// If using a template, render it first
|
||||||
if (options.template) {
|
if (options.template) {
|
||||||
// Look up and render the template in a single operation to avoid duplicate lookups
|
const { html, text, subject } = await renderTemplate(
|
||||||
const { html, text, subject, templateId } = await renderTemplateWithId(
|
|
||||||
payload,
|
payload,
|
||||||
options.template.slug,
|
options.template.slug,
|
||||||
options.template.variables || {}
|
options.template.variables || {}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Template values take precedence over data values
|
||||||
emailData = {
|
emailData = {
|
||||||
...emailData,
|
...emailData,
|
||||||
template: templateId,
|
|
||||||
subject,
|
subject,
|
||||||
html,
|
html,
|
||||||
text,
|
text,
|
||||||
@@ -71,16 +70,20 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
throw new Error('Field "to" is required for sending emails')
|
throw new Error('Field "to" is required for sending emails')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate required fields based on whether template was used
|
||||||
if (options.template) {
|
if (options.template) {
|
||||||
|
// When using template, subject and html should have been set by renderTemplate
|
||||||
if (!emailData.subject || !emailData.html) {
|
if (!emailData.subject || !emailData.html) {
|
||||||
throw new Error(`Template rendering failed: template "${options.template.slug}" did not provide required subject and html content`)
|
throw new Error(`Template rendering failed: template "${options.template.slug}" did not provide required subject and html content`)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// When not using template, user must provide subject and html directly
|
||||||
if (!emailData.subject || !emailData.html) {
|
if (!emailData.subject || !emailData.html) {
|
||||||
throw new Error('Fields "subject" and "html" are required when sending direct emails without a template')
|
throw new Error('Fields "subject" and "html" are required when sending direct emails without a template')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process email addresses using shared validation (handle null values)
|
||||||
if (emailData.to) {
|
if (emailData.to) {
|
||||||
emailData.to = parseAndValidateEmails(emailData.to as string | string[])
|
emailData.to = parseAndValidateEmails(emailData.to as string | string[])
|
||||||
}
|
}
|
||||||
@@ -92,15 +95,19 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
}
|
}
|
||||||
if (emailData.replyTo) {
|
if (emailData.replyTo) {
|
||||||
const validated = parseAndValidateEmails(emailData.replyTo as string | string[])
|
const validated = parseAndValidateEmails(emailData.replyTo as string | string[])
|
||||||
|
// replyTo should be a single email, so take the first one if array
|
||||||
emailData.replyTo = validated && validated.length > 0 ? validated[0] : undefined
|
emailData.replyTo = validated && validated.length > 0 ? validated[0] : undefined
|
||||||
}
|
}
|
||||||
if (emailData.from) {
|
if (emailData.from) {
|
||||||
const validated = parseAndValidateEmails(emailData.from as string | string[])
|
const validated = parseAndValidateEmails(emailData.from as string | string[])
|
||||||
|
// from should be a single email, so take the first one if array
|
||||||
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
emailData.from = validated && validated.length > 0 ? validated[0] : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sanitize fromName to prevent header injection
|
||||||
emailData.fromName = sanitizeFromName(emailData.fromName as string)
|
emailData.fromName = sanitizeFromName(emailData.fromName as string)
|
||||||
|
|
||||||
|
// Normalize Date objects to ISO strings for consistent database storage
|
||||||
if (emailData.scheduledAt instanceof Date) {
|
if (emailData.scheduledAt instanceof Date) {
|
||||||
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
emailData.scheduledAt = emailData.scheduledAt.toISOString()
|
||||||
}
|
}
|
||||||
@@ -117,15 +124,19 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
emailData.updatedAt = emailData.updatedAt.toISOString()
|
emailData.updatedAt = emailData.updatedAt.toISOString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the email in the collection with proper typing
|
||||||
|
// The hooks will automatically create and populate the job relationship
|
||||||
const email = await payload.create({
|
const email = await payload.create({
|
||||||
collection: collectionSlug,
|
collection: collectionSlug,
|
||||||
data: emailData
|
data: emailData
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Validate that the created email has the expected structure
|
||||||
if (!email || typeof email !== 'object' || !email.id) {
|
if (!email || typeof email !== 'object' || !email.id) {
|
||||||
throw new Error('Failed to create email: invalid response from database')
|
throw new Error('Failed to create email: invalid response from database')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If processImmediately is true, get the job from the relationship and process it now
|
||||||
if (options.processImmediately) {
|
if (options.processImmediately) {
|
||||||
const logger = createContextLogger(payload, 'IMMEDIATE')
|
const logger = createContextLogger(payload, 'IMMEDIATE')
|
||||||
|
|
||||||
@@ -133,14 +144,71 @@ export const sendEmail = async <TEmail extends BaseEmailDocument = BaseEmailDocu
|
|||||||
throw new Error('PayloadCMS jobs not configured - cannot process email immediately')
|
throw new Error('PayloadCMS jobs not configured - cannot process email immediately')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poll for the job ID using configurable polling mechanism
|
// Poll for the job with optimized backoff and timeout protection
|
||||||
const { jobId } = await pollForJobId({
|
// This handles the async nature of hooks and ensures we wait for job creation
|
||||||
payload,
|
const maxAttempts = 5 // Reduced from 10 to minimize delay
|
||||||
collectionSlug,
|
const initialDelay = 25 // Reduced from 50ms for faster response
|
||||||
emailId: email.id,
|
const maxTotalTime = 3000 // 3 second total timeout
|
||||||
config: mailingConfig.jobPolling,
|
const startTime = Date.now()
|
||||||
logger,
|
let jobId: string | undefined
|
||||||
})
|
|
||||||
|
|
||||||
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
||||||
|
// Check total timeout before continuing
|
||||||
|
if (Date.now() - startTime > maxTotalTime) {
|
||||||
|
throw new Error(
|
||||||
|
`Job polling timed out after ${maxTotalTime}ms for email ${email.id}. ` +
|
||||||
|
`The auto-scheduling may have failed or is taking longer than expected.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate delay with exponential backoff (25ms, 50ms, 100ms, 200ms, 400ms)
|
||||||
|
// Cap at 400ms per attempt for better responsiveness
|
||||||
|
const delay = Math.min(initialDelay * Math.pow(2, attempt), 400)
|
||||||
|
|
||||||
|
if (attempt > 0) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, delay))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refetch the email to check for jobs
|
||||||
|
const emailWithJobs = await payload.findByID({
|
||||||
|
collection: collectionSlug,
|
||||||
|
id: email.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
if (emailWithJobs.jobs && emailWithJobs.jobs.length > 0) {
|
||||||
|
// Job found! Get the first job ID (should only be one for a new email)
|
||||||
|
const firstJob = Array.isArray(emailWithJobs.jobs) ? emailWithJobs.jobs[0] : emailWithJobs.jobs
|
||||||
|
jobId = typeof firstJob === 'string' ? firstJob : String(firstJob.id || firstJob)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log on later attempts to help with debugging (reduced threshold)
|
||||||
|
if (attempt >= 1) {
|
||||||
|
if (attempt >= 2) {
|
||||||
|
logger.debug(`Waiting for job creation for email ${email.id}, attempt ${attempt + 1}/${maxAttempts}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jobId) {
|
||||||
|
// Distinguish between different failure scenarios for better error handling
|
||||||
|
const timeoutMsg = Date.now() - startTime >= maxTotalTime
|
||||||
|
const errorType = timeoutMsg ? 'POLLING_TIMEOUT' : 'JOB_NOT_FOUND'
|
||||||
|
|
||||||
|
const baseMessage = timeoutMsg
|
||||||
|
? `Job polling timed out after ${maxTotalTime}ms for email ${email.id}`
|
||||||
|
: `No processing job found for email ${email.id} after ${maxAttempts} attempts (${Date.now() - startTime}ms)`
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`${errorType}: ${baseMessage}. ` +
|
||||||
|
`This indicates the email was created but job auto-scheduling failed. ` +
|
||||||
|
`The email exists in the database but immediate processing cannot proceed. ` +
|
||||||
|
`You may need to: 1) Check job queue configuration, 2) Verify database hooks are working, ` +
|
||||||
|
`3) Process the email later using processEmailById('${email.id}').`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await processJobById(payload, jobId)
|
await processJobById(payload, jobId)
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import {CollectionSlug, EmailAdapter, Payload, SendEmailOptions} from 'payload'
|
import { Payload } from 'payload'
|
||||||
import { Liquid } from 'liquidjs'
|
import { Liquid } from 'liquidjs'
|
||||||
import {
|
import {
|
||||||
MailingPluginConfig,
|
MailingPluginConfig,
|
||||||
TemplateVariables,
|
TemplateVariables,
|
||||||
MailingService as IMailingService,
|
MailingService as IMailingService,
|
||||||
BaseEmailDocument, BaseEmailTemplateDocument
|
BaseEmail, BaseEmailTemplate, BaseEmailDocument, BaseEmailTemplateDocument
|
||||||
} from '../types/index.js'
|
} from '../types/index.js'
|
||||||
import { serializeRichTextToHTML, serializeRichTextToText } from '../utils/richTextSerializer.js'
|
import { serializeRichTextToHTML, serializeRichTextToText } from '../utils/richTextSerializer.js'
|
||||||
import { sanitizeDisplayName } from '../utils/helpers.js'
|
import { sanitizeDisplayName } from '../utils/helpers.js'
|
||||||
@@ -12,6 +12,7 @@ import { sanitizeDisplayName } from '../utils/helpers.js'
|
|||||||
export class MailingService implements IMailingService {
|
export class MailingService implements IMailingService {
|
||||||
public payload: Payload
|
public payload: Payload
|
||||||
private config: MailingPluginConfig
|
private config: MailingPluginConfig
|
||||||
|
private emailAdapter: any
|
||||||
private templatesCollection: string
|
private templatesCollection: string
|
||||||
private emailsCollection: string
|
private emailsCollection: string
|
||||||
private liquid: Liquid | null | false = null
|
private liquid: Liquid | null | false = null
|
||||||
@@ -30,13 +31,14 @@ export class MailingService implements IMailingService {
|
|||||||
if (!this.payload.email) {
|
if (!this.payload.email) {
|
||||||
throw new Error('Payload email configuration is required. Please configure email in your Payload config.')
|
throw new Error('Payload email configuration is required. Please configure email in your Payload config.')
|
||||||
}
|
}
|
||||||
|
this.emailAdapter = this.payload.email
|
||||||
}
|
}
|
||||||
|
|
||||||
private ensureInitialized(): void {
|
private ensureInitialized(): void {
|
||||||
if (!this.payload || !this.payload.db) {
|
if (!this.payload || !this.payload.db) {
|
||||||
throw new Error('MailingService payload not properly initialized')
|
throw new Error('MailingService payload not properly initialized')
|
||||||
}
|
}
|
||||||
if (!this.payload.email) {
|
if (!this.emailAdapter) {
|
||||||
throw new Error('Email adapter not configured. Please ensure Payload has email configured.')
|
throw new Error('Email adapter not configured. Please ensure Payload has email configured.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,17 +127,6 @@ export class MailingService implements IMailingService {
|
|||||||
throw new Error(`Email template not found: ${templateSlug}`)
|
throw new Error(`Email template not found: ${templateSlug}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.renderTemplateDocument(template, variables)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render a template document (for when you already have the template loaded)
|
|
||||||
* This avoids duplicate template lookups
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
async renderTemplateDocument(template: BaseEmailTemplateDocument, variables: TemplateVariables): Promise<{ html: string; text: string; subject: string }> {
|
|
||||||
this.ensureInitialized()
|
|
||||||
|
|
||||||
const emailContent = await this.renderEmailTemplate(template, variables)
|
const emailContent = await this.renderEmailTemplate(template, variables)
|
||||||
const subject = await this.renderTemplateString(template.subject || '', variables)
|
const subject = await this.renderTemplateString(template.subject || '', variables)
|
||||||
|
|
||||||
@@ -151,7 +142,7 @@ export class MailingService implements IMailingService {
|
|||||||
const currentTime = new Date().toISOString()
|
const currentTime = new Date().toISOString()
|
||||||
|
|
||||||
const { docs: pendingEmails } = await this.payload.find({
|
const { docs: pendingEmails } = await this.payload.find({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
where: {
|
where: {
|
||||||
and: [
|
and: [
|
||||||
{
|
{
|
||||||
@@ -191,7 +182,7 @@ export class MailingService implements IMailingService {
|
|||||||
const retryTime = new Date(Date.now() - retryDelay).toISOString()
|
const retryTime = new Date(Date.now() - retryDelay).toISOString()
|
||||||
|
|
||||||
const { docs: failedEmails } = await this.payload.find({
|
const { docs: failedEmails } = await this.payload.find({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
where: {
|
where: {
|
||||||
and: [
|
and: [
|
||||||
{
|
{
|
||||||
@@ -231,7 +222,7 @@ export class MailingService implements IMailingService {
|
|||||||
async processEmailItem(emailId: string): Promise<void> {
|
async processEmailItem(emailId: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.payload.update({
|
await this.payload.update({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: 'processing',
|
status: 'processing',
|
||||||
@@ -240,9 +231,8 @@ export class MailingService implements IMailingService {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const email = await this.payload.findByID({
|
const email = await this.payload.findByID({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
depth: 1,
|
|
||||||
}) as BaseEmailDocument
|
}) as BaseEmailDocument
|
||||||
|
|
||||||
// Combine from and fromName for nodemailer using proper sanitization
|
// Combine from and fromName for nodemailer using proper sanitization
|
||||||
@@ -253,7 +243,7 @@ export class MailingService implements IMailingService {
|
|||||||
fromField = this.getDefaultFrom()
|
fromField = this.getDefaultFrom()
|
||||||
}
|
}
|
||||||
|
|
||||||
let mailOptions: SendEmailOptions = {
|
let mailOptions: any = {
|
||||||
from: fromField,
|
from: fromField,
|
||||||
to: email.to,
|
to: email.to,
|
||||||
cc: email.cc || undefined,
|
cc: email.cc || undefined,
|
||||||
@@ -264,19 +254,6 @@ export class MailingService implements IMailingService {
|
|||||||
text: email.text || undefined,
|
text: email.text || undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mailOptions.from) {
|
|
||||||
throw new Error('Email from field is required')
|
|
||||||
}
|
|
||||||
if (!mailOptions.to || (Array.isArray(mailOptions.to) && mailOptions.to.length === 0)) {
|
|
||||||
throw new Error('Email to field is required')
|
|
||||||
}
|
|
||||||
if (!mailOptions.subject) {
|
|
||||||
throw new Error('Email subject is required')
|
|
||||||
}
|
|
||||||
if (!mailOptions.html && !mailOptions.text) {
|
|
||||||
throw new Error('Email content is required')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call beforeSend hook if configured
|
// Call beforeSend hook if configured
|
||||||
if (this.config.beforeSend) {
|
if (this.config.beforeSend) {
|
||||||
try {
|
try {
|
||||||
@@ -302,10 +279,10 @@ export class MailingService implements IMailingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send email using Payload's email adapter
|
// Send email using Payload's email adapter
|
||||||
await this.payload.email.sendEmail(mailOptions)
|
await this.emailAdapter.sendEmail(mailOptions)
|
||||||
|
|
||||||
await this.payload.update({
|
await this.payload.update({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: 'sent',
|
status: 'sent',
|
||||||
@@ -319,7 +296,7 @@ export class MailingService implements IMailingService {
|
|||||||
const maxAttempts = this.config.retryAttempts || 3
|
const maxAttempts = this.config.retryAttempts || 3
|
||||||
|
|
||||||
await this.payload.update({
|
await this.payload.update({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
status: attempts >= maxAttempts ? 'failed' : 'pending',
|
status: attempts >= maxAttempts ? 'failed' : 'pending',
|
||||||
@@ -336,14 +313,14 @@ export class MailingService implements IMailingService {
|
|||||||
|
|
||||||
private async incrementAttempts(emailId: string): Promise<number> {
|
private async incrementAttempts(emailId: string): Promise<number> {
|
||||||
const email = await this.payload.findByID({
|
const email = await this.payload.findByID({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
})
|
})
|
||||||
|
|
||||||
const newAttempts = ((email as any).attempts || 0) + 1
|
const newAttempts = ((email as any).attempts || 0) + 1
|
||||||
|
|
||||||
await this.payload.update({
|
await this.payload.update({
|
||||||
collection: this.emailsCollection as CollectionSlug,
|
collection: this.emailsCollection as any,
|
||||||
id: emailId,
|
id: emailId,
|
||||||
data: {
|
data: {
|
||||||
attempts: newAttempts,
|
attempts: newAttempts,
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
import {Payload, SendEmailOptions} from 'payload'
|
import { Payload } from 'payload'
|
||||||
import type { CollectionConfig, RichTextField } from 'payload'
|
import type { CollectionConfig, RichTextField } from 'payload'
|
||||||
|
|
||||||
// Payload ID type (string or number)
|
|
||||||
export type PayloadID = string | number
|
|
||||||
|
|
||||||
// Payload relation type - can be populated (object with id) or unpopulated (just the ID)
|
|
||||||
export type PayloadRelation<T extends { id: PayloadID }> = T | PayloadID
|
|
||||||
|
|
||||||
// JSON value type that matches Payload's JSON field type
|
// JSON value type that matches Payload's JSON field type
|
||||||
export type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
export type JSONValue = string | number | boolean | { [k: string]: unknown } | unknown[] | null | undefined
|
||||||
|
|
||||||
@@ -14,7 +8,6 @@ export type JSONValue = string | number | boolean | { [k: string]: unknown } | u
|
|||||||
export interface BaseEmailDocument {
|
export interface BaseEmailDocument {
|
||||||
id: string | number
|
id: string | number
|
||||||
template?: any
|
template?: any
|
||||||
templateSlug?: string | null
|
|
||||||
to: string[]
|
to: string[]
|
||||||
cc?: string[] | null
|
cc?: string[] | null
|
||||||
bcc?: string[] | null
|
bcc?: string[] | null
|
||||||
@@ -46,19 +39,29 @@ export interface BaseEmailTemplateDocument {
|
|||||||
updatedAt?: string | Date | null
|
updatedAt?: string | Date | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type BaseEmail<TEmail extends BaseEmailDocument = BaseEmailDocument, TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmail, 'id' | 'template'> & {template: Omit<TEmailTemplate, 'id'> | TEmailTemplate['id'] | undefined | null}
|
||||||
|
|
||||||
|
export type BaseEmailTemplate<TEmailTemplate extends BaseEmailTemplateDocument = BaseEmailTemplateDocument> = Omit<TEmailTemplate, 'id'>
|
||||||
|
|
||||||
export type TemplateRendererHook = (template: string, variables: Record<string, any>) => string | Promise<string>
|
export type TemplateRendererHook = (template: string, variables: Record<string, any>) => string | Promise<string>
|
||||||
|
|
||||||
export type TemplateEngine = 'liquidjs' | 'mustache' | 'simple'
|
export type TemplateEngine = 'liquidjs' | 'mustache' | 'simple'
|
||||||
|
|
||||||
export type BeforeSendHook = (options: SendEmailOptions, email: BaseEmailDocument) => SendEmailOptions | Promise<SendEmailOptions>
|
export interface BeforeSendMailOptions {
|
||||||
|
from: string
|
||||||
export interface JobPollingConfig {
|
to: string[]
|
||||||
maxAttempts?: number // Maximum number of polling attempts (default: 5)
|
cc?: string[]
|
||||||
initialDelay?: number // Initial delay in milliseconds (default: 25)
|
bcc?: string[]
|
||||||
maxTotalTime?: number // Maximum total polling time in milliseconds (default: 3000)
|
replyTo?: string
|
||||||
maxBackoffDelay?: number // Maximum delay between attempts in milliseconds (default: 400)
|
subject: string
|
||||||
|
html: string
|
||||||
|
text?: string
|
||||||
|
attachments?: any[]
|
||||||
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type BeforeSendHook = (options: BeforeSendMailOptions, email: BaseEmailDocument) => BeforeSendMailOptions | Promise<BeforeSendMailOptions>
|
||||||
|
|
||||||
export interface MailingPluginConfig {
|
export interface MailingPluginConfig {
|
||||||
collections?: {
|
collections?: {
|
||||||
templates?: string | Partial<CollectionConfig>
|
templates?: string | Partial<CollectionConfig>
|
||||||
@@ -74,7 +77,6 @@ export interface MailingPluginConfig {
|
|||||||
richTextEditor?: RichTextField['editor']
|
richTextEditor?: RichTextField['editor']
|
||||||
beforeSend?: BeforeSendHook
|
beforeSend?: BeforeSendHook
|
||||||
initOrder?: 'before' | 'after'
|
initOrder?: 'before' | 'after'
|
||||||
jobPolling?: JobPollingConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueuedEmail {
|
export interface QueuedEmail {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Payload } from 'payload'
|
import { Payload } from 'payload'
|
||||||
import { TemplateVariables, PayloadID, PayloadRelation } from '../types/index.js'
|
import { TemplateVariables } from '../types/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse and validate email addresses
|
* Parse and validate email addresses
|
||||||
@@ -74,49 +74,6 @@ export const sanitizeFromName = (fromName: string | null | undefined): string |
|
|||||||
return sanitized.length > 0 ? sanitized : undefined
|
return sanitized.length > 0 ? sanitized : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Type guard to check if a Payload relation is populated (object) or unpopulated (ID)
|
|
||||||
*/
|
|
||||||
export const isPopulated = <T extends { id: PayloadID }>(
|
|
||||||
value: PayloadRelation<T> | null | undefined
|
|
||||||
): value is T => {
|
|
||||||
return value !== null && value !== undefined && typeof value === 'object' && 'id' in value
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolves a Payload relation to just the ID
|
|
||||||
* Handles both populated (object with id) and unpopulated (string/number) values
|
|
||||||
*/
|
|
||||||
export const resolveID = <T extends { id: PayloadID }>(
|
|
||||||
value: PayloadRelation<T> | null | undefined
|
|
||||||
): PayloadID | undefined => {
|
|
||||||
if (value === null || value === undefined) return undefined
|
|
||||||
|
|
||||||
if (typeof value === 'string' || typeof value === 'number') {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof value === 'object' && 'id' in value) {
|
|
||||||
return value.id
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolves an array of Payload relations to an array of IDs
|
|
||||||
* Handles mixed arrays of populated and unpopulated values
|
|
||||||
*/
|
|
||||||
export const resolveIDs = <T extends { id: PayloadID }>(
|
|
||||||
values: (PayloadRelation<T> | null | undefined)[] | null | undefined
|
|
||||||
): PayloadID[] => {
|
|
||||||
if (!values || !Array.isArray(values)) return []
|
|
||||||
|
|
||||||
return values
|
|
||||||
.map(value => resolveID(value))
|
|
||||||
.filter((id): id is PayloadID => id !== undefined)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getMailing = (payload: Payload) => {
|
export const getMailing = (payload: Payload) => {
|
||||||
const mailing = (payload as any).mailing
|
const mailing = (payload as any).mailing
|
||||||
if (!mailing) {
|
if (!mailing) {
|
||||||
@@ -130,53 +87,6 @@ export const renderTemplate = async (payload: Payload, templateSlug: string, var
|
|||||||
return mailing.service.renderTemplate(templateSlug, variables)
|
return mailing.service.renderTemplate(templateSlug, variables)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Render a template and return both rendered content and template ID
|
|
||||||
* This is used by sendEmail to avoid duplicate template lookups
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export const renderTemplateWithId = async (
|
|
||||||
payload: Payload,
|
|
||||||
templateSlug: string,
|
|
||||||
variables: TemplateVariables
|
|
||||||
): Promise<{ html: string; text: string; subject: string; templateId: PayloadID }> => {
|
|
||||||
const mailing = getMailing(payload)
|
|
||||||
const templatesCollection = mailing.config.collections?.templates || 'email-templates'
|
|
||||||
|
|
||||||
// Runtime validation: Ensure the collection exists in Payload
|
|
||||||
if (!payload.collections[templatesCollection]) {
|
|
||||||
throw new Error(
|
|
||||||
`Templates collection '${templatesCollection}' not found. ` +
|
|
||||||
`Available collections: ${Object.keys(payload.collections).join(', ')}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look up the template document once
|
|
||||||
const { docs: templateDocs } = await payload.find({
|
|
||||||
collection: templatesCollection as any,
|
|
||||||
where: {
|
|
||||||
slug: {
|
|
||||||
equals: templateSlug,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
limit: 1,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!templateDocs || templateDocs.length === 0) {
|
|
||||||
throw new Error(`Template not found: ${templateSlug}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const templateDoc = templateDocs[0]
|
|
||||||
|
|
||||||
// Render using the document directly to avoid duplicate lookup
|
|
||||||
const rendered = await mailing.service.renderTemplateDocument(templateDoc, variables)
|
|
||||||
|
|
||||||
return {
|
|
||||||
...rendered,
|
|
||||||
templateId: templateDoc.id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const processEmails = async (payload: Payload): Promise<void> => {
|
export const processEmails = async (payload: Payload): Promise<void> => {
|
||||||
const mailing = getMailing(payload)
|
const mailing = getMailing(payload)
|
||||||
return mailing.service.processEmails()
|
return mailing.service.processEmails()
|
||||||
|
|||||||
@@ -1,115 +0,0 @@
|
|||||||
import { Payload } from 'payload'
|
|
||||||
import { JobPollingConfig } from '../types/index.js'
|
|
||||||
|
|
||||||
export interface PollForJobIdOptions {
|
|
||||||
payload: Payload
|
|
||||||
collectionSlug: string
|
|
||||||
emailId: string | number
|
|
||||||
config?: JobPollingConfig
|
|
||||||
logger?: {
|
|
||||||
debug: (message: string, ...args: any[]) => void
|
|
||||||
info: (message: string, ...args: any[]) => void
|
|
||||||
warn: (message: string, ...args: any[]) => void
|
|
||||||
error: (message: string, ...args: any[]) => void
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PollForJobIdResult {
|
|
||||||
jobId: string
|
|
||||||
attempts: number
|
|
||||||
elapsedTime: number
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default job polling configuration values
|
|
||||||
const DEFAULT_JOB_POLLING_CONFIG: Required<JobPollingConfig> = {
|
|
||||||
maxAttempts: 5,
|
|
||||||
initialDelay: 25,
|
|
||||||
maxTotalTime: 3000,
|
|
||||||
maxBackoffDelay: 400,
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Polls for a job ID associated with an email document using exponential backoff.
|
|
||||||
* This utility handles the complexity of waiting for auto-scheduled jobs to be created.
|
|
||||||
*
|
|
||||||
* The polling mechanism uses exponential backoff with configurable parameters:
|
|
||||||
* - Starts with an initial delay and doubles on each retry
|
|
||||||
* - Caps individual delays at maxBackoffDelay
|
|
||||||
* - Enforces a maximum total polling time
|
|
||||||
*
|
|
||||||
* @param options - Polling options including payload, collection, email ID, and config
|
|
||||||
* @returns Promise resolving to job ID and timing information
|
|
||||||
* @throws Error if job is not found within the configured limits
|
|
||||||
*/
|
|
||||||
export const pollForJobId = async (options: PollForJobIdOptions): Promise<PollForJobIdResult> => {
|
|
||||||
const { payload, collectionSlug, emailId, logger } = options
|
|
||||||
|
|
||||||
// Merge user config with defaults
|
|
||||||
const config: Required<JobPollingConfig> = {
|
|
||||||
...DEFAULT_JOB_POLLING_CONFIG,
|
|
||||||
...options.config,
|
|
||||||
}
|
|
||||||
|
|
||||||
const { maxAttempts, initialDelay, maxTotalTime, maxBackoffDelay } = config
|
|
||||||
const startTime = Date.now()
|
|
||||||
let jobId: string | undefined
|
|
||||||
|
|
||||||
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
||||||
const elapsedTime = Date.now() - startTime
|
|
||||||
|
|
||||||
// Check if we've exceeded the maximum total polling time
|
|
||||||
if (elapsedTime > maxTotalTime) {
|
|
||||||
throw new Error(
|
|
||||||
`Job polling timed out after ${maxTotalTime}ms for email ${emailId}. ` +
|
|
||||||
`The auto-scheduling may have failed or is taking longer than expected.`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate exponential backoff delay, capped at maxBackoffDelay
|
|
||||||
const delay = Math.min(initialDelay * Math.pow(2, attempt), maxBackoffDelay)
|
|
||||||
|
|
||||||
// Wait before checking (skip on first attempt)
|
|
||||||
if (attempt > 0) {
|
|
||||||
await new Promise(resolve => setTimeout(resolve, delay))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch the email document to check for associated jobs
|
|
||||||
const emailWithJobs = await payload.findByID({
|
|
||||||
collection: collectionSlug,
|
|
||||||
id: emailId,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Check if jobs array exists and has entries
|
|
||||||
if (emailWithJobs.jobs && emailWithJobs.jobs.length > 0) {
|
|
||||||
const firstJob = Array.isArray(emailWithJobs.jobs) ? emailWithJobs.jobs[0] : emailWithJobs.jobs
|
|
||||||
jobId = typeof firstJob === 'string' ? firstJob : String(firstJob.id || firstJob)
|
|
||||||
|
|
||||||
return {
|
|
||||||
jobId,
|
|
||||||
attempts: attempt + 1,
|
|
||||||
elapsedTime: Date.now() - startTime,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log progress for attempts after the second try
|
|
||||||
if (attempt >= 2 && logger) {
|
|
||||||
logger.debug(`Waiting for job creation for email ${emailId}, attempt ${attempt + 1}/${maxAttempts}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we reach here, job was not found
|
|
||||||
const elapsedTime = Date.now() - startTime
|
|
||||||
const timeoutMsg = elapsedTime >= maxTotalTime
|
|
||||||
const errorType = timeoutMsg ? 'POLLING_TIMEOUT' : 'JOB_NOT_FOUND'
|
|
||||||
const baseMessage = timeoutMsg
|
|
||||||
? `Job polling timed out after ${maxTotalTime}ms for email ${emailId}`
|
|
||||||
: `No processing job found for email ${emailId} after ${maxAttempts} attempts (${elapsedTime}ms)`
|
|
||||||
|
|
||||||
throw new Error(
|
|
||||||
`${errorType}: ${baseMessage}. ` +
|
|
||||||
`This indicates the email was created but job auto-scheduling failed. ` +
|
|
||||||
`The email exists in the database but immediate processing cannot proceed. ` +
|
|
||||||
`You may need to: 1) Check job queue configuration, 2) Verify database hooks are working, ` +
|
|
||||||
`3) Process the email later using processEmailById('${emailId}').`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -129,11 +129,7 @@ export async function updateEmailJobRelationship(
|
|||||||
id: normalizedEmailId,
|
id: normalizedEmailId,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Extract IDs from job objects or use the value directly if it's already an ID
|
const currentJobs = (currentEmail.jobs || []).map((job: any) => String(job))
|
||||||
// Jobs can be populated (objects with id field) or just IDs (strings/numbers)
|
|
||||||
const currentJobs = (currentEmail.jobs || []).map((job: any) =>
|
|
||||||
typeof job === 'object' && job !== null && job.id ? String(job.id) : String(job)
|
|
||||||
)
|
|
||||||
const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings
|
const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings
|
||||||
|
|
||||||
await payload.update({
|
await payload.update({
|
||||||
|
|||||||
Reference in New Issue
Block a user