Files
Checkmate/server/src/service/infrastructure/emailService.ts
T

158 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
import { fileURLToPath } from "url";
import path from "path";
2026-01-20 22:06:55 +00:00
import { EmailTransportConfig } from "@/types/index.js";
2025-04-20 11:29:53 -07:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SERVICE_NAME = "EmailService";
class EmailService {
static SERVICE_NAME = SERVICE_NAME;
2025-05-15 15:16:36 +02:00
2026-01-20 22:06:55 +00:00
private settingsService: any;
private fs: any;
private path: any;
private compile: any;
private mjml2html: any;
private nodemailer: any;
private logger: any;
private transporter: any;
private templateLookup: Record<string, Function>;
private loadTemplate: (templateName: string) => Function;
constructor(settingsService: any, fs: any, path: any, compile: any, mjml2html: any, nodemailer: any, logger: any) {
2025-04-20 11:29:53 -07:00
this.settingsService = settingsService;
this.fs = fs;
this.path = path;
this.compile = compile;
this.mjml2html = mjml2html;
this.nodemailer = nodemailer;
this.logger = logger;
2025-05-09 14:52:44 -07:00
this.init();
2026-01-20 22:06:55 +00:00
this.templateLookup = {};
this.loadTemplate = () => {
return () => {};
};
2025-05-09 14:52:44 -07:00
}
2025-04-20 11:29:53 -07:00
2025-07-25 15:15:14 -07:00
get serviceName() {
return EmailService.SERVICE_NAME;
}
2025-05-09 14:52:44 -07:00
init = async () => {
2025-04-20 11:29:53 -07:00
this.loadTemplate = (templateName) => {
try {
2026-01-16 22:03:35 +00:00
const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`);
2025-04-20 11:29:53 -07:00
const templateContent = this.fs.readFileSync(templatePath, "utf8");
return this.compile(templateContent);
2026-01-20 22:06:55 +00:00
} catch (error: any) {
2025-04-20 11:29:53 -07:00
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "loadTemplate",
stack: error.stack,
});
}
};
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"),
hardwareIncidentTemplate: this.loadTemplate("hardwareIncident"),
2025-05-09 14:52:44 -07:00
testEmailTemplate: this.loadTemplate("testEmailTemplate"),
2025-04-20 11:29:53 -07:00
};
2025-05-09 14:52:44 -07:00
};
2025-04-20 11:29:53 -07:00
2026-01-20 22:06:55 +00:00
buildEmail = async (template: string, context: Record<string, any>) => {
2025-06-18 10:44:59 +08:00
try {
2026-01-20 22:06:55 +00:00
const mjml = this.templateLookup[template]?.(context);
2025-06-18 10:44:59 +08:00
const html = await this.mjml2html(mjml);
return html.html;
2026-01-20 22:06:55 +00:00
} catch (error: any) {
2025-06-18 10:44:59 +08:00
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "buildEmail",
stack: error.stack,
});
}
};
2026-01-20 22:06:55 +00:00
sendEmail = async (to: string, subject: string, html: string, transportConfig?: EmailTransportConfig) => {
let config: EmailTransportConfig;
2025-05-16 15:02:07 -07:00
if (typeof transportConfig !== "undefined") {
config = transportConfig;
} else {
2025-05-23 12:22:05 -07:00
config = await this.settingsService.getDBSettings();
2025-05-16 15:02:07 -07:00
}
2025-04-20 11:29:53 -07:00
const {
systemEmailHost,
systemEmailPort,
2025-06-12 00:39:47 +05:30
systemEmailSecure,
systemEmailPool,
2025-04-20 11:29:53 -07:00
systemEmailUser,
systemEmailAddress,
systemEmailPassword,
2025-05-15 15:16:36 +02:00
systemEmailConnectionHost,
2025-06-11 15:11:28 +05:30
systemEmailTLSServername,
systemEmailIgnoreTLS,
systemEmailRequireTLS,
systemEmailRejectUnauthorized,
2025-05-16 15:02:07 -07:00
} = config;
2025-04-20 11:29:53 -07:00
2025-06-12 00:39:47 +05:30
const emailConfig = {
2025-04-20 11:29:53 -07:00
host: systemEmailHost,
2025-06-11 14:11:49 +05:30
port: Number(systemEmailPort),
2025-06-12 00:39:47 +05:30
secure: systemEmailSecure,
2025-04-20 11:29:53 -07:00
auth: {
user: systemEmailUser || systemEmailAddress,
pass: systemEmailPassword,
},
2025-06-13 12:22:28 +05:30
name: systemEmailConnectionHost || "localhost",
2025-05-09 14:52:44 -07:00
connectionTimeout: 5000,
2025-06-12 00:39:47 +05:30
pool: systemEmailPool,
2025-06-13 12:22:28 +05:30
tls: {
rejectUnauthorized: systemEmailRejectUnauthorized,
ignoreTLS: systemEmailIgnoreTLS,
requireTLS: systemEmailRequireTLS,
servername: systemEmailTLSServername,
},
2025-04-20 11:29:53 -07:00
};
this.transporter = this.nodemailer.createTransport(emailConfig);
2025-06-25 09:35:41 +08:00
try {
await this.transporter.verify();
} catch (error) {
this.logger.warn({
message: "Email transporter verification failed",
service: SERVICE_NAME,
method: "verifyTransporter",
});
return false;
}
2025-06-18 10:44:59 +08:00
try {
const info = await this.transporter.sendMail({
to: to,
from: systemEmailAddress,
subject: subject,
html: html,
});
return info?.messageId;
2026-01-20 22:06:55 +00:00
} catch (error: any) {
2025-06-18 10:44:59 +08:00
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "sendEmail",
stack: error.stack,
});
}
2025-04-20 11:29:53 -07:00
};
}
2025-05-15 15:16:36 +02:00
2025-04-20 11:29:53 -07:00
export default EmailService;