forked from anchetaWern/google-calendar-twilio
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.js
82 lines (60 loc) · 1.77 KB
/
cache.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
var google = require('./common/google');
var connection = require('./common/db');
var time = require('./common/time');
var CronJob = require('cron').CronJob;
function addAppointment(event_id, summary, start, end){
connection.db.query(
"INSERT INTO appointments SET id = ?, summary = ?, datetime_start = ?, datetime_end = ?, notified = 0",
[event_id, summary, start, end],
function(err, rows, fields){
if(!err){
console.log('added!');
}else{
console.log('error adding to table');
}
}
);
}
function getEvents(err, response){
console.log('response');
console.log(response);
if(err){
console.log('The API returned an error: ' + err);
}
var events = response.items;
if(events.length == 0){
console.log('No upcoming events found.');
}else{
console.log('Upcoming 10 events:');
for(var i = 0; i < events.length; i++){
var event = events[i];
var event_id = event.id;
var summary = event.summary;
var start = event.start.dateTime || event.start.date;
var end = event.end.dateTime || event.end.date;
addAppointment(event_id, summary, start, end);
}
}
}
function cache(){
var current_datetime = time.moment().toISOString();
google.calendar.events.list({
auth: google.oauth2Client,
calendarId: 'primary',
timeMin: current_datetime,
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, getEvents);
}
connection.db.query('SELECT access_token FROM users WHERE id = 1', function(error, results, fields){
if(!error){
var tokens = JSON.parse(results[0].access_token);
google.oauth2Client.setCredentials({
'access_token': tokens.access_token,
'refresh_token': tokens.refresh_token
});
new CronJob('0 0 * * *', cache, null, true, time.config.timezone);
//cache(); //for testing
}
});