Skip to content

Commit

Permalink
add createGroups, retrieveAllGroups and assignGroups logics | modify …
Browse files Browse the repository at this point in the history
…retrieveFiles with shared groups functio | testing HTML and SPECT b00tc4mp#361
  • Loading branch information
abelpriem committed Mar 24, 2024
1 parent 6b87f5a commit bff8570
Show file tree
Hide file tree
Showing 33 changed files with 579 additions and 46 deletions.
32 changes: 26 additions & 6 deletions staff/abel-prieto/PROYECT/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,40 @@

`Register Admin`
- Request: POST /admin Authorization: Bearer ${session.token}
- Response: 201
- Response: 201 "Content-Type": application/json { username, email, password }
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }

`Retrieve All User`
- Request: GET /admin/users/all Authorization: Bearer ${session.token}
- Response: 200
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }

`Retrieve All Groups`
- Request: GET /admin/groups/all Authorization: Bearer ${session.token}
- Response: 200
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }

`Delete User`
- Request: DELETE /users/:userId Authorization: Bearer ${session.token}
- Request: DELETE /admin/users/:userId Authorization: Bearer ${session.token}
- Response: 200
- Response (error): 500|404|406|401 "Content-Type": application/json { error, message }

`Create Commands`
`Create Commands` (PENDING v1.0)
- Request: POST /admin/commands Authorization: Bearer ${session.token}
- Response: 201
- Response: 201 "Content-Type": application/json { command_name }
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }

`Create Groups`
- Request: POST /admin/groups Authorization: Bearer ${session.token}
- Response: 201
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }
- Response: 201 "Content-Type": application/json { group_name }
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }

`Assign Groups`
- Request: PATCH /admin/groups/edit Authorization: Bearer ${session.token}
- Response: 200
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }

`Delete Groups`
- Request: DELETE /admin/groups/delete/:groupId Authorization: Bearer ${session.token}
- Response: 200
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }
42 changes: 42 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/assignGroupsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import jwt from 'jsonwebtoken'
import assignGroups from '../logic/assignGroups.js'
import { errors } from 'com'
const { JsonWebTokenError } = jwt
const { NotFoundError, AuthorizationError, ContentError, TokenError, DuplicityError } = errors

export default async (req, res) => {
const token = req.headers.authorization.substring(7)
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET)

const { selectedGroupId, selectedUserId } = req.body

try {
await assignGroups(userId, selectedUserId, selectedGroupId)
res.status(200).send()
} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof DuplicityError) {
status = 406
}

if (error instanceof TypeError || error instanceof ContentError) {
status = 409
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default (req, res) => {
}

if (error instanceof CredentialsError) {
status = 401
status = 406
}

res.status(status).json({ error: error.constructor.name, message: error.message })
Expand All @@ -31,7 +31,8 @@ export default (req, res) => {
let status = 500

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409

} else if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default (req, res) => {
let status = 500

if (error instanceof TypeError || error instanceof ContentError) {
status = 409
statusss = 409
s
} else if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
Expand Down
39 changes: 39 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/createGroupHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import jwt from 'jsonwebtoken'
import createGroup from "../logic/createGroup.js"
import { errors } from 'com'
const { NotFoundError, ContentError, AuthorizationError, TokenError } = errors
const { JsonWebTokenError } = jwt

export default async (req, res) => {
const token = req.headers.authorization.substring(7)
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET)

const { group } = req.body

try {
await createGroup(userId, group)
res.status(201).send()

} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof TypeError || error instanceof ContentError) {
status = 409
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default async (req, res) => {
}

if (error instanceof TypeError || error instanceof ContentError) {
status = 406
status = 409
}

if (error instanceof JsonWebTokenError) {
Expand Down
8 changes: 4 additions & 4 deletions staff/abel-prieto/PROYECT/API/handlers/deleteUsersHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default async (req, res) => {
const token = req.headers.authorization.substring(7)
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET)

const { userToDelete } = req.params
const { deleteUser } = req.params

try {
await deleteUsers(userId, userToDelete)
await deleteUsers(userId, deleteUser)
res.status(201).send()

} catch (error) {
Expand All @@ -24,11 +24,11 @@ export default async (req, res) => {
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof AuthorizationError) {
status = 409
status = 401
}

if (error instanceof JsonWebTokenError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async (req, res, next) => {
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof AuthorizationError) {
Expand Down
8 changes: 7 additions & 1 deletion staff/abel-prieto/PROYECT/API/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import deleteFileHandler from './deleteFileHandler.js'
import retrieveAllUsersHandler from './retrieveAllUsersHandler.js'
import deleteUsersHandler from './deleteUsersHandler.js'
import registerAdminHandler from './registerAdminHandler.js'
import createGroupHandler from './createGroupHandler.js'
import retrieveAllGroupsHandler from './retrieveAllGroupsHandler.js'
import assignGroupsHandler from './assignGroupsHandler.js'

export {
authenticateUserHandler,
Expand All @@ -29,5 +32,8 @@ export {

retrieveAllUsersHandler,
deleteUsersHandler,
registerAdminHandler
registerAdminHandler,
createGroupHandler,
retrieveAllGroupsHandler,
assignGroupsHandler
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export default async (req, res) => {
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof JsonWebTokenError) {
status = 409
status = 401
error = new TokenError(error.message)
}

Expand Down
6 changes: 3 additions & 3 deletions staff/abel-prieto/PROYECT/API/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { errors } from 'com'
const { DuplicityError, ContentError } = errors

export default (req, res) => {
try {
const { username, email, password } = req.body
const { username, email, password } = req.body

try {
registerUser(username, email, password)
.then(() => res.status(201).send())
.catch(error => {
Expand All @@ -21,7 +21,7 @@ export default (req, res) => {
let status = 500

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

res.status(status).json({ error: error.constructor.name, message: error.message })
Expand Down
38 changes: 38 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/retrieveAllGroupsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import jwt from 'jsonwebtoken'
import retrieveAllGroups from '../logic/retrieveAllGroups.js'
import { errors } from 'com'

const { JsonWebTokenError } = jwt
const { NotFoundError, AuthorizationError, TokenError, ContentError } = errors

export default async (req, res) => {
const token = req.headers.authorization.substring(7)
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET)

try {
const allGroups = await retrieveAllGroups(userId)
res.json(allGroups)

} catch (error) {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 409
}

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
}

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export default async (req, res) => {
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof AuthorizationError) {
status = 409
status = 401
}

if (error instanceof JsonWebTokenError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async (req, res) => {
}

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof JsonWebTokenError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (req, res) => {
let status = 500

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

res.status(status).json({ error: error.constructor.name, message: error.message })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default (req, res) => {
let status = 500

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409

} else if (error instanceof JsonWebTokenError) {
status = 401

Expand Down
4 changes: 2 additions & 2 deletions staff/abel-prieto/PROYECT/API/handlers/uploadFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export default async (req, res) => {
status = 404

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
status = 409
}

if (error instanceof DuplicityError) {
status = 409
status = 406
}

if (error instanceof JsonWebTokenError) {
Expand Down
Loading

0 comments on commit bff8570

Please sign in to comment.