-
Notifications
You must be signed in to change notification settings - Fork 0
/
teacherMatch.js
114 lines (104 loc) · 3.38 KB
/
teacherMatch.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
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
require('dotenv').config()
const Canvas = require('@kth/canvas-api')
const ObjectsToCsv = require('objects-to-csv')
const csv = require('csvtojson')
const cliProgress = require('cli-progress')
const token = process.env.CANVAS_API_TOKEN
const url = process.env.CANVAS_API_URL
const canvas = Canvas(url, token)
const input = './members-short.csv'
const output = './matched-list.csv'
const canvasEmailList = []
const teachArr = []
const studArr = []
const designArr = []
const taArr = []
async function saveUniqueToCsv (arr, file) {
const uArr = arr.reduce((unique, o) => {
if (!unique.some(obj => obj.Name === o.Name && obj.Email === o.Email)) {
unique.push(o)
}
return unique
}, [])
const csv = new ObjectsToCsv(uArr)
await csv.toDisk(file)
console.log('-------------------------------------------------------')
console.log('Done printing contents of ' + file + '.')
console.log('-------------------------------------------------------')
console.log('')
}
async function getUserRoles (emails) {
const bar = new cliProgress.SingleBar({
hideCursor: true
}, cliProgress.Presets.shades_classic)
console.log('Matching Users... \n')
bar.start(emails.length, 0)
for (const email of emails) {
try {
const users = await canvas.list(`accounts/1/users?search_term=${email}`).toArray()
for (const user of users) {
if (user.login_id === email) {
try {
const enrols = await canvas.list(`users/${user.id}/enrollments`).toArray()
const userObj = {
Name: user.sortable_name,
Email: user.login_id,
Role: ''
}
if (enrols.some(e => e.type === 'TeacherEnrollment')) {
userObj.Role = 'Teacher'
teachArr.push(userObj)
bar.increment()
} else {
if (enrols.some(e => e.type === 'TaEnrollment')) {
userObj.Role = 'TA'
taArr.push(userObj)
bar.increment()
} else {
if (enrols.some(e => e.type === 'DesignerEnrollment')) {
userObj.Role = 'Designer'
designArr.push(userObj)
bar.increment()
} else {
userObj.Role = 'Student'
studArr.push(userObj)
bar.increment()
}
}
}
// bar.increment()
} catch (e) {
console.error(`Can't get enrol for ${user.id}. "${e}". Continuing...`)
bar.increment()
}
}
}
} catch (e) {
console.error(`Cannot get ID for ${email}. "${e}". Continuing...`)
bar.increment()
}
}
bar.stop()
}
async function isolateEmails (arr) {
arr.forEach(element => {
canvasEmailList.push(element.Email)
})
}
async function concatUsers (arr1, arr2, arr3, arr4) {
const cArr = arr1.concat(arr2, arr3, arr4)
return cArr
}
async function start () {
const jsonArray = await csv().fromFile(input)
await isolateEmails(jsonArray)
await getUserRoles(canvasEmailList, jsonArray)
const exportArr = await concatUsers(designArr, teachArr, taArr, studArr)
console.log(`\n= Results ======= \n
* Designers: ${designArr.length}, \n
* Teachers: ${teachArr.length}, \n
* TA's: ${taArr.length}, \n
* Students: ${studArr.length} \n`)
await saveUniqueToCsv(exportArr, output)
}
start()