Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import prisma from '../config/prismaClient.js';
import { uploadToCloudinary } from '../utils/cloudinary.utils.js';
import {
deleteFromCloudinary,
uploadToCloudinary,
} from '../utils/cloudinary.utils.js';
import { comparePassword, hashPassword } from '../utils/password.utils.js';
import {
updatePasswordValidation,
Expand Down Expand Up @@ -414,3 +417,42 @@ export const uploadUserProfilePic = async (req, res, next) => {
* @method DELETE
* @access private - Requires admin or user himself
*/
export const deleteUserProfilePic = async (req, res, next) => {
try {
const { userId } = req.params;

// Validate userId
if (!userId) {
return res.status(400).json({
success: false,
message: 'User ID is required',
});
}

// Check if user exists and is not deleted
const user = await prisma.user.findFirst({
where: {
id: userId,
deletedAt: null,
},
});

if (!user || !user.profilePic) {
return res.status(404).json({ message: 'Profile picture not found' });
}

await deleteFromCloudinary(user.profilePic);

const updatedUser = await prisma.user.update({
where: { id: userId },
data: { profilePic: null },
});

res.status(200).json({
message: 'Profile picture deleted successfully',
user: updatedUser,
});
} catch (error) {
next(error);
}
};
8 changes: 8 additions & 0 deletions src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from 'express';
import {
deleteUserProfilePic,
getAllUsers,
getUserById,
restoreUser,
Expand Down Expand Up @@ -64,4 +65,11 @@ router.post(
verifyUserPermission,
uploadUserProfilePic,
);

router.delete(
'/api/users/:userId/profile-picture',
verifyAccessToken,
verifyUserPermission,
deleteUserProfilePic,
);
export default router;