Skip to content

Commit

Permalink
create retrieveGuest logic | testing retrieveGuest | create upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel Prieto committed Feb 24, 2024
1 parent 067c9df commit 1a54330
Show file tree
Hide file tree
Showing 16 changed files with 2,640 additions and 22 deletions.
9 changes: 8 additions & 1 deletion staff/abel-prieto/PROYECT/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
- Response (error) : 500|409|406 "Content-Type: application/json" { error, message }

`Authenticate User`

- Request: POST /users/auth "Content-Type": application/json { email, password }
- Response: 200 "Content-Type": application/json { userId }
- Respone (error): 500|404|406|409 "Content-Type": application/json { error, message }

`Retrieve User`

- Request: GET /users Authorization: Bearer '65d0e63fa0232cfaf1c8c411'
- Response: 200 "Content-Type": application/json { username, [ group ] }
- Response: 200 "Content-Type": application/json { username, [ group ], [ role ] }
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }

`Retrieve Guest`

- Request: GET /users
- Response: 200 "Content-Type": application/json { username, [ group ], [ role ] }
- Response (error) : 500|404|406 "Content-Type": application/json { error, message }
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default (req, res) => {
}

res.status(status).json({ error: error.constructor.name, message: error.message })

return
})
.then(userId => res.json(userId))
} catch (error) {
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
@@ -1,9 +1,11 @@
import authenticateUserHandler from './authenticateUserHandler.js'
import registerUserHandler from './registerUserHandler.js'
import retrieveUserHandler from './retrieveUserHandler.js'
import retrieveGuestHandler from './retrieveGuestHandler.js'

export {
authenticateUserHandler,
registerUserHandler,
retrieveUserHandler
retrieveUserHandler,
retrieveGuestHandler
}
29 changes: 29 additions & 0 deletions staff/abel-prieto/PROYECT/API/handlers/retrieveGuestHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import retrieveGuest from "../logic/retrieveGuest.js"
import { errors } from 'com'
const { NotFoundError, ContentError } = errors

export default (req, res) => {
try {
retrieveGuest()
.catch(error => {
let status = 500

if (error instanceof NotFoundError) {
status = 404
}

res.status(status).json({ error: error.constructor.name, message: error.message })

return
})
.then(guest => res.json(guest))
} catch (error) {
let status = 500

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

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

res.satus(status).json({ error: error.constructor.name, message: error.message })
res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
6 changes: 5 additions & 1 deletion staff/abel-prieto/PROYECT/API/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import cors from 'cors'
import {
authenticateUserHandler,
registerUserHandler,
retrieveUserHandler
retrieveUserHandler,
retrieveGuestHandler
} from './handlers/index.js'

dotenv.config()
Expand All @@ -29,6 +30,9 @@ mongoose.connect(process.env.URL_MONGODB_HIINIT_API)
// RETRIEVE USER
server.get('/users', retrieveUserHandler)

// RETRIEVE GUEST
server.get('/users', retrieveGuestHandler)

server.listen(process.env.PORT, () => console.log(`server online! Listen on: ${process.env.PORT}`))
})
.catch(error => console.error(error))
23 changes: 23 additions & 0 deletions staff/abel-prieto/PROYECT/API/logic/retrieveGuest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { User } from "../data/models.js"
import { errors } from "com"
const { SystemError, NotFoundError} = errors

function retrieveGuest() {
return User.findOne({ email: '[email protected]' }).lean()
.catch(error => { throw new SystemError(error.message)})
.then(guest => {
if (!guest) {
throw new NotFoundError('Guest not found. Try again')
}

// USERNAME - TYPE - ROLE
delete guest._id
delete guest.__v
delete guest.email
delete guest.password

return guest
})
}

export default retrieveGuest
Loading

0 comments on commit 1a54330

Please sign in to comment.