Skip to content

Commit

Permalink
Adding Route and Logic for Updation of Details
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidSumra committed Feb 7, 2024
1 parent 6240140 commit 0645c87
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
47 changes: 47 additions & 0 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,50 @@ export const addCourseToUserPlaylist = catchAsyncError(
}
}
);

export const updateDetails = catchAsyncError(async (req, res, next) => {
try {
// Extract Details from Body
const {
name,
email,
organization,
contactNumber,
address,
officeAddress,
social,
} = req.body;

if (
[name, email, contactNumber, address].some((attr) => attr.trim() === "")
) {
return res
.status(402)
.json({ success: false, message: "Required Fields Missing" });
}

const isUserExist = await User.findById(req.user._id).select("-password");

if (isUserExist) {
const isUpdated = await User.findByIdAndUpdate(
{ _id: isUserExist?._id },
{
$set: {
name,
email,
},
}
);
isUpdated &&
res
.status(200)
.json({ success: true, message: "Information Updated Successfully" });
} else {
res
.status(502)
.json({ success: false, message: "Failed to Update Details" });
}
} catch (error) {
console.log(error);
}
});
3 changes: 3 additions & 0 deletions routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
handleAdminDelete,
AdminGetAllUsers,
updateUserRole,
updateDetails,
} from "../controllers/userController.js";
import { isAuthenticated } from "../middlewares/Auth.js";
import { isAdminAuthenticated } from "../middlewares/adminAuth.js";
Expand All @@ -39,4 +40,6 @@ router.route("/admin/users/:id").delete(isAuthenticated, handleAdminDelete);
router.route("/getAllUsers").get(isAdminAuthenticated, AdminGetAllUsers);
router.route("/admin/users/:id").put(isAuthenticated, updateUserRole);

router.route("/user/details/update").put(isAuthenticated, updateDetails);

export default router;

0 comments on commit 0645c87

Please sign in to comment.