var asyncGetCalendarData = async function (res, calId, results, days) {

    try {
    const key = require('../data/key.json');
    const SCOPES =  ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly' ];
    const jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, SCOPES);
    const calendar = google.calendar('v3');

    await jwtClient.authorize();

    let today = new Date();
    const msecPerDay = 24 * 60 * 60 * 1000;
    const yesterday = new Date(today.getTime() - msecPerDay);
    const maxDays = new Date(today.getTime() + msecPerDay * days);
    today.setHours(0,0,0,0);

    const result = await calendar.events.list({
            auth: jwtClient,
            calendarId: calId,
            maxResults: results,
            timeMax : maxDays.toISOString(),
            timeMin: yesterday.toISOString(), //timeMin: '2016-03-21T13:35:03.850Z',
            singleEvents: true, //needed for orderBy
            orderBy: "startTime",
            fields: "items(end, start, summary, location)"
        });

    res.json(result.data);
    } catch (error) {
        console.log(error);
        res.json([]);
    }
};
Example #2
0
function calendarProvider(opts = {}) {
  const jwtClient = new google.auth.JWT(
    opts.serviceAccount.client_email,
    null,
    opts.serviceAccount.private_key,
    [
      'https://www.googleapis.com/auth/spreadsheets',
      'https://www.googleapis.com/auth/drive',
      'https://www.googleapis.com/auth/calendar',
    ]
  );
  jwtClient.authorize((err, tokens) => {
    if (err) {
      console.log(err);
    } else {
      console.log('Successfully connected!');
    }
  });

  const calendar = google.calendar('v3');

  return new Provider({
    getEvents() {
      return new Promise((resolve, reject) => {
        calendar.events.list(
          {
            auth: jwtClient,
            calendarId: '*****@*****.**',
            timeMin: new Date().toISOString(),
            maxResults: 10,
            singleEvents: true,
            orderBy: 'startTime',
          },
          (err, res) => {
            if (err) return reject(err);

            const events = res.data.items;

            resolve({ events });
          }
        );
      });
    },
  });
}
Example #3
0
import schedule from 'node-schedule'
import { google } from 'googleapis'
import { EventEmitter } from 'events'
import config from '../scripts/config'

const calendar = google.calendar({version: 'v3', auth: config.google.apiKey})
let fetchDelay = 10 * 1000

let eventsMap = {}
let eventsJobs = {}
const scheduler = new EventEmitter()

function sortStartTime (a, b) {
  return a.start.getTime() - b.start.getTime()
}

function fetchCalendar () {
  let now = Date.now()
  calendar.events.list({
    calendarId: config.google.calendarId,
    maxResults: 100, // default is 250
    timeMin: new Date(now - 10 * 60 * 1000).toISOString(),
    timeMax: new Date(now + 24 * 60 * 60 * 1000).toISOString(),
    singleEvents: true
  }, (err, {data}) => {
    if (err) {
      console.error('Scheduler: Error fetching events')
      console.error(err)
      return
    }
Example #4
0
'use strict';

const config = require('./config');
const { google } = require('googleapis');
const gcal = google.calendar({ version: 'v3' });

const helpers = {
  /**
   * Create a helper method to strip ',' from amount and return number to 2 decimals
   * @param {String} invalidNumber
   * @return String
   */
  stripCommas(invalidNumber) {
    return invalidNumber.toString().replace(new RegExp(',', 'g'), '');
  },

  /**
   * Capitalizes first letter of string and returns string
   * @param {String} text
   * @return String
   */
  capitalizeFirstLetter(text) {
    return text.charAt(0).toUpperCase() + text.slice(1);
  },

  /**
   * Create a helper method to handle slack user handle creation
   * @param {String} userId
   * @param {String} username
   * @return String
   */