Add error handling and logging to email service

This commit is contained in:
Alex Holliday committed 2024-08-21 11:41:47 -07:00
1 parent 4a53c4d4f2
commit 4e430547ab
3 files changed
+64 -23

No files matched your search

+6
View File
@@ -231,6 +231,12 @@ class NetworkService {
}); });
} }
async deleteUser(authToken, userId) {
return this.axiosInstance.delete(`/auth/user/${userId}`, {
headers: { Authorization: `Bearer ${authToken}` },
});
}
/** /**
* ************************************ * ************************************
* Forgot password request * Forgot password request
+2 -2
View File
@@ -90,7 +90,7 @@ const registerController = async (req, res, next) => {
const token = issueToken(userForToken); const token = issueToken(userForToken);
req.emailService.buildAndSendEmail( await req.emailService.buildAndSendEmail(
"welcomeEmailTemplate", "welcomeEmailTemplate",
{ name: newUser.firstName }, { name: newUser.firstName },
newUser.email, newUser.email,
@@ -240,7 +240,7 @@ const inviteController = async (req, res, next) => {
} }
const inviteToken = await req.db.requestInviteToken(req, res); const inviteToken = await req.db.requestInviteToken(req, res);
req.emailService.buildAndSendEmail( await req.emailService.buildAndSendEmail(
"employeeActivationTemplate", "employeeActivationTemplate",
{ {
name: firstname, name: firstname,
+56 -21
View File
@@ -3,6 +3,8 @@ const path = require("path");
const nodemailer = require("nodemailer"); const nodemailer = require("nodemailer");
const { compile } = require("handlebars"); const { compile } = require("handlebars");
const mjml2html = require("mjml"); const mjml2html = require("mjml");
const SERVICE_NAME = "EmailService";
const logger = require("../utils/logger");
/** /**
* Represents an email service that can load templates, build, and send emails. * Represents an email service that can load templates, build, and send emails.
@@ -19,12 +21,19 @@ class EmailService {
* @returns {Function} A compiled template function that can be used to generate HTML email content. * @returns {Function} A compiled template function that can be used to generate HTML email content.
*/ */
this.loadTemplate = (templateName) => { this.loadTemplate = (templateName) => {
const templatePath = path.join( try {
__dirname, const templatePath = path.join(
`../templates/${templateName}.mjml` __dirname,
); `../templates/${templateName}.mjml`
const templateContent = fs.readFileSync(templatePath, "utf8"); );
return compile(templateContent); const templateContent = fs.readFileSync(templatePath, "utf8");
return compile(templateContent);
} catch (error) {
logger.error("Error loading Email templates", {
error,
service: this.SERVICE_NAME,
});
}
}; };
/** /**
@@ -67,25 +76,51 @@ class EmailService {
*/ */
buildAndSendEmail = async (template, context, to, subject) => { buildAndSendEmail = async (template, context, to, subject) => {
const buildHtml = async (template, context) => { const buildHtml = async (template, context) => {
const mjml = this.templateLookup[template](context); try {
const html = await mjml2html(mjml); const mjml = this.templateLookup[template](context);
return html.html; const html = await mjml2html(mjml);
return html.html;
} catch (error) {
logger.error("Error building Email HTML", {
error,
service: SERVICE_NAME,
});
}
}; };
const sendEmail = async (to, subject, html) => { const sendEmail = async (to, subject, html) => {
const info = await this.transporter.sendMail({ try {
to: to, const info = await this.transporter.sendMail({
subject: subject, to: to,
html: html, subject: subject,
}); html: html,
return info; });
return info;
} catch (error) {
logger.error("Error sending Email", {
error,
service: SERVICE_NAME,
});
}
}; };
const info = await sendEmail(
to, try {
subject, const info = await sendEmail(
await buildHtml(template, context) to,
); subject,
return info.messageId; await buildHtml(template, context)
);
return info.messageId;
} catch (error) {
error.service = SERVICE_NAME;
if (error.method === undefined) {
error.method = "buildAndSendEmail";
}
logger.error("Error building and sending Email", {
error,
service: SERVICE_NAME,
});
}
}; };
} }