Skip to content

Commit

Permalink
Refactor project creation logic to handle admin authorship
Browse files Browse the repository at this point in the history
  • Loading branch information
guillecro committed Jan 10, 2024
1 parent 485378f commit fa24388
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions controllers/projectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ exports.createProject = async (req, res) => {
// projectAuthorization.onlyEditors is called before this controller
// if code reaches this point, it means the user is an admin or the author of the project


// if user is an admin, the payload might contain an author id
if(req.user.role == 'admin'){
// check if the author exists
if(req.body.author) {
// if it does, then add it to the project data
projectData.author = req.body.author;
} else {
// if it doesn't, then add the admin user id as the author
projectData.author = req.user._id;
}
}
// project data
const projectData = {
author: req.user._id,
Expand All @@ -82,6 +70,19 @@ exports.createProject = async (req, res) => {
closedAt: req.body.closedAt,
version: 1,
}

// if user is an admin, the payload might contain an author id
if(req.user.role == 'admin'){
// check if the author exists
if(req.body.author) {
// if it does, then add it to the project data
projectData.author = req.body.author;
} else {
// if it doesn't, then add the admin user id as the author
projectData.author = req.user._id;
}
}
// if the project is published, then add the publishedAt date
if(req.body.publishedAt) {
projectData.publishedAt = req.body.publishedAt;
projectData.hidden = false;
Expand Down

0 comments on commit fa24388

Please sign in to comment.