@@ -203,20 +203,42 @@ const listEmployees = async (req, res) => {
203203 . limit ( limit )
204204 . exec ( ) ;
205205
206- // Count projects per user for users in this page
207206 const userIds = users . map ( ( u ) => u . _id ) ;
207+
208208 const projectCountsMap = new Map ( ) ;
209+ const activeCountsMap = new Map ( ) ;
210+
209211 if ( userIds . length > 0 ) {
210212 const counts = await ProjectAssignment . aggregate ( [
211213 { $match : { userId : { $in : userIds } } } ,
212214 { $group : { _id : "$userId" , count : { $sum : 1 } } } ,
213215 ] ) . exec ( ) ;
214216 counts . forEach ( ( c ) => projectCountsMap . set ( String ( c . _id ) , c . count ) ) ;
217+
218+ const activeCounts = await ProjectAssignment . aggregate ( [
219+ { $match : { userId : { $in : userIds } } } ,
220+ {
221+ $lookup : {
222+ from : "projects" ,
223+ localField : "projectId" ,
224+ foreignField : "_id" ,
225+ as : "project" ,
226+ } ,
227+ } ,
228+ { $unwind : "$project" } ,
229+ { $match : { "project.status" : "active" } } ,
230+ { $group : { _id : "$userId" , activeCount : { $sum : 1 } } } ,
231+ ] ) . exec ( ) ;
232+
233+ activeCounts . forEach ( ( c ) =>
234+ activeCountsMap . set ( String ( c . _id ) , c . activeCount )
235+ ) ;
215236 }
216237
217238 const mapped = users . map ( ( u ) => {
218239 const out = userDto . mapUserToUserResponse ( u ) ;
219240 out . projectCount = projectCountsMap . get ( String ( u . _id ) ) || 0 ;
241+ out . activeProjectCount = activeCountsMap . get ( String ( u . _id ) ) || 0 ;
220242 return out ;
221243 } ) ;
222244
0 commit comments