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