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
35 changes: 21 additions & 14 deletions backend/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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);
console.error(err);
return res.sendStatus(400);
}
};
Expand All @@ -39,7 +39,7 @@ 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);
console.error(err);
return res.sendStatus(400);
}
};
Expand All @@ -53,17 +53,24 @@ UserController.projectManager_list = async function (req, res) {

try {
const projectManagers = await User.find({
$and: [
{ accessLevel: { $in: ['admin', 'superadmin'] } },
{ managedProjects: { $exists: true, $type: 'array', $ne: [] } },
],
managedProjects: { $exists: true, $type: 'array', $ne: [] },
});

// Collect all unique project IDs
const allProjectIds = [...new Set(projectManagers.flatMap((pm) => pm.managedProjects))];
const allProjectIds = [
...new Set(
projectManagers
.flatMap((pm) => pm.managedProjects)
.filter((id) => typeof id === 'string' && id.match(/^[a-f\d]{24}$/i)),
),
];

// Fetch all projects in one query
const projects = await Project.find({ _id: { $in: allProjectIds } });
const projects = await Project.find(
{ _id: { $in: allProjectIds } },
{ _id: 1, name: 1 }, // projection
);

const projectIdToName = {};
for (const project of projects) {
projectIdToName[project._id.toString()] = project.name;
Expand All @@ -80,7 +87,8 @@ UserController.projectManager_list = async function (req, res) {

return res.status(200).send(updatedProjectManagers);
} catch (err) {
console.log(err);
console.error(err);
console.log('Projectlead error', err);
return res.sendStatus(400);
}
};
Expand All @@ -100,7 +108,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);
console.error(err);
return res.sendStatus(400);
}
};
Expand Down Expand Up @@ -144,7 +152,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);
console.error(err);
return res.sendStatus(400);
}
};
Expand All @@ -162,7 +170,7 @@ UserController.delete = async function (req, res) {
const user = await User.findByIdAndDelete(UserId);
return res.status(200).send(user);
} catch (err) {
console.log(err);
console.error(err);
return res.sendStatus(400);
}
};
Expand Down Expand Up @@ -232,7 +240,6 @@ UserController.signin = function (req, res) {
};

UserController.verifySignIn = async function (req, res) {

let token = req.headers['x-access-token'] || req.headers['authorization'];
if (token.startsWith('Bearer ')) {
// Remove Bearer from string
Expand All @@ -245,7 +252,7 @@ UserController.verifySignIn = async function (req, res) {
res.cookie('token', token, { httpOnly: true });
return res.send(user);
} catch (err) {
console.log(err);
console.error(err);
return res.status(403);
}
};
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/user-admin/UserPermissionSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
variant={isProjectLead ? 'contained' : 'secondary'}
onClick={buttonSwap}
>
Project Leads
Project Members
</Button>
</ButtonGroup>
</Box>
Expand Down
Loading
Loading