Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //update own profile info
- const updateOwnProfileInfo = async (req, res) => {
- const token = req.header("Authorization") //get the token from header
- if(!token){
- res.json({
- message: "Token is not available"
- })
- }else{
- const tokenData = jwt.verify(token, jwtToken)
- const {id, userType } = tokenData //get the data from token
- const {userId:bodyUserId}= req.body //get the user id from req.body
- if(userType.toLowerCase() == "admin"){ //if the user is admin
- const findAdmin = await Admin.findOne({_id: id, userId:bodyUserId}) //find the admin
- if(findAdmin){
- const updateProfileInfo = await Admin.updateOne( //update the admin profile data
- {
- _id: id
- }, //query
- {
- $set: req.body,
- $currentDate: {
- "modificationInfo.updatedAt": true
- }
- }, //update
- {multi: true} //option
- ) //update the data
- if(updateProfileInfo.nModified != 0 ){ //if there have any update happened
- res.status(202).json({
- message: `${findAdmin.personalInfo.FirstName} ${findAdmin.personalInfo.LastName} is updated successfully`
- })
- }else{
- res.status(404).json({
- message: `${findAdmin.personalInfo.FirstName} ${findAdmin.personalInfo.LastName} is not updated successfully`
- })
- }
- }else{
- res.status(404).json({
- message: "Admin not found"
- })
- }
- }else if(userType.toLowerCase() == "customer"){ //if the user is customer
- const findCustomer = await Customer.findOne({_id: id, userId:bodyUserId}) //find the customer
- if(findCustomer){ //if the customer exist then it will execute
- const updateProfileInfo = await Customer.updateOne( //update the customer data
- {
- _id: id
- }, //query
- {
- $set: req.body,
- $currentDate: {
- "modificationInfo.updatedAt": true
- }
- }, //update
- {multi: true} //option
- ) //update the data
- if(updateProfileInfo.nModified != 0 ){ //if there have any update happened
- res.status(202).json({
- message: `${findCustomer.personalInfo.firstName} ${findCustomer.personalInfo.lastName} is updated successfully`
- })
- }else{
- res.status(404).json({
- message: `${findCustomer.personalInfo.firstName} ${findCustomer.personalInfo.lastName} is not updated successfully`
- })
- }
- }else{
- res.status(404).json({
- message: "Customer not found"
- })
- }
- }else if(userType.toLowerCase() == "seller"){ //if the user is seller
- const findSeller = await Seller.findOne({_id: id, userId:bodyUserId}) //find the seller
- if(findSeller){ //if the seller exist then it will execute
- const updateProfileInfo = await Seller.updateOne( //update the seller data
- {
- _id: id
- }, //query
- {
- $set: req.body,
- $currentDate: {
- "modificationInfo.updatedAt": true
- }
- }, //update
- {multi: true} //option
- ) //update the data
- if(updateProfileInfo.nModified != 0 ){ //if there have any update happened
- res.status(202).json({
- message: `${findSeller.personalInfo.firstName} ${findSeller.personalInfo.lastName} is updated successfully`
- })
- }else{
- res.status(404).json({
- message: `${findSeller.personalInfo.firstName} ${findSeller.personalInfo.lastName} is not updated successfully`
- })
- }
- }else{
- res.status(404).json({
- message: "Seller not found"
- })
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement