From 7526fe2a683152e4212f37f7214b620e73661bf0 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Sep 2024 10:52:17 -0700 Subject: [PATCH 1/2] 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); } }; From 484231144d090ad08cddf3b4e5103c7ebc26b6e6 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 6 Sep 2024 10:56:07 -0700 Subject: [PATCH 2/2] Add superadmin check --- Server/controllers/authController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 69483f5e9..fe406a6b6 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -463,12 +463,12 @@ const deleteUserController = async (req, res, next) => { throw new Error(errorMessages.DB_USER_NOT_FOUND); } - // 1. Find all the monitors associated with the user id + // 1. Find all the monitors associated with the team ID if superadmin const result = await req.db.getMonitorsByTeamId({ params: { teamId: user.teamId }, }); - if (result?.monitors.length > 0) { + if (user.role.includes("superadmin") && result?.monitors.length > 0) { //2. Remove all jobs, delete checks and alerts await Promise.all( monitors.map(async (monitor) => {