@@ -3,7 +3,10 @@ const mongoose = require('mongoose');
33
44const getStaffProjects = async ( req , res ) => {
55 try {
6- const userId = req . user . id ; // Get from auth middleware
6+ const userId = req . user && ( req . user . id || req . user . _id ) ;
7+ // Debug: log who is requesting staff projects
8+ console . log ( '[getStaffProjects] requester token payload:' , req . user ) ;
9+ console . log ( '[getStaffProjects] resolved userId:' , userId ) ;
710
811 // Get all project assignments for this user
912 const assignments = await ProjectAssignment . find ( { userId } )
@@ -16,6 +19,8 @@ const getStaffProjects = async (req, res) => {
1619 } ,
1720 } ) ;
1821
22+ console . log ( '[getStaffProjects] assignments found:' , assignments . length ) ;
23+
1924 // Map assignments to project details with role info
2025 const projects = assignments . map ( assignment => ( {
2126 id : assignment . projectId . _id ,
@@ -351,9 +356,56 @@ const updateTaskStatus = async (req, res) => {
351356 }
352357} ;
353358
359+ // exports moved to bottom so all handlers are defined before export
360+
361+ const getStaffTasks = async ( req , res ) => {
362+ try {
363+ const userId = req . user && ( req . user . id || req . user . _id ) ;
364+ console . log ( '[getStaffTasks] requester token payload:' , req . user ) ;
365+ console . log ( '[getStaffTasks] resolved userId:' , userId ) ;
366+ if ( ! userId ) return res . status ( 401 ) . json ( { success : false , error : 'Unauthorized' } ) ;
367+
368+ // Find task assignments for this user and include task + project info
369+ const assignments = await TaskAssignment . find ( { userId } )
370+ . populate ( {
371+ path : 'taskId' ,
372+ populate : [
373+ { path : 'projectId' , select : 'name' } ,
374+ { path : 'createdBy' , select : 'name email' }
375+ ]
376+ } )
377+ . lean ( ) ;
378+
379+ console . log ( '[getStaffTasks] assignments found:' , assignments . length ) ;
380+
381+ const tasks = assignments . map ( a => {
382+ const task = a . taskId || { } ;
383+ const project = task . projectId || { } ;
384+ return {
385+ assignmentId : a . _id ,
386+ taskId : task . _id ,
387+ title : task . title ,
388+ description : task . description ,
389+ status : task . status ,
390+ projectId : project . _id ,
391+ projectName : project . name ,
392+ startDate : task . startDate ,
393+ endDate : task . endDate ,
394+ createdBy : task . createdBy ? { id : task . createdBy . _id , name : task . createdBy . name } : null ,
395+ assignedAt : a . createdAt ,
396+ } ;
397+ } ) ;
398+
399+ return res . json ( { success : true , data : tasks } ) ;
400+ } catch ( err ) {
401+ return res . status ( 500 ) . json ( { success : false , error : 'Internal Server Error' , message : err . message } ) ;
402+ }
403+ } ;
404+
354405module . exports = {
355406 getStaffProjects, // DEV-61
356407 getStaffProjectDetail, // DEV-62
357408 getProjectTasks, // DEV-79
358409 updateTaskStatus, // DEV-80
410+ getStaffTasks, // DEV-81
359411} ;
0 commit comments