-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
79 lines (68 loc) · 2.55 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
75
76
77
78
79
// Require google from googleapis package.
const { google } = require('googleapis')
// Require oAuth2 from our google instance.
const { OAuth2 } = google.auth
// Create a new instance of oAuth and set our Client ID & Client Secret.
const oAuth2Client = new OAuth2(
'YOUR CLIENT ID GOES HERE',
'YOUR CLIENT SECRET GOES HERE'
)
// Call the setCredentials method on our oAuth2Client instance and set our refresh token.
oAuth2Client.setCredentials({
refresh_token: 'YOUR REFRESH TOKEN GOES HERE',
})
// Create a new calender instance.
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)
// Create a dummy event for temp uses in our calendar
const event = {
summary: `Meeting with David`,
location: `3595 California St, San Francisco, CA 94118`,
description: `Meet with David to talk about the new client project and how to integrate the calendar for booking.`,
colorId: 1,
start: {
dateTime: eventStartTime,
timeZone: 'America/Denver',
},
end: {
dateTime: eventEndTime,
timeZone: 'America/Denver',
},
}
// Check if we a busy and have an event on our calendar for the same time.
calendar.freebusy.query(
{
resource: {
timeMin: eventStartTime,
timeMax: eventEndTime,
timeZone: 'America/Denver',
items: [{ id: 'primary' }],
},
},
(err, res) => {
// Check for errors in our query and log them if they exist.
if (err) return console.error('Free Busy Query Error: ', err)
// Create an array of all events on our calendar during that time.
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...`)
}
)