Moved inivtation removal functions to group pre remove middleware

This commit is contained in:
TheHollidayInn
2015-09-08 15:58:53 -05:00
parent 82a3ca8a4d
commit c7d09acd74

View File

@@ -84,6 +84,27 @@ GroupSchema.pre('save', function(next){
next(); next();
}) })
GroupSchema.pre('remove', function(next) {
var group = this;
async.waterfall([
function(cb2) {
User.find({
'invitations.guilds.id': group._id
}, cb2);
},
function(users, cb2) {
if (users) {
users.forEach(function (user, index, array) {
var i = _.findIndex(user.invitations.guilds, {id: group._id});
user.invitations.guilds.splice(i, 1);
user.save();
});
}
cb2();
}
], next);
});
GroupSchema.methods.toJSON = function(){ GroupSchema.methods.toJSON = function(){
var doc = this.toObject(); var doc = this.toObject();
removeDuplicates(doc); removeDuplicates(doc);
@@ -404,29 +425,12 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
group.type === 'party' || group.type === 'party' ||
(group.type === 'guild' && group.privacy === 'private') (group.type === 'guild' && group.privacy === 'private')
)){ )){
async.waterfall([ groupWasRemoved = true;
function(cb2) { Group.findOne({
User.find({ _id: group._id
'invitations.guilds.id': group._id }, function(err, groupDoc){
}, cb2); groupDoc.remove(cb)
}, });
function(users, cb2) {
if (users) {
users.forEach(function (user, index, array) {
var i = _.findIndex(user.invitations.guilds, {id: group._id});
user.invitations.guilds.splice(i, 1);
user.save();
});
}
cb2();
},
function(cb2) {
groupWasRemoved = true;
Group.remove({
_id: group._id
}, cb2);
},
], cb);
}else{ // otherwise just remove a member }else{ // otherwise just remove a member
var update = {$pull: {members: user._id}}; var update = {$pull: {members: user._id}};