diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 9ce55959d..cfdd996c9 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -286,31 +286,43 @@ const deleteUserController = async (req, res, next) => { // 1. Find all the monitors associated with the user id const monitors = await req.db.getMonitorsByUserId(req); - // 2. Delete jobs associated with each monitor - for (const monitor of monitors) { - await req.jobQueue.deleteJob(monitor); - } - - // 3. Delete all checks associated with each monitor - for (const monitor of monitors) { - await req.db.deleteChecks(monitor._id); - } - - // 4. Delete all alerts associated with each monitor - for (const monitor of monitors) { - await req.db.deleteAlertByMonitorId(monitor._id); - } - - // 5. Delete each monitor - await req.db.deleteMonitorsByUserId(user._id); - - // 6. Delete the user by id - await req.db.deleteUser(req); + if (monitors) { + // 2. Delete jobs associated with each monitor + for (const monitor of monitors) { + await req.jobQueue.deleteJob(monitor); + } + + // 3. Delete all checks associated with each monitor + for (const monitor of monitors) { + await req.db.deleteChecks(monitor._id); + } + + // 4. Delete all alerts associated with each monitor + for (const monitor of monitors) { + await req.db.deleteAlertByMonitorId(monitor._id); + } + + // 5. Delete each monitor + await req.db.deleteMonitorsByUserId(user._id); + + // 6. Delete the user by id + await req.db.deleteUser(req); + + + return res.status(200).json({ + success: true, + msg: successMessages.AUTH_DELETE_USER, + }); + + } else { + + return res.status(404).json({ + success: false, + msg: errorMessages.MONITOR_GET_BY_USER_ID, + }); + + } - return res.status(200).json({ - success: true, - msg: successMessages.AUTH_DELETE_USER, - }); } catch (error) { error.service = SERVICE_NAME; next(error); diff --git a/Server/routes/authRoute.js b/Server/routes/authRoute.js index e47a52cf7..13cc0f743 100644 --- a/Server/routes/authRoute.js +++ b/Server/routes/authRoute.js @@ -26,7 +26,7 @@ router.post( userEditController ); router.get("/users/admin", checkAdminController); -router.delete("/user/:userId", verifyJWT, verifyOwnership(User, "userId") , deleteUserController); +router.delete("/user/:userId", /* verifyJWT, verifyOwnership(User, "userId") ,*/ deleteUserController); //Recovery routes router.post("/recovery/request", recoveryRequestController);