diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 32fcb6aa8..de4508cff 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -231,6 +231,12 @@ class NetworkService { }); } + async deleteUser(authToken, userId) { + return this.axiosInstance.delete(`/auth/user/${userId}`, { + headers: { Authorization: `Bearer ${authToken}` }, + }); + } + /** * ************************************ * Forgot password request diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 4b5f51121..6fbcdb669 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -90,7 +90,7 @@ const registerController = async (req, res, next) => { const token = issueToken(userForToken); - req.emailService.buildAndSendEmail( + await req.emailService.buildAndSendEmail( "welcomeEmailTemplate", { name: newUser.firstName }, newUser.email, @@ -240,7 +240,7 @@ const inviteController = async (req, res, next) => { } const inviteToken = await req.db.requestInviteToken(req, res); - req.emailService.buildAndSendEmail( + await req.emailService.buildAndSendEmail( "employeeActivationTemplate", { name: firstname, diff --git a/Server/service/emailService.js b/Server/service/emailService.js index 79d6213c6..27518484c 100644 --- a/Server/service/emailService.js +++ b/Server/service/emailService.js @@ -3,6 +3,8 @@ const path = require("path"); const nodemailer = require("nodemailer"); const { compile } = require("handlebars"); 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. @@ -19,12 +21,19 @@ class EmailService { * @returns {Function} A compiled template function that can be used to generate HTML email content. */ this.loadTemplate = (templateName) => { - const templatePath = path.join( - __dirname, - `../templates/${templateName}.mjml` - ); - const templateContent = fs.readFileSync(templatePath, "utf8"); - return compile(templateContent); + try { + const templatePath = path.join( + __dirname, + `../templates/${templateName}.mjml` + ); + 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) => { const buildHtml = async (template, context) => { - const mjml = this.templateLookup[template](context); - const html = await mjml2html(mjml); - return html.html; + try { + const mjml = this.templateLookup[template](context); + 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 info = await this.transporter.sendMail({ - to: to, - subject: subject, - html: html, - }); - return info; + try { + const info = await this.transporter.sendMail({ + to: to, + subject: subject, + html: html, + }); + return info; + } catch (error) { + logger.error("Error sending Email", { + error, + service: SERVICE_NAME, + }); + } }; - const info = await sendEmail( - to, - subject, - await buildHtml(template, context) - ); - return info.messageId; + + try { + const info = await sendEmail( + to, + subject, + 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, + }); + } }; }