diff --git a/src/controllers/groups.js b/src/controllers/groups.js index 3bdfd601c6..ebb0970f9e 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -335,37 +335,38 @@ api.leave = function(req, res, next) { user.save(cb); }, // Remove user from group challenges - function(cb){ - async.waterfall([ + function(cb){ + async.waterfall([ // Find relevant challenges - function(cb) { - Challenge.find({$and:[ - {_id: {$in: user.challenges}}, // Challenges I am in - {group: group._id}, // that belong to the group I am leaving - ]}, cb); - }, - function(challenges, cb) { - // Update each challenge - Challenge.update({_id:{$in: _.pluck(challenges, '_id')}}, - {$pull:{members:user._id}}, - {multi: true}, function(err) { - if (err) return cb(err); - cb(null, challenges); - }); - }, - function(challenges, cb) { - // Unlink the challenge tasks from user - async.waterfall(challenges.map(function(chal) { - return function(cb) { - var i = user.challenges.indexOf(chal._id) - if (~i) user.challenges.splice(i,1); - user.unlink({cid:chal._id, keep:keep}, function(err){ - if (err) return cb(err); - cb(null); - }); - }}), cb); - }], cb); - }, + function(cb2) { + Challenge.find({ + _id: {$in: user.challenges}, // Challenges I am in + group: group._id // that belong to the group I am leaving + }, cb2); + }, + // Update each challenge + function(challenges, cb2) { + Challenge.update( + {_id:{$in: _.pluck(challenges, '_id')}}, + {$pull:{members:user._id}}, + {multi: true}, + function(err) { + cb2(err, challenges); // pass `challenges` above to cb + } + ); + }, + // Unlink the challenge tasks from user + function(challenges, cb2) { + async.waterfall(challenges.map(function(chal) { + return function(cb3) { + var i = user.challenges.indexOf(chal._id) + if (~i) user.challenges.splice(i,1); + user.unlink({cid:chal._id, keep:keep}, cb3); + } + }), cb2); + } + ], cb); + }, function(cb){ var update = {$pull:{members:user._id}}; if (group.type == 'party' && group.quest.key){ diff --git a/test/api.mocha.js b/test/api.mocha.js index 6bdb128474..681c3aef98 100644 --- a/test/api.mocha.js +++ b/test/api.mocha.js @@ -533,7 +533,7 @@ describe('API', function () { }, function(_user,cb){ user = _user; - request.get(baseURL+'/groups/party').end(function(res){cb(null,res.body)}); + Group.findById(group._id,cb); }, function(_group,cb){ var cummExp = shared.content.quests.vice3.drop.exp + shared.content.quests.dilatory.drop.exp; @@ -563,7 +563,7 @@ describe('API', function () { expect(_.find(_group.members,{_id:party[0]._id}).items.gear.owned.weapon_special_2).to.be(true); expect(_.find(_group.members,{_id:party[1]._id}).items.gear.owned.weapon_special_2).to.be(true); expect(_.find(_group.members,{_id:party[2]._id}).items.gear.owned.weapon_special_2).to.not.be.ok(); - cb2 + cb2() } ],cb)