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 {
|
try {
|
||||||
const monitor = await req.db.deleteMonitor(req, res, next);
|
const monitor = await req.db.deleteMonitor(req, res, next);
|
||||||
|
// Delete associated checks and alerts
|
||||||
|
console.log(monitor);
|
||||||
req.jobQueue.deleteJob(monitor);
|
req.jobQueue.deleteJob(monitor);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -200,13 +202,11 @@ const editMonitor = async (req, res, next) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const editedMonitor = await req.db.editMonitor(req, res);
|
const editedMonitor = await req.db.editMonitor(req, res);
|
||||||
return res
|
return res.status(200).json({
|
||||||
.status(200)
|
success: true,
|
||||||
.json({
|
msg: successMessages.MONITOR_EDIT,
|
||||||
success: true,
|
data: editedMonitor,
|
||||||
msg: successMessages.MONITOR_EDIT,
|
});
|
||||||
data: editedMonitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
@@ -249,9 +249,12 @@ const deleteMonitor = async (req, res) => {
|
|||||||
const monitorId = req.params.monitorId;
|
const monitorId = req.params.monitorId;
|
||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
||||||
|
deleteChecks(monitorId);
|
||||||
|
deleteAlertByMonitorId(monitorId);
|
||||||
if (!monitor) {
|
if (!monitor) {
|
||||||
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
return monitor;
|
return monitor;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -319,6 +322,7 @@ const createCheck = async (checkData) => {
|
|||||||
const getChecks = async (monitorId) => {
|
const getChecks = async (monitorId) => {
|
||||||
try {
|
try {
|
||||||
const checks = await Check.find({ monitorId });
|
const checks = await Check.find({ monitorId });
|
||||||
|
console.log(checks);
|
||||||
return checks;
|
return checks;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw 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 = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -490,4 +503,5 @@ module.exports = {
|
|||||||
getAlertById,
|
getAlertById,
|
||||||
editAlert,
|
editAlert,
|
||||||
deleteAlert,
|
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 the userID does not match the document's userID, return a 403 error
|
||||||
if (userId.toString() !== doc.userId.toString()) {
|
if (userId.toString() !== doc.userId.toString()) {
|
||||||
console.log("boom");
|
console.log(userId.toString(), doc.userId.toString());
|
||||||
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED);
|
const error = new Error(errorMessages.VERIFY_OWNER_UNAUTHORIZED);
|
||||||
error.status = 403;
|
error.status = 403;
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -2,25 +2,42 @@ const router = require("express").Router();
|
|||||||
const alertController = require("../controllers/alertController");
|
const alertController = require("../controllers/alertController");
|
||||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||||
const Alert = require("../models/Alert");
|
const Alert = require("../models/Alert");
|
||||||
|
const Monitor = require("../models/Monitor");
|
||||||
|
|
||||||
// Create alert
|
// Create alert
|
||||||
router.post("/:monitorId", alertController.createAlert);
|
router.post(
|
||||||
|
"/:monitorId",
|
||||||
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
alertController.createAlert
|
||||||
|
);
|
||||||
// Get all alerts for a user
|
// 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
|
// 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
|
// Get a single alert
|
||||||
router.get("/:alertId", alertController.getAlertById);
|
router.get(
|
||||||
|
"/:alertId",
|
||||||
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
|
alertController.getAlertById
|
||||||
|
);
|
||||||
// Edit
|
// Edit
|
||||||
router.post(
|
router.post(
|
||||||
"/edit/:alertId",
|
"/edit/:alertId",
|
||||||
verifyOwnership(Alert, "alertId"),
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
alertController.editAlert
|
alertController.editAlert
|
||||||
);
|
);
|
||||||
//Delete
|
//Delete
|
||||||
router.post(
|
router.post(
|
||||||
"/delete/:alertId",
|
"/delete/:alertId",
|
||||||
verifyOwnership(Alert, "alertId"),
|
verifyOwnership(Monitor, "monitorId"),
|
||||||
alertController.deleteAlert
|
alertController.deleteAlert
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user