fixed pagination and small syntax issues

This commit is contained in:
Pau 2025-04-07 23:22:52 +02:00
parent 027d8c7dee
commit 1e59a5a33a
6 changed files with 81 additions and 133 deletions

View File

@ -9,6 +9,7 @@ import {
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";
const getEndpointsController = (req, res, next, config) => {
const conn = mysql.start(config)
@ -64,34 +65,6 @@ const getEndpointsByUuidController = (req, res, next, config) => {
})
}
const getEndpointsByRouteController = (req, res, next, config) => {
const conn = mysql.start(config)
const route = req.params.route
getEndpointsModel({ route, conn })
.then((response) => {
if (noResults(response)) {
const err = error404()
const error = errorHandler(err, config.environment)
return sendResponseNotFound(res, error)
}
const result = {
_data: {
endpoints: response
}
}
next(result)
})
.catch((err) => {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)
})
}
const postEndpointsController = (req, res, next, config) => {
const conn = mysql.start(config)
const created_by = req.auth.user || null
@ -172,7 +145,6 @@ const deleteEndpointsController = (req, res, next, config) => {
export {
getEndpointsController,
getEndpointsByUuidController,
getEndpointsByRouteController,
postEndpointsController,
putEndpointsController,
softDeleteEndpointsController,

View File

@ -57,7 +57,7 @@ const getPermissionByUuidController = (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)

View File

@ -11,7 +11,7 @@ const postRefreshTokenController = (req, res, next, config) => {
const conn = mysql.start(config)
const { user } = req.auth
getUserListModel({ uuid: user })
getUserListModel({conn, uuid: user})
.then((response) => {
if (noResults(response)) {
const err = error401()
@ -35,9 +35,9 @@ const postRefreshTokenController = (req, res, next, config) => {
role: response[0].role
}
const {token, refreshToken} = generateTokens(tokenPayload)
const {accessToken, refreshToken} = generateTokens(tokenPayload)
//token application in frontend
next({token, refreshToken})
next({accessToken, refreshToken})
})
})
.catch((err) => {

View File

@ -11,29 +11,40 @@ import { sendResponseNotFound } from '../../utils/responses.js'
import { noResults } from '../../validators/result-validators.js'
const getRoleController = (req, res, next, config) => {
const conn = mysql.start(config)
// Parse and validate pagination parameters with defaults
const limit = parseInt(req.query.limit, 10) || 100;
const page = parseInt(req.query.page, 10) || 1;
Promise.all([
getRoleModel({ ...req.query, conn }),
countRoleModel({ ...req.query, conn })
])
.then(([getResults, countResults]) =>
next({
_data: { Role: getResults },
_page: {
totalElements: countResults,
limit: req.query.limit || 100,
page: req.query.page || (countResults && 1) || 0
}
})
)
.catch((err) => {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)
})
// Create sanitized query object
const queryParams = {
...req.query,
limit,
page
};
const conn = mysql.start(config)
Promise.all([
getRoleModel({ ...queryParams, conn }),
countRoleModel({ ...queryParams, conn })
])
.then(([getResults, countResults]) => {
next({
_data: {roles: getResults},
_page: {
totalElements: countResults,
limit: req.query.limit || 100,
page: req.query.page || (countResults && 1) || 0
}
})
})
.catch((err) => {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)
})
}
const getRoleInfoController = (req, res, next, config) => {
@ -50,35 +61,7 @@ const getRoleInfoController = (req, res, next, config) => {
const result = {
_data: {
Role: RoleInformation
}
}
next(result)
})
.catch((err) => {
const error = errorHandler(err, config.environment)
return res.status(error.code).json(error)
})
.finally(() => {
mysql.end(conn)
})
}
const getRoleByNameController = (req, res, next, config) => {
const conn = mysql.start(config)
const name = req.params.name
getRoleModel({ name, conn })
.then((RoleInformation) => {
if (noResults(RoleInformation)) {
const err = error404()
const error = errorHandler(err, config.environment)
return sendResponseNotFound(res, error)
}
const result = {
_data: {
Role: RoleInformation
roles: RoleInformation
}
}
next(result)
@ -99,7 +82,7 @@ const postRoleController = (req, res, next, config) => {
insertRoleModel({ ...req.body, createdBy, conn })
.then((RoleInformation) => {
const result = {
_data: { role: RoleInformation }
_data: { roles: RoleInformation }
}
next(result)
@ -122,7 +105,7 @@ const putRoleController = (req, res, next, config) => {
const result = {
_data: {
message: 'Role modified',
Role: RoleInformation
roles: RoleInformation
}
}
next(result)
@ -160,6 +143,5 @@ export {
getRoleController,
getRoleInfoController,
postRoleController,
putRoleController,
getRoleByNameController
putRoleController
}

View File

@ -1,25 +1,24 @@
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 {
getPermissionModel,
countPermissionModel,
insertPermissionModel,
modifyPermissionModel,
softDeletePermissionModel
} from '../../models/authorization/permissionsModel.js'
getRolesHasPermissionsModel,
countRolesHasPermissionsModel,
insertRolesHasPermissionsModel,
modifyRolesHasPermissionsModel,
softDeleteRolesHasPermissionsModel
} from '../../models/authorization/roles_has_permissionsModel.js'
import { errorHandler } from '../../utils/errors.js'
import { noResults } from '../../validators/result-validators.js'
const getPermissionController = (req, res, next, config) => {
const getRolesHasPermissionsController = (req, res, next, config) => {
const conn = mysql.start(config)
const uuidList = req.query.uuidList && req.query.uuidList.split(',')
Promise.all([
getPermissionModel({...req.query, conn}),
countPermissionModel({...req.query, conn})
getRolesHasPermissionsModel({...req.query, uuidList, conn}),
countRolesHasPermissionsModel({...req.query, uuidList, conn})
])
.then(([getResults, countResults]) => {
next({
_data: {permissions: getResults},
_data: {roles_has_permissions: getResults},
_page: {
totalElements: countResults,
limit: req.query.limit || 100,
@ -36,11 +35,10 @@ const getPermissionController = (req, res, next, config) => {
})
}
const getPermissionByUuidController = (req, res, next, config) => {
const uuid_permission = req.params.uuid
const getRolesHasPermissionsByUuidController = (req, res, next, config) => {
const uuid = req.params.uuid
const conn = mysql.start(config)
getPermissionModel({ uuid_permission, conn })
getRolesHasPermissionsModel({ uuid, conn })
.then((response) => {
if (noResults(response)) {
const err = error404()
@ -50,7 +48,7 @@ const getPermissionByUuidController = (req, res, next, config) => {
const result = {
_data: {
permissions: response
roles_has_permissions: response
}
}
next(result)
@ -64,16 +62,14 @@ const getPermissionByUuidController = (req, res, next, config) => {
})
}
const postPermissionController = (req, res, next, config) => {
const postRolesHasPermissionsController = (req, res, next, config) => {
const conn = mysql.start(config)
const created_by = req.auth.user || null
const createdBy = req.auth.user || null
insertPermissionModel({...req.body, created_by, conn})
.then((response) => {
insertRolesHasPermissionsModel({...req.body, createdBy, conn})
.then((roles_has_permissions) => {
const result = {
_data: {
permissions: response
}
_data: roles_has_permissions
}
next(result)
})
@ -86,16 +82,14 @@ const postPermissionController = (req, res, next, config) => {
})
}
const putPermissionController = (req, res, next, config) => {
const putRolesHasPermissionsController = (req, res, next, config) => {
const conn = mysql.start(config)
const uuid_permission = req.params.uuid
const uuid = req.params.uuid
modifyPermissionModel({...req.body, uuid_permission, conn})
.then((response) => {
modifyRolesHasPermissionsModel({ ...req.body, uuid, modifiedBy, conn })
.then((roles_has_permissions) => {
const result = {
_data: {
permissions: response
}
_data: roles_has_permissions
}
next(result)
})
@ -108,12 +102,13 @@ const putPermissionController = (req, res, next, config) => {
})
}
const softDeletePermissionController = (req, res, next, config) => {
const softDeleteRolesHasPermissionsController = (req, res, next, config) => {
const conn = mysql.start(config)
const uuid_permission = req.params.uuid
const deletedBy = req.auth.user || null
const uuid = req.params.uuid
const { deleted } = req.body
const deletedby = req.auth.user || null
softDeletePermissionModel({uuid_permission, deletedBy, conn})
softDeleteRolesHasPermissionsModel({ uuid, deleted, deletedby, conn })
.then(() => {
const result = {}
next(result)
@ -128,9 +123,9 @@ const softDeletePermissionController = (req, res, next, config) => {
}
export {
getPermissionController,
getPermissionByUuidController,
postPermissionController,
putPermissionController,
softDeletePermissionController
getRolesHasPermissionsController,
getRolesHasPermissionsByUuidController,
postRolesHasPermissionsController,
softDeleteRolesHasPermissionsController,
putRolesHasPermissionsController,
}

View File

@ -1,4 +1,3 @@
import bcrypt from 'bcrypt'
import mysql from '../../adapters/mysql.js'
import {
@ -35,7 +34,7 @@ const getUserListController = (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)