Merge branch 'IDTEAM-1.26.0' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / helpers / renew.ts
blobd9540ec6e7aba17d9f4fc7fa0e5a265b37cd83cf
1 import { PLANS, type PlanIDs } from '@proton/payments';
2 import { type Currency } from '@proton/payments';
3 import { CYCLE } from '@proton/shared/lib/constants';
4 import { getCheckout, getOptimisticCheckResult } from '@proton/shared/lib/helpers/checkout';
5 import type { Cycle, PlansMap } from '@proton/shared/lib/interfaces';
7 // This is currently hardcoded. Once the payments backend supports renewals at different cycles,
8 // it will be changed to more generic code. Currently there is no way to tell which plan renews at which cycle,
9 // so we have to hardcode it.
10 export const isSpecialRenewPlan = (planIDs: PlanIDs) => !!planIDs[PLANS.VPN2024];
12 const getRenewCycle = (cycle: Cycle, planIDs: PlanIDs): CYCLE => {
13     if (!isSpecialRenewPlan(planIDs)) {
14         return cycle;
15     }
17     if (cycle === CYCLE.MONTHLY || cycle === CYCLE.THREE || cycle === CYCLE.YEARLY) {
18         return cycle;
19     }
20     // 15,24,30 all renew at yearly.
21     return CYCLE.YEARLY;
24 export const getOptimisticRenewCycleAndPrice = ({
25     planIDs,
26     plansMap,
27     cycle,
28     currency,
29 }: {
30     cycle: Cycle;
31     planIDs: PlanIDs;
32     plansMap: PlansMap;
33     currency: Currency;
34 }): {
35     renewPrice: number;
36     renewalLength: CYCLE;
37 } => {
38     const nextCycle = getRenewCycle(cycle, planIDs);
39     const latestCheckout = getCheckout({
40         plansMap,
41         planIDs,
42         checkResult: getOptimisticCheckResult({
43             planIDs,
44             plansMap,
45             cycle: nextCycle,
46             currency,
47         }),
48     });
50     return {
51         // The API doesn't return the correct next cycle or RenewAmount for the VPN plan since we don't have chargebee
52         // So we calculate it with the cycle discount here
53         renewPrice: latestCheckout.withDiscountPerCycle,
54         renewalLength: nextCycle,
55     };