-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
355 lines (319 loc) · 12.8 KB
/
app.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Author: Sarthak Joshi
Email: iamsarthakjoshi@gmail.com
*/
// Dependency setup
const express = require('express'),
moment = require('moment'),
{ google } = require('googleapis'),
config = require('./credentials'),
timeslots = require('./timeslots'),
fixedTimeSlots = timeslots.timeslots
// Initialize
var app = express(),
calendar = google.calendar('v3'),
cred = config.credentials // google credentials
;(oAuthClient = new google.auth.OAuth2(
cred.clientID,
cred.clientSecret,
cred.redirectURL
)),
(authorized = false),
(redirectUrl = '/'),
(availableTimeSlots = [])
fixedTimeSlotsNotFormatted = true
// get avaibility of timeslots for each day in given month and year
app.get('/days', function(req, res) {
var year = req.query.year
var month = req.query.month - 1
redirectUrl = `/days?year=${year}&month=${month + 1}`
// If we're not authenticated, fire off the OAuth flow
if (!authorized) {
// Generate an OAuth URL and redirect there
var url = oAuthClient.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/calendar'
})
res.redirect(url)
} else {
var startDate = new Date(year, month, 1)
var endDate = new Date(year, month + 1, 0)
var totalNoOfDays = endDate.getDate()
// Call google to fetch events for today on our calendar
calendar.events.list(
{
calendarId: '15suli79te2e0qfd533pcb8q4g@group.calendar.google.com',
maxResults: 30,
timeMin: new Date(startDate).toISOString(),
timeMax: new Date(endDate).toISOString(),
auth: oAuthClient,
singleEvents: true,
orderBy: 'startTime'
},
function(err, response) {
if (err) {
console.log(`Error fetching appointments: ${err}`)
} else {
// Send our JSON response back to the browser
console.log('Successfully fetched appointments')
var events = response.data.items // get response from google calender api
var countAppointment = 0
var output = { success: false, days: [] }
// filter Canceled Events, Excludes Weekends, Excludes < 9am && > 6pm time slots
var bookedAppoinements = events.filter(
(data) =>
data.status == 'confirmed' &&
(new Date(data.start.dateTime).getUTCDay() != 0 &&
new Date(data.start.dateTime).getUTCDay() != 6) &&
(moment(data.start.dateTime)
.utc()
.hours() > 8 &&
moment(data.start.dateTime)
.utc()
.hours() < 20)
)
for (i = 1; i <= totalNoOfDays; i++) {
// loop through all days of that month
countAppointment = 0 // re-setting count to 0 to exclude unmatched datetimes
bookedAppoinements.map((event, c) => {
// map through filtered events/appointments
var dateOnly = event.start.dateTime
dateOnly = dateOnly.substring(0, dateOnly.indexOf('T') + 1) // get date with yyyy-mm-ddT iso format
var start = event.start.dateTime || event.start.date
if (i == new Date(start).getUTCDate()) {
// check if dates match with filtered event's datetimes
fixedTimeSlots.forEach((d, c) => {
// loop through given fixed timeslots
if (moment(dateOnly + d.start).isSame(start)) {
// if fixed timeslots matches with filtered datetimes
countAppointment++
}
})
}
})
// push timeslot avaibility in output object
if (countAppointment < 12)
output.days.push({ day: i, hasTimeSlots: true })
else output.days.push({ day: i, hasTimeSlots: false })
}
// set output data
output.success = true
// Send JSON response back to the browser
res.header('Content-Type', 'application/json')
res.send(JSON.stringify(output, null, 2))
}
}
)
}
})
// get available timeslots for given day, month and year
app.get('/timeslots', function(req, res) {
const year = req.query.year
const month = req.query.month - 1
const day = req.query.day
redirectUrl = `/timeslots?year=${year}&month=${month + 1}&day=${day}`
var startTime = new Date(year, month, day, 9, 30)
var endTime = new Date(year, month + 1, day, 18, 30)
if (!authorized) {
// Generate an OAuth URL and redirect there
var url = oAuthClient.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/calendar'
})
res.redirect(url)
} else {
calendar.events.list(
{
calendarId: '15suli79te2e0qfd533pcb8q4g@group.calendar.google.com',
maxResults: 30,
timeMin: new Date(startTime).toISOString(),
timeMax: new Date(endTime).toISOString(),
auth: oAuthClient,
singleEvents: true,
orderBy: 'startTime'
},
function(err, response) {
if (err) {
console.log(`Error fetching available timeslots: ${err}`)
} else {
console.log('Successfully fetched available timeslots')
var events = response.data.items // get response from google calender api
var output = { success: false }
var bookedTimeSlots = []
if (fixedTimeSlotsNotFormatted) {
// reformat the fixed timeslots in ISO format
// from 09:00:00.000Z to 2019-10-16T09:00:00.000Z
fixedTimeSlots.forEach((d, c) => {
fixedTimeSlots[c].start = `${year}-${month + 1}-${day}T${d.start}`
fixedTimeSlots[c].end = `${year}-${month + 1}-${day}T${d.end}`
})
fixedTimeSlotsNotFormatted = false
}
// filter Canceled Events, Excludes Weekends, Excludes < 9am && > 6pm time slots
var bookedAppoinements = events.filter(
(data) =>
data.status == 'confirmed' &&
(new Date(data.start.dateTime).getUTCDay() != 0 &&
new Date(data.start.dateTime).getUTCDay() != 6) &&
(moment(data.start.dateTime)
.utc()
.hours() > 8 &&
moment(data.start.dateTime)
.utc()
.hours() < 18)
)
// map through bookedAppoinements to get start/end time of the booked appointments
bookedAppoinements.map((appoinemnt, i) => {
var start = appoinemnt.start.dateTime || appoinemnt.start.date
var end = appoinemnt.end.dateTime || appoinemnt.end.date
// make separate bookedTimeSlots array
fixedTimeSlots.forEach((fixedTS, j) => {
if (moment(fixedTS.start).isSame(start))
bookedTimeSlots.push(fixedTS)
})
})
// get available timeslots by comparing fixed and booked timeslots
availableTimeSlots = fixedTimeSlots.filter(function(item) {
return !bookedTimeSlots.includes(item)
})
// generate output data
console.log(availableTimeSlots)
output.success = true
output.timeSlots = availableTimeSlots
// Send JSON response back to the browser
res.header('Content-Type', 'application/json')
res.send(JSON.stringify(output, null, 2))
}
}
)
}
})
// book appointment time for given time, day, month and year
app.get('/book', function(req, res) {
const year = req.query.year,
mm = req.query.month,
month = (mm.toString().length < 2 ? '0' : '') + mm,
day = (req.query.day.length < 2 ? '0' : '') + req.query.day,
hour = (req.query.hour.length < 2 ? '0' : '') + req.query.hour,
minute = (req.query.minute.length < 2 ? '0' : '') + req.query.minute,
fullDateTime = `${hour}:${minute} ${day}/${month}/${year}`,
reqDateTimeISO = moment(`${year}-${month}-${day}T${hour}:${minute}:00.000Z`) // requested datetime in ISO
var output = { success: false }
redirectUrl = `/book?year=${year}&month=${month}&day=${day}&hour=${hour}&minute=${minute}`
if (!authorized) {
// Generate an OAuth URL and redirect there
var url = oAuthClient.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/calendar'
})
res.redirect(url)
} else {
// var a = availableTimeSlots.includes(availableTimeSlots.find(slot=>moment(slot.start).isSame(reqDateTimeISO)));
// check if request booking time matches with available time slots
var isValidTimeSlot = availableTimeSlots.some((slot) =>
moment(slot.start).isSame(reqDateTimeISO)
)
// get first and last appointment's starting time - *Note: please navigation to bottom line
console.log('TEST ', fixedTimeSlots[0].start)
var firstAppST = moment(fixedTimeSlots[0].start)
var lastAppST = moment(fixedTimeSlots[11].start)
// declare error message for POST requests
const invalidTime = 'Invalid time slot'
pastTime = 'Cannot book time in the past'
lessThan24hr = 'Cannot book with less than 24 hours in advance'
isBetween =
'Cannot book outside bookable timeframe: The time slot ' +
'provided was not on a weekday between 9 am and 6 pm'
// check every given conditions and set corresponding error messages
if (reqDateTimeISO.isBefore(moment())) output.message = pastTime
else if (moment.duration(reqDateTimeISO.diff(moment())).asHours() < 24)
output.message = lessThan24hr
else if (!reqDateTimeISO.isBetween(firstAppST, lastAppST, 'hours', '[]'))
output.message = isBetween
else if (!isValidTimeSlot) output.message = invalidTime
else {
// get available time for requested date-time
var availableTimeSlot = availableTimeSlots.filter((slot) =>
moment(slot.start).isSame(reqDateTimeISO)
)
var startTime = availableTimeSlot[0].start // start-time of appointment
var endTime = availableTimeSlot[0].end // end-time of appointment
// set data for new appointment
var newAppointment = {
summary: `Appoinment for Mr/Ms Zz at ${fullDateTime}`,
location: `zzDigitalzz zzAngelszz, Sydney, Australia`,
description: '',
start: { dateTime: startTime },
end: { dateTime: endTime }
}
// wait until new appointment time is inserted in google calendar
var insertPromise = new Promise((reslove, reject) => {
// insert new appointment time in bookings google calendar
calendar.events.insert(
{
calendarId: '15suli79te2e0qfd533pcb8q4g@group.calendar.google.com',
auth: oAuthClient,
resource: newAppointment
},
(err, response) => {
if (err) {
output.message = `There was an error contacting the Calendar service: ${err}`
console.log(err)
reject('Error booking new appointment.')
} else {
reslove('New appointment booked.')
// set output data
output.success = true
output.startTime = startTime
output.endTime = endTime
}
}
)
})
// display output when insertPromise process is completed
insertPromise.then(() => {
// Send JSON response back to the browser
res.header('Content-Type', 'application/json')
res.send(JSON.stringify(output, null, 2))
})
}
}
})
// Return point for oAuth flow, should match credentials.redirectURL
app.get('/oauth/callback', function(req, res) {
var code = req.query.code
if (code) {
// Get an access token based on our OAuth code
oAuthClient.getToken(code, function(err, tokens) {
if (err) {
console.log(`Error authenticating ${err}`)
} else {
console.log('Successfully authenticated!!!!!')
console.log(tokens)
// Store our credentials and redirect back to our main page
oAuthClient.setCredentials(tokens)
authorized = true
res.redirect(redirectUrl)
}
})
}
})
if (module === require.main) {
// Start the server
const server = app.listen(process.env.PORT || 8081, () => {
const port = server.address().port
console.log(`App listening on port ${port}`)
})
}
/* Note: As per the requirement
"Invalid time slot: The time slot provided was not one of the -
time slots returned in the GET available time slots request",
I have used fixedTimeSlots object values which only gets
ISOFormatted after user search for available timeslots.
So, it does not work as expected before hitting timeslots avaibility request.
Also, availableTimeSlots object is populated during timeslots avaibility request,
which is then and also used while booking appoinment.
If these object shouldn't be tightly coupled, then fixedTimeSlots and
availableTimeSlots should be populated during booking appoinments,
which is of course doable.
*/