Skip to content

Commit 53876f8

Browse files
committed
fixed bug to now show admins and users in project members search
1 parent fbf39f7 commit 53876f8

8 files changed

Lines changed: 274 additions & 551 deletions

File tree

backend/controllers/user.controller.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ UserController.admin_list = async function (req, res) {
4343
};
4444

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

4949
if (headers['x-customrequired-header'] !== expectedHeader) {
@@ -52,33 +52,44 @@ UserController.projectLead_list = async function (req, res) {
5252

5353
try {
5454
const projectManagers = await User.find({
55-
$and: [
56-
{ accessLevel: { $in: ['admin', 'superadmin'] } },
57-
{ managedProjects: { $exists: true, $type: 'array', $ne: [] } },
58-
],
55+
managedProjects: { $exists: true, $type: 'array', $not: { $size: 0 } },
5956
});
6057

6158
const updatedProjectManagers = [];
6259

6360
for (const projectManager of projectManagers) {
6461
const projectManagerObj = projectManager.toObject();
65-
projectManagerObj.isProjectLead = true;
62+
63+
/* Due to the way MongoDB searches for non-empty arrays, sometimes an empty array gets passed
64+
so we need to check if managedProjects is empty */
65+
if (projectManagerObj.managedProjects.length === 0) continue;
66+
67+
projectManagerObj.isProjectMember = true;
6668
const projectNames = [];
6769

6870
for (const projectId of projectManagerObj.managedProjects) {
69-
const projectDetail = await Project.findById(projectId);
70-
if (projectDetail && projectDetail.name) {
71-
projectNames.push(projectDetail.name);
72-
} else {
73-
console.warn('Project detail is null, cannot access name');
71+
// using try-catch block because old user data had invalid strings (aka 'false') for ProjectIds
72+
try {
73+
const projectDetail = await Project.findById(projectId);
74+
if (projectDetail && projectDetail.name) {
75+
projectNames.push(projectDetail.name);
76+
} else {
77+
console.warn('Project detail is null, cannot access name');
78+
}
79+
} catch (error) {
80+
console.warn('Failed to fetch project details for ID:', projectId, error);
7481
}
7582
}
76-
projectManagerObj.managedProjectNames = projectNames;
7783

78-
updatedProjectManagers.push(projectManagerObj);
84+
if (projectNames.length) {
85+
projectManagerObj.managedProjectNames = projectNames;
86+
updatedProjectManagers.push(projectManagerObj);
87+
}
7988
}
89+
8090
return res.status(200).send(updatedProjectManagers);
8191
} catch (err) {
92+
console.log('Projectlead error', err);
8293
return res.sendStatus(400);
8394
}
8495
};

backend/models/project.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Idea for the future: programmingLanguages, numberGithubContributions (pull these
1616
*/
1717

1818
const projectSchema = mongoose.Schema({
19-
name: { type: String, trim: true },
19+
name: { type: String, trim: true, required: true },
2020
description: { type: String, trim: true },
2121
githubIdentifier: { type: String, trim: true },
2222
projectStatus: { type: String }, // Active, Completed, or Paused

backend/models/user.model.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const mongoose = require("mongoose");
1+
const mongoose = require('mongoose');
22
// const bcrypt = require('bcrypt-nodejs');
33

44
mongoose.Promise = global.Promise;
@@ -9,10 +9,10 @@ const userSchema = mongoose.Schema({
99
lastName: { type: String },
1010
},
1111
email: { type: String, unique: true },
12-
accessLevel: {
13-
type: String,
14-
enum: ["user", "admin", "superadmin"], // restricts values to "user", "admin" and "superadmin"
15-
default: "user"
12+
accessLevel: {
13+
type: String,
14+
enum: ['user', 'admin', 'superadmin'], // restricts values to "user", "admin" and "superadmin"
15+
default: 'user',
1616
},
1717
createdDate: { type: Date, default: Date.now },
1818
currentRole: { type: String }, // will remove but need to update check-in form
@@ -27,7 +27,7 @@ const userSchema = mongoose.Schema({
2727
projects: [
2828
{
2929
type: mongoose.Schema.Types.ObjectId,
30-
ref: "Project",
30+
ref: 'Project',
3131
},
3232
],
3333
githubHandle: { type: String }, // handle not including @, not the URL
@@ -37,10 +37,15 @@ const userSchema = mongoose.Schema({
3737
isHflaGithubMember: { type: Boolean }, // pull from API once github handle in place?
3838
githubPublic2FA: { type: Boolean }, // does the user have 2FA enabled on their github and membership set to public?
3939
availability: { type: String }, // availability to meet outside of hacknight times; string for now, more structured in future
40-
managedProjects: [{ type: String}], // Which projects managed by user.
40+
managedProjects: [
41+
{
42+
type: mongoose.Schema.Types.ObjectId,
43+
ref: 'Project',
44+
},
45+
], // Which projects managed by user.
4146
//currentProject: { type: String } // no longer need this as we can get it from Project Team Member table
4247
// password: { type: String, required: true }
43-
isActive: { type: Boolean, default: true }
48+
isActive: { type: Boolean, default: true },
4449
});
4550

4651
userSchema.methods.serialize = function () {
@@ -71,10 +76,10 @@ userSchema.methods.serialize = function () {
7176
githubPublic2FA: this.githubPublic2FA,
7277
availability: this.availability,
7378
managedProjects: this.managedProjects,
74-
isActive: this.isActive
79+
isActive: this.isActive,
7580
};
7681
};
7782

78-
const User = mongoose.model("User", userSchema);
83+
const User = mongoose.model('User', userSchema);
7984

8085
module.exports = { User };

backend/routers/users.router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ router.get('/', UserController.user_list);
88

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

11-
router.get('/projectManagers', UserController.projectLead_list);
11+
router.get('/projectManagers', UserController.projectManager_list);
1212

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

0 commit comments

Comments
 (0)