1 import noop from '@proton/utils/noop';
3 import { PLAN_SERVICES, PLAN_TYPES } from '../constants';
4 import { hasBit } from '../helpers/bitset';
5 import type { Api, Plan, PlanIDs } from '../interfaces';
6 import type { CalendarWithOwnMembers } from '../interfaces/calendar';
7 import { MAX_CALENDARS_FREE } from './constants';
8 import getHasSharedCalendars from './sharing/getHasSharedCalendars';
10 export const willHavePaidMail = (planIDs: PlanIDs, plans: Plan[]) => {
11 const newPlanName = Object.keys(planIDs).find((planName) =>
12 plans.find((plan) => plan.Type === PLAN_TYPES.PLAN && plan.Name === planName)
14 const newPlan = plans.find((plan) => plan.Name === newPlanName);
16 return hasBit(newPlan?.Services, PLAN_SERVICES.MAIL);
19 export const getShouldCalendarPreventSubscripitionChange = async ({
26 willHavePaidMail: boolean;
28 getCalendars: () => Promise<CalendarWithOwnMembers[] | undefined>;
30 if (!hasPaidMail || willHavePaidMail) {
31 // We only prevent subscription change when downgrading the paid-mail condition
34 const calendars = (await getCalendars().catch(noop)) || [];
36 const hasSharedCalendars = await getHasSharedCalendars({ calendars, api, catchErrors: true });
38 return calendars.length > MAX_CALENDARS_FREE || hasSharedCalendars;