@@ -22,6 +22,7 @@ UserController.user_list = async function (req, res) {
2222 const user = await User . find ( query ) ;
2323 return res . status ( 200 ) . send ( user ) ;
2424 } catch ( err ) {
25+ console . log ( err ) ;
2526 return res . sendStatus ( 400 ) ;
2627 }
2728} ;
@@ -38,12 +39,12 @@ UserController.admin_list = async function (req, res) {
3839 const admins = await User . find ( { accessLevel : { $in : [ 'admin' , 'superadmin' ] } } ) ;
3940 return res . status ( 200 ) . send ( admins ) ;
4041 } catch ( err ) {
42+ console . log ( err ) ;
4143 return res . sendStatus ( 400 ) ;
4244 }
4345} ;
4446
45- // Get list of Users with accessLevel 'admin' or 'superadmin' and also managed projects with GET
46- UserController . projectLead_list = async function ( req , res ) {
47+ UserController . projectManager_list = async function ( req , res ) {
4748 const { headers } = req ;
4849
4950 if ( headers [ 'x-customrequired-header' ] !== expectedHeader ) {
@@ -58,27 +59,28 @@ UserController.projectLead_list = async function (req, res) {
5859 ] ,
5960 } ) ;
6061
61- const updatedProjectManagers = [ ] ;
62+ // Collect all unique project IDs
63+ const allProjectIds = [ ...new Set ( projectManagers . flatMap ( ( pm ) => pm . managedProjects ) ) ] ;
6264
63- for ( const projectManager of projectManagers ) {
64- const projectManagerObj = projectManager . toObject ( ) ;
65- projectManagerObj . isProjectLead = true ;
66- const projectNames = [ ] ;
65+ // Fetch all projects in one query
66+ const projects = await Project . find ( { _id : { $in : allProjectIds } } ) ;
67+ const projectIdToName = { } ;
68+ for ( const project of projects ) {
69+ projectIdToName [ project . _id . toString ( ) ] = project . name ;
70+ }
6771
68- 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' ) ;
74- }
75- }
76- projectManagerObj . managedProjectNames = projectNames ;
72+ const updatedProjectManagers = projectManagers . map ( ( pm ) => {
73+ const pmObj = pm . toObject ( ) ;
74+ pmObj . isProjectLead = true ;
75+ pmObj . managedProjectNames = ( pmObj . managedProjects || [ ] )
76+ . map ( ( pid ) => projectIdToName [ pid . toString ( ) ] || null )
77+ . filter ( Boolean ) ;
78+ return pmObj ;
79+ } ) ;
7780
78- updatedProjectManagers . push ( projectManagerObj ) ;
79- }
8081 return res . status ( 200 ) . send ( updatedProjectManagers ) ;
8182 } catch ( err ) {
83+ console . log ( err ) ;
8284 return res . sendStatus ( 400 ) ;
8385 }
8486} ;
@@ -98,6 +100,7 @@ UserController.user_by_id = async function (req, res) {
98100 // and look downstream to see whether 404 would break anything
99101 return res . status ( 200 ) . send ( user ) ;
100102 } catch ( err ) {
103+ console . log ( err ) ;
101104 return res . sendStatus ( 400 ) ;
102105 }
103106} ;
@@ -141,6 +144,7 @@ UserController.update = async function (req, res) {
141144 const user = await User . findOneAndUpdate ( { _id : UserId } , req . body , { new : true } ) ;
142145 return res . status ( 200 ) . send ( user ) ;
143146 } catch ( err ) {
147+ console . log ( err ) ;
144148 return res . sendStatus ( 400 ) ;
145149 }
146150} ;
@@ -158,6 +162,7 @@ UserController.delete = async function (req, res) {
158162 const user = await User . findByIdAndDelete ( UserId ) ;
159163 return res . status ( 200 ) . send ( user ) ;
160164 } catch ( err ) {
165+ console . log ( err ) ;
161166 return res . sendStatus ( 400 ) ;
162167 }
163168} ;
@@ -227,7 +232,7 @@ UserController.signin = function (req, res) {
227232} ;
228233
229234UserController . verifySignIn = async function ( req , res ) {
230- // eslint-disable-next-line dot-notation
235+
231236 let token = req . headers [ 'x-access-token' ] || req . headers [ 'authorization' ] ;
232237 if ( token . startsWith ( 'Bearer ' ) ) {
233238 // Remove Bearer from string
@@ -240,6 +245,7 @@ UserController.verifySignIn = async function (req, res) {
240245 res . cookie ( 'token' , token , { httpOnly : true } ) ;
241246 return res . send ( user ) ;
242247 } catch ( err ) {
248+ console . log ( err ) ;
243249 return res . status ( 403 ) ;
244250 }
245251} ;
0 commit comments