diff --git a/test/api/v3/integration/tasks/GET-tasks_user.test.js b/test/api/v3/integration/tasks/GET-tasks_user.test.js index d1a24ebfc8..87af87d7a5 100644 --- a/test/api/v3/integration/tasks/GET-tasks_user.test.js +++ b/test/api/v3/integration/tasks/GET-tasks_user.test.js @@ -1,3 +1,4 @@ +import moment from 'moment'; import { generateUser, } from '../../../../helpers/api-integration/v3'; @@ -127,4 +128,26 @@ describe('GET /tasks/user', () => { let allCompletedTodos = await user.get('/tasks/user?type=_allCompletedTodos'); expect(allCompletedTodos.length).to.equal(numberOfTodos); }); + + it('returns dailies with isDue for the date specified', async () => { + let startDate = moment().subtract('1', 'days').toDate(); + let createdTasks = await user.post('/tasks/user', [ + { + text: 'test daily', + type: 'daily', + startDate, + frequency: 'daily', + everyX: 2, + }, + ]); + let dailys = await user.get('/tasks/user?type=dailys'); + + expect(dailys.length).to.be.at.least(1); + expect(dailys[0]._id).to.equal(createdTasks._id); + expect(dailys[0].isDue).to.be.false; + + let dailys2 = await user.get(`/tasks/user?type=dailys&dueDate=${startDate}`); + expect(dailys2[0]._id).to.equal(createdTasks._id); + expect(dailys2[0].isDue).to.be.true; + }); }); diff --git a/website/server/controllers/api-v3/tasks.js b/website/server/controllers/api-v3/tasks.js index c48ce8d47c..9105bb1c62 100644 --- a/website/server/controllers/api-v3/tasks.js +++ b/website/server/controllers/api-v3/tasks.js @@ -272,8 +272,9 @@ api.getUserTasks = { if (validationErrors) throw validationErrors; let user = res.locals.user; + let dueDate = req.query.dueDate; - let tasks = await getTasks(req, res, {user}); + let tasks = await getTasks(req, res, {user, dueDate}); return res.respond(200, tasks); }, }; diff --git a/website/server/libs/taskManager.js b/website/server/libs/taskManager.js index 8c4ecb1c79..3b8759965b 100644 --- a/website/server/libs/taskManager.js +++ b/website/server/libs/taskManager.js @@ -1,3 +1,4 @@ +import moment from 'moment'; import * as Tasks from '../models/task'; import { BadRequest, @@ -22,13 +23,16 @@ async function _validateTaskAlias (tasks, res) { }); } -export function setNextDue (task, user) { +export function setNextDue (task, user, dueDateOption) { if (task.type !== 'daily') return; + let dateTaskIsDue = Date.now(); + if (dueDateOption) dateTaskIsDue = moment(dueDateOption); + console.log(dueDateOption) let optionsForShouldDo = user.preferences.toObject(); - task.isDue = shared.shouldDo(Date.now(), task, optionsForShouldDo); + task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo); optionsForShouldDo.nextDue = true; - let nextDue = shared.shouldDo(Date.now(), task, optionsForShouldDo); + let nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo); if (nextDue && nextDue.length > 0) { task.nextDue = nextDue.map((dueDate) => { return dueDate.toISOString(); @@ -120,6 +124,7 @@ export async function getTasks (req, res, options = {}) { user, challenge, group, + dueDate, } = options; let query = {userId: user._id}; @@ -185,6 +190,8 @@ export async function getTasks (req, res, options = {}) { } else { orderedTasks[i] = task; } + + if (dueDate) setNextDue(task, user, dueDate); }); // Remove empty values from the array and add any unordered task