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