mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-05-04 07:29:40 -05:00
Fix monitor notification check.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const mongoose = require("mongoose");
|
||||
const EmailService = require("../service/emailService");
|
||||
const Notification = require("../models/Notification");
|
||||
|
||||
/**
|
||||
* Check Schema for MongoDB collection.
|
||||
@@ -78,15 +79,17 @@ CheckSchema.pre("save", async function (next) {
|
||||
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
||||
|
||||
if (monitor) {
|
||||
// Check if the monitor has notifications
|
||||
if (monitor.notifications && monitor.notifications.length > 0) {
|
||||
const notifications = await Notification.find({ monitorId: this.monitorId });
|
||||
|
||||
// Check if there are any notifications
|
||||
if (notifications && notifications.length > 0) {
|
||||
// Only send email if monitor status has changed
|
||||
if (monitor.status !== this.status) {
|
||||
const emailService = new EmailService();
|
||||
|
||||
if (monitor.status === true && this.status === false) {
|
||||
// Notify users that the monitor is down
|
||||
for (const notification of monitor.notifications) {
|
||||
for (const notification of notifications) {
|
||||
if (notification.email) {
|
||||
await emailService.buildAndSendEmail(
|
||||
"serverIsDownTemplate",
|
||||
@@ -100,7 +103,7 @@ CheckSchema.pre("save", async function (next) {
|
||||
|
||||
if (monitor.status === false && this.status === true) {
|
||||
// Notify users that the monitor is up
|
||||
for (const notification of monitor.notifications) {
|
||||
for (const notification of notifications) {
|
||||
if (notification.email) {
|
||||
await emailService.buildAndSendEmail(
|
||||
"serverIsUpTemplate",
|
||||
@@ -125,4 +128,5 @@ CheckSchema.pre("save", async function (next) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = mongoose.model("Check", CheckSchema);
|
||||
|
||||
Reference in New Issue
Block a user