mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-18 07:39:54 -06:00
Testing email notifications for when monitor goes down.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const mongoose = require("mongoose");
|
||||
const EmailService = require("../service/emailService");
|
||||
|
||||
const CheckSchema = mongoose.Schema(
|
||||
{
|
||||
@@ -11,10 +12,10 @@ const CheckSchema = mongoose.Schema(
|
||||
type: Boolean,
|
||||
},
|
||||
responseTime: {
|
||||
type: Number, // milliseconds
|
||||
type: Number,
|
||||
},
|
||||
statusCode: {
|
||||
type: Number, // 200, ... , 500
|
||||
type: Number,
|
||||
},
|
||||
message: {
|
||||
type: String,
|
||||
@@ -22,33 +23,44 @@ const CheckSchema = mongoose.Schema(
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 60 * 60 * 24 * 30, // 30 days in seconds
|
||||
expires: 60 * 60 * 24 * 30,
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* 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");
|
||||
// Notify user that the monitor is down
|
||||
const emailService = new EmailService();
|
||||
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
||||
const user = users[0];
|
||||
|
||||
await emailService.buildAndSendEmail(
|
||||
"serverIsDownTemplate",
|
||||
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
||||
user.email,
|
||||
`Monitor ${monitor.name} is down`
|
||||
);
|
||||
}
|
||||
|
||||
if (monitor.status === false && this.status === true) {
|
||||
// TODO issue alert
|
||||
console.log("Monitor went up");
|
||||
// Notify user that the monitor is up
|
||||
const emailService = new EmailService();
|
||||
const users = await mongoose.model("User").find({ _id: monitor.userId });
|
||||
const user = users[0];
|
||||
|
||||
await emailService.buildAndSendEmail(
|
||||
"serverIsUpTemplate",
|
||||
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
||||
user.email,
|
||||
`Monitor ${monitor.name} is back up`
|
||||
);
|
||||
}
|
||||
monitor.status = this.status;
|
||||
await monitor.save();
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
const router = require("express").Router();
|
||||
const checkController = require("../controllers/checkController");
|
||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||
const Check = require("../models/Check");
|
||||
const Monitor = require("../models/Monitor");
|
||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||
const { verifyJWT } = require("../middleware/verifyJWT");
|
||||
const checkController = require("../controllers/checkController");
|
||||
|
||||
router.use(verifyJWT);
|
||||
|
||||
router.post(
|
||||
"/:monitorId",
|
||||
@@ -21,4 +25,28 @@ router.post(
|
||||
checkController.deleteChecks
|
||||
);
|
||||
|
||||
// Temporary test route
|
||||
router.post("/test-notification/:monitorId", async (req, res) => {
|
||||
try {
|
||||
const monitorId = req.params.monitorId;
|
||||
const monitor = await Monitor.findById(monitorId);
|
||||
if (!monitor) {
|
||||
return res.status(404).json({ message: "Monitor not found" });
|
||||
}
|
||||
|
||||
const newCheck = new Check({
|
||||
monitorId,
|
||||
status: false,
|
||||
responseTime: 100,
|
||||
statusCode: 500,
|
||||
message: "Server error"
|
||||
});
|
||||
|
||||
await newCheck.save();
|
||||
res.status(201).json({ message: "Check created and email notification sent" });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user