diff --git a/test/api/unit/libs/payments/payments.test.js b/test/api/unit/libs/payments/payments.test.js index 0e8d5e34ae..3ffd1230e4 100644 --- a/test/api/unit/libs/payments/payments.test.js +++ b/test/api/unit/libs/payments/payments.test.js @@ -13,7 +13,7 @@ import { import * as worldState from '../../../../../website/server/libs/worldState'; import { TransactionModel } from '../../../../../website/server/models/transaction'; -describe('payments/index', () => { +describe.only('payments/index', () => { let user; let group; let data; @@ -235,6 +235,48 @@ describe('payments/index', () => { expect(recipient.purchased.plan.customerId).to.eql('customer-id'); }); + it('sets plan.perkMonthCount to zero if user is not subscribed', async () => { + recipient.purchased.plan = plan; + recipient.purchased.plan.perkMonthCount = 1; + recipient.purchased.plan.customerId = undefined; + data.sub.key = 'basic_earned'; + data.gift.subscription.key = 'basic_earned'; + data.gift.subscription.months = 1; + + expect(recipient.purchased.plan.perkMonthCount).to.eql(1); + await api.createSubscription(data); + + expect(recipient.purchased.plan.perkMonthCount).to.eql(0); + }); + + it('adds to plan.perkMonthCount if user is already subscribed', async () => { + recipient.purchased.plan = plan; + recipient.purchased.plan.perkMonthCount = 1; + data.sub.key = 'basic_earned'; + data.gift.subscription.key = 'basic_earned'; + data.gift.subscription.months = 1; + + expect(recipient.purchased.plan.perkMonthCount).to.eql(1); + await api.createSubscription(data); + + expect(recipient.purchased.plan.perkMonthCount).to.eql(2); + }); + + it('awards perks if plan.perkMonthCount reaches 3', async () => { + recipient.purchased.plan = plan; + recipient.purchased.plan.perkMonthCount = 2; + data.sub.key = 'basic_earned'; + data.gift.subscription.key = 'basic_earned'; + data.gift.subscription.months = 1; + + expect(recipient.purchased.plan.perkMonthCount).to.eql(2); + await api.createSubscription(data); + + expect(recipient.purchased.plan.perkMonthCount).to.eql(0); + expect(recipient.purchased.plan.consecutive.trinkets).to.eql(1); + expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(5); + }); + it('sets plan.customerId to "Gift" if it does not already exist', async () => { expect(recipient.purchased.plan.customerId).to.not.exist; @@ -438,6 +480,19 @@ describe('payments/index', () => { expect(user.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate); }); + it('keeps plan.perkMonthCount when changing subscription type', async () => { + await api.createSubscription(data); + user.purchased.plan.perkMonthCount = 2; + await api.createSubscription(data); + expect(user.purchased.plan.perkMonthCount).to.eql(2); + }); + + it('sets plan.perkMonthCount to zero when creating new subscription', async () => { + user.purchased.plan.perkMonthCount = 2; + await api.createSubscription(data); + expect(user.purchased.plan.perkMonthCount).to.eql(0); + }); + it('awards the Royal Purple Jackalope pet', async () => { await api.createSubscription(data); diff --git a/website/server/libs/payments/subscriptions.js b/website/server/libs/payments/subscriptions.js index 193b3276c8..f9a160326c 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -114,6 +114,7 @@ async function prepareSubscriptionValues (data) { let purchaseType = 'subscribe'; let emailType = 'subscription-begins'; let recipientIsSubscribed = recipient.isSubscribed(); + let isNewSubscription = !recipientIsSubscribed // If we are buying a group subscription if (data.groupId) { @@ -154,6 +155,10 @@ async function prepareSubscriptionValues (data) { const { plan } = recipient.purchased; + if (isNewSubscription) { + plan.perkMonthCount = 0; + } + if (data.gift || !autoRenews) { if (plan.customerId && !plan.dateTerminated) { // User has active plan plan.extraMonths += months; @@ -228,6 +233,7 @@ async function prepareSubscriptionValues (data) { itemPurchased, purchaseType, emailType, + isNewSubscription }; } @@ -243,13 +249,16 @@ async function createSubscription (data) { itemPurchased, purchaseType, emailType, + isNewSubscription } = await prepareSubscriptionValues(data); + console.log() // Block sub perks - if (months > 0) { + if (months > 0 && (!data.gift || !isNewSubscription)) { if (!data.gift && !groupId) { plan.consecutive.offset = months; } + console.log("giving benes"); await plan.incrementPerkCounterAndReward(recipient._id, months); } diff --git a/website/server/models/subscriptionPlan.js b/website/server/models/subscriptionPlan.js index 2095c4b6d9..db4c39725b 100644 --- a/website/server/models/subscriptionPlan.js +++ b/website/server/models/subscriptionPlan.js @@ -3,6 +3,9 @@ import validator from 'validator'; import baseModel from '../libs/baseModel'; import { TransactionModel as Transaction } from './transaction'; +// multi-month subscriptions are for multiples of 3 months +const SUBSCRIPTION_BASIC_BLOCK_LENGTH = 3; + export const schema = new mongoose.Schema({ planId: String, subscriptionId: String, @@ -49,6 +52,8 @@ schema.plugin(baseModel, { schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward (userID, adding) { + // if perkMonthCount wasn't used before, initialize it. + if (!this.perkMonthCount) this.perkMonthCount = this.consecutive.count % SUBSCRIPTION_BASIC_BLOCK_LENGTH; this.perkMonthCount += adding; const perks = Math.floor(this.perkMonthCount / 3);