-
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 #53 from atlp-rwanda/187354252-ft-profile-page-set…
…tings [Finishes #187354252] implementing profile page settings
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Request, Response } from 'express'; | ||
import User from '../database/models/user'; | ||
import logger from '../logs/config'; | ||
import uploadImage from '../helpers/claudinary'; | ||
import Role from '../database/models/role'; | ||
import { sendInternalErrorResponse } from '../validations'; | ||
|
||
export const getUserProfile = async (req: Request, res: Response): Promise<void> => { | ||
const userId: string = req.params.userId; | ||
|
||
try { | ||
const user = await User.findByPk(userId, { | ||
attributes: ['id', 'firstName', 'lastName', 'email', 'phoneNumber', 'photoUrl', 'gender'], | ||
include: { | ||
model: Role, | ||
attributes: ['name'], | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
res.status(404).json({ message: 'User not found' }); | ||
return; | ||
} | ||
|
||
res.status(200).json({ ok: true, message: user }); | ||
} catch (error) { | ||
logger.error('Error fetching user profile:: ', error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; | ||
|
||
//construct updated fields | ||
const constructUpdatedFields = (body: any, user: User, uploadedImage?: string) => { | ||
return { | ||
firstName: body.firstName || user.firstName, | ||
lastName: body.lastName || user.lastName, | ||
gender: body.gender || user.gender, | ||
phoneNumber: body.phoneNumber || user.phoneNumber, | ||
photoUrl: uploadedImage || user.photoUrl, | ||
}; | ||
}; | ||
|
||
// Function to construct user response | ||
const constructUserResponse = (user: User, updatedFields: any) => { | ||
return { | ||
id: user.id, | ||
firstName: updatedFields.firstName, | ||
lastName: updatedFields.lastName, | ||
email: user.email, | ||
phoneNumber: updatedFields.phoneNumber, | ||
gender: updatedFields.gender, | ||
photoUrl: updatedFields.photoUrl, | ||
}; | ||
}; | ||
|
||
export const updateUser = async (req: Request, res: Response) => { | ||
try { | ||
const { firstName, lastName, gender, phoneNumber } = req.body; | ||
|
||
const user = await User.findByPk(req.params.id); | ||
|
||
if (!user) { | ||
return res.status(404).json({ ok: false, error: 'User not found' }); | ||
} | ||
|
||
if (!req.file) { | ||
return res.status(400).json({ ok: false, error: 'Profile Image required.' }); | ||
} | ||
|
||
// Upload image to cloudinary | ||
const uploadedImage = await uploadImage(req.file.buffer); | ||
|
||
const updatedFields = constructUpdatedFields(req.body, user, uploadedImage); | ||
|
||
await user.update(updatedFields); | ||
|
||
const userResponse = constructUserResponse(user, updatedFields); | ||
|
||
// Send response | ||
res.status(201).json({ ok: true, message: userResponse }); | ||
} catch (error) { | ||
logger.error('Error updating user profile: ', error); | ||
sendInternalErrorResponse(res, error); | ||
} | ||
}; |
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,87 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: User Profiles API | ||
description: API for managing user profiles | ||
version: 1.0.0 | ||
|
||
paths: | ||
/api/profiles/users/{userId}: | ||
get: | ||
summary: Get a user profile by ID | ||
description: Retrieve a user profile by its ID | ||
tags: | ||
- Profiles | ||
parameters: | ||
- in: path | ||
name: userId | ||
required: true | ||
description: ID of the user profile to retrieve | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: The user profile | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
id: | ||
type: string | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
email: | ||
type: string | ||
phoneNumber: | ||
type: string | ||
photoUrl: | ||
type: string | ||
gender: | ||
type: string | ||
'404': | ||
description: User not found | ||
'500': | ||
description: Internal server error | ||
|
||
patch: | ||
summary: Update a user profile | ||
description: Update an existing user profile | ||
tags: | ||
- Profiles | ||
parameters: | ||
- in: path | ||
name: userId | ||
required: true | ||
description: ID of the user profile to update | ||
schema: | ||
type: string | ||
- in: body | ||
name: body | ||
required: true | ||
description: New data for the user profile | ||
schema: | ||
type: object | ||
properties: | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
email: | ||
type: string | ||
phoneNumber: | ||
type: string | ||
photoUrl: | ||
type: string | ||
gender: | ||
type: string | ||
responses: | ||
'200': | ||
description: User profile updated successfully | ||
'400': | ||
description: Required fields are missing | ||
'404': | ||
description: User not found | ||
'500': | ||
description: Internal server error |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
import { Router } from 'express'; | ||
import { updateUser, getUserProfile } from '../controllers/profileController'; | ||
import multerUpload from '../helpers/multer'; | ||
const router = Router(); | ||
router.get('/users/:userId/', getUserProfile); | ||
router.patch('/users/:id', multerUpload.single('profileImage'), updateUser); | ||
export default router; |