Merge pull request #3159 from bluewave-labs/feat/email-service-ts

feat: email-service js->ts
This commit is contained in:
Alexander Holliday authored and GitHub committed 2026-01-20 14:08:32 -08:00
commit f03ded4109
3 files changed
+42 -37

No files matched your search

@@ -1,28 +1,27 @@
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import path from "path"; import path from "path";
import { EmailTransportConfig } from "@/types/index.js";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
const SERVICE_NAME = "EmailService"; const SERVICE_NAME = "EmailService";
/**
* Represents an email service that can load templates, build, and send emails.
*/
class EmailService { class EmailService {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
/** private settingsService: any;
* Constructs an instance of the EmailService, initializing template loaders and the email transporter. private fs: any;
* @param {Object} settingsService - The settings service to get email configuration. private path: any;
* @param {Object} fs - The file system module. private compile: any;
* @param {Object} path - The path module. private mjml2html: any;
* @param {Function} compile - The Handlebars compile function. private nodemailer: any;
* @param {Function} mjml2html - The MJML to HTML conversion function. private logger: any;
* @param {Object} nodemailer - The nodemailer module. private transporter: any;
* @param {Object} logger - The logger module. private templateLookup: Record<string, Function>;
*/ private loadTemplate: (templateName: string) => Function;
constructor(settingsService, fs, path, compile, mjml2html, nodemailer, logger) {
constructor(settingsService: any, fs: any, path: any, compile: any, mjml2html: any, nodemailer: any, logger: any) {
this.settingsService = settingsService; this.settingsService = settingsService;
this.fs = fs; this.fs = fs;
this.path = path; this.path = path;
@@ -31,6 +30,10 @@ class EmailService {
this.nodemailer = nodemailer; this.nodemailer = nodemailer;
this.logger = logger; this.logger = logger;
this.init(); this.init();
this.templateLookup = {};
this.loadTemplate = () => {
return () => {};
};
} }
get serviceName() { get serviceName() {
@@ -38,18 +41,12 @@ class EmailService {
} }
init = async () => { init = async () => {
/**
* 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) => { this.loadTemplate = (templateName) => {
try { try {
const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`); const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`);
const templateContent = this.fs.readFileSync(templatePath, "utf8"); const templateContent = this.fs.readFileSync(templatePath, "utf8");
return this.compile(templateContent); return this.compile(templateContent);
} catch (error) { } catch (error: any) {
this.logger.error({ this.logger.error({
message: error.message, message: error.message,
service: SERVICE_NAME, service: SERVICE_NAME,
@@ -59,11 +56,6 @@ class EmailService {
} }
}; };
/**
* A lookup object to access preloaded email templates.
* @type {Object.<string, Function>}
* TODO Load less used templates in their respective functions
*/
this.templateLookup = { this.templateLookup = {
welcomeEmailTemplate: this.loadTemplate("welcomeEmail"), welcomeEmailTemplate: this.loadTemplate("welcomeEmail"),
employeeActivationTemplate: this.loadTemplate("employeeActivation"), employeeActivationTemplate: this.loadTemplate("employeeActivation"),
@@ -74,19 +66,14 @@ class EmailService {
hardwareIncidentTemplate: this.loadTemplate("hardwareIncident"), hardwareIncidentTemplate: this.loadTemplate("hardwareIncident"),
testEmailTemplate: this.loadTemplate("testEmailTemplate"), testEmailTemplate: this.loadTemplate("testEmailTemplate"),
}; };
/**
* The email transporter used to send emails.
* @type {Object}
*/
}; };
buildEmail = async (template, context) => { buildEmail = async (template: string, context: Record<string, any>) => {
try { try {
const mjml = this.templateLookup[template](context); const mjml = this.templateLookup[template]?.(context);
const html = await this.mjml2html(mjml); const html = await this.mjml2html(mjml);
return html.html; return html.html;
} catch (error) { } catch (error: any) {
this.logger.error({ this.logger.error({
message: error.message, message: error.message,
service: SERVICE_NAME, service: SERVICE_NAME,
@@ -96,8 +83,8 @@ class EmailService {
} }
}; };
sendEmail = async (to, subject, html, transportConfig) => { sendEmail = async (to: string, subject: string, html: string, transportConfig?: EmailTransportConfig) => {
let config; let config: EmailTransportConfig;
if (typeof transportConfig !== "undefined") { if (typeof transportConfig !== "undefined") {
config = transportConfig; config = transportConfig;
} else { } else {
@@ -157,7 +144,7 @@ class EmailService {
html: html, html: html,
}); });
return info?.messageId; return info?.messageId;
} catch (error) { } catch (error: any) {
this.logger.error({ this.logger.error({
message: error.message, message: error.message,
service: SERVICE_NAME, service: SERVICE_NAME,
+17
View File
@@ -0,0 +1,17 @@
import { Settings } from "./settings.js";
export type EmailTransportConfig = Pick<
Settings,
| "systemEmailHost"
| "systemEmailPort"
| "systemEmailAddress"
| "systemEmailPassword"
| "systemEmailUser"
| "systemEmailConnectionHost"
| "systemEmailTLSServername"
| "systemEmailSecure"
| "systemEmailPool"
| "systemEmailIgnoreTLS"
| "systemEmailRequireTLS"
| "systemEmailRejectUnauthorized"
>;
+1
View File
@@ -10,3 +10,4 @@ export * from "@/types/settings.js";
export * from "@/types/notification.js"; export * from "@/types/notification.js";
export * from "@/types/alert.js"; export * from "@/types/alert.js";
export * from "@/types/incident.js"; export * from "@/types/incident.js";
export * from "@/types/email.js";