Leaving a group (#8517)

* Leaving a group or a guild no longer removes the user from the challenges of that group or guild.

* Updating api docs for leaving group to take into account the default path no longer leaving challenges when leaving a group.

* Updating api docs for leaving group to take into account the default path no longer leaving challenges when leaving a group.

* refactored according to blade's comments to not be a breaking change. The api now accepts a body parameter to specify wether the user
should remain in the groups challenges or leave them. The change also adds more tests around this behavior to confirm that it works
as expected.
This commit is contained in:
Keith Holliday
2017-02-27 13:58:30 -07:00
committed by GitHub
parent 30954fe7c5
commit 68a042cdb9
10 changed files with 324 additions and 279 deletions

View File

@@ -900,7 +900,7 @@ schema.statics.tavernBoss = async function tavernBoss (user, progress) {
}
};
schema.methods.leave = async function leaveGroup (user, keep = 'keep-all') {
schema.methods.leave = async function leaveGroup (user, keep = 'keep-all', keepChallenges = 'leave-challenges') {
let group = this;
let update = {};
@@ -908,16 +908,18 @@ schema.methods.leave = async function leaveGroup (user, keep = 'keep-all') {
throw new NotAuthorized(shared.i18n.t('cannotDeleteActiveGroup'));
}
// Unlink user challenge tasks
let challenges = await Challenge.find({
_id: {$in: user.challenges},
group: group._id,
}).exec();
// only remove user from challenges if it's set to leave-challenges
if (keepChallenges === 'leave-challenges') {
let challenges = await Challenge.find({
_id: {$in: user.challenges},
group: group._id,
}).exec();
let challengesToRemoveUserFrom = challenges.map(chal => {
return chal.unlinkTasks(user, keep);
});
await Bluebird.all(challengesToRemoveUserFrom);
let challengesToRemoveUserFrom = challenges.map(chal => {
return chal.unlinkTasks(user, keep);
});
await Bluebird.all(challengesToRemoveUserFrom);
}
// Unlink group tasks)
let assignedTasks = await Tasks.Task.find({