@@ -6173,7 +6173,14 @@ export class PostgresCohortMembersService {
61736173 userId : string ,
61746174 courses : any [ ] ,
61756175 cohortId : string
6176- ) : Promise < Array < { courseId : string ; status : 'success' | 'failed' ; result ?: any ; error ?: any } > > {
6176+ ) : Promise <
6177+ Array < {
6178+ courseId : string ;
6179+ status : 'success' | 'failed' ;
6180+ result ?: any ;
6181+ error ?: any ;
6182+ } >
6183+ > {
61776184 const lmsBaseUrl = process . env . LMS_SERVICE_URL ;
61786185 const tenantId = process . env . DEFAULT_TENANT_ID ;
61796186 const organisationId = process . env . DEFAULT_ORGANISATION_ID ;
@@ -6212,12 +6219,37 @@ export class PostgresCohortMembersService {
62126219 enrollmentId : 'bulk' , // response.data is an array now
62136220 } ) ;
62146221
6215- // Map to the expected return format
6216- return courseIds . map ( id => ( {
6217- courseId : id ,
6218- status : 'success' ,
6219- result : response . data ,
6220- } ) ) ;
6222+ // Map to the expected return format extracting individual course results
6223+ const successfullyEnrolled = response . data ?. successfullyEnrolled || [ ] ;
6224+ const alreadyEnrolledCourseIds = response . data ?. alreadyEnrolledCourseIds || [ ] ;
6225+ const failedCourseIds = response . data ?. failedCourseIds || [ ] ;
6226+
6227+ return courseIds . map ( ( id ) => {
6228+ if ( failedCourseIds . includes ( id ) ) {
6229+ return {
6230+ courseId : id ,
6231+ status : 'failed' ,
6232+ error : 'LMS API reported failure for this course' ,
6233+ } ;
6234+ }
6235+ if ( alreadyEnrolledCourseIds . includes ( id ) ) {
6236+ // Previously, a 409 threw an error and was recorded as 'failed' in the loop
6237+ return {
6238+ courseId : id ,
6239+ status : 'failed' ,
6240+ error : 'User already enrolled (409 Conflict)' ,
6241+ } ;
6242+ }
6243+
6244+ const successMatch = successfullyEnrolled . find (
6245+ ( e : any ) => e . courseId === id
6246+ ) ;
6247+ return {
6248+ courseId : id ,
6249+ status : 'success' ,
6250+ result : successMatch || { status : 'PUBLISHED' } ,
6251+ } ;
6252+ } ) ;
62216253 } catch ( error ) {
62226254 // Log failed enrollment
62236255 ShortlistingLogger . logLMSEnrollmentFailure ( {
0 commit comments