google calendar | Lookup calendar id by name or id | Update create merge delete event | Search

The getDaysEvents function retrieves events from a specified Google Calendar for a given day, utilizing a cache to improve performance and efficiency. It parses the input date, constructs the date range, fetches events from the API, and returns a filtered list of non-deleted events.

Run example

npm run import -- "days events"

days events

var chrono = require('chrono-node');
var assert = require('assert');
var importer = require('../Core');
var {
    ISODateString,
    listEvents 
} = importer.import("convert date iso",
"list events");

function getDaysEvents(startDate, options) {
    var eventCache = {}; // TODO: fix this, move it outside, only update if calendar is modified?
    startDate = chrono.parseDate(startDate + '');
    var day = new Date(startDate);
    day.setHours(0, 0, 0);
    var dayEnd = new Date(day);
    dayEnd.setDate(dayEnd.getDate() + 1);
    var todaysEvents;
    assert(options.calendarId, 'calendarId must be set by now! ' + JSON.stringify(options, null, 4));
    if(typeof eventCache[options.calendarId] === 'undefined') {
        eventCache[options.calendarId] = {};
    }
    if(typeof eventCache[options.calendarId][day.getTime()] !== 'undefined') {
        todaysEvents = Promise.resolve(eventCache[options.calendarId][day.getTime()])
    } else {
        console.log({
            timeMin: ISODateString(new Date(day.getTime())),
            timeMax: ISODateString(new Date(dayEnd.getTime()))
        });
        todaysEvents = listEvents({
            auth: options.auth,
            calendarId: options.calendarId,
            timeMin: ISODateString(new Date(day.getTime())),
            timeMax: ISODateString(new Date(dayEnd.getTime()))
        })
    }
    return todaysEvents
        .then(m => {
            assert(eventCache[options.calendarId], 'something is seriously wrong with the calendar service "' + options.calendarId + '" ' + JSON.stringify(eventCache, null, 4));
            eventCache[options.calendarId][day.getTime()] = m;
            return m.filter(e => e.event.deleted !== true)
        })
}
module.exports = getDaysEvents;

What the code could have been:

const chrono = require('chrono-node');
const { assert } = require('assert');
const importer = require('../Core');

const {
  ISODateString,
  listEvents
} = importer.import([
  'convert date iso',
  'list events'
]);

class EventCache {
  constructor() {
    this.cache = {};
  }

  get(calendarId) {
    return this.cache[calendarId];
  }

  set(calendarId, day, events) {
    if (!this.cache[calendarId]) {
      this.cache[calendarId] = {};
    }
    this.cache[calendarId][day] = events;
  }
}

class DaysEvents {
  constructor(options) {
    this.options = options;
    this.eventCache = new EventCache();
  }

  getEvents(startDate) {
    assert(this.options.calendarId, 'calendarId must be set by now!'+ JSON.stringify(this.options, null, 4));
    const day = new Date(chrono.parseDate(startDate + '').getTime());
    day.setHours(0, 0, 0);
    const dayEnd = new Date(day.getTime());
    dayEnd.setDate(dayEnd.getDate() + 1);

    if (this.eventCache.get(this.options.calendarId) && this.eventCache.get(this.options.calendarId)[day.getTime()]) {
      return Promise.resolve(this.eventCache.get(this.options.calendarId)[day.getTime()])
       .then(events => events.filter(e => e.event.deleted!== true));
    }

    console.log({
      timeMin: ISODateString(day),
      timeMax: ISODateString(dayEnd)
    });

    return listEvents({
      auth: this.options.auth,
      calendarId: this.options.calendarId,
      timeMin: ISODateString(day),
      timeMax: ISODateString(dayEnd)
    })
     .then(m => {
        this.eventCache.set(this.options.calendarId, day, m);
        return m.filter(e => e.event.deleted!== true);
      });
  }
}

module.exports = function getDaysEvents(startDate, options) {
  return new DaysEvents(options).getEvents(startDate);
};

This code defines a function getDaysEvents that fetches events from a Google Calendar for a specific day.

Here's a breakdown:

  1. Imports:

  2. getDaysEvents Function: