From 82979626445f8d010044e9838f9f1848bf1c81ca Mon Sep 17 00:00:00 2001 From: Pau Date: Fri, 2 May 2025 20:27:50 +0200 Subject: [PATCH] fixed user update for problem with role update --- .../authorization/userController.js | 57 +++++++------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/controllers/authorization/userController.js b/src/controllers/authorization/userController.js index 7ea81d7..0d25d88 100644 --- a/src/controllers/authorization/userController.js +++ b/src/controllers/authorization/userController.js @@ -104,45 +104,32 @@ const postUserController = (req, res, next, config) => { }) } -const putUserController = async (req, res, next, config) => { +const putUserController = (req, res, next, config) => { const conn = mysql.start(config) const uuid = req.params.uuid - const { username, password, email, fk_role } = req.body - try { - let updatedData = { uuid } - - if (username) updatedData.username = username - if (email) updatedData.email = email - if (fk_role) updatedData.fk_role = fk_role - - if (password) { - const hashedPassword = await bcrypt.hash(password, 10) - updatedData.password = hashedPassword - } - - const users = await modifyUserModel({ ...updatedData, conn }) - - if (noResults(users)) { - const err = error404() - const error = errorHandler(err, config.environment) - return sendResponseNotFound(res, error) - } - - const result = { - _data: { - message: 'User modified', - users + modifyUserModel({ ...req.body, uuid, conn }) + .then((users) => { + if (noResults(users)) { + const err = error404() + const error = errorHandler(err, config.environment) + return sendResponseNotFound(res, error) + } + const result = { + _data: { + message: 'User modified', + users + } } - } - next(result) - - } catch (err) { - const error = errorHandler(err, config.environment) - res.status(error.code).json(error) - } finally { - mysql.end(conn) - } + next(result) + }) + .catch((err) => { + const error = errorHandler(err, config.environment) + res.status(error.code).json(error) + }) + .finally(() => { + mysql.end(conn) + }) } const softDeleteUserController = (req, res, next, config) => {