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';
15 subscription?: Subscription;
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;
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
47 isPreferredCurrencyEligible &&
48 isBefore(fromUnixTime(lastSubscriptionEnd), FREE_DOWNGRADER_LIMIT)
52 export default isEligible;