Skip to content

Commit a84a8cb

Browse files
authored
use v2 enrollments, hide org enrollments in my learning (#2604)
* Update transform functions and enrollment displays throughout the dashboard to use v2 enrollments, hide org enrollments from My Learning * fix mistake with casing * more casing issues * fix more issues * more casing issues...
1 parent 2d8a2bf commit a84a8cb

File tree

13 files changed

+302
-99
lines changed

13 files changed

+302
-99
lines changed

frontends/api/src/mitxonline/hooks/enrollment/queries.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { queryOptions } from "@tanstack/react-query"
22
import type {
3-
CourseRunEnrollment,
3+
CourseRunEnrollmentRequestV2,
44
UserProgramEnrollmentDetail,
55
} from "@mitodl/mitxonline-api-axios/v2"
66

@@ -26,8 +26,10 @@ const enrollmentQueries = {
2626
courseRunEnrollmentsList: () =>
2727
queryOptions({
2828
queryKey: enrollmentKeys.courseRunEnrollmentsList(),
29-
queryFn: async (): Promise<CourseRunEnrollment[]> => {
30-
return courseRunEnrollmentsApi.enrollmentsList().then((res) => res.data)
29+
queryFn: async (): Promise<CourseRunEnrollmentRequestV2[]> => {
30+
return courseRunEnrollmentsApi
31+
.userEnrollmentsListV2()
32+
.then((res) => res.data)
3133
},
3234
}),
3335
programEnrollmentsList: (opts?: RawAxiosRequestConfig) =>

frontends/api/src/mitxonline/test-utils/factories/enrollment.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mergeOverrides } from "ol-test-utilities"
33
import type { PartialFactory } from "ol-test-utilities"
44
import type {
55
CourseRunEnrollment,
6+
CourseRunEnrollmentRequestV2,
67
CourseRunGrade,
78
UserProgramEnrollmentDetail,
89
} from "@mitodl/mitxonline-api-axios/v2"
@@ -28,14 +29,16 @@ const grade: PartialFactory<CourseRunGrade> = (overrides = {}) => {
2829
return mergeOverrides<CourseRunGrade>(defaults, overrides)
2930
}
3031

31-
const courseEnrollment: PartialFactory<CourseRunEnrollment> = (
32+
const courseEnrollment: PartialFactory<CourseRunEnrollmentRequestV2> = (
3233
overrides = {},
3334
) => {
3435
const title =
3536
overrides.run?.title ?? overrides.run?.course?.title ?? faker.word.words(3)
3637

37-
const defaults: CourseRunEnrollment = {
38+
const defaults: CourseRunEnrollmentRequestV2 = {
3839
id: uniqueEnrollmentId.enforce(() => faker.number.int()),
40+
b2b_contract_id: null, // Default to personal enrollment (not B2B)
41+
b2b_organization_id: null, // Default to personal enrollment (not B2B)
3942
certificate: {
4043
uuid: faker.string.uuid(),
4144
link: faker.internet.url(),
@@ -95,10 +98,52 @@ const courseEnrollment: PartialFactory<CourseRunEnrollment> = (
9598
],
9699
},
97100
programs: null,
101+
topics: faker.helpers.multiple(
102+
() => ({ name: faker.lorem.word(), id: faker.number.int() }),
103+
{ count: { min: 0, max: 5 } },
104+
),
105+
certificate_type: faker.helpers.arrayElement([
106+
"completion",
107+
"verified",
108+
"professional",
109+
"micromasters",
110+
]),
111+
required_prerequisites: faker.datatype.boolean(),
112+
duration: `${faker.number.int({ min: 4, max: 16 })} weeks`,
113+
min_weeks: faker.number.int({ min: 4, max: 8 }),
114+
max_weeks: faker.number.int({ min: 12, max: 20 }),
115+
min_price: faker.number.int({ min: 0, max: 500 }),
116+
max_price: faker.number.int({ min: 500, max: 2000 }),
117+
time_commitment: `${faker.number.int({ min: 2, max: 15 })} hours per week`,
118+
availability: faker.helpers.arrayElement([
119+
"current",
120+
"starting_soon",
121+
"upcoming",
122+
"archived",
123+
]),
124+
min_weekly_hours: faker.number.int({ min: 2, max: 5 }).toString(),
125+
max_weekly_hours: faker.number.int({ min: 8, max: 15 }).toString(),
126+
include_in_learn_catalog: faker.datatype.boolean({ probability: 0.8 }),
127+
ingest_content_files_for_ai: faker.datatype.boolean({
128+
probability: 0.3,
129+
}),
98130
},
99131
},
100132
}
101-
return mergeOverrides<CourseRunEnrollment>(defaults, overrides)
133+
return mergeOverrides<CourseRunEnrollmentRequestV2>(defaults, overrides)
134+
}
135+
136+
// Type-safe conversion from V2 to V1 enrollment for compatibility
137+
const convertV2ToV1Enrollment = (
138+
v2Enrollment: CourseRunEnrollmentRequestV2,
139+
): CourseRunEnrollment => {
140+
// Remove V2-specific fields and return V1 compatible object
141+
const {
142+
b2b_contract_id: b2bContractId,
143+
b2b_organization_id: b2bOrganizationId,
144+
...v1Compatible
145+
} = v2Enrollment
146+
return v1Compatible as CourseRunEnrollment
102147
}
103148

104149
const programEnrollment: PartialFactory<UserProgramEnrollmentDetail> = (
@@ -143,13 +188,13 @@ const programEnrollment: PartialFactory<UserProgramEnrollmentDetail> = (
143188
],
144189
live: faker.datatype.boolean(),
145190
},
146-
enrollments: [courseEnrollment()],
191+
enrollments: [convertV2ToV1Enrollment(courseEnrollment())],
147192
}
148193
return mergeOverrides<UserProgramEnrollmentDetail>(defaults, overrides)
149194
}
150195

