refactor(groups): cleanup leave-group code

This commit is contained in:
Tyler Renelle 2014-07-11 14:43:11 -06:00
parent 95193f550d
commit a8b2b32557
2 changed files with 33 additions and 32 deletions

View file

@ -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){

View file

@ -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)