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

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

View file

@ -272,7 +272,7 @@ describe 'API', ->
expect(res.body.err).to.be 'No task found.'
done()
it 'POST /api/v1/user/task/:id/up', (done) ->
it 'POST /api/v1/user/task/:id/up (habit)', (done) ->
tid = currentUser.habitIds[0]
request.post("#{baseURL}/user/task/#{tid}/up")
.set('Accept', 'application/json')
@ -284,3 +284,19 @@ describe 'API', ->
expect(res.statusCode).to.be 200
expect(res.body).to.eql { gp: 1, exp: 7.5, lvl: 1, hp: 50, delta: 1 }
done()
it 'POST /api/v1/user/task/:id/up (daily)', (done) ->
tid = currentUser.dailyIds[0]
request.post("#{baseURL}/user/task/#{tid}/up")
.set('Accept', 'application/json')
.set('X-API-User', currentUser.id)
.set('X-API-Key', currentUser.apiToken)
.send({})
.end (res) ->
expect(res.body.err).to.be undefined
expect(res.statusCode).to.be 200
expect(res.body).to.eql { gp: 2, exp: 15, lvl: 1, hp: 50, delta: 1 }
query = model.query('users').withIdAndToken(currentUser.id, currentUser.apiToken)
query.fetch (err, user) ->
expect(user.get("tasks.#{tid}.completed")).to.be true
done()