151196
// Not paginated
152-
const courseEnrollments = (count: number): CourseRunEnrollment[] => {
197+
const courseEnrollments = (count: number): CourseRunEnrollmentRequestV2[] => {
153198
return new Array(count).fill(null).map(() => courseEnrollment())
154199
}
155200

frontends/api/src/mitxonline/test-utils/urls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const enrollment = {
2121
enrollmentsList: () => `${API_BASE_URL}/api/v1/enrollments/`,
2222
courseEnrollment: (id?: number) =>
2323
`${API_BASE_URL}/api/v1/enrollments/${id ? `${id}/` : ""}`,
24+
enrollmentsListV2: () => `${API_BASE_URL}/api/v2/enrollments/`,
2425
}
2526

2627
const programEnrollments = {

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/DashboardDialogs.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("DashboardDialogs", () => {
3737

3838
mockedUseFeatureFlagEnabled.mockReturnValue(true)
3939
setMockResponse.get(
40-
mitxonline.urls.enrollment.courseEnrollment(),
40+
mitxonline.urls.enrollment.enrollmentsListV2(),
4141
enrollments,
4242
)
4343

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/EnrollmentDisplay.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("EnrollmentDisplay", () => {
2525

2626
mockedUseFeatureFlagEnabled.mockReturnValue(true)
2727
setMockResponse.get(
28-
mitxonline.urls.enrollment.courseEnrollment(),
28+
mitxonline.urls.enrollment.enrollmentsListV2(),
2929
enrollments,
3030
)
3131

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/EnrollmentDisplay.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,22 @@ const sortEnrollments = (resources: DashboardCourse[]) => {
9191
const started: DashboardCourse[] = []
9292
const notStarted: DashboardCourse[] = []
9393
resources.forEach((resource) => {
94-
if (resource.enrollment?.status === EnrollmentStatus.Completed) {
95-
completed.push(resource)
96-
} else if (
97-
resource.run.endDate &&
98-
new Date(resource.run.endDate) < new Date()
99-
) {
100-
expired.push(resource)
101-
} else if (
102-
resource.run.startDate &&
103-
new Date(resource.run.startDate) < new Date()
104-
) {
105-
started.push(resource)
106-
} else {
107-
notStarted.push(resource)
94+
if (!resource.enrollment?.b2b_contract_id) {
95+
if (resource.enrollment?.status === EnrollmentStatus.Completed) {
96+
completed.push(resource)
97+
} else if (
98+
resource.run.endDate &&
99+
new Date(resource.run.endDate) < new Date()
100+
) {
101+
expired.push(resource)
102+
} else if (
103+
resource.run.startDate &&
104+
new Date(resource.run.startDate) < new Date()
105+
) {
106+
started.push(resource)
107+
} else {
108+
notStarted.push(resource)
109+
}
108110
}
109111
})
110112

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/test-utils.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { setMockResponse } from "../../../test-utils"
1313
import moment from "moment"
1414
import {
1515
ContractPage,
16-
CourseRunEnrollment,
16+
CourseRunEnrollmentRequestV2,
1717
CourseWithCourseRunsSerializerV2,
1818
IntegrationTypeEnum,
1919
OrganizationPage,
@@ -56,19 +56,22 @@ const dashboardCourse: PartialFactory<DashboardCourse> = (...overrides) => {
5656
const setupEnrollments = (includeExpired: boolean) => {
5757
const completed = [
5858
makeCourseEnrollment({
59+
b2b_contract_id: null, // Personal enrollment, not B2B
5960
run: { title: "C Course Ended" },
6061
grades: [makeGrade({ passed: true })],
6162
}),
6263
]
6364
const expired = includeExpired
6465
? [
6566
makeCourseEnrollment({
67+
b2b_contract_id: null, // Personal enrollment, not B2B
6668
run: {
6769
title: "A Course Ended",
6870
end_date: faker.date.past().toISOString(),
6971
},
7072
}),
7173
makeCourseEnrollment({
74+
b2b_contract_id: null, // Personal enrollment, not B2B
7275
run: {
7376
title: "B Course Ended",
7477
end_date: faker.date.past().toISOString(),
@@ -78,12 +81,14 @@ const setupEnrollments = (includeExpired: boolean) => {
7881
: []
7982
const started = [
8083
makeCourseEnrollment({
84+
b2b_contract_id: null, // Personal enrollment, not B2B
8185
run: {
8286
title: "A Course Started",
8387
start_date: faker.date.past().toISOString(),
8488
},
8589
}),
8690
makeCourseEnrollment({
91+
b2b_contract_id: null, // Personal enrollment, not B2B
8792
run: {
8893
title: "B Course Started",
8994
start_date: faker.date.past().toISOString(),
@@ -92,11 +97,13 @@ const setupEnrollments = (includeExpired: boolean) => {
9297
]
9398
const notStarted = [
9499
makeCourseEnrollment({
100+
b2b_contract_id: null, // Personal enrollment, not B2B
95101
run: {
96102
start_date: moment().add(1, "day").toISOString(), // Sooner first
97103
},
98104
}),
99105
makeCourseEnrollment({
106+
b2b_contract_id: null, // Personal enrollment, not B2B
100107
run: {
101108
start_date: moment().add(5, "day").toISOString(), // Later second
102109
},
@@ -192,14 +199,14 @@ const setupProgramsAndCourses = () => {
192199
const createEnrollmentsForContractRuns = (
193200
courses: CourseWithCourseRunsSerializerV2[],
194201
contractIds: number[],
195-
): CourseRunEnrollment[] => {
202+
): CourseRunEnrollmentRequestV2[] => {
196203
return courses.flatMap((course) =>
197204
course.courseruns
198205
.filter(
199206
(run) => run.b2b_contract && contractIds.includes(run.b2b_contract),
200207
)
201-
.map((run) =>
202-
factories.enrollment.courseEnrollment({
208+
.map((run) => ({
209+
...factories.enrollment.courseEnrollment({
203210
run: {
204211
id: run.id,
205212
course: {
@@ -209,7 +216,9 @@ const createEnrollmentsForContractRuns = (
209216
title: run.title,
210217
},
211218
}),
212-
),
219+
b2b_contract_id: run.b2b_contract ?? null,
220+
b2b_organization_id: null, // Will be set by individual tests as needed
221+
})),
213222
)
214223
}
215224

0 commit comments

Comments
 (0)