Testing email notifications for when monitor goes down.
This commit is contained in:
1 parent
c2a341a3c5
commit
cc7e1bb111
2 files changed
+56
-16
No files matched your search
+26
-14
@@ -1,4 +1,5 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
|
const EmailService = require("../service/emailService");
|
||||||
|
|
||||||
const CheckSchema = mongoose.Schema(
|
const CheckSchema = mongoose.Schema(
|
||||||
{
|
{
|
||||||
@@ -11,10 +12,10 @@ const CheckSchema = mongoose.Schema(
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
},
|
},
|
||||||
responseTime: {
|
responseTime: {
|
||||||
type: Number, // milliseconds
|
type: Number,
|
||||||
},
|
},
|
||||||
statusCode: {
|
statusCode: {
|
||||||
type: Number, // 200, ... , 500
|
type: Number,
|
||||||
},
|
},
|
||||||
message: {
|
message: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -22,33 +23,44 @@ const CheckSchema = mongoose.Schema(
|
|||||||
expiry: {
|
expiry: {
|
||||||
type: Date,
|
type: Date,
|
||||||
default: Date.now,
|
default: Date.now,
|
||||||
expires: 60 * 60 * 24 * 30, // 30 days in seconds
|
expires: 60 * 60 * 24 * 30,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
timestamps: true,
|
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) {
|
CheckSchema.pre("save", async function (next) {
|
||||||
try {
|
try {
|
||||||
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
||||||
if (monitor && monitor.status !== this.status) {
|
if (monitor && monitor.status !== this.status) {
|
||||||
if (monitor.status === true && this.status === false) {
|
if (monitor.status === true && this.status === false) {
|
||||||
// TODO issue alert
|
// Notify user that the monitor is down
|
||||||
console.log("Monitor went 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) {
|
if (monitor.status === false && this.status === true) {
|
||||||
// TODO issue alert
|
// Notify user that the monitor is up
|
||||||
console.log("Monitor went 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;
|
monitor.status = this.status;
|
||||||
await monitor.save();
|
await monitor.save();
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
const checkController = require("../controllers/checkController");
|
const Check = require("../models/Check");
|
||||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
|
||||||
const Monitor = require("../models/Monitor");
|
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(
|
router.post(
|
||||||
"/:monitorId",
|
"/:monitorId",
|
||||||
@@ -21,4 +25,28 @@ router.post(
|
|||||||
checkController.deleteChecks
|
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;
|
module.exports = router;
|
||||||
Reference in new issue
Block a user