Add conditional to avoid extra db operatons
This commit is contained in:
1 parent
9ccf42fe55
commit
031ae06d41
1 file changed
+15
-9
+15
-9
@@ -31,20 +31,26 @@ const CheckSchema = mongoose.Schema(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a check is created, we should update the stauts of the associated monitor
|
||||||
|
* if it has changed. Monitor starts with default status: true
|
||||||
|
*/
|
||||||
|
|
||||||
CheckSchema.pre("save", async function (next) {
|
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.status !== this.status) {
|
||||||
|
if (monitor.status === true && this.status === false) {
|
||||||
|
// TODO issue alert
|
||||||
|
console.log("Monitor went down");
|
||||||
|
}
|
||||||
|
|
||||||
if (monitor.status === true && this.status === false) {
|
if (monitor.status === false && this.status === true) {
|
||||||
console.log("Monitor went down");
|
// TODO issue alert
|
||||||
|
console.log("Monitor went up");
|
||||||
|
}
|
||||||
|
await monitor.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (monitor.status === false && this.status === true) {
|
|
||||||
console.log("Monitor went up");
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor.status = this.status;
|
|
||||||
await monitor.save();
|
|
||||||
|
|
||||||
next;
|
next;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user