-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Topic /k1ch/ Introduce API
POST: clients/{client_id}/permissions
(#129
) * feat: k1ch/ introduce POST:/clients/{client_id}/permissions * chore: k1ch / modify pgErrorHandler to return Error object * chore: k1ch / add tests for admin-permission DB layer * chore: k1ch / add tests for POST:clients/{client_id}/permissions * refactor: k1ch / throw Error instead of object
- Loading branch information
Showing
10 changed files
with
331 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const createError = require('http-errors') | ||
const dbAdminPermission = require('database/layer/admin-permission') | ||
const { checkClientExists, checkPermissionNameUniqueness } = require('./utils') | ||
|
||
/** | ||
* HTTP Request handler | ||
* Create a permission | ||
* | ||
* @param {Object} req - The request object | ||
* @param {Object} res - The response object to send 201 statusCode and the cerated permission on success | ||
* @param {Function} next - The next middleware function | ||
* @returns {Promise<void>} - A Promise that resolves to void when the permission is created | ||
*/ | ||
const createPermission = async (req, res, next) => { | ||
try { | ||
const { client_id: clientId } = req.params | ||
const client = await checkClientExists(clientId) | ||
const payload = { | ||
...req.body, | ||
clientkey: client.key, | ||
} | ||
await checkPermissionNameUniqueness(payload) | ||
const permission = await dbAdminPermission.insertPermission(payload) | ||
res.status(201).send(permission) | ||
} catch ({ httpStatusCode = 500, message }) { | ||
return next(createError(httpStatusCode, { message })) | ||
} | ||
} | ||
|
||
module.exports = { | ||
createPermission, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
const dbAdminRole = require('database/layer/admin-client') | ||
const dbAdminPermission = require('database/layer/admin-permission') | ||
|
||
const checkClientExists = async (clientId) => { | ||
try { | ||
await dbAdminRole.getClient(clientId); | ||
return await dbAdminRole.getClient(clientId); | ||
} catch { | ||
throw { | ||
httpStatusCode: 404, | ||
message: 'Client does not exist!', | ||
} | ||
const error = new Error('Client does not exist!') | ||
error.httpStatusCode = 404 | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* Checks the uniqueness of a permission name for a given client key. | ||
* | ||
* This function queries the database to retrieve permissions by name and client key. | ||
* If any permissions are found, it throws an error indicating the name is already taken. | ||
* | ||
* @async | ||
* @function checkPermissionNameUniqueness | ||
* @param {Object} params - The parameters for checking uniqueness. | ||
* @param {string} params.name - The name of the permission to check. | ||
* @param {string} params.clientkey - The client key associated with the permission. | ||
* @throws {Object} Throws an error with HTTP status code 409 if the permission name is not unique. | ||
* @throws {number} error.httpStatusCode - The HTTP status code indicating conflict (409). | ||
* @throws {string} error.message - The error message indicating the permission name is taken. | ||
*/ | ||
const checkPermissionNameUniqueness = async ({ name, clientkey: clientKey }) => { | ||
const permissions = await dbAdminPermission.getPermissionsByNameClientKey(name, clientKey); | ||
if (permissions?.length) { | ||
const error = new Error('The permission name is taken!') | ||
error.httpStatusCode = 409 | ||
throw error | ||
} | ||
}; | ||
|
||
module.exports = { | ||
checkClientExists, | ||
checkPermissionNameUniqueness, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.