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.
npm run import -- "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;
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:
Imports:
chrono
: A library for parsing natural language dates and times.assert
: A built-in Node.js module for making assertions (checking if conditions are true).importer
: A custom module for importing other modules.ISODateString
: A function from importer
that converts a date to ISO 8601 format.listEvents
: A function from importer
to list events from a Google Calendar.getDaysEvents
Function:
startDate
(a string representing the date) and an options
object as input.eventCache
object to store fetched events for faster retrieval.startDate
using chrono.parseDate
.day
object representing the start of the day (midnight) and a dayEnd
object representing the end of the day.options.calendarId
is set.listEvents
to fetch events for the specified day and calendar, using ISODateString
to format the date range.eventCache
exists for the calendar.