Revert "Revert "Removed user's party invites when last user leaves party""

This commit is contained in:
Kevin Gisi
2015-09-17 23:27:21 -04:00
parent e15cc9ec82
commit 4525ee5358
2 changed files with 61 additions and 7 deletions

View File

@@ -172,11 +172,11 @@ describe "Guilds", ->
cb()
], done
it "deletes all invites to a group when the last member leaves", (done) ->
it "deletes all invites to a group (guild) when the last member leaves", (done) ->
groupToDeleteAfterLeave = undefined
userToRemoveInvite = undefined
request.post(baseURL + "/groups").send(
name: "TestGroupToDeleteAfteLeave"
name: "TestGroupToDeleteAfterLeave"
type: "guild"
privacy: "private"
).end (res) ->
@@ -217,6 +217,53 @@ describe "Guilds", ->
cb()
], done
it "deletes all invites to a group (party) when the last member leaves", (done) ->
partyToDeleteAfterLeave = undefined
userToRemovePartyInvite = undefined
request.post(baseURL + "/groups").send(
name: "TestPartyToDeleteAfterLeave"
type: "party"
).end (res) ->
partyToDeleteAfterLeave = res.body
async.waterfall [
(cb) ->
registerManyUsers 1, cb
(_members, cb) ->
userToRemovePartyInvite = _members[0]
inviteURL = baseURL + "/groups/" + partyToDeleteAfterLeave._id + "/invite"
request.post(inviteURL).send(
uuids: [userToRemovePartyInvite._id]
)
.end ->
cb()
(cb) ->
request.post(baseURL + "/groups/" + partyToDeleteAfterLeave._id + "/leave")
.end (res) ->
expectCode res, 204
cb()
(cb) ->
request.get(baseURL + '/user')
.set("X-API-User", userToRemovePartyInvite._id)
.set("X-API-Key", userToRemovePartyInvite.apiToken)
.end (err, res) ->
expectCode res, 200
party = partyToDeleteAfterLeave
partyInvitation = _.find(res.body.invitations.party, (invite) ->
return invite == party._id
)
expect(partyInvitation).to.not.exist
cb()
(cb) ->
request.post(baseURL + "/groups/" + partyToDeleteAfterLeave._id)
.end (res) ->
expectCode res, 404
cb()
], done
context "removing users groups", ->
it "allows guild leaders to remove a member (but not themselves)", (done) ->
guildToRemoveMember = undefined

View File

@@ -89,15 +89,22 @@ GroupSchema.pre('remove', function(next) {
var group = this;
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) {
var i = _.findIndex(user.invitations.guilds, {id: group._id});
user.invitations.guilds.splice(i, 1);
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();
});
}