From 8dfa21a4b8a6af55da7ff8197d876de33e35fd29 Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Tue, 8 Nov 2022 12:38:24 +0100 Subject: [PATCH] Add field to track when current subscription type started --- test/api/unit/libs/payments/payments.test.js | 54 ++++++++++++++++++- website/server/libs/payments/subscriptions.js | 2 + website/server/models/subscriptionPlan.js | 1 + 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/test/api/unit/libs/payments/payments.test.js b/test/api/unit/libs/payments/payments.test.js index 84475e4b0e..b00733b45b 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; @@ -203,6 +203,28 @@ describe('payments/index', () => { expect(recipient.purchased.plan.dateCreated).to.exist; }); + it('sets plan.dateCurrentTypeCreated if it did not previously exist', async () => { + expect(recipient.purchased.plan.dateCurrentTypeCreated).to.not.exist; + + await api.createSubscription(data); + + expect(recipient.purchased.plan.dateCurrentTypeCreated).to.exist; + }); + + it('keeps plan.dateCreated when changing subscription type', async () => { + await api.createSubscription(data); + const initialDate = recipient.purchased.plan.dateCreated + await api.createSubscription(data); + expect(recipient.purchased.plan.dateCreated).to.eql(initialDate); + }); + + it('sets plan.dateCurrentTypeCreated when changing subscription type', async () => { + await api.createSubscription(data); + const initialDate = recipient.purchased.plan.dateCurrentTypeCreated + await api.createSubscription(data); + expect(recipient.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate); + }); + it('does not change plan.customerId if it already exists', async () => { recipient.purchased.plan = plan; data.customerId = 'purchaserCustomerId'; @@ -386,6 +408,36 @@ describe('payments/index', () => { expect(user.purchased.plan.dateCreated).to.exist; }); + it('sets plan.dateCreated if it did not previously exist', async () => { + expect(user.purchased.plan.dateCreated).to.not.exist; + + await api.createSubscription(data); + + expect(user.purchased.plan.dateCreated).to.exist; + }); + + it('sets plan.dateCurrentTypeCreated if it did not previously exist', async () => { + expect(user.purchased.plan.dateCurrentTypeCreated).to.not.exist; + + await api.createSubscription(data); + + expect(user.purchased.plan.dateCurrentTypeCreated).to.exist; + }); + + it('keeps plan.dateCreated when changing subscription type', async () => { + await api.createSubscription(data); + const initialDate = user.purchased.plan.dateCreated + await api.createSubscription(data); + expect(user.purchased.plan.dateCreated).to.eql(initialDate); + }); + + it('sets plan.dateCurrentTypeCreated when changing subscription type', async () => { + await api.createSubscription(data); + const initialDate = user.purchased.plan.dateCurrentTypeCreated + await api.createSubscription(data); + expect(user.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate); + }); + 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 75a49312e2..78d339eb65 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -144,6 +144,7 @@ async function prepareSubscriptionValues (data) { plan.dateTerminated = moment().add({ months }).toDate(); plan.dateCreated = today; } + plan.dateCurrentTypeCreated = today; } if (!plan.customerId) { @@ -160,6 +161,7 @@ async function prepareSubscriptionValues (data) { planId: block.key, customerId: data.customerId, dateUpdated: today, + dateCurrentTypeCreated: today, paymentMethod: data.paymentMethod, extraMonths: Number(plan.extraMonths) + _dateDiff(today, plan.dateTerminated), dateTerminated: null, diff --git a/website/server/models/subscriptionPlan.js b/website/server/models/subscriptionPlan.js index dc04661142..df6d28ecd5 100644 --- a/website/server/models/subscriptionPlan.js +++ b/website/server/models/subscriptionPlan.js @@ -13,6 +13,7 @@ export const schema = new mongoose.Schema({ dateCreated: Date, dateTerminated: Date, dateUpdated: Date, + dateCurrentTypeCreated: Date, extraMonths: { $type: Number, default: 0 }, gemsBought: { $type: Number, default: 0 }, mysteryItems: { $type: Array, default: () => [] },