[#1708] fix to taskDefault when creating a task and scoring

This commit is contained in:
Tyler Renelle
2013-11-01 12:45:13 -07:00
parent 2da33b0355
commit fbbcecc6e2
2 changed files with 11 additions and 14 deletions
+7 -10
View File
@@ -69,10 +69,9 @@ function deleteTask(user, task) {
}; };
function addTask(user, task) { function addTask(user, task) {
var type = task.type || 'habit' task = helpers.taskDefaults(task);
user[type+'s'].unshift(task); user[task.type+'s'].unshift(task);
// FIXME will likely have to use taskSchema instead, so we can populate the defaults, add the _id, and return the added task return task;
return user[task.type+'s'][0];
} }
/* /*
@@ -109,11 +108,10 @@ api.scoreTask = function(req, res, next) {
return res.json(500, {err: ":direction must be 'up' or 'down'"}); return res.json(500, {err: ":direction must be 'up' or 'down'"});
} }
// If exists already, score it // If exists already, score it
var existing; if (task = user.tasks[id]) {
if (existing = user.tasks[id]) {
// Set completed if type is daily or todo and task exists // Set completed if type is daily or todo and task exists
if (existing.type === 'daily' || existing.type === 'todo') { if (task.type === 'daily' || task.type === 'todo') {
existing.completed = direction === 'up'; task.completed = direction === 'up';
} }
} else { } else {
// If it doesn't exist, this is likely a 3rd party up/down - create a new one, then score it // If it doesn't exist, this is likely a 3rd party up/down - create a new one, then score it
@@ -130,9 +128,8 @@ api.scoreTask = function(req, res, next) {
if (task.type === 'daily' || task.type === 'todo') { if (task.type === 'daily' || task.type === 'todo') {
task.completed = direction === 'up'; task.completed = direction === 'up';
} }
addTask(user, task); task = addTask(user, task);
} }
task = user.tasks[id];
var delta = algos.score(user, task, direction); var delta = algos.score(user, task, direction);
//user.markModified('flags'); //user.markModified('flags');
user.save(function(err, saved) { user.save(function(err, saved) {