Skip to content

Commit c59903b

Browse files
Pass courseId in array for shortlisting users
1 parent 398e69f commit c59903b

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/adapters/postgres/cohortMembers-adapter.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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({

src/pathways/common/services/lms-client.service.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,32 @@ export class LmsClientService {
486486
};
487487

488488
try {
489-
this.logger.debug(`Enrolling user ${userId} to ${courseIds.length} courses in bulk`);
489+
this.logger.debug(
490+
`Enrolling user ${userId} to ${courseIds.length} courses in bulk`
491+
);
490492
const res = await axios.post(
491493
enrollUrl,
492494
{ learnerId: userId, courseId: courseIds, status: 'published' },
493-
{ headers, params: { userId }, timeout: 30000, validateStatus: () => true }
495+
{
496+
headers,
497+
params: { userId },
498+
timeout: 30000,
499+
validateStatus: () => true,
500+
}
494501
);
495502

496503
if (res.status >= 200 && res.status < 300) {
497-
const alreadyEnrolledCount = res.data?.alreadyEnrolledCourseIds?.length || 0;
504+
const alreadyEnrolledCount =
505+
res.data?.alreadyEnrolledCourseIds?.length || 0;
506+
return { success: true, alreadyEnrolledCount };
507+
}
508+
509+
// 409 Conflict = already enrolled; treat as success so assignment can proceed
510+
if (res.status === 409) {
511+
this.logger.debug(
512+
`User ${userId} already enrolled in courses (409); treating as success`
513+
);
514+
const alreadyEnrolledCount = courseIds.length;
498515
return { success: true, alreadyEnrolledCount };
499516
}
500517

0 commit comments

Comments
 (0)