-
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.
[Finishes #187354252] implementing profile page settings
- Loading branch information
1 parent
512caa3
commit 4f14099
Showing
4 changed files
with
169 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,71 @@ | ||
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); | ||
} | ||
}; | ||
|
||
// Function to update user profile | ||
export const updateUser = async (req: Request, res: Response) => { | ||
try { | ||
const { firstName, lastName, gender, phoneNumber } = req.body; | ||
|
||
const user = await User.findOne({ where: { id: req.params.id } }); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let uploadedImage: any; | ||
if (!req.file) { | ||
res.status(400).json({ ok: false, error: 'Profile Image required.' }); | ||
} | ||
if (req.file) { | ||
uploadedImage = await uploadImage(req.file.buffer); | ||
} | ||
const updatedFields = { | ||
firstName: firstName || user?.firstName, | ||
lastName: lastName || user?.lastName, | ||
gender: gender || user?.gender, | ||
phoneNumber: phoneNumber || user?.phoneNumber, | ||
photoUrl: uploadedImage || user?.photoUrl, | ||
}; | ||
|
||
await user?.update(updatedFields); | ||
const userResponse = { | ||
id: user?.id, | ||
firstName: updatedFields.firstName, | ||
lastName: updatedFields.lastName, | ||
email: user?.email, | ||
phoneNumber: updatedFields.phoneNumber, | ||
gender: updatedFields.gender, | ||
photoUrl: updatedFields.photoUrl, | ||
}; | ||
|
||
res.status(201).json({ ok: true, message: userResponse }); | ||
} catch (error) { | ||
logger.error('Error Edit User Role: ', 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; |