-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
149 lines (133 loc) · 4.32 KB
/
calendar.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
// Google OAuth Configuration
var googleConfig = {
clientID: '868837249571-s0tkgajk3e55clddt3r4gv2rrnedqamm.apps.googleusercontent.com',
clientSecret: 'momI8AqT7Lf-wjkZ82BHxfoj',
calendarId: '[email protected]',
redirectURL: 'http://localhost:3000/auth'
};
// Dependency setup
var express = require('express'),
moment = require('moment'),
google = require('googleapis');
// Initialization
var app = express(),
calendar = google.calendar('v3');
oAuthClient = new google.auth.OAuth2(googleConfig.clientID, googleConfig.clientSecret, googleConfig.redirectURL),
authed = false;
// Response for localhost:3000/
var gCal = {};
gCal.getFreeTimes = function(callback) {
// If we're not authenticated, fire off the OAuth flow
if (!authed) {
// Generate an OAuth URL and redirect there
var url = oAuthClient.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/calendar.readonly'
});
res.redirect(url);
} else {
var today = moment();
today = today.toISOString();
var nextWeek = moment().add(7, 'd');
nextWeek = nextWeek.toISOString();
calendar.events.list({
calendarId: googleConfig.calendarId,
maxResuts: 20,
timeMin: today,
timeMax: nextWeek,
auth: oAuthClient
}, function (err, events) {
if(err) {
console.log("error fetching events");
console.log(err);
} else {
console.log("Successfully fetched events");
console.log(events);
for(var i = 0; i < events.items.length; i++) {
console.log([events.items[i].start.dateTime, events.items[i].end.dateTime]);
};
var slotStartTimes = [];
for(var i = today.slice(8, 10); i <= nextWeek.slice(8, 10); i++) {
console.log("Day " + i);
slotStartTimes.push(findSlotsAvailable(events, i));
if(slotStartTimes.length == 0) {
slotStartTimes.push(17);
}
}
return callback(slotStartTimes);
}
});
}
}
function findSlotsAvailable(events, currentDay) {
// var flag = false;
var slots = {};
for(var i = 0; i < events.items.length; i++) {
if(events.items[i].start.dateTime.slice(8, 10) == currentDay) {
flag = true;
for(var j = 6; j < 24; j++) {
if((j + 3 <= events.items[i].start.dateTime.slice(11, 13)) || (j >= events.items[i].end.dateTime.slice(11, 13))) {
console.log(j + "-" + (j + 3) + " available");
slots.push(j);
} else {
console.log(j + "-" + (j + 3) + " not available");
}
}
}
};
return slots;
// return flag;
}
gCal.insertEvent = function(dateName, location, startTime, endTime, callback) { // starttime and endtime are iso strings
var date_event = {
'summary': 'Tinder Date: ' + dateName,
'location': location,
'start': {
'dateTime': startTime, //get from list of available times, selected from user
'timeZone': 'America/New_York',
},
'end': {
'dateTime': endTime, //same as above (slot time is say 3 hours??)
'timeZone': 'America/New_York',
}
};
console.log("AUTH CLIENT!!!!");
console.log(oAuthClient);
// var googleCalendar = new gcal.googleCalendar();
calendar.events.insert({
auth: oAuthClient,
calendarId: googleConfig.calendarId,
resource: date_event,
}, function(err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ');
console.log(err);
return;
}
console.log('Event created: %s', event.htmlLink);
return event;
});
}
// Return point for oAuth flow, should match googleConfig.redirectURL
gCal.auth = function(callback){
var code = req.param('code');
if(code) {
// Get an access token based on our OAuth code
oAuthClient.getToken(code, function(err, tokens) {
if (err) {
console.log('Error authenticating')
console.log(err);
} else {
console.log('Successfully authenticated');
console.log(tokens);
// Store our credentials and redirect back to our main page
oAuthClient.setCredentials(tokens);
authed = true;
console.log("redirecting to /calendar");
res.redirect('/calendar');
// parametrize this to pass in where to redirect
}
});
}
};
module.exports = gCal;