Merge branch 'docs-header-fix' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / offers / operations / blackFridayInbox2024FreeYearly / eligibility.ts
blobc6b049e9cb59eae566d65560d02ba7ad6173daa3
1 import { fromUnixTime, isBefore } from 'date-fns';
3 import { type Currency } from '@proton/payments';
4 import { getAppFromPathnameSafe } from '@proton/shared/lib/apps/slugHelper';
5 import { APPS } from '@proton/shared/lib/constants';
6 import { isManagedExternally } from '@proton/shared/lib/helpers/subscription';
7 import type { ProtonConfig, Subscription, UserModel } from '@proton/shared/lib/interfaces';
9 import hasEligibileCurrencyForBF from '../../helpers/hasEligibileCurrencyForBF';
10 import { FREE_DOWNGRADER_LIMIT } from '../../helpers/offerPeriods';
12 interface Props {
13     subscription?: Subscription;
14     protonConfig: ProtonConfig;
15     user: UserModel;
16     lastSubscriptionEnd?: number;
17     preferredCurrency: Currency;
20 const isEligible = ({ subscription, protonConfig, user, lastSubscriptionEnd = 0, preferredCurrency }: Props) => {
21     const parentApp = getAppFromPathnameSafe(window.location.pathname);
22     const hasValidApp =
23         (protonConfig.APP_NAME === APPS.PROTONACCOUNT && parentApp === APPS.PROTONMAIL) ||
24         (protonConfig.APP_NAME === APPS.PROTONACCOUNT && parentApp === APPS.PROTONCALENDAR) ||
25         protonConfig.APP_NAME === APPS.PROTONCALENDAR ||
26         protonConfig.APP_NAME === APPS.PROTONMAIL;
27     const { canPay, isDelinquent, isFree } = user;
28     const notDelinquent = !isDelinquent;
29     const isNotExternal = !isManagedExternally(subscription);
31     const isPreferredCurrencyEligible = hasEligibileCurrencyForBF(preferredCurrency);
33     return (
34         hasValidApp &&
35         isNotExternal &&
36         canPay &&
37         notDelinquent &&
38         isPreferredCurrencyEligible &&
39         isFree &&
40         isBefore(fromUnixTime(lastSubscriptionEnd), FREE_DOWNGRADER_LIMIT)
41     );
44 export default isEligible;