diff --git a/common/script/index.coffee b/common/script/index.coffee index 191c2e12d2..5881a9be6d 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -186,6 +186,13 @@ preenHistory = (history) -> newHistory + +### + Preen 3-day past-completed To-Dos from Angular & mobile app +### +api.preenTodos = (tasks) -> + _.where(tasks, (t) -> !t.completed || (t.challenge && t.challenge.id) || moment(t.dateCompleted).isAfter(moment().subtract({days:3}))) + ### Update the in-browser store with new gear. FIXME this was in user.fns, but it was causing strange issues there ### @@ -613,6 +620,11 @@ api.wrap = (user, main=true) -> return cb?({code:404, message: i18n.t('messageTaskNotFound', req.language)}) unless task return cb?('?to=__&from=__ are required') unless to? and from? tasks = user["#{task.type}s"] + if task.type is 'todo' and tasks[from] isnt task # client indices don't match because of preened tasks + preenedTasks = api.preenTodos(tasks); + to = tasks.indexOf(preenedTasks[to]) unless to == -1 # Push To Bottom doesn't require readjustment + from = tasks.indexOf(preenedTasks[from]) + return cb?({code:404, message: i18n.t('messageTaskNotFound', req.language)}) unless tasks[from] is task movedTask = tasks.splice(from, 1)[0] if to == -1 # we've used the Push To Bottom feature tasks.push(movedTask) diff --git a/website/src/controllers/user.js b/website/src/controllers/user.js index 80a8321191..b77d10df7f 100644 --- a/website/src/controllers/user.js +++ b/website/src/controllers/user.js @@ -635,9 +635,7 @@ api.batchUpdate = function(req, res, next) { // Fetch full user object } else if (response.wasModified){ // Preen 3-day past-completed To-Dos from Angular & mobile app - response.todos = _.where(response.todos, function(t) { - return !t.completed || (t.challenge && t.challenge.id) || moment(t.dateCompleted).isAfter(moment().subtract({days:3})); - }); + response.todos = shared.preenTodos(response.todos); res.json(200, response); // return only the version number