mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Free group plan subscription from a party/guild no longer gets cancelled after leaving a guild - fixes #11055 (#11228)
* bug fix: free group plan subscription from party/guild is no longer cancelled when leaving a guild * free group plan subscription from party/guild no longer gets cancelled when leaving a guild * bug fix: free group plan subscription from party/guild is no longer cancelled when leaving a guild * free group plan subscription from party/guild no longer gets cancelled when leaving a guild * Added tests to check if a member of a group plan maintains free subscription when leaving another group without a plan * Added tests to check if member of a group plan still keeps free subscription when leaving some other group without a plan * bug fix: free group plan subscription from party/guild is no longer cancelled when leaving a guild * free group plan subscription from party/guild no longer gets cancelled when leaving a guild * bug fix: free group plan subscription from party/guild is no longer cancelled when leaving a guild * free group plan subscription from party/guild no longer gets cancelled when leaving a guild * Added tests to check if a member of a group plan maintains free subscription when leaving another group without a plan * Added tests to check if member of a group plan still keeps free subscription when leaving some other group without a plan
This commit is contained in:
committed by
Sabe Jones
parent
f0e6703546
commit
5a83f93ade
@@ -276,25 +276,26 @@ describe('POST /groups/:groupId/leave', () => {
|
||||
});
|
||||
});
|
||||
|
||||
context('Leaving a group plan', () => {
|
||||
it('cancels the free subscription', async () => {
|
||||
// Create group
|
||||
each(typesOfGroups, (groupDetails, groupType) => {
|
||||
context(`Leaving a group plan when the group is a ${groupType}`, () => {
|
||||
let groupWithPlan;
|
||||
let leader;
|
||||
let member;
|
||||
|
||||
beforeEach(async () => {
|
||||
let { group, groupLeader, members } = await createAndPopulateGroup({
|
||||
groupDetails: {
|
||||
name: 'Test Private Guild',
|
||||
type: 'guild',
|
||||
},
|
||||
groupDetails,
|
||||
members: 1,
|
||||
});
|
||||
|
||||
let leader = groupLeader;
|
||||
let member = members[0];
|
||||
leader = groupLeader;
|
||||
member = members[0];
|
||||
groupWithPlan = group;
|
||||
let userWithFreePlan = await User.findById(leader._id).exec();
|
||||
|
||||
// Create subscription
|
||||
let paymentData = {
|
||||
user: userWithFreePlan,
|
||||
groupId: group._id,
|
||||
groupId: groupWithPlan._id,
|
||||
sub: {
|
||||
key: 'basic_3mo',
|
||||
},
|
||||
@@ -307,13 +308,38 @@ describe('POST /groups/:groupId/leave', () => {
|
||||
};
|
||||
await payments.createSubscription(paymentData);
|
||||
await member.sync();
|
||||
});
|
||||
|
||||
it('cancels the free subscription', async () => {
|
||||
expect(member.purchased.plan.planId).to.equal('group_plan_auto');
|
||||
expect(member.purchased.plan.dateTerminated).to.not.exist;
|
||||
|
||||
// Leave
|
||||
await member.post(`/groups/${group._id}/leave`);
|
||||
await member.post(`/groups/${groupWithPlan._id}/leave`);
|
||||
await member.sync();
|
||||
expect(member.purchased.plan.dateTerminated).to.exist;
|
||||
});
|
||||
|
||||
it('preserves the free subscription when leaving a any other group without a plan', async () => {
|
||||
// Joining a guild without a group plan
|
||||
let { group: groupWithNoPlan } = await createAndPopulateGroup({
|
||||
groupDetails: {
|
||||
name: 'Group Without Plan',
|
||||
type: 'guild',
|
||||
privacy: 'public',
|
||||
},
|
||||
});
|
||||
|
||||
await member.post(`/groups/${groupWithNoPlan._id}/join`);
|
||||
await member.sync();
|
||||
expect(member.purchased.plan.planId).to.equal('group_plan_auto');
|
||||
expect(member.purchased.plan.dateTerminated).to.not.exist;
|
||||
|
||||
// Leaving the guild without a group plan
|
||||
await member.post(`/groups/${groupWithNoPlan._id}/leave`);
|
||||
await member.sync();
|
||||
expect(member.purchased.plan.dateTerminated).to.not.exist;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -776,7 +776,7 @@ api.leaveGroup = {
|
||||
|
||||
if (group.type !== 'party') {
|
||||
let guildIndex = user.guilds.indexOf(group._id);
|
||||
user.guilds.splice(guildIndex, 1);
|
||||
if (guildIndex >= 0) user.guilds.splice(guildIndex, 1);
|
||||
}
|
||||
|
||||
let isMemberOfGroupPlan = await user.isMemberOfGroupPlan();
|
||||
|
||||
@@ -211,13 +211,13 @@ async function cancelGroupSubscriptionForUser (user, group, userWasRemoved = fal
|
||||
if (user.party._id) userGroups.push(user.party._id);
|
||||
|
||||
let index = userGroups.indexOf(group._id);
|
||||
userGroups.splice(index, 1);
|
||||
if (index >= 0) userGroups.splice(index, 1);
|
||||
|
||||
let groupPlansQuery = {
|
||||
type: {$in: ['guild', 'party']},
|
||||
// type: { $in: ['guild', 'party'] },
|
||||
// privacy: 'private',
|
||||
_id: {$in: userGroups},
|
||||
'purchased.plan.dateTerminated': null,
|
||||
'purchased.plan.dateTerminated': { $type: 'null' },
|
||||
};
|
||||
|
||||
let groupFields = `${basicGroupFields} purchased`;
|
||||
|
||||
@@ -392,7 +392,7 @@ schema.methods.canGetGems = async function canObtainGems () {
|
||||
schema.methods.isMemberOfGroupPlan = async function isMemberOfGroupPlan () {
|
||||
const groups = await getUserGroupData(this);
|
||||
|
||||
return groups.every(g => {
|
||||
return groups.some(g => {
|
||||
return g.isSubscribed();
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user