Removed redundant code when removing group invitations

This commit is contained in:
TheHollidayInn
2015-09-11 12:14:01 -05:00
parent 5c8c5a9ed3
commit 26e6126a25

View File

@@ -87,36 +87,23 @@ GroupSchema.pre('save', function(next){
GroupSchema.pre('remove', function(next) { GroupSchema.pre('remove', function(next) {
var group = this; var group = this;
if ( group.type == "guild" ) {
async.waterfall([ async.waterfall([
function(cb) { function(cb) {
User.find({ var invitationQuery = {};
'invitations.guilds.id': group._id var groupType = group.type;
}, cb); //Add an 's' to group type guild because the model has the plural version
if (group.type == "guild") groupType += "s";
invitationQuery['invitations.' + groupType + '.id'] = group._id;
User.find(invitationQuery, cb);
}, },
function(users, cb) { function(users, cb) {
if (users) { if (users) {
users.forEach(function (user, index, array) { users.forEach(function (user, index, array) {
if ( group.type == "party" ) {
user.invitations.party = {};
} else {
var i = _.findIndex(user.invitations.guilds, {id: group._id}); var i = _.findIndex(user.invitations.guilds, {id: group._id});
user.invitations.guilds.splice(i, 1); user.invitations.guilds.splice(i, 1);
user.save();
});
}
cb();
}
], next);
} else if ( group.type == "party" ) {
async.waterfall([
function(cb) {
User.find({
'invitations.party.id': group._id
}, cb);
},
function(users, cb) {
if (users) {
users.forEach(function (user, index, array) {
if (user.invitations.party.id === group._id) {
user.invitations.party = {};
} }
user.save(); user.save();
}); });
@@ -124,9 +111,6 @@ GroupSchema.pre('remove', function(next) {
cb(); cb();
} }
], next); ], next);
}
}); });
GroupSchema.post('remove', function(group) { GroupSchema.post('remove', function(group) {