mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 13:47:33 +01:00
refactor(db-schema): group.isSubscribed() method name changed to group.hasActiveGroupPlan()
This commit is contained in:
@@ -2380,29 +2380,29 @@ describe('Group Model', () => {
|
||||
});
|
||||
});
|
||||
|
||||
context('isSubscribed', () => {
|
||||
context('hasActiveGroupPlan', () => {
|
||||
it('returns false if group does not have customer id', () => {
|
||||
expect(party.isSubscribed()).to.be.undefined;
|
||||
expect(party.hasActiveGroupPlan()).to.be.undefined;
|
||||
});
|
||||
|
||||
it('returns true if group does not have plan.dateTerminated', () => {
|
||||
party.purchased.plan.customerId = 'test-id';
|
||||
|
||||
expect(party.isSubscribed()).to.be.true;
|
||||
expect(party.hasActiveGroupPlan()).to.be.true;
|
||||
});
|
||||
|
||||
it('returns true if group if plan.dateTerminated is after today', () => {
|
||||
party.purchased.plan.customerId = 'test-id';
|
||||
party.purchased.plan.dateTerminated = moment().add(1, 'days').toDate();
|
||||
|
||||
expect(party.isSubscribed()).to.be.true;
|
||||
expect(party.hasActiveGroupPlan()).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false if group if plan.dateTerminated is before today', () => {
|
||||
party.purchased.plan.customerId = 'test-id';
|
||||
party.purchased.plan.dateTerminated = moment().subtract(1, 'days').toDate();
|
||||
|
||||
expect(party.isSubscribed()).to.be.false;
|
||||
expect(party.hasActiveGroupPlan()).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1315,7 +1315,7 @@ api.getGroupPlans = {
|
||||
.select('leaderOnly leader purchased name managers')
|
||||
.exec();
|
||||
|
||||
const groupPlans = groups.filter(group => group.isSubscribed());
|
||||
const groupPlans = groups.filter(group => group.hasActiveGroupPlan());
|
||||
|
||||
res.respond(200, groupPlans);
|
||||
},
|
||||
|
||||
@@ -10,7 +10,7 @@ const questScrolls = shared.content.quests;
|
||||
|
||||
// @TODO: Don't use this method when the group can be saved.
|
||||
export async function getGroupChat (group) {
|
||||
const maxChatCount = group.isSubscribed() ? MAX_SUBBED_GROUP_CHAT_COUNT : MAX_CHAT_COUNT;
|
||||
const maxChatCount = group.hasActiveGroupPlan() ? MAX_SUBBED_GROUP_CHAT_COUNT : MAX_CHAT_COUNT;
|
||||
|
||||
const groupChat = await Chat.find({ groupId: group._id })
|
||||
.limit(maxChatCount)
|
||||
|
||||
@@ -73,7 +73,7 @@ function inviteUserToGuild (userToInvite, group, inviter, publicGuild, res) {
|
||||
publicGuild,
|
||||
};
|
||||
|
||||
if (group.isSubscribed() && !group.hasNotCancelled()) guildInvite.cancelledPlan = true;
|
||||
if (group.hasActiveGroupPlan() && !group.hasNotCancelled()) guildInvite.cancelledPlan = true;
|
||||
|
||||
userToInvite.invitations.guilds.push(guildInvite);
|
||||
}
|
||||
@@ -94,7 +94,7 @@ async function inviteUserToParty (userToInvite, group, inviter, res) {
|
||||
}
|
||||
|
||||
const partyInvite = { id: group._id, name: group.name, inviter: inviter._id };
|
||||
if (group.isSubscribed() && !group.hasNotCancelled()) partyInvite.cancelledPlan = true;
|
||||
if (group.hasActiveGroupPlan() && !group.hasNotCancelled()) partyInvite.cancelledPlan = true;
|
||||
|
||||
userToInvite.invitations.parties.push(partyInvite);
|
||||
userToInvite.invitations.party = partyInvite;
|
||||
@@ -166,7 +166,7 @@ async function inviteByEmail (invite, group, inviter, req, res) {
|
||||
userReturnInfo = invite.email;
|
||||
|
||||
let cancelledPlan = false;
|
||||
if (group.isSubscribed() && !group.hasNotCancelled()) cancelledPlan = true;
|
||||
if (group.hasActiveGroupPlan() && !group.hasNotCancelled()) cancelledPlan = true;
|
||||
|
||||
const groupQueryString = JSON.stringify({
|
||||
id: group._id,
|
||||
|
||||
@@ -73,6 +73,7 @@ async function createSubscription (data) {
|
||||
let itemPurchased = 'Subscription';
|
||||
let purchaseType = 'subscribe';
|
||||
let emailType = 'subscription-begins';
|
||||
let recipientIsSubscribed = recipient.isSubscribed();
|
||||
|
||||
// If we are buying a group subscription
|
||||
if (data.groupId) {
|
||||
@@ -93,6 +94,7 @@ async function createSubscription (data) {
|
||||
itemPurchased = 'Group-Subscription';
|
||||
purchaseType = 'group-subscribe';
|
||||
emailType = 'group-subscription-begins';
|
||||
recipientIsSubscribed = group.hasActiveGroupPlan();
|
||||
groupId = group._id;
|
||||
recipient.purchased.plan.quantity = data.sub.quantity;
|
||||
|
||||
@@ -105,7 +107,7 @@ async function createSubscription (data) {
|
||||
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
||||
plan.extraMonths += months;
|
||||
} else {
|
||||
if (!recipient.isSubscribed() || !plan.dateUpdated) plan.dateUpdated = today;
|
||||
if (!recipientIsSubscribed || !plan.dateUpdated) plan.dateUpdated = today;
|
||||
if (moment(plan.dateTerminated).isAfter()) {
|
||||
plan.dateTerminated = moment(plan.dateTerminated).add({ months }).toDate();
|
||||
} else {
|
||||
|
||||
@@ -153,7 +153,7 @@ schema.plugin(baseModel, {
|
||||
noSet: ['_id', 'balance', 'quest', 'memberCount', 'chat', 'challengeCount', 'tasksOrder', 'purchased', 'managers'],
|
||||
private: ['purchased.plan'],
|
||||
toJSONTransform (plainObj, originalDoc) {
|
||||
if (plainObj.purchased) plainObj.purchased.active = originalDoc.isSubscribed();
|
||||
if (plainObj.purchased) plainObj.purchased.active = originalDoc.hasActiveGroupPlan();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1665,7 +1665,7 @@ schema.methods.checkChatSpam = function groupCheckChatSpam (user) {
|
||||
return false;
|
||||
};
|
||||
|
||||
schema.methods.isSubscribed = function isSubscribed () {
|
||||
schema.methods.hasActiveGroupPlan = function hasActiveGroupPlan () {
|
||||
const now = new Date();
|
||||
const { plan } = this.purchased;
|
||||
return plan && plan.customerId
|
||||
@@ -1674,12 +1674,12 @@ schema.methods.isSubscribed = function isSubscribed () {
|
||||
|
||||
schema.methods.hasNotCancelled = function hasNotCancelled () {
|
||||
const { plan } = this.purchased;
|
||||
return Boolean(this.isSubscribed() && !plan.dateTerminated);
|
||||
return Boolean(this.hasActiveGroupPlan() && !plan.dateTerminated);
|
||||
};
|
||||
|
||||
schema.methods.hasCancelled = function hasNotCancelled () {
|
||||
const { plan } = this.purchased;
|
||||
return Boolean(this.isSubscribed() && plan.dateTerminated);
|
||||
return Boolean(this.hasActiveGroupPlan() && plan.dateTerminated);
|
||||
};
|
||||
|
||||
schema.methods.updateGroupPlan = async function updateGroupPlan (removingMember) {
|
||||
|
||||
@@ -477,13 +477,13 @@ schema.methods.canGetGems = async function canObtainGems () {
|
||||
const groups = await getUserGroupData(user);
|
||||
|
||||
return groups
|
||||
.every(g => !g.isSubscribed() || g.leader === user._id || g.leaderOnly.getGems !== true);
|
||||
.every(g => !g.hasActiveGroupPlan() || g.leader === user._id || g.leaderOnly.getGems !== true);
|
||||
};
|
||||
|
||||
schema.methods.isMemberOfGroupPlan = async function isMemberOfGroupPlan () {
|
||||
const groups = await getUserGroupData(this);
|
||||
|
||||
return groups.some(g => g.isSubscribed());
|
||||
return groups.some(g => g.hasActiveGroupPlan());
|
||||
};
|
||||
|
||||
schema.methods.isAdmin = function isAdmin () {
|
||||
|
||||
Reference in New Issue
Block a user