Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: user.ctl.updateUserDetails + Route /api/user/details #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions config/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
SAVE_SUCCESS: 'Requested record created successfully',
VERIFY_EMAIL: 'Verify your email',
VERIFIED_SUCCESSFULLY: 'Email verified successfully',
USER_TOKEN_EXPIRED: 'Your section is expired, Login again',
USER_TOKEN_EXPIRED: 'Your session is expired, Login again',
USER_TOKEN_NOTFOUND: 'You are not logged in, Login now',
LOGIN_LIMIT_REACHED: 'Too many login request, try after some time!',
UNAUTHORIZED_REQUEST: 'Unauthorized request !',
Expand Down Expand Up @@ -57,6 +57,9 @@ module.exports = {
PROPERTY_ADDIMAGE_LIMIT_EXCEED:"Property Images Limit Exceed !",
NO_PROPERTY_IMAGE_SELECTED: "No Image Selected !",

//User Update
INVALID_PHONE:"Invalid Phone Number!",

PLANS: {
"PREMIUMTENANT": {
type: "PREMIUMTENANT",
Expand All @@ -82,5 +85,6 @@ module.exports = {
}
},
PROPERTY_TYPE: ['ROOM', 'HOUSE', 'FLAT', 'COMMERCIAL_SPACE'],
OFFER_TYPE: ['RENT','SELL']
OFFER_TYPE: ['RENT','SELL'],
PHONE_REGEX : /^\d{10}$/
}
65 changes: 65 additions & 0 deletions controllers/user.ctl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,70 @@ userControllers.getUserDocuments = async (req, res) => {
}
}

userControllers.updateUserDetails = async (req, res) => {
try {
// Update User Details
const userId = req.user.id;
const userRole = req.user.role;

// Get phone_no, address, username from the request body
const { username, phone_no, address } = req.body;

// Validating at least one field to update
if (!username && !phone_no && !address) {
return res.status(Constant.BAD_REQUEST).json({
code: Constant.BAD_REQUEST,
message: Constant.REQUEST_BAD_REQUEST
});
}

// Validate phone_no format
if (phone_no && !Constant.PHONE_REGEX.test(phone_no)) {
return res.status(Constant.BAD_REQUEST).json({
code: Constant.BAD_REQUEST,
message: Constant.INVALID_PHONE
});
}

// Constructing the update object based on the updated fields
const updateObject = {};
if (username) updateObject.username = username;
if (phone_no) updateObject.phone_no = phone_no;
if (address) updateObject.address = address;

let user;
// Updating user details based on the user's role
if (userRole === 'TENANT') {
// If tenant
user = await db.tenant.findOne({ where: { id: userId } });
if (user) {
// Update user details
await db.tenant.update(updateObject, { where: { id: userId } });
user = await db.tenant.findOne({ where: { id: userId } });
}
} else if (userRole === 'LANDLORD') {
// If landlord
user = await db.landlord.findOne({ where: { id: userId } });
if (user) {
// Update user details
await db.landlord.update(updateObject, { where: { id: userId } });
user = await db.landlord.findOne({ where: { id: userId } });
}
}

//If user details updated successfully
return res.status(Constant.SUCCESS_CODE).json({
code: Constant.SUCCESS_CODE,
message: Constant.UPDATE_SUCCESS,
});

} catch (error) {
return res.status(Constant.SERVER_ERROR).json({
code: Constant.SERVER_ERROR,
message: Constant.SOMETHING_WENT_WRONG
});
}
};


module.exports = userControllers;
1 change: 1 addition & 0 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const rateLimiter = require("../middileware/rate-limit");
const auth = require("../middileware/auth");

router.get("/documents", rateLimiter.commonOperationsRouteRateLimiter, auth.checkAuthentication, userControllers.getUserDocuments);
router.post("/details", rateLimiter.commonOperationsRouteRateLimiter, auth.checkAuthentication, userControllers.updateUserDetails);

module.exports = router;
Loading