fixed user update for problem with role update

This commit is contained in:
Pau 2025-05-02 20:27:50 +02:00
parent b827eff9df
commit 8297962644
1 changed files with 22 additions and 35 deletions

View File

@ -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 conn = mysql.start(config)
const uuid = req.params.uuid const uuid = req.params.uuid
const { username, password, email, fk_role } = req.body
try { modifyUserModel({ ...req.body, uuid, conn })
let updatedData = { uuid } .then((users) => {
if (noResults(users)) {
if (username) updatedData.username = username const err = error404()
if (email) updatedData.email = email const error = errorHandler(err, config.environment)
if (fk_role) updatedData.fk_role = fk_role return sendResponseNotFound(res, error)
}
if (password) { const result = {
const hashedPassword = await bcrypt.hash(password, 10) _data: {
updatedData.password = hashedPassword message: 'User modified',
} users
}
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
} }
} next(result)
next(result) })
.catch((err) => {
} catch (err) { const error = errorHandler(err, config.environment)
const error = errorHandler(err, config.environment) res.status(error.code).json(error)
res.status(error.code).json(error) })
} finally { .finally(() => {
mysql.end(conn) mysql.end(conn)
} })
} }
const softDeleteUserController = (req, res, next, config) => { const softDeleteUserController = (req, res, next, config) => {