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

View File

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

View File

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

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

View File

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