From 7526fe2a683152e4212f37f7214b620e73661bf0 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Sep 2024 10:52:17 -0700 Subject: [PATCH] fix delete user method --- Server/controllers/authController.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 0cf297a47..69483f5e9 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -465,11 +465,10 @@ const deleteUserController = async (req, res, next) => { // 1. Find all the monitors associated with the user id - const monitors = await req.db.getMonitorsByUserId({ - params: { userId: _id }, + const result = await req.db.getMonitorsByTeamId({ + params: { teamId: user.teamId }, }); - - if (monitors) { + if (result?.monitors.length > 0) { //2. Remove all jobs, delete checks and alerts await Promise.all( monitors.map(async (monitor) => { @@ -484,22 +483,17 @@ const deleteUserController = async (req, res, next) => { // 3. Delete each monitor await req.db.deleteMonitorsByUserId(user._id); - - // 4. Delete the user by id - await req.db.deleteUser(user._id); - - 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, - }); } + // 4. Delete the user by id + await req.db.deleteUser(user._id); + + return res.status(200).json({ + success: true, + msg: successMessages.AUTH_DELETE_USER, + }); } catch (error) { error.service = SERVICE_NAME; + error.method === undefined && error.method === "deleteUserController"; next(error); } };