diff --git a/config/constant.js b/config/constant.js index 6a7442f..4ebd31d 100644 --- a/config/constant.js +++ b/config/constant.js @@ -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 !', @@ -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", @@ -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}$/ } \ No newline at end of file diff --git a/controllers/user.ctl.js b/controllers/user.ctl.js index d59075f..147d146 100644 --- a/controllers/user.ctl.js +++ b/controllers/user.ctl.js @@ -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; \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index 5bbe4c6..59a8e26 100644 --- a/routes/user.js +++ b/routes/user.js @@ -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; \ No newline at end of file