Skip to content

Instantly share code, notes, and snippets.

@uF4No
Created May 4, 2019 18:13
Show Gist options
  • Save uF4No/e0a21b917f31841e1d03f08621962b93 to your computer and use it in GitHub Desktop.
Save uF4No/e0a21b917f31841e1d03f08621962b93 to your computer and use it in GitHub Desktop.
Methods to interact with the Google Calendar API
/**
* calendarService.js
*
* Methods to interact with the Google Calendar API
*
*/
const {google} = require('googleapis');
const debug = require('debug')('gcal:calendarService')
/**
* creates a Google Calendar instance using the OAuth2 client and call the list events with the filter
* @param {google.auth.OAuth2} auth The OAuth2 client already authenticated
* @param {object} filter Properties to filter by
*/
async function getEvents(auth, filter){
try{
const calendar = google.calendar({
version: 'v3',
auth
})
const filterBy = {
calendarId: 'primary',
timeMin: (new Date(filter.timeMin).toISOString()) || (new Date('2014-01-01')).toISOString(),
timeMax: (new Date(filter.timeMax).toISOString()) || (new Date()).toISOString(),
maxResults: filter.maxResults ,
singleEvents: true,
orderBy: 'startTime',
}
debug('Searching with filter %j', filterBy)
const events = await calendar.events.list(filterBy)
debug('found events: ', events)
return events
}catch(err){
debug('Captured error in getEvents: %s', err)
console.log(err)
}
}
module.exports = {getEvents}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment