diff --git a/Server/service/emailService.js b/Server/service/emailService.js index 2f9179032..519162ba0 100644 --- a/Server/service/emailService.js +++ b/Server/service/emailService.js @@ -12,7 +12,6 @@ const SERVICE_NAME = "EmailService"; class EmailService { /** * Constructs an instance of the EmailService, initializing template loaders and the email transporter. -<<<<<<< HEAD * @param {Object} settingsService - The settings service to get email configuration. * @param {Object} fs - The file system module. * @param {Object} path - The path module. @@ -104,135 +103,12 @@ class EmailService { return html.html; } catch (error) { this.logger.error("Error building Email HTML", { -======= - */ - constructor(settingsService) { - this.settingsService = settingsService; - /** - * Loads an email template from the filesystem. - * - * @param {string} templateName - The name of the template to load. - * @returns {Function} A compiled template function that can be used to generate HTML email content. - */ - this.loadTemplate = (templateName) => { - 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, - }); - } - }; - - /** - * A lookup object to access preloaded email templates. - * @type {Object.} - * TODO Load less used templates in their respective functions - */ - this.templateLookup = { - welcomeEmailTemplate: this.loadTemplate("welcomeEmail"), - employeeActivationTemplate: this.loadTemplate("employeeActivation"), - noIncidentsThisWeekTemplate: this.loadTemplate("noIncidentsThisWeek"), - serverIsDownTemplate: this.loadTemplate("serverIsDown"), - serverIsUpTemplate: this.loadTemplate("serverIsUp"), - passwordResetTemplate: this.loadTemplate("passwordReset"), - }; - /** - * A lookup object to access preloaded email templates. - * @type {Object.} - * TODO Load less used templates in their respective functions - */ - this.templateLookup = { - welcomeEmailTemplate: this.loadTemplate("welcomeEmail"), - employeeActivationTemplate: this.loadTemplate("employeeActivation"), - noIncidentsThisWeekTemplate: this.loadTemplate("noIncidentsThisWeek"), - serverIsDownTemplate: this.loadTemplate("serverIsDown"), - serverIsUpTemplate: this.loadTemplate("serverIsUp"), - passwordResetTemplate: this.loadTemplate("passwordReset"), - }; - - /** - * The email transporter used to send emails. - * @type {Object} - */ - /** - * The email transporter used to send emails. - * @type {Object} - */ - - const { systemEmailHost, systemEmailPort, systemEmailAddress, systemEmailPassword } = - this.settingsService.getSettings(); - const { systemEmailHost, systemEmailPort, systemEmailAddress, systemEmailPassword } = - this.settingsService.getSettings(); - - const emailConfig = { - host: systemEmailHost, - port: systemEmailPort, - secure: true, - auth: { - user: systemEmailAddress, - pass: systemEmailPassword, - }, - }; - const emailConfig = { - host: systemEmailHost, - port: systemEmailPort, - secure: true, - auth: { - user: systemEmailAddress, - pass: systemEmailPassword, - }, - }; - - this.transporter = nodemailer.createTransport(emailConfig); - } - this.transporter = this.nodemailer.createTransport(emailConfig); - } - - /** - * Asynchronously builds and sends an email using a specified template and context. - * - * @param {string} template - The name of the template to use for the email body. - * @param {Object} context - The data context to render the template with. - * @param {string} to - The recipient's email address. - * @param {string} subject - The subject of the email. - * @returns {Promise} A promise that resolves to the messageId of the sent email. - */ - buildAndSendEmail = async (template, context, to, subject) => { - const buildHtml = async (template, context) => { - 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) => { - try { - const info = await this.transporter.sendMail({ - to: to, - subject: subject, - html: html, - }); - return info; - } catch (error) { - logger.error("Error sending Email", { ->>>>>>> develop - error, - service: SERVICE_NAME, - }); - } - }; - -<<<<<<< HEAD const sendEmail = async (to, subject, html) => { try { const info = await this.transporter.sendMail({ @@ -251,21 +127,6 @@ class EmailService { const html = await buildHtml(template, context); const info = await sendEmail(to, subject, html); 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, - }); - } ->>>>>>> develop }; } export default EmailService;