From cc7e1bb111a27024afb170e3cd502152ab6fa804 Mon Sep 17 00:00:00 2001 From: M M Date: Thu, 1 Aug 2024 20:42:14 -0700 Subject: [PATCH] Testing email notifications for when monitor goes down. --- Server/models/Check.js | 40 ++++++++++++++++++++++++------------- Server/routes/checkRoute.js | 32 +++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Server/models/Check.js b/Server/models/Check.js index 7732fd312..43139d505 100644 --- a/Server/models/Check.js +++ b/Server/models/Check.js @@ -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(); diff --git a/Server/routes/checkRoute.js b/Server/routes/checkRoute.js index 6569dd49a..ff65fd2fc 100644 --- a/Server/routes/checkRoute.js +++ b/Server/routes/checkRoute.js @@ -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;