Server status update bug fixed, simplified email notification

This commit is contained in:
Alex Holliday committed 2024-08-12 19:43:25 -07:00
1 parent c558577c8c
commit f80936dd71
1 file changed
+11 -16
+11 -16
View File
@@ -85,45 +85,40 @@ CheckSchema.pre("save", async function (next) {
}); });
// Check if there are any notifications // Check if there are any notifications
if (notifications && notifications.length > 0) {
// Only send email if monitor status has changed // Only send email if monitor status has changed
if (monitor.status !== this.status) { if (monitor.status !== this.status) {
const emailService = new EmailService(); const emailService = new EmailService();
let template = "";
let status = "";
if (monitor.status === true && this.status === false) { if (monitor.status === true && this.status === false) {
// Notify users that the monitor is down template = "serverIsDownTemplate";
for (const notification of notifications) { status = "down";
if (notification.type === "email") {
await emailService.buildAndSendEmail(
"serverIsDownTemplate",
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is down`
);
}
}
} }
if (monitor.status === false && this.status === true) { if (monitor.status === false && this.status === true) {
// Notify users that the monitor is up // Notify users that the monitor is up
template = "serverIsUpTemplate";
status = "up";
}
for (const notification of notifications) { for (const notification of notifications) {
if (notification.type === "email") { if (notification.type === "email") {
await emailService.buildAndSendEmail( await emailService.buildAndSendEmail(
"serverIsUpTemplate", template,
{ monitorName: monitor.name, monitorUrl: monitor.url }, { monitorName: monitor.name, monitorUrl: monitor.url },
notification.address, notification.address,
`Monitor ${monitor.name} is back up` `Monitor ${monitor.name} is ${status}`
); );
} }
} }
}
// Update monitor status // Update monitor status
monitor.status = this.status; monitor.status = this.status;
await monitor.save(); await monitor.save();
} }
} }
}
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} finally { } finally {