Updated plan updated date if user has cancelled (#9773)

* Updated plan updated date if user has cancelled

* Added test for plan with only date updated
This commit is contained in:
Keith Holliday
2018-01-08 12:50:15 -06:00
committed by GitHub
parent 587847f5e9
commit d3ee3ca53d
3 changed files with 21 additions and 4 deletions

View File

@@ -153,6 +153,24 @@ describe('payments/index', () => {
expect(recipient.purchased.plan.dateUpdated).to.exist; 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 () => { it('sets plan.dateCreated if it did not previously exist', async () => {
expect(recipient.purchased.plan.dateCreated).to.not.exist; expect(recipient.purchased.plan.dateCreated).to.not.exist;

View File

@@ -307,7 +307,7 @@ api.createSubscription = async function createSubscription (data) {
if (plan.customerId && !plan.dateTerminated) { // User has active plan if (plan.customerId && !plan.dateTerminated) { // User has active plan
plan.extraMonths += months; plan.extraMonths += months;
} else { } else {
if (!plan.dateUpdated) plan.dateUpdated = today; if (!recipient.isSubscribed() || !plan.dateUpdated) plan.dateUpdated = today;
if (moment(plan.dateTerminated).isAfter()) { if (moment(plan.dateTerminated).isAfter()) {
plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate(); plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate();
} else { } else {

View File

@@ -19,9 +19,8 @@ import paypalPayments from '../../libs/paypalPayments';
const daysSince = common.daysSince; const daysSince = common.daysSince;
schema.methods.isSubscribed = function isSubscribed () { schema.methods.isSubscribed = function isSubscribed () {
let now = new Date(); const now = new Date();
let plan = this.purchased.plan; const plan = this.purchased.plan;
return plan && plan.customerId && (!plan.dateTerminated || moment(plan.dateTerminated).isAfter(now)); return plan && plan.customerId && (!plan.dateTerminated || moment(plan.dateTerminated).isAfter(now));
}; };