-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from idurar/feat/profile-update-module
profile controller api
- Loading branch information
Showing
28 changed files
with
293 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
backend/controllers/coreControllers/adminController/updateProfile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const mongoose = require('mongoose'); | ||
const Admin = mongoose.model('Admin'); | ||
|
||
const updateProfile = async (req, res) => { | ||
try { | ||
console.log('🚀 ~ file: updateProfile.js:7 ~ updateProfile ~ req.admin:', req.admin._id); | ||
console.log('🚀 ~ file: updateProfile.js:10 ~ updateProfile ~ req.params.id:', req.params.id); | ||
if (req.admin._id == req.params.id) { | ||
console.log( | ||
'🚀 ~ file: updateProfile.js:10 ~ updateProfile ~ req.admin._id === req.params.id' | ||
); | ||
} | ||
if (req.admin._id != req.params.id) | ||
return res.status(403).json({ | ||
success: false, | ||
result: null, | ||
message: "you don't have permission to edit this profile", | ||
}); | ||
|
||
let updates = { | ||
role: req.body.role, | ||
email: req.body.email, | ||
name: req.body.name, | ||
surname: req.body.surname, | ||
photo: req.body.photo, | ||
}; | ||
console.log('🚀 ~ file: updateProfile.js:41 ~ updateProfile ~ updates:', updates); | ||
|
||
// Find document by id and updates with the required fields | ||
const result = await Admin.findOneAndUpdate( | ||
{ _id: req.params.id, removed: false }, | ||
{ $set: updates }, | ||
{ | ||
new: true, // return the new result instead of the old one | ||
} | ||
).exec(); | ||
console.log('🚀 ~ file: updateProfile.js:50 ~ updateProfile ~ result:', result); | ||
|
||
if (!result) { | ||
return res.status(404).json({ | ||
success: false, | ||
result: null, | ||
message: 'No document found by this id: ' + req.params.id, | ||
}); | ||
} | ||
return res.status(200).json({ | ||
success: true, | ||
result: { | ||
_id: result?._id, | ||
enabled: result?.enabled, | ||
email: result?.email, | ||
name: result?.name, | ||
surname: result?.surname, | ||
photo: result?.photo, | ||
role: result?.role, | ||
}, | ||
message: 'we update this document by this id: ' + req.params.id, | ||
}); | ||
} catch (error) { | ||
// Server Error | ||
return res.status(500).json({ | ||
success: false, | ||
result: null, | ||
message: 'Oops there is an Error', | ||
error: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = updateProfile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
//this middleware will check if the admin is logged in, if not he will be redirected to the login page :) | ||
module.exports = (req, res, next) => { | ||
console.log('🚀 ~ file: setFilePathToBody.js:4 ~ req.file:', req.file); | ||
if (req.file) { | ||
console.log('🚀 ~ file: setFilePathToBody.js:5 ~ req.file:', req.file); | ||
req.body.filePath = req.file.path; | ||
console.log('🚀 ~ file: setFilePathToBody.js:6 ~ req.file.path:', req.file.path); | ||
} | ||
// if (req.files) { | ||
// req.body[req.files.fieldname] = req.files.path | ||
// } | ||
next(); | ||
module.exports = (fieldName = 'filePath') => { | ||
return (req, res, next) => { | ||
if (req.file) { | ||
req.body[fieldName] = req.file.path; | ||
console.log('🚀 ~ file: setFilePathToBody.js:6 ~ return ~ fieldName:', fieldName); | ||
} | ||
// if (req.files) { | ||
// req.body[req.files.fieldname] = req.files.path | ||
// } | ||
next(); | ||
}; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.