From 9ccf42fe556fefd494ab65d1c55fab185aefee13 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 3 Jul 2024 11:41:12 -0700 Subject: [PATCH] Monitor now has a status, status will be updated on new check --- Server/models/Check.js | 17 +++++++++++++++++ Server/models/Monitor.js | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/Server/models/Check.js b/Server/models/Check.js index d57030202..7e46f418d 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -31,4 +31,21 @@ const CheckSchema = mongoose.Schema( } ); +CheckSchema.pre("save", async function (next) { + const monitor = await mongoose.model("Monitor").findById(this.monitorId); + + if (monitor.status === true && this.status === false) { + console.log("Monitor went down"); + } + + if (monitor.status === false && this.status === true) { + console.log("Monitor went up"); + } + + monitor.status = this.status; + await monitor.save(); + + 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,