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) {
var group = this;
if ( group.type == "guild" ) {
async.waterfall([
function(cb) {
User.find({
'invitations.guilds.id': group._id
}, cb);
var invitationQuery = {};
var groupType = group.type;
//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) {
if (users) {
users.forEach(function (user, index, array) {
if ( group.type == "party" ) {
user.invitations.party = {};
} else {
var i = _.findIndex(user.invitations.guilds, {id: group._id});
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();
});
@@ -124,9 +111,6 @@ GroupSchema.pre('remove', function(next) {
cb();
}
], next);
}
});
GroupSchema.post('remove', function(group) {