Merge branch 'docs-header-fix' into 'main'
[ProtonMail-WebClient.git] / packages / components / containers / offers / operations / goUnlimited2022 / eligibility.ts
blob98cf70e23d0cec9ff9515c3406dd3e41d7776899
1 import { addDays, fromUnixTime, isBefore } from 'date-fns';
3 import { PLANS } from '@proton/payments';
4 import { APPS } from '@proton/shared/lib/constants';
5 import { getPlan, isManagedExternally, isTrial } from '@proton/shared/lib/helpers/subscription';
6 import type { ProtonConfig, Subscription, UserModel } from '@proton/shared/lib/interfaces';
8 interface Props {
9     user: UserModel;
10     subscription?: Subscription;
11     protonConfig: ProtonConfig;
14 const isEligible = ({ user, subscription, protonConfig }: Props) => {
15     const isValidApp = protonConfig?.APP_NAME === APPS.PROTONMAIL || protonConfig?.APP_NAME === APPS.PROTONACCOUNT;
16     const createDate = subscription?.CreateTime ? fromUnixTime(subscription.CreateTime) : new Date();
17     const plan = getPlan(subscription);
18     return (
19         [PLANS.MAIL, PLANS.VPN].includes(plan?.Name as PLANS) &&
20         !isTrial(subscription) &&
21         isBefore(createDate, addDays(new Date(), -7)) &&
22         user.canPay &&
23         isValidApp &&
24         !user.isDelinquent &&
25         !isManagedExternally(subscription)
26     );
29 export default isEligible;