Align sendEmail and sendEmailTask with updated BaseEmail typing

- Refactor `sendEmail` to return extended type with `id` for better type inference.
- Update `sendEmailTask` to use `BaseEmail` instead of `Email`.
- Add `outputSchema` in `sendEmailTask` for consistent output structure.
This commit is contained in:
2025-09-13 22:46:30 +02:00
parent 6ad90874cf
commit 06f9c2cb5b
2 changed files with 13 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
import { sendEmail } from '../sendEmail.js' import { sendEmail } from '../sendEmail.js'
import { Email } from '../payload-types.js' import { Email } from '../payload-types.js'
import {BaseEmail} from "../types/index.js"
export interface SendEmailTaskInput { export interface SendEmailTaskInput {
// Template mode fields // Template mode fields
@@ -116,6 +117,12 @@ export const sendEmailJob = {
} }
} }
], ],
outputSchema: [
{
name: 'id',
type: 'text' as const
}
],
handler: async ({ input, payload }: any) => { handler: async ({ input, payload }: any) => {
// Cast input to our expected type // Cast input to our expected type
const taskInput = input as SendEmailTaskInput const taskInput = input as SendEmailTaskInput
@@ -154,18 +161,12 @@ export const sendEmailJob = {
}) })
// Use the sendEmail helper to create the email // Use the sendEmail helper to create the email
const email = await sendEmail<Email>(payload, sendEmailOptions) const email = await sendEmail<BaseEmail>(payload, sendEmailOptions)
return { return {
output: { output: {
success: true, success: true,
emailId: email.id, id: email.id,
message: `Email queued successfully with ID: ${email.id}`,
mode: taskInput.templateSlug ? 'template' : 'direct',
templateSlug: taskInput.templateSlug || null,
subject: email.subject,
recipients: Array.isArray(email.to) ? email.to.length : 1,
scheduledAt: email.scheduledAt || null
} }
} }

View File

@@ -36,10 +36,10 @@ export interface SendEmailOptions<T extends BaseEmail = BaseEmail> {
* }) * })
* ``` * ```
*/ */
export const sendEmail = async <T extends BaseEmail = BaseEmail>( export const sendEmail = async <T extends BaseEmail = BaseEmail, ID = string | number>(
payload: Payload, payload: Payload,
options: SendEmailOptions<T> options: SendEmailOptions<T>
): Promise<T> => { ): Promise<T & {id: ID}> => {
const mailing = getMailing(payload) const mailing = getMailing(payload)
const collectionSlug = options.collectionSlug || mailing.collections.emails || 'emails' const collectionSlug = options.collectionSlug || mailing.collections.emails || 'emails'
@@ -97,7 +97,7 @@ export const sendEmail = async <T extends BaseEmail = BaseEmail>(
data: emailData data: emailData
}) })
return email as T return email as T & {id: ID}
} }
export default sendEmail export default sendEmail