-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from tbf-challenge/246-endpoint-to-update-user
246 endpoint to update user
- Loading branch information
Showing
11 changed files
with
561 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
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 was deleted.
Oops, something went wrong.
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,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 |
Oops, something went wrong.