mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Should do diff week fix (#9551)
* Fixed week difference when changing year * Added year switchover test * Fixed start date setting
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user