Make POST /db/classroom/:id/members use update to avoid race conditions

This commit is contained in:
Scott Erickson 2015-11-05 13:07:36 -08:00
parent 6de6479f3b
commit f57cf3a83d
2 changed files with 8 additions and 4 deletions

View file

@ -57,10 +57,11 @@ ClassroomHandler = class ClassroomHandler extends Handler
members = _.clone(classroom.get('members'))
if _.any(members, (memberID) -> memberID.equals(req.user.get('_id')))
return @sendSuccess(res, @formatEntity(req, classroom))
update = { $push: { members : req.user.get('_id')}}
classroom.update update, (err) =>
return @sendDatabaseError(res, err) if err
members.push req.user.get('_id')
classroom.set('members', members)
classroom.save (err, classroom) =>
return @sendDatabaseError(res, err) if err
return @sendSuccess(res, @formatEntity(req, classroom))
formatEntity: (req, doc) ->

View file

@ -123,12 +123,15 @@ describe 'POST /db/classroom/:id/members', ->
data = { name: 'Classroom 5' }
request.post {uri: classroomsURL, json: data }, (err, res, body) ->
classroomCode = body.code
classroomID = body._id
expect(res.statusCode).toBe(200)
loginNewUser (user2) ->
url = getURL("/db/classroom/~/members")
data = { code: classroomCode }
request.post { uri: url, json: data }, (err, res, body) ->
expect(res.statusCode).toBe(200)
Classroom.findById classroomID, (err, classroom) ->
expect(classroom.get('members').length).toBe(1)
done()