-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (53 loc) · 1.57 KB
/
index.js
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
'use strict';
const bent = require('bent');
const learnerID = ''; //add learn ID here
const apiToken = ''; //add token here
const endpoint = ''; //add baseurl here
const headers = {
'Authorization': `Bearer ${apiToken}`
};
async function getUser (userId) {
const path = `/incoming/v2/users/${userId}`;
return bent(endpoint, 'json', 'GET', headers)(path);
}
async function getAllCourses () {
const path = '/incoming/v2/content';
let courses = [];
async function* getCoursePage() {
let page;
while(true) {
const response = await bent(endpoint, 'json', 'GET', headers)(`${path}?cursor=${page}`);
if(!response.pageInfo.hasMore) {
return;
}
yield response.contentItems;
page = response.pageInfo.cursor;
}
}
for await (let coursePage of getCoursePage()) {
courses = courses.concat(coursePage);
}
return courses;
}
async function updateUser (user, slugs) {
const path = `/incoming/v2/users/${learnerID}`;
const putBody = {
upsert: true,
email: user.email,
replaceCourseAccess: true,
courseSlugs: slugs
};
return bent(endpoint, 'PUT', headers)(path, putBody);
}
async function removeCourse () {
const userDetails = await getUser(learnerID);
const purchasedCourses = userDetails.purchasedCourses;
const allCourses = await getAllCourses();
const slugs = purchasedCourses.map(course => {
return allCourses.find(d => d.id === course.courseId).slug;
});
// remove one
slugs.pop();
return (await updateUser(userDetails, slugs)).text();
}
removeCourse().then(console.log).catch(console.log);