Fixed bug that sends an email even if a user does not have notifications turned on.
This commit is contained in:
1 parent
3bcf564ee4
commit
e6fac616db
1 file changed
+36
-28
+36
-28
@@ -34,36 +34,43 @@ const CheckSchema = mongoose.Schema(
|
|||||||
CheckSchema.pre("save", async function (next) {
|
CheckSchema.pre("save", async function (next) {
|
||||||
try {
|
try {
|
||||||
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
||||||
if (monitor && monitor.status !== this.status) {
|
if (monitor) {
|
||||||
if (monitor.status === true && this.status === false) {
|
// Check if the monitor has notifications
|
||||||
// Notify user that the monitor is down
|
if (monitor.notifications && monitor.notifications.length > 0) {
|
||||||
const emailService = new EmailService();
|
if (monitor.status !== this.status) {
|
||||||
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
const emailService = new EmailService();
|
||||||
const user = users[0];
|
|
||||||
|
|
||||||
await emailService.buildAndSendEmail(
|
if (monitor.status === true && this.status === false) {
|
||||||
"serverIsDownTemplate",
|
// Notify user that the monitor is down
|
||||||
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
||||||
user.email,
|
if (users.length > 0) {
|
||||||
`Monitor ${monitor.name} is down`
|
const user = users[0];
|
||||||
);
|
await emailService.buildAndSendEmail(
|
||||||
|
"serverIsDownTemplate",
|
||||||
|
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
||||||
|
user.email,
|
||||||
|
`Monitor ${monitor.name} is down`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor.status === false && this.status === true) {
|
||||||
|
// Notify user that the monitor is up
|
||||||
|
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
||||||
|
if (users.length > 0) {
|
||||||
|
const user = users[0];
|
||||||
|
await emailService.buildAndSendEmail(
|
||||||
|
"serverIsUpTemplate",
|
||||||
|
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
||||||
|
user.email,
|
||||||
|
`Monitor ${monitor.name} is back up`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
monitor.status = this.status;
|
||||||
|
await monitor.save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (monitor.status === false && this.status === true) {
|
|
||||||
// Notify user that the monitor is up
|
|
||||||
const emailService = new EmailService();
|
|
||||||
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
|
||||||
const user = users[0];
|
|
||||||
|
|
||||||
await emailService.buildAndSendEmail(
|
|
||||||
"serverIsUpTemplate",
|
|
||||||
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
|
||||||
user.email,
|
|
||||||
`Monitor ${monitor.name} is back up`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
monitor.status = this.status;
|
|
||||||
await monitor.save();
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@@ -72,4 +79,5 @@ CheckSchema.pre("save", async function (next) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = mongoose.model("Check", CheckSchema);
|
module.exports = mongoose.model("Check", CheckSchema);
|
||||||
Reference in new issue
Block a user