diff --git a/src/controllers/authorization/userController.js b/src/controllers/authorization/userController.js index 48791d8..d077d2d 100644 --- a/src/controllers/authorization/userController.js +++ b/src/controllers/authorization/userController.js @@ -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 uuid = req.params.uuid + const { username, password, email, fk_role } = req.body - 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) => { + 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 res.status(error.code).json(error) - }) - .finally(() => { - mysql.end(conn) - }) + return sendResponseNotFound(res, error) + } + + 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) => { diff --git a/src/controllers/resource_types/coordinatesController.js b/src/controllers/resource_types/coordinatesController.js index 682a0eb..9746c88 100644 --- a/src/controllers/resource_types/coordinatesController.js +++ b/src/controllers/resource_types/coordinatesController.js @@ -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 diff --git a/src/controllers/resource_types/usersHasPlacesController.js b/src/controllers/resource_types/usersHasPlacesController.js index df9d4c2..a4b7afd 100644 --- a/src/controllers/resource_types/usersHasPlacesController.js +++ b/src/controllers/resource_types/usersHasPlacesController.js @@ -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)