Skip to content
Merged
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
44 changes: 25 additions & 19 deletions backend/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ UserController.user_list = async function (req, res) {
const user = await User.find(query);
return res.status(200).send(user);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};
Expand All @@ -38,12 +39,12 @@ UserController.admin_list = async function (req, res) {
const admins = await User.find({ accessLevel: { $in: ['admin', 'superadmin'] } });
return res.status(200).send(admins);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};

// Get list of Users with accessLevel 'admin' or 'superadmin' and also managed projects with GET
UserController.projectLead_list = async function (req, res) {
UserController.projectManager_list = async function (req, res) {
const { headers } = req;

if (headers['x-customrequired-header'] !== expectedHeader) {
Expand All @@ -58,27 +59,28 @@ UserController.projectLead_list = async function (req, res) {
],
});

const updatedProjectManagers = [];
// Collect all unique project IDs
const allProjectIds = [...new Set(projectManagers.flatMap((pm) => pm.managedProjects))];

for (const projectManager of projectManagers) {
const projectManagerObj = projectManager.toObject();
projectManagerObj.isProjectLead = true;
const projectNames = [];
// Fetch all projects in one query
const projects = await Project.find({ _id: { $in: allProjectIds } });
const projectIdToName = {};
for (const project of projects) {
projectIdToName[project._id.toString()] = project.name;
}

for (const projectId of projectManagerObj.managedProjects) {
const projectDetail = await Project.findById(projectId);
if (projectDetail && projectDetail.name) {
projectNames.push(projectDetail.name);
} else {
console.warn('Project detail is null, cannot access name');
}
}
projectManagerObj.managedProjectNames = projectNames;
const updatedProjectManagers = projectManagers.map((pm) => {
const pmObj = pm.toObject();
pmObj.isProjectLead = true;
pmObj.managedProjectNames = (pmObj.managedProjects || [])
.map((pid) => projectIdToName[pid.toString()] || null)
.filter(Boolean);
return pmObj;
});

updatedProjectManagers.push(projectManagerObj);
}
return res.status(200).send(updatedProjectManagers);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};
Expand All @@ -98,6 +100,7 @@ UserController.user_by_id = async function (req, res) {
// and look downstream to see whether 404 would break anything
return res.status(200).send(user);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};
Expand Down Expand Up @@ -141,6 +144,7 @@ UserController.update = async function (req, res) {
const user = await User.findOneAndUpdate({ _id: UserId }, req.body, { new: true });
return res.status(200).send(user);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};
Expand All @@ -158,6 +162,7 @@ UserController.delete = async function (req, res) {
const user = await User.findByIdAndDelete(UserId);
return res.status(200).send(user);
} catch (err) {
console.log(err);
return res.sendStatus(400);
}
};
Expand Down Expand Up @@ -227,7 +232,7 @@ UserController.signin = function (req, res) {
};

UserController.verifySignIn = async function (req, res) {
// eslint-disable-next-line dot-notation
let token = req.headers['x-access-token'] || req.headers['authorization'];
if (token.startsWith('Bearer ')) {
// Remove Bearer from string
Expand All @@ -240,6 +245,7 @@ UserController.verifySignIn = async function (req, res) {
res.cookie('token', token, { httpOnly: true });
return res.send(user);
} catch (err) {
console.log(err);
return res.status(403);
}
};
Expand Down
2 changes: 1 addition & 1 deletion backend/routers/users.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ router.get('/', UserController.user_list);

router.get('/admins', UserController.admin_list);

router.get('/projectManagers', UserController.projectLead_list);
router.get('/projectManagers', UserController.projectManager_list);

router.post('/', UserController.create);

Expand Down
Loading