@@ -42,7 +42,8 @@ UserController.admin_list = async function (req, res) {
4242 }
4343} ;
4444
45- UserController . projectManager_list = async function ( req , res ) {
45+ // Get list of Users with accessLevel 'admin' or 'superadmin' and also managed projects with GET
46+ UserController . projectLead_list = async function ( req , res ) {
4647 const { headers } = req ;
4748
4849 if ( headers [ 'x-customrequired-header' ] !== expectedHeader ) {
@@ -51,68 +52,33 @@ UserController.projectManager_list = async function (req, res) {
5152
5253 try {
5354 const projectManagers = await User . find ( {
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 ) ;
55+ $and : [
56+ { accessLevel : { $in : [ 'admin' , 'superadmin' ] } } ,
57+ { managedProjects : { $exists : true , $type : 'array' , $ne : [ ] } } ,
58+ ] ,
8459 } ) ;
8560
8661 const updatedProjectManagers = [ ] ;
8762
88- for ( const projectManager of validProjectManagers ) {
63+ for ( const projectManager of projectManagers ) {
8964 const projectManagerObj = projectManager . toObject ( ) ;
90- projectManagerObj . isProjectMember = true ;
65+ projectManagerObj . isProjectLead = true ;
9166 const projectNames = [ ] ;
9267
9368 for ( const projectId of projectManagerObj . managedProjects ) {
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 ) ;
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' ) ;
10474 }
10575 }
76+ projectManagerObj . managedProjectNames = projectNames ;
10677
107- if ( projectNames . length ) {
108- projectManagerObj . managedProjectNames = projectNames ;
109- updatedProjectManagers . push ( projectManagerObj ) ;
110- }
78+ updatedProjectManagers . push ( projectManagerObj ) ;
11179 }
112-
11380 return res . status ( 200 ) . send ( updatedProjectManagers ) ;
11481 } catch ( err ) {
115- console . log ( 'Projectlead error' , err ) ;
11682 return res . sendStatus ( 400 ) ;
11783 }
11884} ;
0 commit comments