diff --git a/Server/models/Check.js b/Server/models/Check.js index d57030202..339c21482 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -31,4 +31,32 @@ 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 + * @param {function} next - Callback to proceed to the next middleware. + */ + +CheckSchema.pre("save", async function (next) { + try { + const monitor = await mongoose.model("Monitor").findById(this.monitorId); + if (monitor && monitor.status !== this.status) { + if (monitor.status === true && this.status === false) { + // TODO issue alert + console.log("Monitor went down"); + } + + if (monitor.status === false && this.status === true) { + // TODO issue alert + console.log("Monitor went up"); + } + await monitor.save(); + } + } catch (error) { + console.log(error); + } finally { + next(); + } +}); + module.exports = mongoose.model("Check", CheckSchema); diff --git a/Server/models/Monitor.js b/Server/models/Monitor.js index 8455aa312..4b3bd7c63 100644 --- a/Server/models/Monitor.js +++ b/Server/models/Monitor.js @@ -14,6 +14,10 @@ const MonitorSchema = mongoose.Schema( description: { type: String, }, + status: { + type: Boolean, + default: true, + }, type: { type: String, required: true,