From 00d12e83bdb1a2068780e9c615d1618686666b87 Mon Sep 17 00:00:00 2001 From: hamboomger Date: Thu, 26 Mar 2020 19:31:07 +0200 Subject: [PATCH] refactor(db-schema): group.isSubscribed() method name changed to group.hasActiveGroupPlan() --- test/api/unit/models/group.test.js | 10 +++++----- website/server/controllers/api-v3/groups.js | 2 +- website/server/libs/chat/group-chat.js | 2 +- website/server/libs/invites/index.js | 6 +++--- website/server/libs/payments/subscriptions.js | 4 +++- website/server/models/group.js | 8 ++++---- website/server/models/user/methods.js | 4 ++-- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/test/api/unit/models/group.test.js b/test/api/unit/models/group.test.js index 5bb1967d92..a8b01b683a 100644 --- a/test/api/unit/models/group.test.js +++ b/test/api/unit/models/group.test.js @@ -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; }); }); diff --git a/website/server/controllers/api-v3/groups.js b/website/server/controllers/api-v3/groups.js index e71e2b63f7..b90b226659 100644 --- a/website/server/controllers/api-v3/groups.js +++ b/website/server/controllers/api-v3/groups.js @@ -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); }, diff --git a/website/server/libs/chat/group-chat.js b/website/server/libs/chat/group-chat.js index 1a864b0f47..dc6b0d8278 100644 --- a/website/server/libs/chat/group-chat.js +++ b/website/server/libs/chat/group-chat.js @@ -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) diff --git a/website/server/libs/invites/index.js b/website/server/libs/invites/index.js index d389546b3b..e10cc67793 100644 --- a/website/server/libs/invites/index.js +++ b/website/server/libs/invites/index.js @@ -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, diff --git a/website/server/libs/payments/subscriptions.js b/website/server/libs/payments/subscriptions.js index 62fbe75b64..96acf3451e 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -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 { diff --git a/website/server/models/group.js b/website/server/models/group.js index 32c12d5b7a..45ea6bc73f 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -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) { diff --git a/website/server/models/user/methods.js b/website/server/models/user/methods.js index 2bf8227e3a..70821e0b95 100644 --- a/website/server/models/user/methods.js +++ b/website/server/models/user/methods.js @@ -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 () {