From 20a99e526d82779c67c6e0824814332a7093ce02 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Mon, 20 Nov 2017 12:22:01 -0600 Subject: [PATCH] Should do diff week fix (#9551) * Fixed week difference when changing year * Added year switchover test * Fixed start date setting --- test/common/shouldDo.test.js | 34 +++++++++++++++++++++++++++------- website/common/script/cron.js | 10 ++++++++-- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/test/common/shouldDo.test.js b/test/common/shouldDo.test.js index 4159d5133e..9658f35045 100644 --- a/test/common/shouldDo.test.js +++ b/test/common/shouldDo.test.js @@ -644,7 +644,27 @@ describe('shouldDo', () => { day = moment(); dailyTask.repeat[DAY_MAPPING[day.day()]] = true; dailyTask.everyX = 3; - let threeWeeksFromToday = day.add(6, 'weeks').day(day.day()).toDate(); + const threeWeeksFromToday = day.add(6, 'weeks').day(day.day()).toDate(); + + expect(shouldDo(threeWeeksFromToday, dailyTask, options)).to.equal(true); + }); + + it('activates Daily on every (x) week on weekday across a year', () => { + dailyTask.repeat = { + su: false, + s: false, + f: false, + th: false, + w: false, + t: false, + m: false, + }; + + day = moment('2017-11-19'); + dailyTask.startDate = day.toDate(); + dailyTask.repeat[DAY_MAPPING[day.day()]] = true; + dailyTask.everyX = 3; + const threeWeeksFromToday = moment('2018-01-21'); expect(shouldDo(threeWeeksFromToday, dailyTask, options)).to.equal(true); }); @@ -970,7 +990,7 @@ describe('shouldDo', () => { m: false, }; - let today = moment('2017-01-26'); + let today = moment('2017-01-26:00:00.000-00:00'); let week = today.monthWeek(); let dayOfWeek = today.day(); dailyTask.startDate = today.toDate(); @@ -979,7 +999,7 @@ describe('shouldDo', () => { dailyTask.everyX = 2; dailyTask.frequency = 'monthly'; - day = moment('2017-03-24'); + day = moment('2017-03-24:00:00.000-00:00'); expect(shouldDo(day, dailyTask, options)).to.equal(false); }); @@ -995,7 +1015,7 @@ describe('shouldDo', () => { m: false, }; - let today = moment('2017-01-27'); + let today = moment('2017-01-27:00:00.000-00:00'); let week = today.monthWeek(); let dayOfWeek = today.day(); dailyTask.startDate = today.toDate(); @@ -1004,7 +1024,7 @@ describe('shouldDo', () => { dailyTask.everyX = 2; dailyTask.frequency = 'monthly'; - day = moment('2017-03-24'); + day = moment('2017-03-24:00:00.000-00:00'); expect(shouldDo(day, dailyTask, options)).to.equal(true); }); @@ -1020,7 +1040,7 @@ describe('shouldDo', () => { m: false, }; - let today = moment('2017-01-27'); + let today = moment('2017-01-27:00:00.000-00:00'); let week = today.monthWeek(); let dayOfWeek = today.day(); dailyTask.startDate = today.toDate(); @@ -1029,7 +1049,7 @@ describe('shouldDo', () => { dailyTask.everyX = 2; dailyTask.frequency = 'monthly'; - day = moment('2017-03-24'); + day = moment('2017-03-24:00:00.000-00:00'); expect(shouldDo(day, dailyTask, options)).to.equal(true); }); diff --git a/website/common/script/cron.js b/website/common/script/cron.js index fa1f2a388b..3f38aba433 100644 --- a/website/common/script/cron.js +++ b/website/common/script/cron.js @@ -76,6 +76,7 @@ export function startOfDay (options = {}) { if (moment(o.now).hour() < o.dayStart) { dayStart.subtract({ days: 1 }); } + return dayStart; } @@ -138,7 +139,7 @@ export function shouldDo (day, dailyTask, options = {}) { } else if (dailyTask.frequency === 'weekly') { let schedule = moment(startDate).recur(); - let differenceInWeeks = moment(startOfDayWithCDSTime).week() - moment(startDate).week(); + let differenceInWeeks = moment(startOfDayWithCDSTime).diff(moment(startDate), 'week'); let matchEveryX = differenceInWeeks % dailyTask.everyX === 0; if (daysOfTheWeek.length === 0) return false; @@ -163,7 +164,11 @@ export function shouldDo (day, dailyTask, options = {}) { } else if (dailyTask.frequency === 'monthly') { let schedule = moment(startDate).recur(); - let differenceInMonths = moment(startOfDayWithCDSTime).month() - moment(startDate).month(); + // Use startOf to ensure that we are always comparing month + // to the next rather than a month from the day + let differenceInMonths = moment(startOfDayWithCDSTime).startOf('month') + .diff(moment(startDate).startOf('month'), 'month', true); + let matchEveryX = differenceInMonths % dailyTask.everyX === 0; if (dailyTask.weeksOfMonth && dailyTask.weeksOfMonth.length > 0) { @@ -194,6 +199,7 @@ export function shouldDo (day, dailyTask, options = {}) { } return filteredDates; } + return schedule.matches(startOfDayWithCDSTime) && matchEveryX; } else if (dailyTask.daysOfMonth && dailyTask.daysOfMonth.length > 0) { schedule = schedule.every(dailyTask.daysOfMonth).daysOfMonth();