diff --git a/test/api/v3/integration/tasks/PUT-tasks_id.test.js b/test/api/v3/integration/tasks/PUT-tasks_id.test.js index ef50ad8db4..f7ffa638b3 100644 --- a/test/api/v3/integration/tasks/PUT-tasks_id.test.js +++ b/test/api/v3/integration/tasks/PUT-tasks_id.test.js @@ -499,6 +499,45 @@ describe('PUT /tasks/:id', () => { }); }); + context('monthly dailys', () => { + let monthly; + + beforeEach(async () => { + const date1 = moment.utc('2020-07-01').toDate(); + monthly = await user.post('/tasks/user', { + text: 'test monthly', + type: 'daily', + frequency: 'monthly', + startDate: date1, + daysOfMonth: [date1.getDate()], + }); + }); + + it('updates days of month when start date updated', async () => { + const date2 = moment.utc('2020-07-01').toDate(); + const savedMonthly = await user.put(`/tasks/${monthly._id}`, { + startDate: date2, + }); + + expect(savedMonthly.daysOfMonth).to.deep.equal([moment(date2).date()]); + }); + + it('updates next due when start date updated', async () => { + const date2 = moment.utc('2020-07-01').toDate(); + const savedMonthly = await user.put(`/tasks/${monthly._id}`, { + startDate: date2, + }); + + expect(savedMonthly.nextDue.length).to.eql(6); + expect(moment(savedMonthly.nextDue[0]).toDate()).to.eql(moment.utc('2020-08-01').toDate()); + expect(moment(savedMonthly.nextDue[1]).toDate()).to.eql(moment.utc('2020-09-01').toDate()); + expect(moment(savedMonthly.nextDue[2]).toDate()).to.eql(moment.utc('2020-10-01').toDate()); + expect(moment(savedMonthly.nextDue[3]).toDate()).to.eql(moment.utc('2020-11-01').toDate()); + expect(moment(savedMonthly.nextDue[4]).toDate()).to.eql(moment.utc('2020-12-01').toDate()); + expect(moment(savedMonthly.nextDue[5]).toDate()).to.eql(moment.utc('2021-01-01').toDate()); + }); + }); + context('rewards', () => { let reward; diff --git a/website/server/controllers/api-v3/tasks.js b/website/server/controllers/api-v3/tasks.js index 159cabadac..646c473e10 100644 --- a/website/server/controllers/api-v3/tasks.js +++ b/website/server/controllers/api-v3/tasks.js @@ -681,6 +681,15 @@ api.updateTask = { task.group.managerNotes = sanitizedObj.managerNotes; } + // If the task was set to repeat monthly on a day of the month, and the start date was updated, + // the task will then need to be updated to repeat on the same day of the month as the new + // start date. For example, if the start date is updated to 7/2/2020, the daily should + // repeat on the 2nd day of the month. It's possible that a task can repeat monthly on a + // week of the month, in which case we won't update the repetition at all. + if (task.frequency === 'monthly' && task.daysOfMonth.length && task.startDate) { + task.daysOfMonth = [moment(task.startDate).date()]; + } + setNextDue(task, user); const savedTask = await task.save();