This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathholiday-reminder.js
91 lines (77 loc) · 2.26 KB
/
holiday-reminder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const moment = require('moment-timezone');
const scheduler = require('node-schedule');
const holidays = require('@18f/us-federal-holidays');
const CHANNEL = process.env.HUBOT_HOLIDAY_REMINDER_CHANNEL || 'general';
const TIMEZONE =
process.env.HUBOT_HOLIDAY_REMINDER_TIMEZONE || 'America/New_York';
const reportingTime = moment(
process.env.HUBOT_HOLIDAY_REMINDER_TIME || '15:00',
'HH:mm'
);
const SUPPRESS_HERE = process.env.HUBOT_HOLIDAY_REMINDER_SUPPRESS_HERE;
const suppressHere = (() => {
if (SUPPRESS_HERE) {
const upper = SUPPRESS_HERE.toUpperCase();
if (upper === 'TRUE' || upper === 'YES' || upper === 'Y') {
return true;
}
if (!Number.isNaN(+SUPPRESS_HERE) && +SUPPRESS_HERE > 0) {
return true;
}
}
return false;
})();
const getNextHoliday = () => {
const now = moment.tz(TIMEZONE);
return holidays
.allForYear(now.year())
.concat(holidays.allForYear(now.year() + 1))
.map(h => ({
...h,
date: moment.tz(h.dateString, 'YYYY-MM-DD', TIMEZONE)
}))
.filter(h => h.date.isAfter(now))
.shift();
};
const previousWeekday = date => {
const source = moment(date);
source.subtract(1, 'day');
let dow = source.format('dddd');
while (dow === 'Saturday' || dow === 'Sunday') {
source.subtract(1, 'day');
dow = source.format('dddd');
}
return source;
};
const postReminder = (robot, holiday) => {
robot.messageRoom(
CHANNEL,
`${suppressHere ? '' : '@here '}Remember that *${holiday.date.format(
'dddd'
)}* is a federal holiday in observance of *${holiday.name}*!`
);
};
const scheduleReminder = (
robot,
fn = { getNextHoliday, previousWeekday, postReminder, scheduler }
) => {
const nextHoliday = fn.getNextHoliday();
const target = fn.previousWeekday(nextHoliday.date);
target.hour(reportingTime.hour());
target.minute(reportingTime.minute());
fn.scheduler.scheduleJob(target.toDate(), () => {
fn.postReminder(robot, nextHoliday);
// Tomorrow, schedule the next holiday reminder
fn.scheduler.scheduleJob(target.add(1, 'day').toDate(), () => {
scheduleReminder(robot);
});
});
};
module.exports = scheduleReminder;
// Expose for testing
module.exports.testing = {
getNextHoliday,
postReminder,
previousWeekday,
scheduleReminder
};