fixed structure and error handling

This commit is contained in:
Pau 2025-04-06 18:17:50 +02:00
parent 3713c4624a
commit b0044ad9b6
6 changed files with 95 additions and 97 deletions

View File

@ -29,7 +29,7 @@ const getEndpointsController = (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)
@ -57,7 +57,7 @@ const getEndpointsByUuidController = (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)
@ -85,7 +85,7 @@ const getEndpointsByRouteController = (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)
@ -94,7 +94,7 @@ const getEndpointsByRouteController = (req, res, next, config) => {
const postEndpointsController = (req, res, next, config) => { const postEndpointsController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const created_by = req.headers['uuid_requester'] || null const created_by = req.auth.user || null
insertEndpointsModel({ ...req.body, created_by, conn }) insertEndpointsModel({ ...req.body, created_by, conn })
.then((endpoints) => { .then((endpoints) => {
@ -105,7 +105,7 @@ const postEndpointsController = (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)
@ -125,7 +125,7 @@ const putEndpointsController = (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)
@ -135,17 +135,16 @@ const putEndpointsController = (req, res, next, config) => {
const softDeleteEndpointsController = (req, res, next, config) => { const softDeleteEndpointsController = (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 { deleted } = req.body const deletedby = req.auth.user || null
const deletedby = req.headers['uuid_requester'] || null
softDeleteEndpointsModel({ uuid, deleted, deletedby, conn }) softDeleteEndpointsModel({ uuid, deletedby, conn })
.then(() => { .then(() => {
const result = {} const result = {}
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -163,7 +162,7 @@ const deleteEndpointsController = (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)

View File

@ -52,7 +52,7 @@ const postLoginController = (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);
@ -69,7 +69,8 @@ const _getRolePermissions = (config, roleName, conn) => {
return response return response
}) })
.catch((err) => { .catch((err) => {
return errorHandler(err, config.environment); const error = errorHandler(err, config.environment);
return sendResponseUnauthorized(res, error);
}) })
} }

View File

@ -29,7 +29,7 @@ const getPermissionController = (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)
@ -57,7 +57,7 @@ const getPermissionByUuidController = (req, res, next, config) => {
}) })
.catch((err) => { .catch((err) => {
const error = errorHandler(err, config.environment) const error = errorHandler(err, config.environment)
return res.status(error.code).json(error) res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -66,7 +66,7 @@ const getPermissionByUuidController = (req, res, next, config) => {
const postPermissionController = (req, res, next, config) => { const postPermissionController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const created_by = req.headers['uuid_requester'] || null const created_by = req.auth.user || null
insertPermissionModel({...req.body, created_by, conn}) insertPermissionModel({...req.body, created_by, conn})
.then((response) => { .then((response) => {
@ -101,7 +101,7 @@ const putPermissionController = (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)
@ -111,17 +111,16 @@ const putPermissionController = (req, res, next, config) => {
const softDeletePermissionController = (req, res, next, config) => { const softDeletePermissionController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const uuid_permission = req.params.uuid const uuid_permission = req.params.uuid
const deletedBy = req.headers['uuid_requester'] || null const deletedBy = req.auth.user || null
const {deleted} = req.body
softDeletePermissionModel({uuid_permission, deleted, deletedBy, conn}) softDeletePermissionModel({uuid_permission, deletedBy, conn})
.then(() => { .then(() => {
const result = {} const result = {}
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -29,7 +29,7 @@ const getRoleController = (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)
@ -57,7 +57,7 @@ const getRoleInfoController = (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)
@ -85,7 +85,7 @@ const getRoleByNameController = (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)
@ -94,19 +94,19 @@ const getRoleByNameController = (req, res, next, config) => {
const postRoleController = (req, res, next, config) => { const postRoleController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const createdby = req.headers['uuid-requester'] || null const createdBy = req.auth.user || null
insertRoleModel({ ...req.body, createdby, conn }) insertRoleModel({ ...req.body, createdBy, conn })
.then((RoleInformation) => { .then((RoleInformation) => {
const result = { const result = {
_data: { Role: RoleInformation } _data: { role: RoleInformation }
} }
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -121,7 +121,7 @@ const putRoleController = (req, res, next, config) => {
.then((RoleInformation) => { .then((RoleInformation) => {
const result = { const result = {
_data: { _data: {
message: 'Role created', message: 'Role modified',
Role: RoleInformation Role: RoleInformation
} }
} }
@ -129,7 +129,7 @@ const putRoleController = (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)
@ -139,17 +139,16 @@ const putRoleController = (req, res, next, config) => {
const deleteRoleController = (req, res, next, config) => { const deleteRoleController = (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 { deleted } = req.body const deletedby = req.auth.user || null
const deletedby = req.headers['uuid-requester'] || null
softDeleteRoleModel({ uuid, deleted, deletedby, conn }) softDeleteRoleModel({ uuid, deletedby, conn })
.then(() => { .then(() => {
const result = {} const result = {}
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)

View File

@ -1,24 +1,25 @@
import mysql from '../../adapters/mysql' import { error404, errorHandler } from '../../utils/errors.js'
import { sendResponseNotFound } from '../../utils/responses.js'
import { noResults } from '../../validators/result-validators.js'
import mysql from '../../adapters/mysql.js'
import { import {
getRolesHasPermissionsModel, getPermissionModel,
countRolesHasPermissionsModel, countPermissionModel,
insertRolesHasPermissionsModel, insertPermissionModel,
modifyRolesHasPermissionsModel, modifyPermissionModel,
softDeleteRolesHasPermissionsModel softDeletePermissionModel
} from '../../repositories/authorization/roles_has_permissionsRepository' } from '../../models/authorization/permissionsModel.js'
import { error404, errorHandler } from '../../utils/errors'
import { noResults } from '../../validators/result-validators'
const getRolesHasPermissionsController = (req, res, next, config) => { const getPermissionController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
Promise.all([ Promise.all([
getRolesHasPermissionsModel({...req.query, uuidList, conn}), getPermissionModel({...req.query, conn}),
countRolesHasPermissionsModel({...req.query, uuidList, conn}) countPermissionModel({...req.query, conn})
]) ])
.then(([getResults, countResults]) => { .then(([getResults, countResults]) => {
next({ next({
_data: {roles_has_permissions: getResults}, _data: {permissions: getResults},
_page: { _page: {
totalElements: countResults, totalElements: countResults,
limit: req.query.limit || 100, limit: req.query.limit || 100,
@ -28,18 +29,18 @@ const getRolesHasPermissionsController = (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)
}) })
} }
const getRolesHasPermissionsControllerByRoleName = (req, res, next, config) => { const getPermissionByUuidController = (req, res, next, config) => {
const uuid_permission = req.params.uuid
const conn = mysql.start(config) const conn = mysql.start(config)
const roleName = req.params.name
getRolesHasPermissionsModel({ roleName, conn }) getPermissionModel({ uuid_permission, conn })
.then((response) => { .then((response) => {
if (noResults(response)) { if (noResults(response)) {
const err = error404() const err = error404()
@ -49,83 +50,77 @@ const getRolesHasPermissionsControllerByRoleName = (req, res, next, config) => {
const result = { const result = {
_data: { _data: {
roles_has_permissions: response permissions: response
} }
} }
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
}) })
} }
const getPermissionsByRoleController = (req, res, next, config) => { const postPermissionController = (req, res, next, config) => {
const uuid_role = req.params.uuid
const conn = mysql.start(config) const conn = mysql.start(config)
const created_by = req.auth.user || null
getRolesHasPermissionsModel({ uuid_role, conn }) insertPermissionModel({...req.body, created_by, 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: {
roles_has_permissions: response permissions: response
} }
} }
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
}) })
} }
const postRolesHasPermissionsController = (req, res, next, config) => { const putPermissionController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const createdBy = req.headers['uuid-requester'] || null const uuid_permission = req.params.uuid
const roleUuid = req.body.uuid
insertRolesHasPermissionsModel({...req.body, roleUuid, createdBy, conn}) modifyPermissionModel({...req.body, uuid_permission, conn})
.then((roles_has_permissions) => { .then((response) => {
const result = { const result = {
_data: roles_has_permissions _data: {
permissions: response
}
} }
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
}) })
} }
const softDeleteRolesHasPermissionsController = (req, res, next, config) => { const softDeletePermissionController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const uuid = req.params.uuid const uuid_permission = req.params.uuid
const { deleted } = req.body const deletedBy = req.auth.user || null
const deletedby = req.headers['uuid-requester'] || null
softDeleteRolesHasPermissionsModel({ uuid, deleted, deletedby, conn }) softDeletePermissionModel({uuid_permission, deletedBy, conn})
.then(() => { .then(() => {
const result = {} const result = {}
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -133,9 +128,9 @@ const softDeleteRolesHasPermissionsController = (req, res, next, config) => {
} }
export { export {
getRolesHasPermissionsController, getPermissionController,
getPermissionsByRoleController, getPermissionByUuidController,
postRolesHasPermissionsController, postPermissionController,
softDeleteRolesHasPermissionsController, putPermissionController,
getRolesHasPermissionsControllerByRoleName softDeletePermissionController
} }

View File

@ -1,3 +1,4 @@
import bcrypt from 'bcrypt' import bcrypt from 'bcrypt'
import mysql from '../../adapters/mysql.js' import mysql from '../../adapters/mysql.js'
import { import {
@ -65,7 +66,7 @@ const getUserInfoController = (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)
@ -74,13 +75,18 @@ const getUserInfoController = (req, res, next, config) => {
const postUserController = (req, res, next, config) => { const postUserController = (req, res, next, config) => {
const conn = mysql.start(config) const conn = mysql.start(config)
const createdby = req.headers['uuid-requester'] || null const createdBy = req.auth.user || null
const { username, password} = req.body const {username, password, email, fk_role} = req.body
if (!username || !password) {
const err = error404()
return sendResponseNotFound(res, err)
}
//encrypt password //encrypt password
bcrypt bcrypt
.hash(password, config.saltRounds) .hash(password, config.saltRounds)
.then(hash => insertUserModel({ username, password: hash, email, fk_role, createdby, conn })) .then(hash => {
insertUserModel({ conn, username, password: hash, email, fk_role, createdBy })
})
.then((users) => { .then((users) => {
const result = { const result = {
_data: { _data: {
@ -92,7 +98,7 @@ const postUserController = (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).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)
@ -115,7 +121,7 @@ const putUserController = (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)
@ -125,17 +131,16 @@ const putUserController = (req, res, next, config) => {
const softDeleteUserController = (req, res, next, config) => { const softDeleteUserController = (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 { deleted } = req.body const deletedby = req.auth.user || null
const deletedby = req.headers['uuid-requester'] || null
softDeleteUserModel({ uuid, deleted, deletedby, conn }) softDeleteUserModel({ uuid, deletedby, conn })
.then(() => { .then(() => {
const result = {} const result = {}
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) return res.status(error.code).json(error)
}) })
.finally(() => { .finally(() => {
mysql.end(conn) mysql.end(conn)