diff --git a/Server/models/Check.js b/Server/models/Check.js index 7732fd312..bf4413977 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -1,57 +1,125 @@ const mongoose = require("mongoose"); +const EmailService = require("../service/emailService"); +const Notification = require("../models/Notification"); +/** + * Check Schema for MongoDB collection. + * + * Represents a check associated with a monitor, storing information + * about the status and response of a particular check event. + */ const CheckSchema = mongoose.Schema( { + /** + * Reference to the associated Monitor document. + * + * @type {mongoose.Schema.Types.ObjectId} + */ monitorId: { type: mongoose.Schema.Types.ObjectId, ref: "Monitor", immutable: true, }, + /** + * Status of the check (true for up, false for down). + * + * @type {Boolean} + */ status: { type: Boolean, }, + /** + * Response time of the check in milliseconds. + * + * @type {Number} + */ responseTime: { - type: Number, // milliseconds + type: Number, }, + /** + * HTTP status code received during the check. + * + * @type {Number} + */ statusCode: { - type: Number, // 200, ... , 500 + type: Number, }, + /** + * Message or description of the check result. + * + * @type {String} + */ message: { type: String, }, + /** + * Expiry date of the check, auto-calculated to expire after 30 days. + * + * @type {Date} + */ expiry: { type: Date, default: Date.now, - expires: 60 * 60 * 24 * 30, // 30 days in seconds + expires: 60 * 60 * 24 * 30, // 30 days }, }, - { - timestamps: true, + timestamps: true, // Adds createdAt and updatedAt timestamps } ); /** - * 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. + * Pre-save middleware to handle status change notifications. + * + * This middleware checks if the status of the monitor associated + * with the check has changed and sends notifications if necessary. */ - 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"); + if (monitor) { + const notifications = await Notification.find({ monitorId: this.monitorId }); + + // Check if there are any notifications + if (notifications && notifications.length > 0) { + // Only send email if monitor status has changed + if (monitor.status !== this.status) { + const emailService = new EmailService(); + + if (monitor.status === true && this.status === false) { + // Notify users that the monitor is down + for (const notification of notifications) { + if (notification.type === "email") { + await emailService.buildAndSendEmail( + "serverIsDownTemplate", + { monitorName: monitor.name, monitorUrl: monitor.url }, + notification.address, + `Monitor ${monitor.name} is down` + ); + } + } + } + + if (monitor.status === false && this.status === true) { + // Notify users that the monitor is up + for (const notification of notifications) { + if (notification.type === "email") { + await emailService.buildAndSendEmail( + "serverIsUpTemplate", + { monitorName: monitor.name, monitorUrl: monitor.url }, + notification.address, + `Monitor ${monitor.name} is back up` + ); + } + } + } + + // Update monitor status + monitor.status = this.status; + await monitor.save(); + } } - monitor.status = this.status; - await monitor.save(); } } catch (error) { console.log(error); @@ -60,4 +128,5 @@ CheckSchema.pre("save", async function (next) { } }); + module.exports = mongoose.model("Check", CheckSchema); diff --git a/Server/routes/checkRoute.js b/Server/routes/checkRoute.js index 6569dd49a..0216343c8 100644 --- a/Server/routes/checkRoute.js +++ b/Server/routes/checkRoute.js @@ -21,4 +21,4 @@ router.post( checkController.deleteChecks ); -module.exports = router; +module.exports = router; \ No newline at end of file