diff --git a/test/api/v3/unit/libs/payments.test.js b/test/api/v3/unit/libs/payments.test.js index 058f312125..17b6870db0 100644 --- a/test/api/v3/unit/libs/payments.test.js +++ b/test/api/v3/unit/libs/payments.test.js @@ -153,6 +153,24 @@ describe('payments/index', () => { expect(recipient.purchased.plan.dateUpdated).to.exist; }); + it('sets plan.dateUpdated if it did exist but the user has cancelled', async () => { + recipient.purchased.plan.dateUpdated = moment().subtract(1, 'days').toDate(); + recipient.purchased.plan.dateTerminated = moment().subtract(1, 'days').toDate(); + recipient.purchased.plan.customerId = 'testing'; + + await api.createSubscription(data); + + expect(moment(recipient.purchased.plan.dateUpdated).date()).to.eql(moment().date()); + }); + + it('sets plan.dateUpdated if it did exist but the user has a corrupt plan', async () => { + recipient.purchased.plan.dateUpdated = moment().subtract(1, 'days').toDate(); + + await api.createSubscription(data); + + expect(moment(recipient.purchased.plan.dateUpdated).date()).to.eql(moment().date()); + }); + it('sets plan.dateCreated if it did not previously exist', async () => { expect(recipient.purchased.plan.dateCreated).to.not.exist; diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 5d453a172f..9b9882ae2c 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -307,7 +307,7 @@ api.createSubscription = async function createSubscription (data) { if (plan.customerId && !plan.dateTerminated) { // User has active plan plan.extraMonths += months; } else { - if (!plan.dateUpdated) plan.dateUpdated = today; + if (!recipient.isSubscribed() || !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/user/methods.js b/website/server/models/user/methods.js index 909f57d83f..f752dc165f 100644 --- a/website/server/models/user/methods.js +++ b/website/server/models/user/methods.js @@ -19,9 +19,8 @@ import paypalPayments from '../../libs/paypalPayments'; const daysSince = common.daysSince; schema.methods.isSubscribed = function isSubscribed () { - let now = new Date(); - let plan = this.purchased.plan; - + const now = new Date(); + const plan = this.purchased.plan; return plan && plan.customerId && (!plan.dateTerminated || moment(plan.dateTerminated).isAfter(now)); };