Refactor EmailService to inject dependencies

This commit is contained in:
Alex Holliday committed 2024-10-17 14:00:22 +08:00
1 parent ab0f4e49bc
commit 17ed90f76b
1 file changed
+25 -24
+25 -24
View File
@@ -1,11 +1,6 @@
import fs from "fs";
import path from "path";
import nodemailer from "nodemailer";
import pkg from "handlebars";
const { compile } = pkg;
import mjml2html from "mjml";
import logger from "../utils/logger.js";
import { fileURLToPath } from "url";
import path from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -17,9 +12,23 @@ const SERVICE_NAME = "EmailService";
class EmailService {
/**
* Constructs an instance of the EmailService, initializing template loaders and the email transporter.
* @param {Object} settingsService - The settings service to get email configuration.
* @param {Object} fs - The file system module.
* @param {Object} path - The path module.
* @param {Function} compile - The Handlebars compile function.
* @param {Function} mjml2html - The MJML to HTML conversion function.
* @param {Object} nodemailer - The nodemailer module.
* @param {Object} logger - The logger module.
*/
constructor(settingsService) {
constructor(settingsService, fs, path, compile, mjml2html, nodemailer, logger) {
this.settingsService = settingsService;
this.fs = fs;
this.path = path;
this.compile = compile;
this.mjml2html = mjml2html;
this.nodemailer = nodemailer;
this.logger = logger;
/**
* Loads an email template from the filesystem.
*
@@ -28,12 +37,12 @@ class EmailService {
*/
this.loadTemplate = (templateName) => {
try {
const templatePath = path.join(
const templatePath = this.path.join(
__dirname,
`../templates/${templateName}.mjml`
);
const templateContent = fs.readFileSync(templatePath, "utf8");
return compile(templateContent);
const templateContent = this.fs.readFileSync(templatePath, "utf8");
return this.compile(templateContent);
} catch (error) {
logger.error("Error loading Email templates", {
error,
@@ -61,12 +70,8 @@ class EmailService {
* @type {Object}
*/
const {
systemEmailHost,
systemEmailPort,
systemEmailAddress,
systemEmailPassword,
} = this.settingsService.getSettings();
const { systemEmailHost, systemEmailPort, systemEmailAddress, systemEmailPassword } =
this.settingsService.getSettings();
const emailConfig = {
host: systemEmailHost,
@@ -78,7 +83,7 @@ class EmailService {
},
};
this.transporter = nodemailer.createTransport(emailConfig);
this.transporter = this.nodemailer.createTransport(emailConfig);
}
/**
@@ -94,7 +99,7 @@ class EmailService {
const buildHtml = async (template, context) => {
try {
const mjml = this.templateLookup[template](context);
const html = await mjml2html(mjml);
const html = await this.mjml2html(mjml);
return html.html;
} catch (error) {
logger.error("Error building Email HTML", {
@@ -121,11 +126,7 @@ class EmailService {
};
try {
const info = await sendEmail(
to,
subject,
await buildHtml(template, context)
);
const info = await sendEmail(to, subject, await buildHtml(template, context));
return info.messageId;
} catch (error) {
error.service = SERVICE_NAME;