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,31 +104,17 @@ 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 {
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 })
modifyUserModel({ ...req.body, uuid, conn })
.then((users) => {
if (noResults(users)) { if (noResults(users)) {
const err = error404() const err = error404()
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return sendResponseNotFound(res, error) return sendResponseNotFound(res, error)
} }
const result = { const result = {
_data: { _data: {
message: 'User modified', message: 'User modified',
@ -136,13 +122,14 @@ const putUserController = async (req, res, next, config) => {
} }
} }
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) => {