@@ -42,8 +42,7 @@ UserController.admin_list = async function (req, res) {
4242 }
4343} ;
4444
45- // Get list of Users with accessLevel 'admin' or 'superadmin' and also managed projects with GET
46- UserController . projectLead_list = async function ( req , res ) {
45+ UserController . projectManager_list = async function ( req , res ) {
4746 const { headers } = req ;
4847
4948 if ( headers [ 'x-customrequired-header' ] !== expectedHeader ) {
@@ -52,33 +51,68 @@ UserController.projectLead_list = async function (req, res) {
5251
5352 try {
5453 const projectManagers = await User . find ( {
55- $and : [
56- { accessLevel : { $in : [ 'admin' , 'superadmin' ] } } ,
57- { managedProjects : { $exists : true , $type : 'array' , $ne : [ ] } } ,
58- ] ,
54+ managedProjects : { $exists : true , $type : 'array' , $not : { $size : 0 } } ,
55+ } ) ;
56+
57+ // Filter out managers with empty arrays early
58+ const validProjectManagers = projectManagers . filter (
59+ ( manager ) => manager . managedProjects && manager . managedProjects . length > 0 ,
60+ ) ;
61+
62+ if ( validProjectManagers . length === 0 ) {
63+ return res . status ( 200 ) . send ( [ ] ) ;
64+ }
65+
66+ // Collect all unique project IDs to fetch in one query
67+ const allProjectIds = new Set ( ) ;
68+ validProjectManagers . forEach ( ( manager ) => {
69+ manager . managedProjects . forEach ( ( projectId ) => {
70+ // Filter out invalid project IDs (non-string or obviously invalid values)
71+ if ( typeof projectId === 'string' && projectId !== 'false' && projectId . length > 0 ) {
72+ allProjectIds . add ( projectId ) ;
73+ }
74+ } ) ;
75+ } ) ;
76+
77+ // Fetch all projects in a single query
78+ const projects = await Project . find ( { _id : { $in : Array . from ( allProjectIds ) } } ) ;
79+
80+ // Create a map for O(1) lookup
81+ const projectMap = new Map ( ) ;
82+ projects . forEach ( ( project ) => {
83+ projectMap . set ( project . _id . toString ( ) , project . name ) ;
5984 } ) ;
6085
6186 const updatedProjectManagers = [ ] ;
6287
63- for ( const projectManager of projectManagers ) {
88+ for ( const projectManager of validProjectManagers ) {
6489 const projectManagerObj = projectManager . toObject ( ) ;
65- projectManagerObj . isProjectLead = true ;
90+ projectManagerObj . isProjectMember = true ;
6691 const projectNames = [ ] ;
6792
6893 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' ) ;
94+ // using try-catch block because old user data had invalid strings (aka 'false') for ProjectIds
95+ try {
96+ const projectName = projectMap . get ( projectId . toString ( ) ) ;
97+ if ( projectName ) {
98+ projectNames . push ( projectName ) ;
99+ } else {
100+ console . warn ( 'Project detail is null, cannot access name' ) ;
101+ }
102+ } catch ( error ) {
103+ console . warn ( 'Failed to fetch project details for ID:' , projectId , error ) ;
74104 }
75105 }
76- projectManagerObj . managedProjectNames = projectNames ;
77106
78- updatedProjectManagers . push ( projectManagerObj ) ;
107+ if ( projectNames . length ) {
108+ projectManagerObj . managedProjectNames = projectNames ;
109+ updatedProjectManagers . push ( projectManagerObj ) ;
110+ }
79111 }
112+
80113 return res . status ( 200 ) . send ( updatedProjectManagers ) ;
81114 } catch ( err ) {
115+ console . log ( 'Projectlead error' , err ) ;
82116 return res . sendStatus ( 400 ) ;
83117 }
84118} ;
0 commit comments