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

View File

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

View File

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