-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
389 lines (364 loc) · 18.2 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
console.log("Starting...");
const fs = require('fs');
const readline = require('readline');
const stc = require('string-to-color');
const nearestColor = require('nearest-color').from({
1: "#7986cb",
2: "#33b679",
3: "#8e24aa",
4: "#e67c73",
5: "#f6c026",
6: "#f5511d",
7: "#039be5",
8: "#616161",
9: "#3f51b5",
10: "#0b8043",
11: "#d60000"
});
const {google} = require('googleapis');
const zermeloPrivateAPI = require('./zermelo-private-api.js');
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const SCOPES_ADMIN = SCOPES.concat(['https://www.googleapis.com/auth/admin.directory.user.readonly']);
const TOKEN_PATH = __dirname + '/token.json';
const SERVICE_JSON = __dirname + '/service.json';
const CREDENTIALS_PATH = __dirname + '/credentials.json';
const CREDENTIALS_Z_PATH = __dirname + '/credentials-z.json';
let servicePrivateKey = require('./service.json');
// from https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES_ADMIN
});
console.log("Authorize this app by visiting this URL: ", authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter the code from that page here: ", function(code) {
rl.close();
oAuth2Client.getToken(code, function(err, token) {
if (err) return console.error("Error retrieving access token", err);
oAuth2Client.setCredentials(token);
fs.writeFile(TOKEN_PATH, JSON.stringify(token), function(err) {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function updateCalendar(auth) {
const calendar = google.calendar({version: 'v3', auth});
console.log("Creating Google Admin SDK Service...");
const adminService = google.admin({ version: "directory_v1", auth });
console.log("Retrieving last known Zermelo lessons...");
let zermeloCredentials = JSON.parse(fs.readFileSync(CREDENTIALS_Z_PATH));
zermeloPrivateAPI.setSchool(zermeloCredentials.school);
zermeloPrivateAPI.setBranchOfSchool(zermeloCredentials.branch);
zermeloPrivateAPI.setApiToken(zermeloCredentials.token);
zermeloPrivateAPI.getLastUpcomingAppointments().then(function(lastSchedule) {
console.log("Retrieving updated Zermelo lessons...");
zermeloPrivateAPI.getUpcomingAppointments()
.then(function(schedule) {
console.log("Lessons fetched");
console.log("Comparing schedules and finding deleted lessons...");
var lastScheduleLength = lastSchedule.length;
var allOldInstances = [];
var allNewInstances = [];
for (var i = 0; i < lastSchedule.length; i++) {
allOldInstances.push(lastSchedule[i]["appointmentInstance"]);
}
for (var i = 0; i < schedule.length; i++) {
allNewInstances.push(schedule[i]["appointmentInstance"]);
}
var deletedInstances = allOldInstances.diff(allNewInstances);
var deletedSchedule = [];
for (var i = 0; i < deletedInstances.length; i++) {
for (var j = 0; j < lastScheduleLength; j++) {
if (lastSchedule[j]["appointmentInstance"] == deletedInstances[i]) {
lastSchedule[j]["cancelled"] = true;
deletedSchedule.push(lastSchedule[j]);
break;
}
}
}
if (deletedSchedule.length > 0) {
console.log("Found deleted lessons. Adding them back to the schedule, but now cancelled...");
schedule.concat(deletedSchedule);
}
else {
console.log("No lessons to delete have been found.");
}
let lessonCount = schedule.length;
// lessonCount = 3; /* for debugging */
preAddToCalender(adminService, auth, calendar, schedule, 0, lessonCount);
}).catch(function(err) {
console.error(err);
});
});
}
function iterateNextLesson(adminService, auth, calendar, schedule, i, lessonCount) {
i++;
if (i < lessonCount) {
console.log("Another round incoming");
setTimeout(function() {
console.log("Running now");
preAddToCalender(adminService, auth, calendar, schedule, i, lessonCount);
}, 250);
}
}
function addToCalendar(userAuth, calendar, lesson, noMeeting) {
return new Promise(function(resolve, reject) {
calendar.events.get({
auth: userAuth,
calendarId: 'primary',
eventId: 'damstederooster'+lesson['appointmentInstance']
}, function(err, res) {
if (err) {
// event does not exist yet, create it if the lesson was not cancelled
if (!lesson["cancelled"]) {
let calEvent = {
id: 'damstederooster'+lesson['appointmentInstance'],
summary: lesson["subjects"].join(", ").toUpperCase() + " van " + lesson["teachers"].join(", ").toUpperCase(),
description: "<b>" + lesson["subjects"].join(", ").toUpperCase() + " van " + lesson["teachers"].join(", ").toUpperCase() + " aan " + lesson["groups"].join(", ").toUpperCase() + "</b>" + (lesson["changeDescription"] ? "<br><br>"+lesson["changeDescription"] : ""),
location: lesson["locations"].join(", "),
colorId: parseInt(nearestColor(stc(lesson["subjects"][0]))["name"]),
start: {
dateTime: new Date(lesson["start"] * 1000).toISOString(),
timeZone: "Europe/Amsterdam"
},
end: {
dateTime: new Date(lesson["end"] * 1000).toISOString(),
timeZone: "Europe/Amsterdam"
},
attendees: [
{
email: "[email protected]",
displayName: "Systeembeheer Damstede",
optional: true,
responseStatus: "declined",
comment: "Systeembeheer neemt geen deel aan de les, maar is wel toegevoegd om eventueel de les te kunnen ondersteunen."
}
],
guestsCanInviteOthers: false,
guestsCanModify: false,
guestsCanSeeOtherGuests: true,
reminders: {
useDefault: false,
overrides: [ ]
},
source: {
title: "Zermelo Rooster",
url: "https://damstedelyceum.zportal.nl/"
},
status: (lesson["cancelled"] ? "cancelled" : "confirmed"),
};
for (let d = 0; d < lesson["teachers"].length; d++) {
calEvent.attendees.push({
email: lesson["teachers"][d] + "@damstede.eu"
});
}
for (let d = 0; d < lesson["students"].length; d++) {
calEvent.attendees.push({
email: "n" + lesson["students"][d] + "@damstede.eu"
});
}
calendar.events.insert({
auth: userAuth,
calendarId: 'primary',
resource: calEvent
}, function(errInsert, newEvent) {
if (errInsert) {
reject("There was an error contacting the calendar service", errInsert);
return;
}
if (noMeeting !== true) {
calendar.events.patch({
auth: userAuth,
calendarId: 'primary',
eventId: 'damstederooster'+lesson['appointmentInstance'],
resource: {
conferenceData: {
createRequest: {
requestId: new Date().getFullYear() + lesson['appointmentInstance']
}
}
},
sendNotifications: false,
conferenceDataVersion: 1
}, function(errInsert, newEvent)
{
console.log("Lesson added");
resolve();
});
}
else {
console.log("Lesson added");
resolve();
}
});
}
else {
console.log("Not adding the lesson after all, as it seems to have been cancelled");
resolve();
}
}
else {
// event exists, update it (if needed)
console.log("Lesson already exists in the schedule! Updating info (if needed)...");
if (lesson["teachers"].length > 0 && lesson["students"].length > 0) {
if (lesson["cancelled"]) {
calendar.events.delete({
auth: userAuth,
calendarId: 'primary',
eventId: 'damstederooster'+lesson['appointmentInstance'],
sendUpdates: "all"
});
console.log("Lesson deleted, as it was cancelled");
}
else {
let calEvent = {
status: "confirmed",
summary: lesson["subjects"].join(", ").toUpperCase() + " van " + lesson["teachers"].join(", ").toUpperCase(),
description: "<b>" + lesson["subjects"].join(", ").toUpperCase() + " van " + lesson["teachers"].join(", ").toUpperCase() + " aan " + lesson["groups"].join(", ").toUpperCase() + "</b>" + (lesson["changeDescription"] ? "<br><br>"+lesson["changeDescription"] : ""),
location: lesson["locations"].join(", "),
colorId: parseInt(nearestColor(stc(lesson["subjects"][0]))["name"]),
start: {
dateTime: new Date(lesson["start"] * 1000).toISOString(),
timeZone: "Europe/Amsterdam"
},
end: {
dateTime: new Date(lesson["end"] * 1000).toISOString(),
timeZone: "Europe/Amsterdam"
},
attendees: [
{
email: "[email protected]",
displayName: "Systeembeheer Damstede",
optional: true,
responseStatus: "declined",
comment: "Systeembeheer neemt geen deel aan de les, maar is wel toegevoegd om eventueel de les te kunnen ondersteunen."
}
]
};
for (let d = 0; d < lesson["teachers"].length; d++) {
calEvent.attendees.push({
email: lesson["teachers"][d] + "@damstede.eu"
});
}
for (let d = 0; d < lesson["students"].length; d++) {
calEvent.attendees.push({
email: "n" + lesson["students"][d] + "@damstede.eu"
});
}
calendar.events.patch({
auth: userAuth,
calendarId: 'primary',
eventId: 'damstederooster'+lesson['appointmentInstance'],
resource: calEvent,
conferenceDataVersion: 1
});
console.log("Lesson modified");
}
}
else {
calendar.events.patch({
auth: userAuth,
calendarId: 'primary',
eventId: 'damstederooster'+lesson['appointmentInstance'],
resource: {
status: "cancelled"
}
});
console.log("Lesson modified (set to cancelled)");
}
resolve();
}
});
});
}
function preAddToCalender(adminService, auth, calendar, schedule, i, lessonCount) {
console.log("Adding " + i + " to calendar... ("+(lessonCount - i - 1) + " remaining)");
console.log(schedule[i]["subjects"].join(", ") + " for " + schedule[i]["groups"].join(", ") + " by " + schedule[i]["teachers"].join(", ") + " in " + schedule[i]["locations"].join(", ") + ". Cancelled=" + schedule[i]["cancelled"].toString());
if (schedule[i]["teachers"].length > 0 && schedule[i]["students"].length > 0) {
console.log("Retrieving account for "+schedule[i]["teachers"][0]+"...");
adminService.users.get({
userKey: schedule[i]["teachers"][0]+"@damstede.eu"
}, function(err, res) {
if (err) {
console.warn("User does not exist. The teacher code might not have been added as an alias to an account in your organization.");
console.log("Adding lesson to the admin calendar, without a meeting attached...");
addToCalendar(auth, calendar, schedule[i], true)
.then(function() {
// logging happens in addToCalendar()
})
.catch(function(errMsg, err) {
console.error(errMsg, err);
})
.finally(function() {
iterateNextLesson(adminService, auth, calendar, schedule, i, lessonCount);
});
}
else {
for (let em = 0; em < res.data.emails.length; em++) {
if (res.data.emails[em]["primary"] === true) {
console.log("Logging in as "+res.data.emails[em]["address"]+"...");
let jwtClient = new google.auth.JWT(servicePrivateKey.client_email, SERVICE_JSON, servicePrivateKey.private_key, SCOPES, res.data.emails[em]["address"]);
jwtClient.authorize(function(err, res) {
if (err) {
console.error(err);
console.log("Could not login as user. Adding lesson to the admin calendar instead...");
addToCalendar(auth, calendar, schedule[i])
.then(function() {
// logging happens in addToCalendar()
})
.catch(function(errMsg, err) {
console.error(errMsg, err);
})
.finally(function() {
iterateNextLesson(adminService, auth, calendar, schedule, i, lessonCount);
});
}
else {
console.log("Adding lesson to user's calendar...");
addToCalendar(jwtClient, calendar, schedule[i])
.then(function() {
// logging happens in addToCalendar()
})
.catch(function(errMsg, err) {
console.error(errMsg, err);
})
.finally(function() {
iterateNextLesson(adminService, auth, calendar, schedule, i, lessonCount);
});
}
});
return;
}
}
}
});
return;
}
else {
console.log("Lesson does not contain students or teachers!");
iterateNextLesson(adminService, auth, calendar, schedule, i, lessonCount);
}
}
fs.readFile(CREDENTIALS_PATH, function(err, content) {
if (err) return console.log('Error loading client secret file: ', err);
authorize(JSON.parse(content), updateCalendar);
});