Mark todos and dailies as completed via api

This commit is contained in:
Daniel Saewitz
2013-03-22 16:01:40 -04:00
parent 7c77126129
commit b56bc6b1d9
2 changed files with 38 additions and 10 deletions
+21 -9
View File
@@ -160,7 +160,8 @@ router.get '/user/tasks', auth, (req, res) ->
###
scoreTask = (req, res, next) ->
{taskId, direction} = req.params
{title, service, icon} = req.body
{title, service, icon, type} = req.body
type ||= 'habit'
# Send error responses for improper API call
return res.send(500, ':taskId required') unless taskId
@@ -171,21 +172,32 @@ scoreTask = (req, res, next) ->
model.ref('_user', user)
# Create task if doesn't exist
existingTask = model.at "_user.tasks.#{taskId}"
# TODO add service & icon to task
unless model.get("_user.tasks.#{taskId}")
model.refList "_habitList", "_user.tasks", "_user.habitIds"
model.at('_habitList').push
# If task exists, set it's compltion
if existingTask.get()
# Set completed if type is daily or todo
existingTask.set 'completed', (direction is 'up') if /^(daily|todo)$/.test existingTask.get('type')
else
task =
id: taskId
type: 'habit'
type: type
text: (title || taskId)
value: 0
up: true
down: true
notes: "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
switch type
when 'habit'
task.up = true
task.down = true
when 'daily', 'todo'
task.completed = direction is 'up'
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
model.at("_#{type}List").push task
delta = scoring.score(model, taskId, direction)
result = model.get ('_user.stats')
result = model.get '_user.stats'
result.delta = delta
res.json result