From 67bcfde6dc73b32f24b6b1a54e80a80f268c317c Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Mon, 30 Nov 2015 20:14:53 +0100 Subject: [PATCH] implement move task --- common/locales/en/api-v3.json | 3 +- website/src/controllers/api-v3/tasks.js | 50 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/common/locales/en/api-v3.json b/common/locales/en/api-v3.json index 8a0c5d8930..d9fb392743 100644 --- a/common/locales/en/api-v3.json +++ b/common/locales/en/api-v3.json @@ -21,5 +21,6 @@ "cantDeleteChallengeTasks": "A task belonging to a challenge can't be deleted.", "checklistOnlyDailyTodo": "Checklists are supported only on dailies and todos", "checklistItemNotFound": "No checklist item was wound with given id.", - "itemIdRequired": "\"itemId\" must be a valid UUID" + "itemIdRequired": "\"itemId\" must be a valid UUID", + "positionRequired": "\"position\" is required and must be a number." } diff --git a/website/src/controllers/api-v3/tasks.js b/website/src/controllers/api-v3/tasks.js index e1a31c2079..c6209de2a4 100644 --- a/website/src/controllers/api-v3/tasks.js +++ b/website/src/controllers/api-v3/tasks.js @@ -143,6 +143,56 @@ api.updateTask = { }, }; +// completed todos cannot be moved, they'll be returned ordered by date of completion +/** + * @api {put} /tasks/move/:taskId/to/:position Move a task to a new position + * @apiVersion 3.0.0 + * @apiName MoveTask + * @apiGroup Task + * + * @apiParam {UUID} taskId The task _id + * @apiParam {Number} position Where to move the task (-1 means push to bottom) + * + * @apiSuccess {object} emoty An empty object + */ +api.moveTask = { + method: 'POST', + url: '/tasks/move/:taskId/to/:position', + middlewares: [authWithHeaders()], + handler (req, res, next) { + req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID(); + req.checkParams('position', res.t('positionRequired')).notEmpty().isNumeric(); + + let user = res.locals.user; + let to = Number(req.params.position); + + Tasks.Task.findOne({ + _id: req.params.taskId, + userId: user._id, + }).exec() + .then((task) => { + if (!task) throw new NotFound(res.t('taskNotFound')); + let order = user.tasksOrder[`${task.type}s`]; + let currentIndex = order.indexOf(task._id); + + // If for some reason the task isn't ordered (should never happen) + // or if the task is moved to a non existing position + // or if the task is moved to postion -1 (push to bottom) + // -> push task at end of list + if (currentIndex === -1 || !order[to] || to === -1) { + order.push(task._id); + } else { + let taskToMove = order.splice(currentIndex, 1)[0]; + order.splice(to, 0, taskToMove); + } + + return user.save(); + }) + .then(() => res.respond(200, {})) // TODO what to return + .catch(next); + }, +}; + /** * @api {post} /tasks/:taskId/checklist/addItem Add an item to a checklist, creating the checklist if it doesn't exist * @apiVersion 3.0.0