-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (69 loc) · 2.19 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
var fs = require("fs");
var moment = require("moment");
var google = require("googleapis");
var googleAuth = require("google-auth-library");
var privatekey = require("./private_key.json");
class GCalHelper {
constructor() {
this.jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
["https://www.googleapis.com/auth/calendar"]
);
//authenticate request
this.jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
} else {
// console.log("Successfully connected!");
}
});
}
addEvents(event, callback) {
let calendar = google.calendar("v3");
calendar.events.insert(
{
auth: this.jwtClient,
calendarId: "primary",
resource: event
},
function(err, response) {
if (err) {
console.log("The API returned an error: " + err);
return;
}
callback(response);
}
);
}
/**
* Return list of events between given search range.
*
* @param {Date} start Start of search range.
* @param {Date} end End of search range.
* @param {Function} callback Callback function.
*/
listEvents(start, end, callback) {
if (typeof start === "undefined") start = moment().startOf("month");
if (typeof end === "undefined") end = moment().endOf("month");
let calendar = google.calendar("v3");
calendar.events.list(
{
auth: this.jwtClient,
calendarId: "primary",
timeMin: moment(start).toDate(),
timeMax: moment(end).toDate()
},
function(err, response) {
if (err) {
console.log("The API returned an error: " + err);
}
var events = response.data.items;
if (typeof callback === "function") callback(events);
}
);
}
}
module.exports = GCalHelper;