generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
187 lines (165 loc) · 5.73 KB
/
api.js
File metadata and controls
187 lines (165 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { getAuthenticatedHttpClient, getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { getConfig, camelCaseObject } from '@edx/frontend-platform';
export async function fetchLearningPaths() {
const client = getAuthenticatedHttpClient();
// FIXME: This API has pagination.
const response = await client.get(`${getConfig().LMS_BASE_URL}/api/learning_paths/v1/learning-paths/`);
const data = response.data.results || response.data;
return camelCaseObject(data);
}
export async function fetchLearningPathDetail(key) {
const client = getAuthenticatedHttpClient();
const response = await client.get(`${getConfig().LMS_BASE_URL}/api/learning_paths/v1/learning-paths/${key}/`);
return camelCaseObject(response.data);
}
export async function fetchLearnerDashboard() {
const response = await getAuthenticatedHttpClient().get(`${getConfig().LMS_BASE_URL}/api/learner_home/init/`);
const courses = response.data.courses || [];
const emailConfirmation = response.data.emailConfirmation || {};
const enterpriseDashboard = response.data.enterpriseDashboard || {};
const processedCourses = camelCaseObject(courses.map(course => {
const { courseRun, course: courseInfo, enrollment } = course;
return {
id: courseRun.courseId,
number: courseRun.courseId.split(':')[1].split('+')[1],
org: courseRun.courseId.split(':')[1].split('+')[0],
run: courseRun.courseId.split(':')[1].split('+')[2],
name: courseInfo.courseName,
shortDescription: null,
endDate: courseRun.endDate,
startDate: courseRun.startDate,
courseImageAssetPath: courseInfo.bannerImgSrc,
isStarted: courseRun.isStarted,
isArchived: courseRun.isArchived,
enrollmentDate: enrollment?.lastEnrolled || null,
};
}));
return {
courses: processedCourses,
emailConfirmation: camelCaseObject(emailConfirmation),
enterpriseDashboard: camelCaseObject(enterpriseDashboard),
};
}
export async function fetchCourseDetails(courseId) {
const response = await getAuthenticatedHttpClient().get(
`${getConfig().LMS_BASE_URL}/api/courses/v1/courses/${encodeURIComponent(courseId)}/`,
);
const { data } = response;
return camelCaseObject({
id: data.course_id,
number: data.number,
org: data.org,
run: data.id.split(':')[1].split('+')[2],
name: data.name,
shortDescription: data.short_description,
endDate: data.end,
startDate: data.start,
courseImageAssetPath: data.media.course_image.uri,
description: data.overview,
selfPaced: data.pacing === 'self',
duration: data.effort,
});
}
export async function fetchAllCourseCompletions() {
const { username } = getAuthenticatedUser();
const client = getAuthenticatedHttpClient();
let allResults = [];
let nextUrl = `${getConfig().LMS_BASE_URL}/completion-aggregator/v1/course/?username=${username}&page_size=10000&include_optional=true`;
while (nextUrl) {
// eslint-disable-next-line no-await-in-loop
const response = await client.get(nextUrl);
const results = response.data.results || [];
allResults = [...allResults, ...results];
nextUrl = response.data.pagination?.next ? response.data.pagination.next : null;
}
return camelCaseObject(allResults.map(item => ({
course_key: item.course_key,
completion: item.completion,
optional_completion: item.optional_completion,
})));
}
export async function enrollInLearningPath(learningPathId) {
const client = getAuthenticatedHttpClient();
try {
const response = await client.post(
`${getConfig().LMS_BASE_URL}/api/learning_paths/v1/${learningPathId}/enrollments/`,
);
return {
success: true,
status: response.status,
};
} catch (error) {
return {
success: false,
status: error.response?.status,
error,
};
}
}
export async function enrollInCourse(learningPathId, courseId) {
const client = getAuthenticatedHttpClient();
try {
const response = await client.post(
`${getConfig().LMS_BASE_URL}/api/learning_paths/v1/${learningPathId}/enrollments/${courseId}/`,
);
return {
success: true,
status: response.status,
};
} catch (error) {
return {
success: false,
status: error.response?.status,
error,
};
}
}
export async function fetchCourseEnrollmentStatus(courseId) {
const client = getAuthenticatedHttpClient();
try {
const response = await client.get(
`${getConfig().LMS_BASE_URL}/api/enrollment/v1/enrollment/${courseId}`,
);
return {
isEnrolled: response.data?.is_active === true,
data: camelCaseObject(response.data),
};
} catch (error) {
// Handle API errors - they indicate the user is not enrolled.
return {
isEnrolled: false,
error,
};
}
}
export async function fetchOrganizations() {
const client = getAuthenticatedHttpClient();
let allResults = [];
let nextUrl = `${getConfig().LMS_BASE_URL}/api/organizations/v0/organizations/?page_size=100`;
while (nextUrl) {
// eslint-disable-next-line no-await-in-loop
const response = await client.get(nextUrl);
const results = response.data.results || [];
allResults = [...allResults, ...results];
nextUrl = response.data.next || null;
}
return camelCaseObject(allResults.map(org => ({
shortName: org.short_name,
name: org.name,
logo: org.logo,
})));
}
export async function fetchCredentialConfiguration(learningContextKey) {
const client = getAuthenticatedHttpClient();
try {
const response = await client.get(
`${getConfig().LMS_BASE_URL}/api/learning_credentials/v1/configured/${encodeURIComponent(learningContextKey)}/`,
);
return camelCaseObject(response.data);
} catch (error) {
return {
hasCredentials: false,
credentialCount: 0,
};
}
}