@@ -172,6 +172,17 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
172172 const allCompletedCourseIds = Array . from ( new Set ( [ ...completedMandatoryCourseIds , ...completedOptionalCourseIds ] ) ) ;
173173 console . log ( 'Unique Completed Course IDs (duplicates removed):' , allCompletedCourseIds ) ;
174174
175+ // If no completed courses, set empty top performers data and return
176+ if ( allCompletedCourseIds . length === 0 ) {
177+ console . log ( 'No completed courses found, setting empty top performers data' ) ;
178+ setTopPerformersData ( {
179+ usersData : {
180+ '5 Highest Course Completing Users' : [ ]
181+ }
182+ } ) ;
183+ return ;
184+ }
185+
175186 // Collect structured course data from all completed courses
176187 const getAllStructuredCourseData = async ( ) => {
177188 const allStructuredData : Array < { courseId : string , units : Array < { unitId : string , contentIds : string [ ] } > } > = [ ] ;
@@ -288,7 +299,11 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
288299
289300 console . log ( 'individualProgressData' , apiResponse ?. getUserDetails ) ;
290301 console . log ( 'totalCount' , apiResponse ?. totalCount ) ;
291- const currentEmployeeIds = apiResponse ?. getUserDetails ?. map ( ( item : any ) => item . userId ) ;
302+ // Handle empty array or undefined getUserDetails
303+ const userDetails = apiResponse ?. getUserDetails || [ ] ;
304+ const currentEmployeeIds = userDetails . length > 0
305+ ? userDetails . map ( ( item : any ) => item . userId )
306+ : [ ] ;
292307
293308 // Fetch certificate status for current employees if course identifiers are available
294309 let userMandatoryCertificateStatus = { data : [ ] } ;
@@ -301,7 +316,7 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
301316 console . log ( 'activeOptionalIds' , activeOptionalIds ) ;
302317 console . log ( 'currentEmployeeIds' , currentEmployeeIds ) ;
303318
304- if ( activeMandatoryIds . length > 0 && activeOptionalIds . length > 0 && currentEmployeeIds . length > 0 ) {
319+ if ( ( activeMandatoryIds . length > 0 || activeOptionalIds . length > 0 ) && currentEmployeeIds . length > 0 ) {
305320 try {
306321 [ userMandatoryCertificateStatus , userOptionalCertificateStatus ] = await Promise . all ( [
307322 fetchUserCertificateStatus ( currentEmployeeIds , activeMandatoryIds ) ,
@@ -317,7 +332,6 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
317332 // Process certificate status data for easier lookup
318333 const mandatoryStatusMap = new Map ( ) ;
319334 const optionalStatusMap = new Map ( ) ;
320-
321335 // Process mandatory certificate status
322336 userMandatoryCertificateStatus . data ?. forEach ( ( item : any ) => {
323337 if ( ! mandatoryStatusMap . has ( item . userId ) ) {
@@ -377,7 +391,8 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
377391 } ) ;
378392
379393 // Transform API data to EmployeeProgress format with calculated progress
380- const transformedProgressData : EmployeeProgress [ ] = apiResponse ?. getUserDetails ?. map ( ( user : any ) => {
394+ const transformedProgressData : EmployeeProgress [ ] = userDetails . length > 0
395+ ? userDetails . map ( ( user : any ) => {
381396 const mandatoryStats = mandatoryStatusMap . get ( user . userId ) || {
382397 completed : 0 ,
383398 inProgress : 0 ,
@@ -424,7 +439,8 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
424439 mandatoryCompletedIdentifiers : mandatoryStats . completedIdentifiers || [ ] ,
425440 optionalCompletedIdentifiers : optionalStats . completedIdentifiers || [ ]
426441 } ;
427- } ) || [ ] ;
442+ } )
443+ : [ ] ;
428444
429445
430446 console . log ( 'transformedProgressData with calculated progress' , transformedProgressData ) ;
@@ -526,9 +542,14 @@ const [employeeDataResponse, setEmployeeDataResponse] = useState<any[]>([]);
526542 filters : { emp_manager :userId } ,
527543 } ) ;
528544 console . log ( 'employeeDataResponse' , employeeDataResponse ) ;
529- setEmployeeDataResponse ( employeeDataResponse ?. getUserDetails || [ ] ) ;
530- employeeUserIds = employeeDataResponse ?. getUserDetails ?. map ( ( item : any ) => item . userId ) ;
531- setEmployeeUserIds ( employeeUserIds ) ;
545+
546+ // Handle empty array or undefined getUserDetails
547+ const employeeDetails = employeeDataResponse ?. getUserDetails || [ ] ;
548+ setEmployeeDataResponse ( employeeDetails ) ;
549+ employeeUserIds = employeeDetails . length > 0
550+ ? employeeDetails . map ( ( item : any ) => item . userId )
551+ : [ ] ;
552+ setEmployeeUserIds ( employeeUserIds ) ;
532553 }
533554 console . log ( 'employeeUserIds' , employeeUserIds ) ;
534555 // Check if tenantId is available before calling certificate status APIs
0 commit comments