refactor(payments): unit tests created for calculation of subscription termination date

This commit is contained in:
hamboomger
2020-03-29 15:54:13 +03:00
parent fdf7e3a665
commit 0bc836b490
3 changed files with 98 additions and 14 deletions

View File

@@ -0,0 +1,32 @@
import moment from 'moment';
const DEFAULT_REMAINING_DAYS = 30;
const DEFAULT_REMAINING_DAYS_FOR_GROUP_PLAN = 2;
/**
* paymentsApiConstants is provided as parameter because of a dependency cycle
* with subscriptions api which will occur if api.constants would be used directly
*/
export function calculateSubscriptionTerminationDate (
nextBill, purchasedPlan, paymentsApiConstants,
) {
const defaultRemainingDays = (
purchasedPlan.customerId === paymentsApiConstants.GROUP_PLAN_CUSTOMER_ID
) ? DEFAULT_REMAINING_DAYS_FOR_GROUP_PLAN
: DEFAULT_REMAINING_DAYS;
const now = moment();
const remaining = nextBill
? moment(nextBill).diff(new Date(), 'days', true)
: defaultRemainingDays;
const extraMonths = Math.max(purchasedPlan.extraMonths, 0);
const extraDays = Math.ceil(30.5 * extraMonths);
const nowStr = `${now.format('MM')}/${now.format('DD')}/${now.format('YYYY')}`;
const nowStrFormat = 'MM/DD/YYYY';
return moment(nowStr, nowStrFormat)
.add({ days: remaining })
.add({ days: extraDays })
.toDate();
}