9014943a86
commit6a70487fa6Author: Matteo Pagliazzi <matteopagliazzi@gmail.com> Date: Mon Apr 20 23:48:48 2020 +0200 remove un-needed code commit4fa381f153Merge:97209e40adb7448e2cfeAuthor: Matteo Pagliazzi <matteopagliazzi@gmail.com> Date: Mon Apr 20 23:30:19 2020 +0200 Merge branch 'bugfix-extramonths-lost-when-subscription-terminated' of https://github.com/hamboomger/habitica into hamboomger-bugfix-extramonths-lost-when-subscription-terminated commitb7448e2cfeAuthor: hamboomger <hamboomger@gmail.com> Date: Tue Mar 31 18:23:08 2020 +0300 fix(server-api): cancelSubscription() is no longer called twice when user leaves group commit0bc836b490Author: hamboomger <hamboomger@gmail.com> Date: Sun Mar 29 15:54:13 2020 +0300 refactor(payments): unit tests created for calculation of subscription termination date commitfdf7e3a665Author: hamboomger <hamboomger@gmail.com> Date: Thu Mar 26 19:33:35 2020 +0200 fix(db-schema): typo fixed in group.hasCancelled() schema metod name commit00d12e83bdAuthor: hamboomger <hamboomger@gmail.com> Date: Thu Mar 26 19:31:07 2020 +0200 refactor(db-schema): group.isSubscribed() method name changed to group.hasActiveGroupPlan()
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
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();
|
|
}
|