refactor build and send
This commit is contained in:
1 parent
85e81184e5
commit
47131633d0
6 files changed
+132
-125
No files matched your search
@@ -96,21 +96,28 @@ class AuthController {
|
|||||||
|
|
||||||
const token = this.issueToken(userForToken, appSettings);
|
const token = this.issueToken(userForToken, appSettings);
|
||||||
|
|
||||||
this.emailService
|
try {
|
||||||
.buildAndSendEmail(
|
const html = await this.emailService.buildEmail("welcomeEmailTemplate", {
|
||||||
"welcomeEmailTemplate",
|
name: newUser.firstName,
|
||||||
{ name: newUser.firstName },
|
|
||||||
newUser.email,
|
|
||||||
"Welcome to Uptime Monitor"
|
|
||||||
)
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error({
|
|
||||||
message: error.message,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "registerUser",
|
|
||||||
stack: error.stack,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
this.emailService
|
||||||
|
.sendEmail(newUser.email, "Welcome to Uptime Monitor", html)
|
||||||
|
.catch((error) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "registerUser",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "registerUser",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.success({
|
res.success({
|
||||||
msg: this.stringService.authCreateUser,
|
msg: this.stringService.authCreateUser,
|
||||||
@@ -300,15 +307,16 @@ class AuthController {
|
|||||||
const name = user.firstName;
|
const name = user.firstName;
|
||||||
const { clientHost } = this.settingsService.getSettings();
|
const { clientHost } = this.settingsService.getSettings();
|
||||||
const url = `${clientHost}/set-new-password/${recoveryToken.token}`;
|
const url = `${clientHost}/set-new-password/${recoveryToken.token}`;
|
||||||
const msgId = await this.emailService.buildAndSendEmail(
|
|
||||||
"passwordResetTemplate",
|
const html = await this.emailService.buildEmail("passwordResetTemplate", {
|
||||||
{
|
name,
|
||||||
name,
|
|
||||||
email,
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
email,
|
email,
|
||||||
"Checkmate Password Reset"
|
url,
|
||||||
|
});
|
||||||
|
const msgId = await this.emailService.sendEmail(
|
||||||
|
email,
|
||||||
|
"Checkmate Password Reset",
|
||||||
|
html
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
|
|||||||
@@ -71,24 +71,25 @@ class InviteController {
|
|||||||
|
|
||||||
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
const inviteToken = await this.db.requestInviteToken({ ...req.body });
|
||||||
const { clientHost } = this.settingsService.getSettings();
|
const { clientHost } = this.settingsService.getSettings();
|
||||||
this.emailService
|
|
||||||
.buildAndSendEmail(
|
try {
|
||||||
"employeeActivationTemplate",
|
const html = await this.emailService.buildEmail("employeeActivationTemplate", {
|
||||||
{
|
name: firstname,
|
||||||
name: firstname,
|
link: `${clientHost}/register/${inviteToken.token}`,
|
||||||
link: `${clientHost}/register/${inviteToken.token}`,
|
|
||||||
},
|
|
||||||
req.body.email,
|
|
||||||
"Welcome to Uptime Monitor"
|
|
||||||
)
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error({
|
|
||||||
message: error.message,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "issueInvitation",
|
|
||||||
stack: error.stack,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
await this.emailService.sendEmail(
|
||||||
|
req.body.email,
|
||||||
|
"Welcome to Uptime Monitor",
|
||||||
|
html
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "sendInviteEmail",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.inviteIssued,
|
msg: this.stringService.inviteIssued,
|
||||||
|
|||||||
@@ -580,12 +580,8 @@ class MonitorController {
|
|||||||
const subject = this.stringService.testEmailSubject;
|
const subject = this.stringService.testEmailSubject;
|
||||||
const context = { testName: "Monitoring System" };
|
const context = { testName: "Monitoring System" };
|
||||||
|
|
||||||
const messageId = await this.emailService.buildAndSendEmail(
|
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
||||||
"testEmailTemplate",
|
const messageId = await this.emailService.sendEmail(to, subject, html);
|
||||||
context,
|
|
||||||
to,
|
|
||||||
subject
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!messageId) {
|
if (!messageId) {
|
||||||
return res.error({
|
return res.error({
|
||||||
|
|||||||
@@ -90,26 +90,21 @@ class SettingsController {
|
|||||||
const subject = this.stringService.testEmailSubject;
|
const subject = this.stringService.testEmailSubject;
|
||||||
const context = { testName: "Monitoring System" };
|
const context = { testName: "Monitoring System" };
|
||||||
|
|
||||||
const messageId = await this.emailService.buildAndSendEmail(
|
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
||||||
"testEmailTemplate",
|
const messageId = await this.emailService.sendEmail(to, subject, html, {
|
||||||
context,
|
systemEmailHost,
|
||||||
to,
|
systemEmailPort,
|
||||||
subject,
|
systemEmailUser,
|
||||||
{
|
systemEmailAddress,
|
||||||
systemEmailHost,
|
systemEmailPassword,
|
||||||
systemEmailPort,
|
systemEmailConnectionHost,
|
||||||
systemEmailUser,
|
systemEmailSecure,
|
||||||
systemEmailAddress,
|
systemEmailPool,
|
||||||
systemEmailPassword,
|
systemEmailIgnoreTLS,
|
||||||
systemEmailConnectionHost,
|
systemEmailRequireTLS,
|
||||||
systemEmailSecure,
|
systemEmailRejectUnauthorized,
|
||||||
systemEmailPool,
|
systemEmailTLSServername,
|
||||||
systemEmailIgnoreTLS,
|
});
|
||||||
systemEmailRequireTLS,
|
|
||||||
systemEmailRejectUnauthorized,
|
|
||||||
systemEmailTLSServername,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!messageId) {
|
if (!messageId) {
|
||||||
return res.error({
|
return res.error({
|
||||||
|
|||||||
@@ -80,24 +80,28 @@ class EmailService {
|
|||||||
*/
|
*/
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
buildEmail = async (template, context) => {
|
||||||
* Asynchronously builds and sends an email using a specified template and context.
|
try {
|
||||||
*
|
const mjml = this.templateLookup[template](context);
|
||||||
* @param {string} template - The name of the template to use for the email body.
|
const html = await this.mjml2html(mjml);
|
||||||
* @param {Object} context - The data context to render the template with.
|
return html.html;
|
||||||
* @param {string} to - The recipient's email address.
|
} catch (error) {
|
||||||
* @param {string} subject - The subject of the email.
|
this.logger.error({
|
||||||
* @returns {Promise<string>} A promise that resolves to the messageId of the sent email.
|
message: error.message,
|
||||||
*/
|
service: SERVICE_NAME,
|
||||||
buildAndSendEmail = async (template, context, to, subject, transportConfig) => {
|
method: "buildEmail",
|
||||||
// TODO - Consider an update transporter method so this only needs to be recreated when smtp settings change
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
sendEmail = async (to, subject, html, transportConfig) => {
|
||||||
let config;
|
let config;
|
||||||
if (typeof transportConfig !== "undefined") {
|
if (typeof transportConfig !== "undefined") {
|
||||||
config = transportConfig;
|
config = transportConfig;
|
||||||
} else {
|
} else {
|
||||||
config = await this.settingsService.getDBSettings();
|
config = await this.settingsService.getDBSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
systemEmailHost,
|
systemEmailHost,
|
||||||
systemEmailPort,
|
systemEmailPort,
|
||||||
@@ -131,45 +135,24 @@ class EmailService {
|
|||||||
servername: systemEmailTLSServername,
|
servername: systemEmailTLSServername,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
this.transporter = this.nodemailer.createTransport(emailConfig);
|
this.transporter = this.nodemailer.createTransport(emailConfig);
|
||||||
|
|
||||||
const buildHtml = async (template, context) => {
|
try {
|
||||||
try {
|
const info = await this.transporter.sendMail({
|
||||||
const mjml = this.templateLookup[template](context);
|
to: to,
|
||||||
const html = await this.mjml2html(mjml);
|
from: systemEmailAddress,
|
||||||
return html.html;
|
subject: subject,
|
||||||
} catch (error) {
|
html: html,
|
||||||
this.logger.error({
|
});
|
||||||
message: error.message,
|
return info?.messageId;
|
||||||
service: SERVICE_NAME,
|
} catch (error) {
|
||||||
method: "buildAndSendEmail",
|
this.logger.error({
|
||||||
stack: error.stack,
|
message: error.message,
|
||||||
});
|
service: SERVICE_NAME,
|
||||||
}
|
method: "sendEmail",
|
||||||
};
|
stack: error.stack,
|
||||||
|
});
|
||||||
const sendEmail = async (to, subject, html) => {
|
}
|
||||||
try {
|
|
||||||
const info = await this.transporter.sendMail({
|
|
||||||
to: to,
|
|
||||||
from: systemEmailAddress,
|
|
||||||
subject: subject,
|
|
||||||
html: html,
|
|
||||||
});
|
|
||||||
return info;
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error.message,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "sendEmail",
|
|
||||||
stack: error.stack,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const html = await buildHtml(template, context);
|
|
||||||
const info = await sendEmail(to, subject, html);
|
|
||||||
return info?.messageId;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -218,7 +218,16 @@ class NotificationService {
|
|||||||
const template = "hardwareIncidentTemplate";
|
const template = "hardwareIncidentTemplate";
|
||||||
const context = { monitor: monitor.name, url: monitor.url, alerts };
|
const context = { monitor: monitor.name, url: monitor.url, alerts };
|
||||||
const subject = `Monitor ${monitor.name} infrastructure alerts`;
|
const subject = `Monitor ${monitor.name} infrastructure alerts`;
|
||||||
this.emailService.buildAndSendEmail(template, context, address, subject);
|
const html = await this.emailService.buildEmail(template, context);
|
||||||
|
this.emailService.sendEmail(address, subject, html).catch((error) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
method: "sendHardwareEmail",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,17 +240,22 @@ class NotificationService {
|
|||||||
const subject = this.stringService.testEmailSubject;
|
const subject = this.stringService.testEmailSubject;
|
||||||
const context = { testName: "Monitoring System" };
|
const context = { testName: "Monitoring System" };
|
||||||
|
|
||||||
const messageId = await this.emailService.buildAndSendEmail(
|
try {
|
||||||
"testEmailTemplate",
|
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
||||||
context,
|
const messageId = await this.emailService.sendEmail(to, subject, html);
|
||||||
to,
|
|
||||||
subject
|
|
||||||
);
|
|
||||||
|
|
||||||
if (messageId) {
|
if (messageId) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
method: "sendTestEmail",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -259,7 +273,17 @@ class NotificationService {
|
|||||||
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
||||||
const context = { monitor: monitor.name, url: monitor.url };
|
const context = { monitor: monitor.name, url: monitor.url };
|
||||||
const subject = `Monitor ${monitor.name} is ${status === true ? "up" : "down"}`;
|
const subject = `Monitor ${monitor.name} is ${status === true ? "up" : "down"}`;
|
||||||
this.emailService.buildAndSendEmail(template, context, address, subject);
|
|
||||||
|
const html = await this.emailService.buildEmail(template, context);
|
||||||
|
this.emailService.sendEmail(address, subject, html).catch((error) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
method: "sendEmail",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user