Skip to content

Commit 6620249

Browse files
authored
Merge pull request #86 from TaskTrial/feature/84/delete-user-pic
Feature/84/delete user pic
2 parents 5dd5a80 + 7374729 commit 6620249

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/controllers/user.controller.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import prisma from '../config/prismaClient.js';
2-
import { uploadToCloudinary } from '../utils/cloudinary.utils.js';
2+
import {
3+
deleteFromCloudinary,
4+
uploadToCloudinary,
5+
} from '../utils/cloudinary.utils.js';
36
import { comparePassword, hashPassword } from '../utils/password.utils.js';
47
import {
58
updatePasswordValidation,
@@ -414,3 +417,42 @@ export const uploadUserProfilePic = async (req, res, next) => {
414417
* @method DELETE
415418
* @access private - Requires admin or user himself
416419
*/
420+
export const deleteUserProfilePic = async (req, res, next) => {
421+
try {
422+
const { userId } = req.params;
423+
424+
// Validate userId
425+
if (!userId) {
426+
return res.status(400).json({
427+
success: false,
428+
message: 'User ID is required',
429+
});
430+
}
431+
432+
// Check if user exists and is not deleted
433+
const user = await prisma.user.findFirst({
434+
where: {
435+
id: userId,
436+
deletedAt: null,
437+
},
438+
});
439+
440+
if (!user || !user.profilePic) {
441+
return res.status(404).json({ message: 'Profile picture not found' });
442+
}
443+
444+
await deleteFromCloudinary(user.profilePic);
445+
446+
const updatedUser = await prisma.user.update({
447+
where: { id: userId },
448+
data: { profilePic: null },
449+
});
450+
451+
res.status(200).json({
452+
message: 'Profile picture deleted successfully',
453+
user: updatedUser,
454+
});
455+
} catch (error) {
456+
next(error);
457+
}
458+
};

src/routes/user.routes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Router } from 'express';
22
import {
3+
deleteUserProfilePic,
34
getAllUsers,
45
getUserById,
56
restoreUser,
@@ -64,4 +65,11 @@ router.post(
6465
verifyUserPermission,
6566
uploadUserProfilePic,
6667
);
68+
69+
router.delete(
70+
'/api/users/:userId/profile-picture',
71+
verifyAccessToken,
72+
verifyUserPermission,
73+
deleteUserProfilePic,
74+
);
6775
export default router;

0 commit comments

Comments
 (0)