/
index.js
185 lines (158 loc) · 4.46 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
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
var errors = [];
var response = [];
var i = 0;
const fileSelector = document.getElementById("file-selector");
fileSelector.addEventListener("change", (event) => {
const fileList = event.target.files;
console.log(`fileList`, fileList);
resetApp();
for (const file of fileList) {
getMetadata(file);
readFile(file).then((ntids) => {
// console.log(Date.now(), ntids);
progress.max = ntids.length;
(async () => {
for (const ntid of ntids) {
await doNtid(ntid);
}
})();
});
}
});
function resetApp() {
i = 0;
errors = [];
response = [];
progress_val.innerHTML = "";
response_val.innerHTML = "";
errors_val.innerHTML = "";
progress.max = 0;
progress.value = 0;
save_to_file.disabled = true;
}
function getMetadata(file) {
// Not supported in Safari for iOS.
const name = file.name ? file.name : "NOT SUPPORTED";
// Not supported in Firefox for Android or Opera for Android.
const type = file.type ? file.type : "NOT SUPPORTED";
// Unknown cross-browser support.
const size = file.size ? file.size : "NOT SUPPORTED";
console.log({ file, name, type, size });
}
function readFile(file) {
return new Promise((resolve, reject) => {
if (file.type && String(file.name).indexOf(".csv") === -1) {
markError(`File is not a csv, ${file.type}, ${file}`);
return;
}
const reader = new FileReader();
reader.addEventListener("load", (event) => {
let fileContent = event.target.result;
if (!fileContent) {
markError("Can not open the file");
reject();
}
if (String(fileContent).indexOf(",") !== -1) {
markError("Bad NTIDs file");
reject();
}
console.log(Date.now(), fileContent);
return resolve(String(fileContent).trim().split("\n"));
});
// load
reader.readAsText(file);
});
}
async function doNtid(ntid) {
progress_val.innerHTML = `${i}/${progress.max}`;
ntid = String(ntid).replace(/\s+/g, "");
if (ntid) {
try {
const auth = await getNtidToken(ntid);
const progress = await getProgress(auth.user.id, auth.token);
await saveNtidResult(progress, ntid, auth);
} catch (error) {
markError(error);
}
}
i++;
progress.value = i;
progress_val.innerHTML = `${i}/${progress.max}`;
if (i == progress.max) {
save_to_file.disabled = false;
}
}
async function saveNtidResult(progress, ntid, auth) {
if (progress && "classStatus" in progress) {
progress["user"] = auth.user;
console.log(`🚀 > processNtid > progress`, progress);
response.push(progress);
response_val.innerHTML = JSON.stringify(response);
} else {
throw ntid + " - " + JSON.stringify(progress);
}
}
async function getNtidToken(ntid) {
const response = await fetch(
"https://recommend-trng2.gst.sprint.com/AuthenticationController/api/auth",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ loginId: ntid, password: ntid }), // body data type must match "Content-Type" header
}
);
const auth = response.json(); // parses JSON response into native JavaScript objects
if (
!auth ||
!("token" in auth) ||
!("user" in auth) ||
!("id" in auth.user)
) {
console.error(auth);
throw "Invalid NTID: " + ntid;
}
console.log("AUTH:", auth);
return auth;
}
async function getProgress(studentId, token) {
const response = await fetch(
"https://recommend-trng2.gst.sprint.com/UserController/getStudentProgressByClass",
{
method: "POST",
headers: new Headers({
Authorization: "Bearer " + token,
"Content-Type": "application/json",
}),
body: JSON.stringify({ studentId }),
}
);
return response.json();
}
function download() {
var data = response_val.innerHTML;
var file = new Blob([data], { type: "text/json" });
var filename =
"sprint-ntt-" + new Date().toISOString().split("T").shift() + ".json";
if (window.navigator.msSaveOrOpenBlob)
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else {
// Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
});
}
}
function markError(error) {
errors.push(error);
errors_val.innerHTML = JSON.stringify(errors);
}