diff --git a/src/server/api.coffee b/src/server/api.coffee index c644a4af52..ed16e54ced 100644 --- a/src/server/api.coffee +++ b/src/server/api.coffee @@ -74,7 +74,7 @@ router.get '/user/task/:id', auth, (req, res) -> ### validateTask = (req, res, next) -> task = {} - newTask = { type, text, notes, value, up, down, completed } = req.body + newTask = { type, text, notes, value, up, down, completed } = req.task || req.body # If we're updating, get the task from the user if req.method is 'PUT' or req.method is 'DELETE' @@ -122,6 +122,28 @@ router.delete '/user/task/:id', auth, validateTask, (req, res) -> res.send 204 +### + POST /user/tasks +### +router.post '/user/tasks', auth, (req, res) -> + for idx, task of req.body + if task.id + if task.del + req.user.del "tasks.#{task.id}" + task = deleted: true + else + req.user.set "tasks.#{task.id}", task + else + model = req.getModel() + type = task.type || 'habit' + model.ref '_user', req.user + model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids" + model.at("_#{type}List").push task + req.body[idx] = task + + res.json 201, req.body + + ### POST /user/task/ ### diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index d6331b9a2c..929cbfa3cf 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -300,3 +300,41 @@ describe 'API', -> query.fetch (err, user) -> expect(user.get("tasks.#{tid}.completed")).to.be true done() + + it 'POST /api/v1/user/task (array)', (done) -> + habitId = currentUser.habitIds[0] + dailyId = currentUser.dailyIds[0] + arr = [{ + id: habitId + text: 'hello' + notes: 'note' + },{ + text: 'new task' + notes: 'notes!' + },{ + id: dailyId + del: true + }] + + request.post("#{baseURL}/user/tasks") + .set('Accept', 'application/json') + .set('X-API-User', currentUser.id) + .set('X-API-Key', currentUser.apiToken) + .send(arr) + .end (res) -> + expect(res.body.err).to.be undefined + expect(res.statusCode).to.be 201 + expect(res.body[0]).to.eql {id: habitId,text: 'hello',notes: 'note'} + expect(res.body[1].id).to.be.a 'string' + expect(res.body[1].text).to.be 'new task' + expect(res.body[1].notes).to.be 'notes!' + expect(res.body[2]).to.eql deleted: true + + query = model.query('users').withIdAndToken(currentUser.id, currentUser.apiToken) + query.fetch (err, user) -> + expect(user.get("tasks.#{habitId}")).to.eql {id: habitId,text: 'hello',notes: 'note'} + expect(user.get("tasks.#{dailyId}")).to.be undefined + expect(user.get("tasks.#{res.body[1].id}")).to.eql id: res.body[1].id, text: 'new task', notes: 'notes!' + done() + +