Fix critical bugs and improve type safety

- Fix hard-coded collection name in sendEmailTask - now uses configurable collection name
- Add type validation for task input with proper error handling
- Add email format validation with regex to prevent invalid email addresses
- Fix potential memory leak in plugin initialization by properly initializing MailingService
- Add runtime validation for required fields
- Improve error messages and validation feedback

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-13 19:15:55 +02:00
parent 43557c9a03
commit 6db27093d1
2 changed files with 48 additions and 11 deletions

View File

@@ -116,9 +116,20 @@ export const sendEmailJob = {
}
],
handler: async ({ input, payload }: any) => {
// Cast input to our expected type
// Get mailing context from payload
const mailingContext = (payload as any).mailing
if (!mailingContext) {
throw new Error('Mailing plugin not properly initialized')
}
// Cast input to our expected type with validation
const taskInput = input as SendEmailTaskInput
// Validate required fields
if (!taskInput.to) {
throw new Error('Field "to" is required')
}
try {
let html: string
let text: string | undefined
@@ -145,11 +156,25 @@ export const sendEmailJob = {
text = taskInput.text
}
// Parse email addresses
// Parse and validate email addresses
const parseEmails = (emails: string | string[] | undefined): string[] | undefined => {
if (!emails) return undefined
if (Array.isArray(emails)) return emails
return emails.split(',').map(email => email.trim()).filter(Boolean)
let emailList: string[]
if (Array.isArray(emails)) {
emailList = emails
} else {
emailList = emails.split(',').map(email => email.trim()).filter(Boolean)
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const invalidEmails = emailList.filter(email => !emailRegex.test(email))
if (invalidEmails.length > 0) {
throw new Error(`Invalid email addresses: ${invalidEmails.join(', ')}`)
}
return emailList
}
// Prepare email data
@@ -176,9 +201,9 @@ export const sendEmailJob = {
}
})
// Create the email in the collection
// Create the email in the collection using configurable collection name
const email = await payload.create({
collection: 'emails', // Default collection name
collection: mailingContext.collections.emails,
data: emailData
})