From bfd0dd5d3249e2b625e2de374c8650d43ec44754 Mon Sep 17 00:00:00 2001 From: beingPro007 <120173992+beingPro007@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:45:08 +0530 Subject: [PATCH] **For Now User Profile is done** --Functionalites Add are --addProfilePicture --updateProfile --profile --deleteProfilePicture --addAddress --deleteProfile --- src/controllers/profile.controller.js | 33 ++++++++++++++++++++++++--- src/routes/profile.route.js | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/controllers/profile.controller.js b/src/controllers/profile.controller.js index f503a2c..f0cf105 100644 --- a/src/controllers/profile.controller.js +++ b/src/controllers/profile.controller.js @@ -209,16 +209,43 @@ const deleteProfile = asynchandler(async (req, res) => { throw new ApiError(400, 'User not found!!'); } - const deletedUser = await findByIdAndDelete(userId); + // Aggregate to find the user details + const userToDelete = await User.aggregate([ + { + $match: { + _id: userId, + }, + }, + { + $project: { + _id: 1, + username: 1, + fullName: 1, + phoneNumber: 1, + }, + }, + ]); + + // Check if user exists + if (userToDelete.length === 0) { + throw new ApiError(404, 'User not found!!'); + } + + // Delete the user + const deletedUser = await User.findByIdAndDelete(userId); if (!deletedUser) { - throw new ApiError(400, "User can't be able to deelte!!!"); + throw new ApiError(400, "User can't be deleted!!!"); } return res .status(200) - .json(new ApiResponse(200, 'User Deleted Successfully!!!'), deletedUser); + .json( + new ApiResponse(200, 'User Deleted Successfully!!!', userToDelete[0]) + ); }); + + export { addProfilePicture, updateProfile, diff --git a/src/routes/profile.route.js b/src/routes/profile.route.js index 2fc6201..3467655 100644 --- a/src/routes/profile.route.js +++ b/src/routes/profile.route.js @@ -45,7 +45,7 @@ router.route("/profile/addAdress").post( router.route("/profile/deleteProfile").delete( verifyJwt, - checkRole('admin'), + checkRole(['customer']), deleteProfile )