Fix monitor notification check.

This commit is contained in:
M M committed 2024-08-06 20:45:27 -07:00
1 parent e42b65054d
commit f7161f0302
1 file changed
+8 -4
+8 -4
View File
@@ -1,5 +1,6 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const EmailService = require("../service/emailService"); const EmailService = require("../service/emailService");
const Notification = require("../models/Notification");
/** /**
* Check Schema for MongoDB collection. * Check Schema for MongoDB collection.
@@ -78,15 +79,17 @@ CheckSchema.pre("save", async function (next) {
const monitor = await mongoose.model("Monitor").findById(this.monitorId); const monitor = await mongoose.model("Monitor").findById(this.monitorId);
if (monitor) { if (monitor) {
// Check if the monitor has notifications const notifications = await Notification.find({ monitorId: this.monitorId });
if (monitor.notifications && monitor.notifications.length > 0) {
// 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();
if (monitor.status === true && this.status === false) { if (monitor.status === true && this.status === false) {
// Notify users that the monitor is down // Notify users that the monitor is down
for (const notification of monitor.notifications) { for (const notification of notifications) {
if (notification.email) { if (notification.email) {
await emailService.buildAndSendEmail( await emailService.buildAndSendEmail(
"serverIsDownTemplate", "serverIsDownTemplate",
@@ -100,7 +103,7 @@ CheckSchema.pre("save", async function (next) {
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
for (const notification of monitor.notifications) { for (const notification of notifications) {
if (notification.email) { if (notification.email) {
await emailService.buildAndSendEmail( await emailService.buildAndSendEmail(
"serverIsUpTemplate", "serverIsUpTemplate",
@@ -125,4 +128,5 @@ CheckSchema.pre("save", async function (next) {
} }
}); });
module.exports = mongoose.model("Check", CheckSchema); module.exports = mongoose.model("Check", CheckSchema);