fix(subscriptions): use correct termination date field and add extra check when it is missing

This commit is contained in:
Matteo Pagliazzi
2020-05-14 12:56:12 +02:00
parent 41e22640f5
commit 092d6726b8
2 changed files with 17 additions and 3 deletions

View File

@@ -72,10 +72,18 @@ describe('#calculateSubscriptionTerminationDate', () => {
it('returns current terminated date if it exists and is later than newly calculated date', () => { it('returns current terminated date if it exists and is later than newly calculated date', () => {
const expectedTerminationDate = moment().add({ months: 5 }).toDate(); const expectedTerminationDate = moment().add({ months: 5 }).toDate();
plan.terminationDate = expectedTerminationDate; plan.dateTerminated = expectedTerminationDate;
const terminationDate = calculateSubscriptionTerminationDate(nextBill, plan, groupPlanId); const terminationDate = calculateSubscriptionTerminationDate(nextBill, plan, groupPlanId);
expect(terminationDate).to.equal(expectedTerminationDate); expect(terminationDate).to.equal(expectedTerminationDate);
}); });
it('returns the calculated termination date if the plan does not have one', () => {
nextBill = moment().add(5, 'days');
const expectedTerminationDate = moment().add(5, 'days');
const terminationDate = calculateSubscriptionTerminationDate(nextBill, plan, groupPlanId);
expect(expectedTerminationDate.diff(terminationDate, 'days')).to.eql(0);
});
}); });

View File

@@ -18,6 +18,12 @@ export default function calculateSubscriptionTerminationDate (
const calculatedTerminationDate = moment().startOf('day').add({ days: remaining + extraDays }); const calculatedTerminationDate = moment().startOf('day').add({ days: remaining + extraDays });
return calculatedTerminationDate.isBefore(purchasedPlan.terminationDate) // If a termination date is already set, use the one further in the future
? purchasedPlan.terminationDate : calculatedTerminationDate.toDate(); if (purchasedPlan.dateTerminated) {
return calculatedTerminationDate.isBefore(purchasedPlan.dateTerminated)
? purchasedPlan.dateTerminated : calculatedTerminationDate.toDate();
}
// Otherwise the calculated one
return calculatedTerminationDate.toDate();
} }