-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrades.js
127 lines (122 loc) · 6.14 KB
/
grades.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
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
const async = require('async'),
configs = require('../configurations'),
helpers = require('../helpers'),
request = require('superagent'),
express = require('express'),
router = express.Router();
module.exports = function (appContext) {
/* GET /finalgrades
* Returns final grades with user information for all users in the given org unit based on the orgUnitId
* passed in through the query parameters.
*/
router.get('/finalgrades', function (req, res) {
const orgUnitId = req.query.orgUnitId;
const classlistApiPath = '/d2l/api/le/1.12/' + orgUnitId + '/classlist/';
const accessToken = req.cookies[configs.cookieName].accessToken;
let userId = '';
let userKey = '';
let userContext = null;
if (accessToken) {
console.log('Attempting to retrieve final grades using OAuth 2.0 authentication.');
const classlistRoute = helpers.createUrl(classlistApiPath, configs);
request
.get( classlistRoute )
.set('Authorization', `Bearer ${accessToken}`)
.end(function(error, response) {
if (error) {
console.log('Error calling the who am I route', error);
return res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
return res.status(response.statusCode).send(response.error);
} else {
const users = JSON.parse(response.text);
fetchFinalGrades(orgUnitId, users, accessToken, userContext, res);
}
});
} else {
console.log('Attempting to retrieve final grades using ID Key Authentication.');
userId = req.cookies[configs.cookieName].userId;
userKey = req.cookies[configs.cookieName].userKey;
userContext = appContext.createUserContextWithValues(configs.instanceScheme + '//' + configs.instanceUrl, configs.instancePort, userId, userKey);
const apiCallUrl = userContext.createAuthenticatedUrl(classlistApiPath, 'GET');
request
.get( apiCallUrl )
.end(function(error, response) {
if (error) {
console.log('Error calling the who am I route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
const users = JSON.parse(response.text);
fetchFinalGrades(orgUnitId, users, accessToken, userContext, res);
}
});
}
});
/* function fetchFinalGrades
* This function is used to asynchronously call the final grades route for each user.
*/
function fetchFinalGrades(orgUnitId, users, accessToken, userContext, res) {
let finalGradeBlocks = [];
const gradesApiPath = '/d2l/api/le/1.12/' + orgUnitId + '/grades/final/values/';
async.each(
users,
function(user, callback){
if (accessToken) {
const gradesRoute = helpers.createUrl(gradesApiPath + user.Identifier, configs);
request
.get( gradesRoute )
.set('Authorization', `Bearer ${accessToken}`)
.end(function(error, response) {
if (error) {
console.log('Error calling the Final Grades route', error);
callback({message: 'Error calling the Final Grades Route'});
} else if(response.statusCode !== 200) {
if(response.statusCode === 404){
finalGradeBlocks.push({ FinalGrade: {}, User: user });
callback(null);
} else {
callback(response.error);
}
} else {
const finalGradeBlock = JSON.parse(response.text);
finalGradeBlocks.push({ FinalGrade: finalGradeBlock, User: user });
callback(null);
}
});
} else {
const gradesRoute = userContext.createAuthenticatedUrl(gradesApiPath + user.Identifier, 'GET');
request
.get( gradesRoute )
.end(function(error, response) {
if (error) {
console.log('Error calling the Final Grades route', error);
callback({message: 'Error calling the Final Grades Route'});
} else if(response.statusCode !== 200) {
if(response.statusCode === 404){
finalGradeBlocks.push({ FinalGrade: {}, User: user });
callback(null);
} else {
callback(response.error);
}
} else {
const finalGradeBlock = JSON.parse(response.text);
finalGradeBlocks.push({ FinalGrade: finalGradeBlock, User: user });
callback(null);
}
});
}
},
function(err) {
if (err) {
res.send({ error: err });
} else {
res.send(JSON.stringify(finalGradeBlocks));
}
}
);
}
return router;
};