Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / offers / operations / blackFridayVPN2024Monthly / eligibility.ts
blob0653197b61308565997fe8db7e8d4d042d2d7531
1 import { fromUnixTime, isBefore } from 'date-fns';
3 import { type Currency, PLANS } from '@proton/payments';
4 import { getAppFromPathnameSafe } from '@proton/shared/lib/apps/slugHelper';
5 import { APPS } from '@proton/shared/lib/constants';
6 import { getPlan, hasMonthly, isManagedExternally } from '@proton/shared/lib/helpers/subscription';
7 import type { ProtonConfig, Subscription, UserModel } from '@proton/shared/lib/interfaces';
9 import { usedBfOffer } from '../../bfOffer';
10 import hasOneBF2024Coupon from '../../helpers/hasBF2024Coupons';
11 import hasEligibileCurrencyForBF from '../../helpers/hasEligibileCurrencyForBF';
12 import { FREE_DOWNGRADER_LIMIT } from '../../helpers/offerPeriods';
14 interface Props {
15     subscription?: Subscription;
16     user: UserModel;
17     protonConfig: ProtonConfig;
18     lastSubscriptionEnd?: number;
19     preferredCurrency: Currency;
22 const isEligible = ({ subscription, protonConfig, user, lastSubscriptionEnd = 0, preferredCurrency }: Props) => {
23     const parentApp = getAppFromPathnameSafe(window.location.pathname);
24     const plan = getPlan(subscription);
25     const isMonthly = hasMonthly(subscription);
26     const hasVPNPlus = plan?.Name === PLANS.VPN || plan?.Name === PLANS.VPN2024;
28     const hasValidApp =
29         (protonConfig?.APP_NAME === APPS.PROTONACCOUNT && parentApp === APPS.PROTONVPN_SETTINGS) ||
30         protonConfig.APP_NAME === APPS.PROTONVPN_SETTINGS;
31     const { canPay, isDelinquent } = user;
32     const isNotExternal = !isManagedExternally(subscription);
33     const isNotDelinquent = !isDelinquent;
34     const hasBF2024Coupon = hasOneBF2024Coupon(subscription) || usedBfOffer;
35     const hasVPNPlus1month = isMonthly && hasVPNPlus;
37     const isPreferredCurrencyEligible = hasEligibileCurrencyForBF(preferredCurrency);
39     // only VPN 1 MONTH PLUS
40     return (
41         hasValidApp &&
42         hasVPNPlus1month &&
43         !hasBF2024Coupon &&
44         canPay &&
45         isNotExternal &&
46         isNotDelinquent &&
47         isPreferredCurrencyEligible &&
48         isBefore(fromUnixTime(lastSubscriptionEnd), FREE_DOWNGRADER_LIMIT)
49     );
52 export default isEligible;