-
Notifications
You must be signed in to change notification settings - Fork 3
/
services.js
70 lines (69 loc) · 2.86 KB
/
services.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
const { google } = require('googleapis');
module.exports = {
/**
* Get the next 10 events on the test calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
getTests: (auth, serverRes) => {
const calendar = google.calendar({ version: 'v3', auth });
// Fetching all of the user's calendars
calendar.calendarList.list((err, res) => {
// Här får jag inget tillbaks: Error: No access, refresh token or API key is set.
if (err) console.log(err);
const calendars = res.data.items;
calendars.forEach(cal => {
// Looking for the calendar with information about my tests
if (cal.summary == "Prov") {
// List the 10 next events in that calendar
calendar.events.list({
calendarId: cal.id,
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
serverRes.send(events);
} else {
serverRes.sendStatus(404)
}
});
}
});
});
},
/**
* Get the next 10 events on the assignments calendar.
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
getAssignments: (auth, serverRes) => {
const calendar = google.calendar({ version: 'v3', auth });
// Fetching all of the user's calendars
calendar.calendarList.list((err, res) => {
const calendars = res.data.items;
calendars.forEach(cal => {
// Looking for the calendar with information about my assignments
if (cal.summary == "Uppgift") {
// List the 10 next events in that calendar
calendar.events.list({
calendarId: cal.id,
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
serverRes.send(events)
} else {
serverRes.sendStatus(404)
}
});
}
});
});
}
}