Files
habitica/website/server/libs/payments/util.js
T
Matteo Pagliazzi 9014943a86 Squashed commit of the following:
commit 6a70487fa6
Author: Matteo Pagliazzi <matteopagliazzi@gmail.com>
Date:   Mon Apr 20 23:48:48 2020 +0200

    remove un-needed code

commit 4fa381f153
Merge: 97209e40ad b7448e2cfe
Author: 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

commit b7448e2cfe
Author: 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

commit 0bc836b490
Author: hamboomger <hamboomger@gmail.com>
Date:   Sun Mar 29 15:54:13 2020 +0300

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

commit fdf7e3a665
Author: hamboomger <hamboomger@gmail.com>
Date:   Thu Mar 26 19:33:35 2020 +0200

    fix(db-schema): typo fixed in group.hasCancelled() schema metod name

commit 00d12e83bd
Author: hamboomger <hamboomger@gmail.com>
Date:   Thu Mar 26 19:31:07 2020 +0200

    refactor(db-schema): group.isSubscribed() method name changed to group.hasActiveGroupPlan()
2020-04-20 23:50:08 +02:00

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();
}