diff --git a/test/common/libs/taskDefaults.test.js b/test/common/libs/taskDefaults.test.js index 756447a8df..1ec59b562c 100644 --- a/test/common/libs/taskDefaults.test.js +++ b/test/common/libs/taskDefaults.test.js @@ -1,8 +1,11 @@ +import moment from 'moment'; + import taskDefaults from '../../../website/common/script/libs/taskDefaults'; +import { generateUser } from '../../helpers/common.helper'; describe('taskDefaults', () => { it('applies defaults to undefined type or habit', () => { - let task = taskDefaults(); + let task = taskDefaults({}, generateUser()); expect(task.type).to.eql('habit'); expect(task._id).to.exist; expect(task.text).to.eql(task._id); @@ -18,7 +21,7 @@ describe('taskDefaults', () => { }); it('applies defaults to a daily', () => { - let task = taskDefaults({ type: 'daily' }); + let task = taskDefaults({ type: 'daily' }, generateUser()); expect(task.type).to.eql('daily'); expect(task._id).to.exist; expect(task.text).to.eql(task._id); @@ -42,7 +45,7 @@ describe('taskDefaults', () => { }); it('applies defaults a reward', () => { - let task = taskDefaults({ type: 'reward' }); + let task = taskDefaults({ type: 'reward' }, generateUser()); expect(task.type).to.eql('reward'); expect(task._id).to.exist; expect(task.text).to.eql(task._id); @@ -52,7 +55,7 @@ describe('taskDefaults', () => { }); it('applies defaults a todo', () => { - let task = taskDefaults({ type: 'todo' }); + let task = taskDefaults({ type: 'todo' }, generateUser()); expect(task.type).to.eql('todo'); expect(task._id).to.exist; expect(task.text).to.eql(task._id); @@ -61,4 +64,18 @@ describe('taskDefaults', () => { expect(task.priority).to.eql(1); expect(task.completed).to.eql(false); }); + + it('starts a task yesterday if user cron is later today', () => { + // Configure to have a day start that's *always* tomorrow. + let user = generateUser({'preferences.dayStart': 25}); + let task = taskDefaults({ type: 'daily' }, user); + + expect(task.startDate).to.eql( + moment() + .zone(user.preferences.timezoneOffset, 'hour') + .startOf('day') + .subtract(1, 'day') + .toDate() + ); + }); }); diff --git a/test/common/shouldDo.test.js b/test/common/shouldDo.test.js index b4b2feaae1..dedcaa4951 100644 --- a/test/common/shouldDo.test.js +++ b/test/common/shouldDo.test.js @@ -228,7 +228,7 @@ describe('shouldDo', () => { options.timezoneOffset = 0; options.nextDue = true; - day = moment('2017-05-01').toDate(); + day = moment.utc('2017-05-01').toDate(); dailyTask.frequency = 'daily'; dailyTask.everyX = 2; dailyTask.startDate = day; @@ -391,7 +391,7 @@ describe('shouldDo', () => { options.timezoneOffset = 0; options.nextDue = true; - day = moment('2017-05-01').toDate(); + day = moment.utc('2017-05-01').toDate(); dailyTask.frequency = 'weekly'; dailyTask.everyX = 1; dailyTask.repeat = { @@ -780,7 +780,7 @@ describe('shouldDo', () => { options.timezoneOffset = 0; options.nextDue = true; - day = moment('2017-05-01').toDate(); + day = moment.utc('2017-05-01').toDate(); dailyTask.frequency = 'monthly'; dailyTask.everyX = 3; @@ -817,7 +817,7 @@ describe('shouldDo', () => { expect(moment(nextDue[4]).toDate()).to.eql(moment.utc('2017-10-02').toDate()); expect(moment(nextDue[5]).toDate()).to.eql(moment.utc('2017-11-06').toDate()); - day = moment('2017-05-08').toDate(); + day = moment.utc('2017-05-08').toDate(); dailyTask.daysOfMonth = []; dailyTask.weeksOfMonth = [1]; @@ -841,7 +841,7 @@ describe('shouldDo', () => { expect(moment(nextDue[4]).toDate()).to.eql(moment.utc('2017-10-09').toDate()); expect(moment(nextDue[5]).toDate()).to.eql(moment.utc('2017-11-13').toDate()); - day = moment('2017-05-29').toDate(); + day = moment.utc('2017-05-29').toDate(); dailyTask.daysOfMonth = []; dailyTask.weeksOfMonth = [4]; @@ -1143,7 +1143,7 @@ describe('shouldDo', () => { options.timezoneOffset = 0; options.nextDue = true; - day = moment('2017-05-01').toDate(); + day = moment.utc('2017-05-01').toDate(); dailyTask.frequency = 'yearly'; dailyTask.everyX = 5; diff --git a/website/client/components/challenges/challengeDetail.vue b/website/client/components/challenges/challengeDetail.vue index e164475aa0..b3a7b1230f 100644 --- a/website/client/components/challenges/challengeDetail.vue +++ b/website/client/components/challenges/challengeDetail.vue @@ -317,7 +317,7 @@ export default { }, createTask (type) { this.taskFormPurpose = 'create'; - this.creatingTask = taskDefaults({type, text: ''}); + this.creatingTask = taskDefaults({type, text: ''}, this.user); this.workingTask = this.creatingTask; // Necessary otherwise the first time the modal is not rendered Vue.nextTick(() => { diff --git a/website/client/components/chat/copyAsTodoModal.vue b/website/client/components/chat/copyAsTodoModal.vue index daea542136..cd29534ee6 100644 --- a/website/client/components/chat/copyAsTodoModal.vue +++ b/website/client/components/chat/copyAsTodoModal.vue @@ -45,7 +45,7 @@ export default { type: 'todo', notes, }; - this.task = taskDefaults(newTask); + this.task = taskDefaults(newTask, this.$store.state.user.data); this.$root.$emit('bv::show::modal', 'copyAsTodo'); }); }, diff --git a/website/client/components/group-plans/taskInformation.vue b/website/client/components/group-plans/taskInformation.vue index 01aeb6c3e8..b65b216a11 100644 --- a/website/client/components/group-plans/taskInformation.vue +++ b/website/client/components/group-plans/taskInformation.vue @@ -401,7 +401,7 @@ export default { }, createTask (type) { this.taskFormPurpose = 'create'; - this.creatingTask = taskDefaults({type, text: ''}); + this.creatingTask = taskDefaults({type, text: ''}, this.user); this.workingTask = this.creatingTask; // Necessary otherwise the first time the modal is not rendered Vue.nextTick(() => { diff --git a/website/client/components/tasks/column.vue b/website/client/components/tasks/column.vue index 58c2ff139f..4ea4ea606e 100644 --- a/website/client/components/tasks/column.vue +++ b/website/client/components/tasks/column.vue @@ -540,7 +540,7 @@ export default { const tasks = text.split('\n').reverse().filter(taskText => { return taskText ? true : false; }).map(taskText => { - const task = taskDefaults({type: this.type, text: taskText}); + const task = taskDefaults({type: this.type, text: taskText}, this.user); task.tags = this.selectedTags; return task; }); diff --git a/website/client/components/tasks/user.vue b/website/client/components/tasks/user.vue index bf5dd16585..18fd696b3a 100644 --- a/website/client/components/tasks/user.vue +++ b/website/client/components/tasks/user.vue @@ -445,7 +445,7 @@ export default { }, createTask (type) { this.openCreateBtn = false; - this.creatingTask = taskDefaults({type, text: ''}); + this.creatingTask = taskDefaults({type, text: ''}, this.user); this.creatingTask.tags = this.selectedTags; // Necessary otherwise the first time the modal is not rendered diff --git a/website/common/script/libs/taskDefaults.js b/website/common/script/libs/taskDefaults.js index 52c8f2f54a..8408119585 100644 --- a/website/common/script/libs/taskDefaults.js +++ b/website/common/script/libs/taskDefaults.js @@ -9,7 +9,7 @@ import moment from 'moment'; const tasksTypes = ['habit', 'daily', 'todo', 'reward']; -module.exports = function taskDefaults (task = {}) { +module.exports = function taskDefaults (task, user) { if (!task.type || tasksTypes.indexOf(task.type) === -1) { task.type = 'habit'; } @@ -65,6 +65,14 @@ module.exports = function taskDefaults (task = {}) { } if (task.type === 'daily') { + let now = moment().zone(user.preferences.timezoneOffset); + let startOfDay = now.clone().startOf('day'); + let startOfDayWithCDSTime = startOfDay + .clone() + .add({ + hours: user.preferences.dayStart, + }); + defaults(task, { streak: 0, repeat: { @@ -76,7 +84,10 @@ module.exports = function taskDefaults (task = {}) { s: true, su: true, }, - startDate: moment().startOf('day').toDate(), + // If cron will happen today, start the daily yesterday + startDate: startOfDayWithCDSTime.isAfter(now) ? + startOfDay.clone().subtract(1, 'day').toDate() : + startOfDay.toDate(), everyX: 1, frequency: 'weekly', daysOfMonth: [], diff --git a/website/common/script/ops/addTask.js b/website/common/script/ops/addTask.js index 506bde22b8..92badf72fc 100644 --- a/website/common/script/ops/addTask.js +++ b/website/common/script/ops/addTask.js @@ -4,7 +4,7 @@ import clone from 'lodash/clone'; // TODO move to client since it's only used there? module.exports = function addTask (user, req = {body: {}}) { - let task = taskDefaults(req.body); + let task = taskDefaults(req.body, user); user.tasksOrder[`${task.type}s`].unshift(task._id); user[`${task.type}s`].unshift(task);