Skip to content

Commit

Permalink
fix issue mentioned in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawaic6 committed May 13, 2024
1 parent 4bbbe87 commit c00550d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 39 deletions.
25 changes: 3 additions & 22 deletions src/__test__/userController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ describe('User Login Tests', () => {
firstName: 'NewFirstName',
lastName: 'NewLastName',
email: 'newemail@example.com',
oldPassword: 'bosco@gmail.com',
newPassword: 'newPassword123',
Password: 'bosco@gmail.com',
};

const response = await request(app)
Expand All @@ -354,32 +353,14 @@ describe('User Login Tests', () => {
expect(response.body.error).toBe('User not found');
});

it('should return 400 when old password is incorrect', async () => {
if(user){
const newUserData = {
firstName: 'NewFirstName',
lastName: 'NewLastName',
email: 'newemail@example.com',
oldPassword: 'wrongpin123',
newPassword: 'newPassword123',
};
const response = await request(app)

.put(`/api/v1/updateProfile/${user.id}`)
.send(newUserData);
expect(response.statusCode).toBe(400);
expect(response.body.error).toBe('Old password is incorrect');
}
});


it('should return 400 when email is already taken', async () => {
if(user){
const newUserData = {
firstName: 'NewFirstName',
lastName: 'NewLastName',
email: 'test@gmail.com',
oldPassword: 'bosco@gmail.com',
newPassword: 'newPassword123',
Password: 'bosco@gmail.com',
};
const response = await request(app)
.put(`/api/v1/updateProfile/${user.id}`)
Expand Down
19 changes: 5 additions & 14 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const Login = async (req: Request, res: Response) => {

export const updateProfile = async (req: Request, res: Response) => {
const userId: number = parseInt(req.params.id);
const { firstName, lastName, email, oldPassword, newPassword } = req.body as UpdateRrofileRequestBody;
const { firstName, lastName, email } = req.body as UpdateRrofileRequestBody;

try {

Expand All @@ -241,12 +241,7 @@ try {
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const passwordMatch = await bcrypt.compare(oldPassword, user.password);

if (!passwordMatch) {
return res.status(400).json({ error: 'Old password is incorrect' });
}
user.firstName = firstName || user.firstName;
user.firstName = firstName || user.firstName;
user.lastName = lastName || user.lastName;
if (email && email !== user.email) {
const emailExists = await userRepository.findOne({ where: { email } });
Expand All @@ -257,10 +252,6 @@ try {

user.email = email;
}
if (newPassword) {
const hashedPassword = await bcrypt.hash(newPassword, 10);
user.password = hashedPassword;
}
const errors = await validate(user);


Expand All @@ -271,15 +262,15 @@ await userRepository.save(user);

return res.status(201).json({ message: 'User updated successfully' });
} catch (error) {
return res.status(500).json({ error: 'Internal server error'});
res.status(500).send(error);
}
};
export const verify2FA = async (req: Request, res: Response): Promise<void> => {
try {
const { code } = req.body;
const { userId } = req.params;

// Use the repository to find the user by their id

const user = await userRepository.findOne({ where: { id: Number(userId) } });

if (!user) {
Expand All @@ -298,6 +289,6 @@ export const verify2FA = async (req: Request, res: Response): Promise<void> => {
res.status(200).json({ token });

} catch (error) {
res.status(500).json({ error: (error as Error).message });
res.status(500).send(error);
}
};
2 changes: 1 addition & 1 deletion src/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function errorHandler(func: MiddlewareFunction) {
return async (req: Request, res: Response, next: NextFunction) => {
try {
await func(req, res, next);
} catch (error) { // Removed the type annotation from the catch clause variable because it caused liting errors
} catch (error:any) { // Removed the type annotation from the catch clause variable because it caused liting errors
const message = error.detail || 'Internal Server Error';
res.status(500).send(message);
}
Expand Down
5 changes: 3 additions & 2 deletions src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router } from 'express';
import errorHandler from'../middlewares/errorHandler'
import {
registerUser,
confirmEmail,
Expand All @@ -18,8 +19,8 @@ route.delete('/delete/:id', deleteUser);
route.delete('/deleteAllUsers', deleteAllUsers);
route.post('/login',Login)
route.get('/all-users', getAllUsers);
route.post('/verify2FA/:userId', verify2FA);
route.post('/verify2FA/:userId',errorHandler ,verify2FA);

route.put('/updateProfile/:id',updateProfile);
route.put('/updateProfile/:id',errorHandler,updateProfile);
export default route;

0 comments on commit c00550d

Please sign in to comment.