Files
Checkmate/Server/models/Check.js
T

129 lines
3.1 KiB
JavaScript
Raw Normal View History

const mongoose = require("mongoose");
const EmailService = require("../service/emailService");
2024-08-06 20:45:27 -07:00
const Notification = require("../models/Notification");
2024-08-06 19:36:43 -07:00
/**
* Check Schema for MongoDB collection.
*
2024-08-06 19:36:43 -07:00
* Represents a check associated with a monitor, storing information
* about the status and response of a particular check event.
*/
const CheckSchema = mongoose.Schema(
{
2024-08-06 19:36:43 -07:00
/**
* Reference to the associated Monitor document.
*
2024-08-06 19:36:43 -07:00
* @type {mongoose.Schema.Types.ObjectId}
*/
monitorId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Monitor",
immutable: true,
},
2024-08-06 19:36:43 -07:00
/**
* Status of the check (true for up, false for down).
*
2024-08-06 19:36:43 -07:00
* @type {Boolean}
*/
status: {
type: Boolean,
},
2024-08-06 19:36:43 -07:00
/**
* Response time of the check in milliseconds.
*
2024-08-06 19:36:43 -07:00
* @type {Number}
*/
responseTime: {
type: Number,
},
2024-08-06 19:36:43 -07:00
/**
* HTTP status code received during the check.
*
2024-08-06 19:36:43 -07:00
* @type {Number}
*/
statusCode: {
type: Number,
},
2024-08-06 19:36:43 -07:00
/**
* Message or description of the check result.
*
2024-08-06 19:36:43 -07:00
* @type {String}
*/
message: {
type: String,
},
2024-08-06 19:36:43 -07:00
/**
* Expiry date of the check, auto-calculated to expire after 30 days.
*
2024-08-06 19:36:43 -07:00
* @type {Date}
*/
2024-08-12 15:15:13 -07:00
expiry: {
type: Date,
default: Date.now,
2024-08-06 19:36:43 -07:00
expires: 60 * 60 * 24 * 30, // 30 days
},
},
{
2024-08-06 19:36:43 -07:00
timestamps: true, // Adds createdAt and updatedAt timestamps
}
);
2024-08-06 19:36:43 -07:00
/**
* Pre-save middleware to handle status change notifications.
*
2024-08-06 19:36:43 -07:00
* 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) {
2024-07-03 12:32:42 -07:00
try {
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
if (monitor) {
const notifications = await Notification.find({
monitorId: this.monitorId,
});
2024-08-06 20:45:27 -07:00
// Check if there are any notifications
// Only send email if monitor status has changed
if (monitor.status !== this.status) {
const emailService = new EmailService();
let template = "";
let status = "";
if (monitor.status === true && this.status === false) {
template = "serverIsDownTemplate";
status = "down";
}
if (monitor.status === false && this.status === true) {
// Notify users that the monitor is up
template = "serverIsUpTemplate";
status = "up";
}
for (const notification of notifications) {
if (notification.type === "email") {
await emailService.buildAndSendEmail(
template,
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is ${status}`
);
}
}
// Update monitor status
monitor.status = this.status;
await monitor.save();
2024-07-03 12:32:42 -07:00
}
2024-07-03 11:46:30 -07:00
}
2024-07-03 12:32:42 -07:00
} catch (error) {
console.log(error);
} finally {
next();
}
});
module.exports = mongoose.model("Check", CheckSchema);