added 409 error if conflict with database
This commit is contained in:
parent
c22a8d0fbe
commit
5ca63437ca
|
|
@ -91,12 +91,25 @@ const putEndpointsController = (req, res, next, config) => {
|
||||||
|
|
||||||
modifyEndpointsModel({ ...req.body, uuid, conn })
|
modifyEndpointsModel({ ...req.body, uuid, conn })
|
||||||
.then((endpoints) => {
|
.then((endpoints) => {
|
||||||
|
if (noResults(endpoints)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
const result = {
|
const result = {
|
||||||
_data: { endpoints }
|
_data: { endpoints }
|
||||||
}
|
}
|
||||||
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)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,9 @@ 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.auth.user || null
|
const createdBy = req.auth.user || null
|
||||||
|
|
||||||
insertPermissionModel({...req.body, created_by, conn})
|
insertPermissionModel({...req.body, createdBy, conn})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const result = {
|
const result = {
|
||||||
_data: {
|
_data: {
|
||||||
|
|
@ -78,6 +78,14 @@ const postPermissionController = (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)
|
||||||
})
|
})
|
||||||
|
|
@ -90,8 +98,14 @@ const putPermissionController = (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
|
||||||
|
|
||||||
modifyPermissionModel({...req.body, uuid_permission, conn})
|
modifyPermissionModel({...req.body, uuid: uuid_permission, 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: {
|
||||||
permissions: response
|
permissions: response
|
||||||
|
|
@ -113,7 +127,7 @@ const softDeletePermissionController = (req, res, next, config) => {
|
||||||
const uuid_permission = req.params.uuid
|
const uuid_permission = req.params.uuid
|
||||||
const deletedBy = req.auth.user || null
|
const deletedBy = req.auth.user || null
|
||||||
|
|
||||||
softDeletePermissionModel({uuid_permission, deletedBy, conn})
|
softDeletePermissionModel({uuid: uuid_permission, deletedBy, conn})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const result = {}
|
const result = {}
|
||||||
next(result)
|
next(result)
|
||||||
|
|
|
||||||
|
|
@ -29,12 +29,16 @@ const getRoleController = (req, res, next, config) => {
|
||||||
countRoleModel({ ...queryParams, conn })
|
countRoleModel({ ...queryParams, conn })
|
||||||
])
|
])
|
||||||
.then(([getResults, countResults]) => {
|
.then(([getResults, countResults]) => {
|
||||||
|
// Calculate total pages for frontend pagination
|
||||||
|
const totalPages = Math.ceil(countResults / limit);
|
||||||
|
|
||||||
next({
|
next({
|
||||||
_data: {roles: getResults},
|
_data: { role: getResults },
|
||||||
_page: {
|
_page: {
|
||||||
totalElements: countResults,
|
totalElements: countResults,
|
||||||
limit: req.query.limit || 100,
|
totalPages,
|
||||||
page: req.query.page || (countResults && 1) || 0
|
limit,
|
||||||
|
page
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -52,8 +56,8 @@ const getRoleInfoController = (req, res, next, config) => {
|
||||||
const uuid = req.params.uuid
|
const uuid = req.params.uuid
|
||||||
|
|
||||||
getRoleModel({ uuid, conn })
|
getRoleModel({ uuid, conn })
|
||||||
.then((RoleInformation) => {
|
.then((roleInformation) => {
|
||||||
if (noResults(RoleInformation)) {
|
if (noResults(roleInformation)) {
|
||||||
const err = error404()
|
const err = error404()
|
||||||
const error = errorHandler(err, config.environment)
|
const error = errorHandler(err, config.environment)
|
||||||
return sendResponseNotFound(res, error)
|
return sendResponseNotFound(res, error)
|
||||||
|
|
@ -61,7 +65,7 @@ const getRoleInfoController = (req, res, next, config) => {
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
_data: {
|
_data: {
|
||||||
roles: RoleInformation
|
role: roleInformation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next(result)
|
next(result)
|
||||||
|
|
@ -80,14 +84,22 @@ const postRoleController = (req, res, next, config) => {
|
||||||
const createdBy = req.auth.user || 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: { roles: RoleInformation }
|
_data: { role: roleInformation }
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
|
|
@ -101,11 +113,16 @@ const putRoleController = (req, res, next, config) => {
|
||||||
const uuid = req.params.uuid
|
const uuid = req.params.uuid
|
||||||
|
|
||||||
modifyRoleModel({ ...req.body, uuid, conn })
|
modifyRoleModel({ ...req.body, uuid, conn })
|
||||||
.then((RoleInformation) => {
|
.then((roleInformation) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
const result = {
|
const result = {
|
||||||
_data: {
|
_data: {
|
||||||
message: 'Role modified',
|
message: 'Role modified',
|
||||||
roles: RoleInformation
|
role: roleInformation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next(result)
|
next(result)
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import {
|
||||||
} from '../../models/authorization/roles_has_permissionsModel.js'
|
} from '../../models/authorization/roles_has_permissionsModel.js'
|
||||||
import { errorHandler } from '../../utils/errors.js'
|
import { errorHandler } from '../../utils/errors.js'
|
||||||
import { noResults } from '../../validators/result-validators.js'
|
import { noResults } from '../../validators/result-validators.js'
|
||||||
|
import { error404 } from '../../utils/errors.js'
|
||||||
|
import { sendResponseNotFound } from '../../utils/responses.js'
|
||||||
|
|
||||||
const getRolesHasPermissionsController = (req, res, next, config) => {
|
const getRolesHasPermissionsController = (req, res, next, config) => {
|
||||||
const conn = mysql.start(config)
|
const conn = mysql.start(config)
|
||||||
|
|
@ -88,6 +90,11 @@ const putRolesHasPermissionsController = (req, res, next, config) => {
|
||||||
|
|
||||||
modifyRolesHasPermissionsModel({ ...req.body, uuid, modifiedBy, conn })
|
modifyRolesHasPermissionsModel({ ...req.body, uuid, modifiedBy, conn })
|
||||||
.then((roles_has_permissions) => {
|
.then((roles_has_permissions) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
const result = {
|
const result = {
|
||||||
_data: roles_has_permissions
|
_data: roles_has_permissions
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,14 @@ const postUserController = (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).json(error)
|
return res.status(error).json(error)
|
||||||
})
|
})
|
||||||
|
|
@ -110,6 +118,11 @@ const putUserController = (req, res, next, config) => {
|
||||||
|
|
||||||
modifyUserModel({ ...req.body, uuid, conn })
|
modifyUserModel({ ...req.body, uuid, conn })
|
||||||
.then((users) => {
|
.then((users) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
const result = {
|
const result = {
|
||||||
_data: {
|
_data: {
|
||||||
message: 'User modified',
|
message: 'User modified',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue