mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-14 13:49:40 -06:00
Add conditional to avoid extra db operatons
This commit is contained in:
@@ -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) {
|
||||
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) {
|
||||
console.log("Monitor went down");
|
||||
if (monitor.status === false && this.status === true) {
|
||||
// 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;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user