Skip to content

Commit

Permalink
Merge pull request #118 from JavidSumra/feature/API_Update_Details
Browse files Browse the repository at this point in the history
Feature Addition: User Details Update API Endpoint
  • Loading branch information
anand-harsh authored Feb 8, 2024
2 parents 439d1b8 + 29a7bef commit 2638255
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
49 changes: 49 additions & 0 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,52 @@ 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() === "" || !attr
)
) {
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 2638255

Please sign in to comment.