forked from Lalla22/You-Can-Do-It-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (58 loc) · 2.11 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
const { google } = require('googleapis')
const { OAuth2 } = google.auth
const oAuth2Client = new OAuth2('725875135322-ntvidf9jg41l9onofhqo593poaj22p6m.apps.googleusercontent.com', '8Xk0mAg8cyEXQIIPa-JNBIo8'
)
oAuth2Client.setCredentials({
refresh_token: '1//04tbg88IcQwP6CgYIARAAGAQSNwF-L9IrfiSiGsVX1dECNHuA1QJcJ0vachpvhjPrhHyrameHpQJbly001jWyY3OZVN7Z5mmuVuA',
})
const calendar = google.calendar({ version: 'v3', auth: oAuth2Client })
// Create a new event start date instance for temp uses in our calendar.
const eventStartTime = new Date()
eventStartTime.setDate(eventStartTime.getDay() + 2)
// Create a new event end date instance for temp uses in our calendar.
const eventEndTime = new Date()
eventEndTime.setDate(eventEndTime.getDay() + 4)
eventEndTime.setMinutes(eventEndTime.getMinutes() + 45)
//dummy test event
const event = {
summary: 'Task to be done',
location: '515 Malcolm X Boulevard (135th St and, Malcolm X Blvd, New York, NY 10037',
description: 'Daily Reminder to do task.',
colorId: 1,
start: {
dateTime: eventStartTime,
timeZone: 'America/New York',
},
end: {
dateTime: eventEndTime,
timeZone: 'America/New York',
},
}
calendar.freebusy.query(
{
resource: {
timeMin: eventStartTime,
timeMax: eventEndTime,
timeZone: 'America/New York',
items: [{ id: 'primary' }],
},
},
(err, res) => {
if (err) return console.error('Free Busy Query Error: ', err)
const eventArr = res.data.calendars.primary.busy
// Check if event array is empty which means we are not busy
if (eventArr.length === 0)
// If we are not busy create a new calendar event.
return calendar.events.insert(
{ calendarId: 'primary', resource: event },
err => {
// Check for errors and log them if they exist.
if (err) return console.error('Error Creating Calender Event:', err)
// Else log that the event was created.
return console.log('Calendar event successfully created.')
}
)
// If event array is not empty log that we are busy.
return console.log(`Sorry I'm busy...`)
}
)