Refactor EmailService to inject dependencies
This commit is contained in:
1 parent
ab0f4e49bc
commit
17ed90f76b
1 file changed
+25
-24
@@ -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 { fileURLToPath } from "url";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
@@ -17,9 +12,23 @@ const SERVICE_NAME = "EmailService";
|
|||||||
class EmailService {
|
class EmailService {
|
||||||
/**
|
/**
|
||||||
* Constructs an instance of the EmailService, initializing template loaders and the email transporter.
|
* 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.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.
|
* Loads an email template from the filesystem.
|
||||||
*
|
*
|
||||||
@@ -28,12 +37,12 @@ class EmailService {
|
|||||||
*/
|
*/
|
||||||
this.loadTemplate = (templateName) => {
|
this.loadTemplate = (templateName) => {
|
||||||
try {
|
try {
|
||||||
const templatePath = path.join(
|
const templatePath = this.path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
`../templates/${templateName}.mjml`
|
`../templates/${templateName}.mjml`
|
||||||
);
|
);
|
||||||
const templateContent = fs.readFileSync(templatePath, "utf8");
|
const templateContent = this.fs.readFileSync(templatePath, "utf8");
|
||||||
return compile(templateContent);
|
return this.compile(templateContent);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error loading Email templates", {
|
logger.error("Error loading Email templates", {
|
||||||
error,
|
error,
|
||||||
@@ -61,12 +70,8 @@ class EmailService {
|
|||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {
|
const { systemEmailHost, systemEmailPort, systemEmailAddress, systemEmailPassword } =
|
||||||
systemEmailHost,
|
this.settingsService.getSettings();
|
||||||
systemEmailPort,
|
|
||||||
systemEmailAddress,
|
|
||||||
systemEmailPassword,
|
|
||||||
} = this.settingsService.getSettings();
|
|
||||||
|
|
||||||
const emailConfig = {
|
const emailConfig = {
|
||||||
host: systemEmailHost,
|
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) => {
|
const buildHtml = async (template, context) => {
|
||||||
try {
|
try {
|
||||||
const mjml = this.templateLookup[template](context);
|
const mjml = this.templateLookup[template](context);
|
||||||
const html = await mjml2html(mjml);
|
const html = await this.mjml2html(mjml);
|
||||||
return html.html;
|
return html.html;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error building Email HTML", {
|
logger.error("Error building Email HTML", {
|
||||||
@@ -121,11 +126,7 @@ class EmailService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const info = await sendEmail(
|
const info = await sendEmail(to, subject, await buildHtml(template, context));
|
||||||
to,
|
|
||||||
subject,
|
|
||||||
await buildHtml(template, context)
|
|
||||||
);
|
|
||||||
return info.messageId;
|
return info.messageId;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
|
|||||||
Reference in new issue
Block a user