diff --git a/src/jobs/sendEmailTask.ts b/src/jobs/sendEmailTask.ts index 0c808af..eac8344 100644 --- a/src/jobs/sendEmailTask.ts +++ b/src/jobs/sendEmailTask.ts @@ -1,5 +1,5 @@ import { sendEmail } from '../sendEmail.js' -import { Email } from '../payload-types.js' +import {Email, EmailTemplate} from '../payload-types.js' import {BaseEmail} from "../types/index.js" export interface SendEmailTaskInput { @@ -161,7 +161,7 @@ export const sendEmailJob = { }) // Use the sendEmail helper to create the email - const email = await sendEmail(payload, sendEmailOptions) + const email = await sendEmail(payload, sendEmailOptions) return { output: { diff --git a/src/sendEmail.ts b/src/sendEmail.ts index e83052f..a14b1cc 100644 --- a/src/sendEmail.ts +++ b/src/sendEmail.ts @@ -1,6 +1,6 @@ import { Payload } from 'payload' import { getMailing, renderTemplate, parseAndValidateEmails } from './utils/helpers.js' -import {Email} from "./payload-types.js" +import {Email, EmailTemplate} from "./payload-types.js" import {BaseEmail} from "./types/index.js" // Options for sending emails @@ -36,14 +36,14 @@ export interface SendEmailOptions { * }) * ``` */ -export const sendEmail = async ( +export const sendEmail = async ( payload: Payload, - options: SendEmailOptions -): Promise => { + options: SendEmailOptions> +): Promise => { const mailing = getMailing(payload) const collectionSlug = options.collectionSlug || mailing.collections.emails || 'emails' - let emailData: Partial = { ...options.data } as Partial + let emailData: Partial = { ...options.data } as Partial // If using a template, render it first if (options.template) { @@ -59,7 +59,7 @@ export const sendEmail = async + } as Partial } // Validate required fields @@ -97,7 +97,7 @@ export const sendEmail = async { + private async getTemplateBySlug(templateSlug: string): Promise { try { const { docs } = await this.payload.find({ collection: this.templatesCollection as any, @@ -314,7 +312,7 @@ export class MailingService implements IMailingService { limit: 1, }) - return docs.length > 0 ? docs[0] as EmailTemplate : null + return docs.length > 0 ? docs[0] as BaseEmailTemplate : null } catch (error) { console.error(`Template with slug '${templateSlug}' not found:`, error) return null @@ -379,7 +377,7 @@ export class MailingService implements IMailingService { }) } - private async renderEmailTemplate(template: EmailTemplate, variables: Record = {}): Promise<{ html: string; text: string }> { + private async renderEmailTemplate(template: BaseEmailTemplate, variables: Record = {}): Promise<{ html: string; text: string }> { if (!template.content) { return { html: '', text: '' } } diff --git a/src/types/index.ts b/src/types/index.ts index c8a997c..d14d8d7 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,9 +1,11 @@ import { Payload } from 'payload' import type { CollectionConfig, RichTextField } from 'payload' import { Transporter } from 'nodemailer' -import {Email} from "../payload-types.js" +import {Email, EmailTemplate} from "../payload-types.js" -export type BaseEmail = Omit & {template: Omit} +export type BaseEmail = Omit & {template: Omit | TEmailTemplate['id'] | undefined | null} + +export type BaseEmailTemplate = Omit export type TemplateRendererHook = (template: string, variables: Record) => string | Promise @@ -37,16 +39,6 @@ export interface MailingTransportConfig { } } -export interface EmailTemplate { - id: string - name: string - slug: string - subject: string - content: any // Lexical editor state - createdAt: string - updatedAt: string -} - export interface QueuedEmail { id: string