This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
googleApiFunctions.js
150 lines (138 loc) · 3.76 KB
/
googleApiFunctions.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
/*
request functions for the google calendar api
these all require an auth object
in our case, the auth object is our jwtclient(service account)
general format is function(calendarId,authInput,[parameters],callback)
*/
//checks if busy at certain time, returns 'busy' or 'notBusy'
const {google} = require('googleapis');
const calendar = google.calendar('v3');
function createCalendar(summary,authInput,callback) {
// Create calendar with given name and share it with the default
let calendarId = null;
calendar.calendars.insert({
auth:authInput,
resource: {
summary:summary,
timeZone: 'Europe/London'
}
},
function(err,res){
if(err){
callback('error');
return 0;
}
let calendarId = res.data.id;
calendar.acl.insert({
auth:authInput,
calendarId:calendarId,
resource: {
role: 'reader',
scope: {
type: 'user',
value: 'group6.se.durham@gmail.com'
},
}
},
function(err,res){
if(err){
callback('error');
return 0;
}
callback(false,calendarId);
});
});
}
//deletes a calendar by calendar Id
function deleteCalendar(calendarId,authInput,callback){
calendar.calendars.delete({
auth:authInput,
calendarId: calendarId
},function(err){
if (err){
callback(err);
return;
}
callback(false);
}
);
}
function addEvent(calendarId,authInput,startTime,endTime,eventId,bookingDescription,callback){
calendar.events.insert({
auth: authInput,
calendarId: calendarId,
resource:{
start:{dateTime:startTime,timeZone:'Europe/London'},
end: {dateTime:endTime,timeZone:'Europe/London'},
summary:bookingDescription,
id: eventId,
}
},function(err,response){
if(err){
callback(err);
}
else{
callback(false);
}
});
}
//update an event
function updateEvent(calendarId,authInput,startTime,endTime,summary,description,eventId,callback){
calendar.events.update({
auth: authInput,
calendarId: calendarId,
eventId: eventId,
resource:{
start:{dateTime:startTime,timeZone:'Europe/London'},
end: {dateTime:endTime,timeZone:'Europe/London'},
description:description,
summary:summary
}
},function(err,response){
if(err){
callback(err);
}
else{
callback(false);
}
});
}
//deletes event with given ID
function deleteEvent(calendarId, authInput, eventId, callback){
calendar.events.delete({
auth:authInput,
calendarId:calendarId,
eventId:eventId
},function (err, response) {
if (err) {
if (typeof callback === "function") {
callback(err);
}
}
else{
if (typeof callback === "function") {
callback(false);
}
}
});
}
function getEvent(calendarId,authInput,eventId,callback) {
calendar.events.get({
auth:authInput,
calendarId:calendarId,
eventId:eventId
}, function(err,response){
if(err){
callback(err);
}
else{
callback(false,response);
}
});
}
module.exports.deleteCalendar = deleteCalendar;
module.exports.createCalendar = createCalendar;
module.exports.addEvent = addEvent;
module.exports.deleteEvent = deleteEvent;
module.exports.getEvent = getEvent;
module.exports.updateEvent = updateEvent;