Last active
October 7, 2020 00:04
-
-
Save easterncoder/6a2ffa29a65071f00a95e330cabe3e90 to your computer and use it in GitHub Desktop.
IFTTT Filter to search for a 1-hour slot in Google Calendar to schedule our event in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Author: Mike Lopez <[email protected]> | |
* Description: Find a 1-hour slot in Google Calendar to schedule our event in | |
*/ | |
// sort events by start date (because nope, they're not sorted) | |
GoogleCalendar.listEventsForDateRange.sort(function(a, b) { | |
if (moment(a.Start).isSame(b.Start)) { | |
return 0; | |
} | |
return moment(a.Start).isBefore(b.Start) ? -1 : 1; | |
}); | |
// initialize start date | |
var eventStart = window_hours(moment(Meta.currentUserTime).utc()); | |
// find a slot | |
var gEvent; | |
while (gEvent = GoogleCalendar.listEventsForDateRange.shift()) { | |
// slot has to be >= 60 minutes | |
if (moment(gEvent.Start).diff(eventStart, 'minutes') >= 60) { | |
// found a slot, stop the loop | |
break; | |
} | |
// set eventStart to end of current event | |
eventStart = window_hours(moment(gEvent.End)); | |
} | |
// set start time | |
GoogleCalendar.addDetailedEvent.setStartTime( | |
moment(eventStart).add( | |
Math.floor(Math.random() * 11), // add some randomness to start time | |
'minutes' | |
).format() | |
); | |
// set end time | |
GoogleCalendar.addDetailedEvent.setEndTime( | |
moment(eventStart).add(1, 'hour').format() | |
); | |
/** | |
* Adjust eventStart to within window hours | |
* | |
* @param moment eventStart Moment object | |
* @return moment | |
*/ | |
function window_hours(eventStart: any) { | |
// window hours 9am-4pm +8GMT Mondays to Saturdays only | |
if (eventStart.hour() < 1) { | |
// too early, advance to 9am +8GMT | |
eventStart.hour(1).minute(0); | |
} | |
if (eventStart.hour() > 8) { | |
// too late, advance hour to 9am +8GMT tomorrow | |
eventStart.add(1, 'day').hour(1).minute(0); | |
} | |
if (eventStart.day() == 0) { | |
// sunday, advanced to Monday | |
eventStart.add(1, 'day'); | |
} | |
return eventStart; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment