diff --git a/src/config/job-config/actions/emailaction/emailaction.interface.ts b/src/config/job-config/actions/emailaction/emailaction.interface.ts index fc14f2b52..e9996ac32 100644 --- a/src/config/job-config/actions/emailaction/emailaction.interface.ts +++ b/src/config/job-config/actions/emailaction/emailaction.interface.ts @@ -5,7 +5,7 @@ export const actionType = "email"; export interface EmailJobActionOptions extends JobActionOptions { actionType: typeof actionType; to: string; - from: string; + from?: string; subject: string; bodyTemplateFile: string; } @@ -24,7 +24,7 @@ export function isEmailJobActionOptions( return ( opts.actionType === actionType && typeof opts.to === "string" && - typeof opts.from === "string" && + (opts.from === undefined || typeof opts.from === "string") && typeof opts.subject === "string" && typeof opts.bodyTemplateFile === "string" ); diff --git a/src/config/job-config/actions/emailaction/emailaction.spec.ts b/src/config/job-config/actions/emailaction/emailaction.spec.ts index 4ff8f2063..ff8b3b4b5 100644 --- a/src/config/job-config/actions/emailaction/emailaction.spec.ts +++ b/src/config/job-config/actions/emailaction/emailaction.spec.ts @@ -2,8 +2,10 @@ import { EmailJobAction } from "./emailaction"; import { EmailJobActionOptions } from "./emailaction.interface"; import { JobClass } from "../../../../jobs/schemas/job.schema"; import { MailService } from "src/common/mail.service"; +import { MailerService } from "@nestjs-modules/mailer"; jest.mock("src/common/mail.service"); +jest.mock("@nestjs-modules/mailer"); describe("EmailJobAction", () => { const config: EmailJobActionOptions = { @@ -59,3 +61,56 @@ describe("EmailJobAction", () => { ); }); }); + +describe("EmailJobAction with default sender", () => { + const config: EmailJobActionOptions = { + actionType: "email", + to: "recipient@example.com", + subject: "Job {{id}} completed", + bodyTemplateFile: "src/common/email-templates/job-template.spec.html", + }; + + const mailService = { + sendMail: jest.fn(), + } as unknown as MailService; + + const mailerService = { + sendMail: jest.fn(), + } as unknown as MailerService; + + const action = new EmailJobAction(mailService, config); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("should be configured successfully", async () => { + expect(action).toBeDefined(); + }); + + it("should send an email with default sender successfully", async () => { + const job = { + id: "12345", + type: "testemail", + } as JobClass; + + const defaultFrom = "default-sender@example.com"; + (mailerService.sendMail as jest.Mock).mockImplementation((mailOptions) => { + mailOptions.from = defaultFrom; + return Promise.resolve(); + }); + + (mailService.sendMail as jest.Mock).mockImplementation((mailOptions) => { + return mailerService.sendMail(mailOptions); + }); + + await action.performJob(job); + + expect(mailerService.sendMail).toHaveBeenCalledWith({ + to: "recipient@example.com", + from: defaultFrom, + subject: "Job 12345 completed", + html: "Your testemail job with ID 12345 has been completed successfully.", + }); + }); +}); diff --git a/src/config/job-config/actions/emailaction/emailaction.ts b/src/config/job-config/actions/emailaction/emailaction.ts index 078a296b8..2c7679ba2 100644 --- a/src/config/job-config/actions/emailaction/emailaction.ts +++ b/src/config/job-config/actions/emailaction/emailaction.ts @@ -17,7 +17,7 @@ import { MailService } from "src/common/mail.service"; */ export class EmailJobAction implements JobAction { private toTemplate: TemplateJob; - private from: string; + private from?: string = undefined; private subjectTemplate: TemplateJob; private bodyTemplate: TemplateJob; @@ -36,7 +36,9 @@ export class EmailJobAction implements JobAction { Logger.log("EmailJobAction parameters are valid.", "EmailJobAction"); - this.from = options.from as string; + if (options["from"]) { + this.from = options.from as string; + } this.toTemplate = compileJob(options.to); this.subjectTemplate = compileJob(options.subject);