From b4180c227ae35b99d1ad1ec7bc8390935dda01b4 Mon Sep 17 00:00:00 2001 From: Adam Simpson Date: Thu, 24 Mar 2016 07:09:12 -0400 Subject: [PATCH] fix: check if lunch date is this week closes #2 --- src/generalAnnouncement.js | 25 +++++++++++++++---------- src/privateReminder.js | 25 +++++++++++++++---------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/generalAnnouncement.js b/src/generalAnnouncement.js index d08b769..7fcefea 100644 --- a/src/generalAnnouncement.js +++ b/src/generalAnnouncement.js @@ -18,16 +18,21 @@ const getData = () => { const parseData = (data) => { const dates = getDateColumn(data); const next = getNextLunch(dates); - const names = parseNames(data, next); - const slackNames = returnSlackNames(names, namesObj); - const formattedNames = formatSlackNames(slackNames).toString(); - const msg = ` - <@dayton> ${formattedNames} are scheduled to help with lunch on Friday. - - ${process.env.sheetUrl} - `; - - postToSlack('#general', msg); + const today = new Date(); + //1000 * 3600 * 24 is milliseconds in 1 day + const dayInMilliseconds = 1000 * 3600 * 24; + if ((next - today) / (dayInMilliseconds) < 7) { + const names = parseNames(data, next); + const slackNames = returnSlackNames(names, namesObj); + const formattedNames = formatSlackNames(slackNames).toString(); + const msg = ` + <@dayton> ${formattedNames} are scheduled to help with lunch on Friday. + + ${process.env.sheetUrl} + `; + + postToSlack('#general', msg); + } }; const formatSlackNames = (names) => { diff --git a/src/privateReminder.js b/src/privateReminder.js index 180288e..e06b36f 100644 --- a/src/privateReminder.js +++ b/src/privateReminder.js @@ -18,17 +18,22 @@ const getData = () => { const parseData = (data) => { const dates = getDateColumn(data); const next = getNextLunch(dates); - const names = parseNames(data, next); - const slackNames = returnSlackNames(names, namesObj); - const msg = ` - You are scheduled to help with lunch on Friday with ${names}.\n If you are unable to do so please find a replacement and update the spreadsheet. - - ${process.env.sheetUrl} - `; + const today = new Date(); + //1000 * 3600 * 24 is milliseconds in 1 day + const dayInMilliseconds = 1000 * 3600 * 24; + if ((next - today) / (dayInMilliseconds) < 7) { + const names = parseNames(data, next); + const slackNames = returnSlackNames(names, namesObj); + const msg = ` + You are scheduled to help with lunch on Friday with ${names}.\n If you are unable to do so please find a replacement and update the spreadsheet. - slackNames.forEach((helper) => { - postToSlack(`@${helper}`, msg); - }); + ${process.env.sheetUrl} + `; + + slackNames.forEach((helper) => { + postToSlack(`@${helper}`, msg); + }); + } }; const date = new Date();