Skip to content

Commit

Permalink
[Finishes #187354252] implementing profile page settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hatfelicien committed May 3, 2024
1 parent 512caa3 commit 4f14099
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/controllers/profileController.ts
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);
}
};
87 changes: 87 additions & 0 deletions src/docs/profile.yaml
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
3 changes: 3 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Router } from 'express';
import userRoute from './userRoute';
import profileRoutes from './profileRoutes';
import authRoute from './authRoute';
import roleRoute from './roleRoute';

const router = Router();

router.use('/users', userRoute);
// Profile routes
router.use('/profiles', profileRoutes);
router.use('/auth', authRoute);
router.use('/roles', roleRoute);

Expand Down
8 changes: 8 additions & 0 deletions src/routes/profileRoutes.ts
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;

0 comments on commit 4f14099

Please sign in to comment.