Skip to content

Commit

Permalink
add deleteGroup logic with HTML and SPECT testing | refactor handlers…
Browse files Browse the repository at this point in the history
… with ASYNC-AWAIT b00tc4mp#361
  • Loading branch information
abelpriem committed Mar 24, 2024
1 parent 156ce70 commit 239c2be
Show file tree
Hide file tree
Showing 24 changed files with 395 additions and 209 deletions.
2 changes: 1 addition & 1 deletion staff/abel-prieto/PROYECT/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
- Response: 200
- Response (error) : 500|409|406|401 "Content-Type": application/json { error, message }

`Delete Groups`
`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 }
7 changes: 2 additions & 5 deletions staff/abel-prieto/PROYECT/API/handlers/assignGroupsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default async (req, res) => {
try {
await assignGroups(userId, selectedUserId, selectedGroupId)
res.status(200).send()

} catch (error) {
let status = 500

Expand All @@ -24,11 +25,7 @@ export default async (req, res) => {
status = 404
}

if (error instanceof DuplicityError) {
status = 406
}

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

Expand Down
34 changes: 14 additions & 20 deletions staff/abel-prieto/PROYECT/API/handlers/authenticateUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@ import { errors } from 'com'
const { NotFoundError, CredentialsError, ContentError, TokenError } = errors
const { JsonWebTokenError } = jwt

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

authenticateUser(email, password)
.then(userId => {
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRE })
export default async (req, res) => {
const { email, password } = req.body

res.json(token)
})
.catch(error => {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}
try {
const userId = await authenticateUser(email, password)
const token = jwt.sign({ sub: userId }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRE })

if (error instanceof CredentialsError) {
status = 406
}
res.json(token)

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof CredentialsError) {
status = 406
}

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

Expand Down
26 changes: 11 additions & 15 deletions staff/abel-prieto/PROYECT/API/handlers/changeUserEmailHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@ import changeUserEmail from '../logic/changeUserEmail.js'
const { JsonWebTokenError } = jwt
const { NotFoundError, CredentialsError, ContentError, TokenError } = errors

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

const { newEmail, password, againPassword } = req.body

try {
changeUserEmail(userId, newEmail, password, againPassword)
.then(() => res.status(201).send())
.catch(error => {
let status = 500
await changeUserEmail(userId, newEmail, password, againPassword)
res.status(201).send()

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof CredentialsError) {
status = 406
}

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof CredentialsError) {
status = 406
}

if (error instanceof TypeError || error instanceof ContentError) {
status = 409
}
Expand Down
30 changes: 13 additions & 17 deletions staff/abel-prieto/PROYECT/API/handlers/changeUserPasswordHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,30 @@ import changeUserPassword from '../logic/changeUserPassword.js'
const { JsonWebTokenError } = jwt
const { NotFoundError, CredentialsError, ContentError, TokenError } = errors

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

const { password, newPassword, againNewPassword } = req.body

try {
changeUserPassword(userId, password, newPassword, againNewPassword)
.then(() => res.status(201).send())
.catch(error => {
let status = 500
await changeUserPassword(userId, password, newPassword, againNewPassword)
res.status(201).send()

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof CredentialsError) {
status = 406
}

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

if (error instanceof CredentialsError) {
status = 406
}

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

} 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/deleteGroupHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import jwt from 'jsonwebtoken'
import deleteGroup from '../logic/deleteGroup.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)

const { groupId } = req.params

try {
await deleteGroup(userId, groupId)
res.status(200).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 })
}
}
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 @@ -19,6 +19,10 @@ export default async (req, res) => {
} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}
Expand All @@ -27,10 +31,6 @@ export default async (req, res) => {
status = 409
}

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
Expand Down
9 changes: 5 additions & 4 deletions staff/abel-prieto/PROYECT/API/handlers/downloadFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ export default async (req, res, next) => {
const { path, originalName } = file

res.download(path, originalName)

} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}
Expand All @@ -27,10 +32,6 @@ export default async (req, res, next) => {
status = 409
}

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
Expand Down
4 changes: 3 additions & 1 deletion staff/abel-prieto/PROYECT/API/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import registerAdminHandler from './registerAdminHandler.js'
import createGroupHandler from './createGroupHandler.js'
import retrieveAllGroupsHandler from './retrieveAllGroupsHandler.js'
import assignGroupsHandler from './assignGroupsHandler.js'
import deleteGroupHandler from './deleteGroupHandler.js'

export {
authenticateUserHandler,
Expand All @@ -35,5 +36,6 @@ export {
registerAdminHandler,
createGroupHandler,
retrieveAllGroupsHandler,
assignGroupsHandler
assignGroupsHandler,
deleteGroupHandler
}
16 changes: 4 additions & 12 deletions staff/abel-prieto/PROYECT/API/handlers/registerUserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@ import registerUser from '../logic/registerUser.js'
import { errors } from 'com'
const { DuplicityError, ContentError } = errors

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

try {
registerUser(username, email, password)
.then(() => res.status(201).send())
.catch(error => {
let status = 500
await registerUser(username, email, password)
res.status(201).send()

if (error instanceof DuplicityError) {
status = 409
}

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default async (req, res) => {
} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}
Expand All @@ -24,10 +28,6 @@ export default async (req, res) => {
status = 409
}

if (error instanceof AuthorizationError) {
status = 401
}

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 @@ -17,6 +17,10 @@ export default async (req, res) => {
} catch (error) {
let status = 500

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof NotFoundError) {
status = 404
}
Expand All @@ -25,10 +29,6 @@ export default async (req, res) => {
status = 409
}

if (error instanceof AuthorizationError) {
status = 401
}

if (error instanceof JsonWebTokenError) {
status = 401
error = new TokenError(error.message)
Expand Down
18 changes: 7 additions & 11 deletions staff/abel-prieto/PROYECT/API/handlers/retrieveGuestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ import retrieveGuest from "../logic/retrieveGuest.js"
import { errors } from 'com'
const { NotFoundError, ContentError } = errors

export default (req, res) => {
export default async (req, res) => {
try {
retrieveGuest()
.then(guest => res.json(guest))
.catch(error => {
let status = 500
const guest = await retrieveGuest()
res.json(guest)

if (error instanceof NotFoundError) {
status = 404
}

res.status(status).json({ error: error.constructor.name, message: error.message })
})
} catch (error) {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

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

0 comments on commit 239c2be

Please sign in to comment.