Skip to content

Commit d1efc0c

Browse files
authored
Merge pull request #132 from Riott/issue-130
Issue 130 !sch handle all day events
2 parents e79b452 + f7d14cb commit d1efc0c

File tree

6 files changed

+328
-61
lines changed

6 files changed

+328
-61
lines changed

lib/commands/implementations/schedule.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@ function getNextStreamDate(input, services) {
77
.findNextStreamDay()
88
.then((nextStreamDay) => {
99
if (!nextStreamDay) {
10-
return new CommandOutput(null, `Nothing scheduled ${services.schedule.link()}`);
10+
return new CommandOutput(null, `Nothing is scheduled. ${services.schedule.link()}`);
1111
}
1212

13-
return new CommandOutput(
14-
null,
15-
`${nextStreamDay.name}${
16-
nextStreamDay.allDay ? ', an all-day event,' : ''
17-
} scheduled to begin ${moment(nextStreamDay.start).fromNow()} ${services.schedule.link()}`,
18-
);
13+
const { allDay, name, start, childEvent } = nextStreamDay;
14+
const isHappening = start.isBefore(moment());
15+
let scheduleOutput = '';
16+
17+
if (isHappening && allDay) {
18+
scheduleOutput = `${name}, an all-day event, is scheduled today.`;
19+
if (childEvent) {
20+
const childEventIsHappening = moment(childEvent.start).isBefore(moment());
21+
22+
// prettier-ignore
23+
scheduleOutput += childEventIsHappening
24+
? ` ${childEvent.name} is currently happening! It started ${childEvent.start.fromNow()}.`
25+
: ` Coming up ${childEvent.start.fromNow()} is ${childEvent.name}.`;
26+
}
27+
} else if (isHappening) {
28+
scheduleOutput = `${name} is currently happening! It started ${start.fromNow()}.`;
29+
} else {
30+
scheduleOutput = `${name} is the next scheduled event. It starts ${start.fromNow()}.`;
31+
}
32+
33+
scheduleOutput += ` ${services.schedule.link()}`;
34+
35+
return new CommandOutput(null, scheduleOutput);
1936
})
2037
.catch((err) => new CommandOutput(err, "Oops. Something didn't work. Check the logs."));
2138
}

lib/services/schedule.js

+39-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
const google = require('googleapis').google; // eslint-disable-line prefer-destructuring
22
const moment = require('moment-timezone');
33

4+
/**
5+
* @typedef {Object} Event
6+
* @property {boolean} allDay
7+
* @property {moment.Moment} start
8+
* @property {string} name
9+
*/
10+
11+
/**
12+
* @returns {Event}
13+
*/
14+
function buildEvent(event) {
15+
// All-day events don't have a start time, only a start day.
16+
const allDay = !event.start.dateTime;
17+
18+
const start = allDay ? moment.utc(event.start.date) : moment.utc(event.start.dateTime);
19+
20+
return {
21+
allDay,
22+
start,
23+
name: event.summary,
24+
};
25+
}
26+
427
class GoogleCal {
528
constructor(configuration) {
629
this.configuration = configuration;
@@ -12,29 +35,23 @@ class GoogleCal {
1235
}
1336

1437
findNextStreamDay() {
15-
return this.getListOfUpcomingEvents(this.configuration.GOOGLE_CALENDAR_ID).then((events) => {
16-
const now = moment.tz('UTC');
17-
const nextEvent = events.find((event) => {
18-
if (event.start.date !== undefined) {
19-
return true;
38+
return this.getListOfUpcomingEvents(this.configuration.GOOGLE_CALENDAR_ID).then(
39+
(googleEvents) => {
40+
const events = googleEvents.map(buildEvent);
41+
if (!events.length) {
42+
return null;
2043
}
2144

22-
return !(
23-
moment.tz(event.end.dateTime, 'YYYY-MM-DDTHH:mm:ss', event.end.timeZone).isAfter(now) &&
24-
moment.tz(event.start.dateTime, 'YYYY-MM-DDTHH:mm:ss', event.start.timeZone).isBefore(now)
25-
);
26-
});
27-
28-
if (!nextEvent) {
29-
return null;
30-
}
31-
32-
return {
33-
start: nextEvent.start.dateTime ?? nextEvent.start.date,
34-
name: nextEvent.summary,
35-
allDay: !nextEvent.start.dateTime, // All-day events don't have a start time, only a start day.
36-
};
37-
});
45+
const nextEvent = events[0];
46+
if (nextEvent.allDay) {
47+
nextEvent.childEvent = events.find((event) => {
48+
return !event.allDay && event.start.isSame(nextEvent.start, 'day');
49+
});
50+
}
51+
52+
return nextEvent;
53+
},
54+
);
3855
}
3956

4057
link() {
@@ -49,6 +66,7 @@ class GoogleCal {
4966
singleEvents: true,
5067
timeMin: new Date().toISOString(),
5168
maxResults: 5,
69+
timeZone: 'UTC',
5270
})
5371
.then((response) => response.data.items);
5472
}

package-lock.json

+167-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"bunyan": "^1.8.14",
2121
"fast-levenshtein": "^3.0.0",
2222
"google-auth-library": "^6.1.1",
23-
"googleapis": "^61.0.0",
23+
"googleapis": "^118.0.0",
2424
"inspector-influx": "^2.7.0",
2525
"inspector-metrics": "^1.21.0",
2626
"lodash": "^4.17.20",

0 commit comments

Comments
 (0)