Skip to content

Commit

Permalink
Merge pull request #249 from tbf-challenge/246-endpoint-to-update-user
Browse files Browse the repository at this point in the history
246 endpoint to update user
  • Loading branch information
jocrah authored Oct 13, 2022
2 parents 4e69e32 + 426ab4d commit 661b3e1
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 56 deletions.
103 changes: 103 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,109 @@ paths:
type: string
security:
- access_token: []
/users/{id}:
patch:
security:
- access_token: []
tags:
- user
operationId: Update a user
summary: Update a user
description: Update a user
parameters:
- in : path
name : id
schema:
type: integer
required: true
description : Numeric ID of the user to get
requestBody:
description: Update a user
content:
application/json:
schema:
type: object
properties:
firstName:
type: string
description: First name of user
example: John
lastName:
type: string
description: Last name of user
example: Doe
gender:
type: string
description: gender of the user
example: non-binary
address:
type: string
description: address of the user
example: 10, Ogunlana Drive, Surulere, Lagos
jobRole:
type: string
description: job role of the user
example: Software Developer
department:
type: string
description: department of the user
example: Software Development
profilePictureUrl:
type: string
description: profile picture url of the user
example: https://imgbb.com/
responses:
'200':
description: Successfully updating a user
content:
application/json:
schema:
type: object
properties:
status:
type: string
description: successful updating a user
example: success
data:
type: object
description: Data for updating a user
properties:
id:
type: integer
description: user id
example: 10
firstName:
type: string
description: First name of user
example: John
lastName:
type: string
description: Last name of user
example: Doe
email:
type: string
description: Email of user
example: [email protected]
gender:
type: string
description: gender of the user
example: non-binary
address:
type: string
description: address of the user
example: 10, Ogunlana Drive, Surulere, Lagos
jobRole:
type: string
description: job role of the user
example: Software Developer
department:
type: string
description: department of the user
example: Software Development
refreshToken:
type: string
description: Refresh token
example: 44yytruy76iiui945436t
components:
schemas:
User:
Expand Down
34 changes: 13 additions & 21 deletions src/routes/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const express = require('express')
const userSevice = require('../../services/users')
const userService = require('../../services/users')
const validateSchema = require('../../middleware/validateSchema')
const isAuthenticated = require('../../middleware/isAuthenticated')
const isAdmin = require('../../middleware/isAdmin')
const { catchAsync , AppError} = require('../../lib')
const { transformUserResponse } = require('../common/transformers')


const {
Expand Down Expand Up @@ -36,7 +37,7 @@ const ERROR_MAP = {
[ InvalidResetTokenError.name ] : 401
}

const transformUserResponse = (userDetails) => ({
const transformCreateUserResponse = (userDetails) => ({
accessToken : userDetails.accessToken,
userId : userDetails.userId,
refreshToken : userDetails.refreshToken
Expand All @@ -49,24 +50,15 @@ router.post(
catchAsync(async (req, res) => {
const { email, password } = req.body

const userDetails = await userSevice
const userDetails = await userService
.signInUserByEmail(email, password)
return res.json({
status: 'success',
data:{
...transformUserResponse(userDetails),
...transformUserResponse(userDetails.user),
refreshToken: userDetails.user.refreshToken,
userId : userDetails.user.id,
firstName: userDetails.user.firstName,
lastName: userDetails.user.lastName,
email: userDetails.user.email,
gender: userDetails.user.gender,
role: userDetails.user.role,
department: userDetails.user.department,
address: userDetails.user.department,
jobRole: userDetails.user.jobRole,
createdAt : userDetails.user.createdAt,
profilePictureUrl: userDetails.user.profilePictureUrl
accessToken : userDetails.accessToken
}
})
})
Expand All @@ -81,7 +73,7 @@ router.post(
catchAsync(async (req, res) => {
const { email } = req.body

const { email:userEmail, status } = await userSevice.inviteUser(email)
const { email:userEmail, status } = await userService.inviteUser(email)

res.status(200).json({
status: 'success',
Expand Down Expand Up @@ -109,7 +101,7 @@ router.post(


const userDetails =
await userSevice.createNewUser({
await userService.createNewUser({
firstName,
lastName,
email,
Expand All @@ -122,7 +114,7 @@ router.post(
status: 'success',
data: {
message: 'User account successfully created',
...transformUserResponse(userDetails)
...transformCreateUserResponse(userDetails)
}
})
})
Expand All @@ -138,7 +130,7 @@ router.post(
refreshToken : currentRefreshToken
} = req.body

const userDetails = await userSevice.getNewTokens(
const userDetails = await userService.getNewTokens(
email,
currentRefreshToken
)
Expand All @@ -158,7 +150,7 @@ router.get(
token
} = req.params
const userDetails =
await userSevice.getInvitedUserDetail(
await userService.getInvitedUserDetail(
token
)

Expand All @@ -177,7 +169,7 @@ router.post('/password',
catchAsync(async (req, res) => {
const { email } = req.body

await userSevice.sendPasswordResetLink(email)
await userService.sendPasswordResetLink(email)

return res.status(200).json({
status: 'success',
Expand All @@ -195,7 +187,7 @@ router.patch(
const { token } = req.params
const { newPassword } = req.body

await userSevice.resetPassword({ token, newPassword })
await userService.resetPassword({ token, newPassword })

return res.status(200).json({
status: "success",
Expand Down
16 changes: 15 additions & 1 deletion src/routes/common/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ const transformGifResponse = (gif) => ({

})

const transformUserResponse = (userDetails) => ({
userId: userDetails.id,
firstName: userDetails.firstName,
lastName: userDetails.lastName,
email: userDetails.email,
gender: userDetails.gender,
jobRole: userDetails.jobRole,
department: userDetails.department,
address: userDetails.address,
profilePictureUrl: userDetails.profilePictureUrl
})


module.exports = {
transformArticleResponse,
transformGifResponse
transformGifResponse,
transformUserResponse
}
32 changes: 0 additions & 32 deletions src/routes/users.js

This file was deleted.

67 changes: 67 additions & 0 deletions src/routes/users/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const express = require("express")
const db = require("../../db")
const isAuthenticated = require("../../middleware/isAuthenticated")
const userService = require('../../services/users')
const validateSchema = require('../../middleware/validateSchema')
const {
updateUserSchema
} = require("../../schema")
const { catchAsync, AppError } = require("../../lib")
const { UserNotFoundError } = require("../../services/errors")
const { transformUserResponse }= require("../common/transformers")

const ERROR_MAP = {
[ UserNotFoundError.name ] : 404
}


const router = express.Router()

const fetchUsers = (req, res) => {
res.send("get users")
}
const createUsers = () => {}
const getUser = async (req, res) => {
const { id } = req.params
const { rows } = await db.query("SELECT * FROM users WHERE id = $1", [id])
res.send(rows[0])
}
const updateUser = catchAsync(async (req, res ) => {

const { id } = req.params


const userDetails = await userService.updateUser(id, req.body)

res.status(200).json({
status: "success",
data: transformUserResponse(userDetails)
})
})
const deleteUser = () => {}

// isAuthenticated middle to protect all posts related requests
router.use(isAuthenticated())

router
.route("/")
.get(fetchUsers)
.post(createUsers)
router
.route("/:id")
.get(getUser)
.patch(validateSchema(updateUserSchema), updateUser)
.delete(deleteUser)

router
.use((err, req, res, next)=> {
const error = err
error.success = false
if(ERROR_MAP[error.name] ){
next(new AppError( error.message ,ERROR_MAP[error.name] ))

}
next(err)
})

module.exports = router
Loading

0 comments on commit 661b3e1

Please sign in to comment.