From 81cd302accfd27136ef361ecbe38740160c5a3de Mon Sep 17 00:00:00 2001 From: MuhammadKhalilzadeh Date: Sat, 29 Jun 2024 11:58:27 +0330 Subject: [PATCH] modifications --- Server/controllers/authController.js | 42 +++++++++++++++++++--------- Server/db/MongoDB.js | 9 +++++- Server/routes/authRoute.js | 4 +-- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 66e2787f4..198d3d382 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -236,6 +236,15 @@ const resetPasswordController = async (req, res, next) => { } }; +/** + * Deletes a user and all associated monitors, checks, and alerts. + * + * @param {Object} req - The request object. + * @param {Object} res - The response object. + * @param {Function} next - The next middleware function. + * @returns {Object} The response object with success status and message. + * @throws {Error} If user validation fails or user is not found in the database. + */ const deleteUserController = async (req, res, next) => { try { // Validate user @@ -247,22 +256,29 @@ const deleteUserController = async (req, res, next) => { throw new Error(errorMessages.DB_USER_NOT_FOUND); } - // Step 1: Find all monitors associated with the user + // 1. Find all the monitors associated with the user id const monitors = await req.db.getMonitorsByUserId(req.params.userId); - - // Step 2: For each monitor, delete all associated checks and alerts - if (monitors) { - for (const monitor of monitors) { - await req.db.deleteChecks(monitor.id); - await req.db.deleteAlertByMonitorId(monitor.id); - } + + // 2. Delete jobs associated with each monitor + for (const monitor of monitors) { + await req.jobQueue.deleteJob(monitor); } - - // Step 3: Delete all monitors associated with the user + + // 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(req.params.userId); - - // Step 4: Finally, delete the user - await req.db.deleteUser(req.params.userId); + + // 6. Delete the user by id + await req.db.deleteUserById(req.params.userId); return res.status(200).json({ success: true, diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 84887bcde..c69edbe92 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -396,7 +396,7 @@ const getChecks = async (monitorId) => { const deleteChecks = async (monitorId) => { try { const result = await Check.deleteMany({ monitorId }); - return result; + return result.deletedCount; } catch (error) { throw error; } @@ -519,6 +519,13 @@ const deleteAlert = async (alertId) => { } }; +/** + * Deletes alerts by monitor ID. + * + * @param {string} monitorId - The ID of the monitor. + * @returns {Promise} A promise that resolves to the result of the delete operation. + * @throws {Error} If an error occurs while deleting the alerts. + */ const deleteAlertByMonitorId = async (monitorId) => { try { const result = await Alert.deleteMany({ monitorId }); diff --git a/Server/routes/authRoute.js b/Server/routes/authRoute.js index e6ff182c8..e383d4457 100644 --- a/Server/routes/authRoute.js +++ b/Server/routes/authRoute.js @@ -15,13 +15,11 @@ const { router.post("/register", registerController); router.post("/login", loginController); router.post("/user/:userId", verifyJWT, userEditController); +router.delete("/delete/:userId", deleteUserController); //Recovery routes router.post("/recovery/request", recoveryRequestController); router.post("/recovery/validate", validateRecoveryTokenController); router.post("/recovery/reset/", resetPasswordController); -//Delete routes -router.delete("/delete/:userId", deleteUserController); - module.exports = router;