Added delete cascade for Montiors. Alerts and Checks now also deleted
This commit is contained in:
1 parent
86d9389abc
commit
6801d066a1
4 files changed
+45
-14
No files matched your search
@@ -161,6 +161,8 @@ const deleteMonitor = async (req, res, next) => {
|
||||
|
||||
try {
|
||||
const monitor = await req.db.deleteMonitor(req, res, next);
|
||||
// Delete associated checks and alerts
|
||||
console.log(monitor);
|
||||
req.jobQueue.deleteJob(monitor);
|
||||
|
||||
/**
|
||||
@@ -200,13 +202,11 @@ const editMonitor = async (req, res, next) => {
|
||||
|
||||
try {
|
||||
const editedMonitor = await req.db.editMonitor(req, res);
|
||||
return res
|
||||
.status(200)
|
||||
.json({
|
||||
success: true,
|
||||
msg: successMessages.MONITOR_EDIT,
|
||||
data: editedMonitor,
|
||||
});
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
msg: successMessages.MONITOR_EDIT,
|
||||
data: editedMonitor,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
|
||||
@@ -249,9 +249,12 @@ const deleteMonitor = async (req, res) => {
|
||||
const monitorId = req.params.monitorId;
|
||||
try {
|
||||
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
||||
deleteChecks(monitorId);
|
||||
deleteAlertByMonitorId(monitorId);
|
||||
if (!monitor) {
|
||||
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
||||
}
|
||||
|
||||
return monitor;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -319,6 +322,7 @@ const createCheck = async (checkData) => {
|
||||
const getChecks = async (monitorId) => {
|
||||
try {
|
||||
const checks = await Check.find({ monitorId });
|
||||
console.log(checks);
|
||||
return checks;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -467,6 +471,15 @@ const deleteAlert = async (alertId) => {
|
||||
}
|
||||
};
|
||||
|
||||
const deleteAlertByMonitorId = async (monitorId) => {
|
||||
try {
|
||||
const result = await Alert.deleteMany({ monitorId });
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
connect,
|
||||
insertUser,
|
||||
@@ -490,4 +503,5 @@ module.exports = {
|
||||
getAlertById,
|
||||
editAlert,
|
||||
deleteAlert,
|
||||
deleteAlertByMonitorId,
|
||||
};
|
||||
@@ -20,7 +20,7 @@ const verifyOwnership = (Model, paramName) => {
|
||||
|
||||
// If the userID does not match the document's userID, return a 403 error
|
||||
if (userId.toString() !== doc.userId.toString()) {
|
||||
console.log("boom");
|
||||
console.log(userId.toString(), doc.userId.toString());
|
||||
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED);
|
||||
error.status = 403;
|
||||
throw error;
|
||||
|
||||
@@ -2,25 +2,42 @@ const router = require("express").Router();
|
||||
const alertController = require("../controllers/alertController");
|
||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||
const Alert = require("../models/Alert");
|
||||
const Monitor = require("../models/Monitor");
|
||||
|
||||
// Create alert
|
||||
router.post("/:monitorId", alertController.createAlert);
|
||||
router.post(
|
||||
"/:monitorId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.createAlert
|
||||
);
|
||||
// Get all alerts for a user
|
||||
router.get("/user/:userId", alertController.getAlertsByUserId);
|
||||
router.get(
|
||||
"/user/:userId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.getAlertsByUserId
|
||||
);
|
||||
// Get all alerts for a monitor
|
||||
router.get("/monitor/:monitorId", alertController.getAlertsByMonitorId);
|
||||
router.get(
|
||||
"/monitor/:monitorId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.getAlertsByMonitorId
|
||||
);
|
||||
// Get a single alert
|
||||
router.get("/:alertId", alertController.getAlertById);
|
||||
router.get(
|
||||
"/:alertId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.getAlertById
|
||||
);
|
||||
// Edit
|
||||
router.post(
|
||||
"/edit/:alertId",
|
||||
verifyOwnership(Alert, "alertId"),
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.editAlert
|
||||
);
|
||||
//Delete
|
||||
router.post(
|
||||
"/delete/:alertId",
|
||||
verifyOwnership(Alert, "alertId"),
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
alertController.deleteAlert
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user