fixing small problems regarding api functionality

This commit is contained in:
Pau 2025-04-14 20:45:35 +02:00
parent 7a38575ffc
commit 10f7f8c91a
3 changed files with 50 additions and 24 deletions

View File

@ -112,17 +112,31 @@ const postUserController = (req, res, next, config) => {
})
}
const putUserController = (req, res, next, config) => {
const putUserController = async (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 })
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',
@ -130,14 +144,13 @@ const putUserController = (req, res, next, config) => {
}
}
next(result)
})
.catch((err) => {
} catch (err) {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
.finally(() => {
} finally {
mysql.end(conn)
})
}
}
const softDeleteUserController = (req, res, next, config) => {

View File

@ -78,6 +78,14 @@ const postCoordinatesController = (req, res, next, config) => {
next(result)
})
.catch((err) => {
if (err.code === 'ER_DUP_ENTRY') {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
}
if (err.code === 'ER_BAD_NULL_ERROR') {
const error = error404()
return res.status(error.code).json(error)
}
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
@ -92,6 +100,11 @@ const putCoordinatesController = (req, res, next, config) => {
modifyCoordinatesModel({ ...req.body, uuid: uuid_coordinates, conn })
.then((response) => {
if (noResults(response)) {
const err = error404()
const error = errorHandler(err, config.environment)
return sendResponseNotFound(res, error)
}
const result = {
_data: {
coordinates: response

View File

@ -86,7 +86,7 @@ const getUserHasPlacesByUserUuidController = (req, res, next, config) => {
})
.catch((err) => {
const error = errorHandler(err, config.environment)
res.status(error.code).json(error)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)
@ -114,7 +114,7 @@ const getUserHasPlacesByPlaceUuidController = (req, res, next, config) => {
})
.catch((err) => {
const error = errorHandler(err, config.environment)
res.status(error.code).json(error)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)