Cancel users free group plan when they leave a group (#9543)

* Cancel users free group plan when they leave a group

* Fixed lint
This commit is contained in:
Keith Holliday
2017-11-20 12:34:41 -06:00
committed by GitHub
parent 20a99e526d
commit 5f468d16b7
3 changed files with 76 additions and 12 deletions

View File

@@ -275,6 +275,19 @@ schema.methods.daysUserHasMissed = function daysUserHasMissed (now, req = {}) {
return {daysMissed, timezoneOffsetFromUserPrefs};
};
async function getUserGroupData (user) {
const userGroups = user.getGroups();
const groups = await Group
.find({
_id: {$in: userGroups},
})
.select('leaderOnly leader purchased')
.exec();
return groups;
}
// Determine if the user can get gems: some groups restrict their members ability to obtain them.
// User is allowed to buy gems if no group has `leaderOnly.getGems` === true or if
// its the group leader
@@ -286,16 +299,17 @@ schema.methods.canGetGems = async function canObtainGems () {
return true;
}
const userGroups = user.getGroups();
const groups = await Group
.find({
_id: {$in: userGroups},
})
.select('leaderOnly leader purchased')
.exec();
const groups = await getUserGroupData(user);
return groups.every(g => {
return !g.isSubscribed() || g.leader === user._id || g.leaderOnly.getGems !== true;
});
};
schema.methods.isMemberOfGroupPlan = async function isMemberOfGroupPlan () {
const groups = await getUserGroupData(this);
return groups.every(g => {
return g.isSubscribed();
});
};