-
Notifications
You must be signed in to change notification settings - Fork 16
/
google_calendar.js
164 lines (137 loc) · 3.73 KB
/
google_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const {google} = require('googleapis');
require('dotenv').config();
// Provide the required configuration
const CREDENTIALS = JSON.parse(process.env.CREDENTIALS);
const calendarId = process.env.CALENDAR_ID;
// Google calendar API settings
const SCOPES = 'https://www.googleapis.com/auth/calendar';
const calendar = google.calendar({version : "v3"});
const auth = new google.auth.JWT(
CREDENTIALS.client_email,
null,
CREDENTIALS.private_key,
SCOPES
);
// Your TIMEOFFSET Offset
const TIMEOFFSET = '+05:30';
// Get date-time string for calender
const dateTimeForCalander = () => {
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
if (month < 10) {
month = `0${month}`;
}
let day = date.getDate();
if (day < 10) {
day = `0${day}`;
}
let hour = date.getHours();
if (hour < 10) {
hour = `0${hour}`;
}
let minute = date.getMinutes();
if (minute < 10) {
minute = `0${minute}`;
}
let newDateTime = `${year}-${month}-${day}T${hour}:${minute}:00.000${TIMEOFFSET}`;
let event = new Date(Date.parse(newDateTime));
let startDate = event;
// Delay in end time is 1
let endDate = new Date(new Date(startDate).setHours(startDate.getHours()+1));
return {
'start': startDate,
'end': endDate
}
};
// Insert new event to Google Calendar
const insertEvent = async (event) => {
try {
let response = await calendar.events.insert({
auth: auth,
calendarId: calendarId,
resource: event
});
if (response['status'] == 200 && response['statusText'] === 'OK') {
return 1;
} else {
return 0;
}
} catch (error) {
console.log(`Error at insertEvent --> ${error}`);
return 0;
}
};
// let dateTime = dateTimeForCalander();
// // Event for Google Calendar
// let event = {
// 'summary': `This is the summary.`,
// 'description': `This is the description.`,
// 'start': {
// 'dateTime': dateTime['start'],
// 'timeZone': 'Asia/Kolkata'
// },
// 'end': {
// 'dateTime': dateTime['end'],
// 'timeZone': 'Asia/Kolkata'
// }
// };
// insertEvent(event)
// .then((res) => {
// console.log(res);
// })
// .catch((err) => {
// console.log(err);
// });
// Get all the events between two dates
const getEvents = async (dateTimeStart, dateTimeEnd) => {
try {
let response = await calendar.events.list({
auth: auth,
calendarId: calendarId,
timeMin: dateTimeStart,
timeMax: dateTimeEnd,
timeZone: 'Asia/Kolkata'
});
let items = response['data']['items'];
return items;
} catch (error) {
console.log(`Error at getEvents --> ${error}`);
return 0;
}
};
// let start = '2020-10-03T00:00:00.000Z';
// let end = '2020-10-04T00:00:00.000Z';
// getEvents(start, end)
// .then((res) => {
// console.log(res);
// })
// .catch((err) => {
// console.log(err);
// });
// Delete an event from eventID
const deleteEvent = async (eventId) => {
try {
let response = await calendar.events.delete({
auth: auth,
calendarId: calendarId,
eventId: eventId
});
if (response.data === '') {
return 1;
} else {
return 0;
}
} catch (error) {
console.log(`Error at deleteEvent --> ${error}`);
return 0;
}
};
let eventId = 'hkkdmeseuhhpagc862rfg6nvq4';
deleteEvent(eventId)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});