Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / helpers / addons.ts
blob32edbedad467a3195dafc6e7c88e308dc9824c5f
1 import {
2     ADDON_NAMES,
3     DOMAIN_ADDON_PREFIX,
4     type FreeSubscription,
5     IP_ADDON_PREFIX,
6     MEMBER_ADDON_PREFIX,
7     PLANS,
8     type PlanIDs,
9     SCRIBE_ADDON_PREFIX,
10     isFreeSubscription,
11 } from '@proton/payments';
13 import type { Addon, SubscriptionModel } from '../interfaces';
15 type AddonOrName = Addon | ADDON_NAMES | PLANS;
17 function isAddonType(addonOrName: AddonOrName, addonPrefix: string): boolean {
18     let addonName: ADDON_NAMES | PLANS;
19     if (typeof addonOrName === 'string') {
20         addonName = addonOrName;
21     } else {
22         addonName = addonOrName.Name;
23     }
25     return addonName.startsWith(addonPrefix);
28 export const isScribeAddon: AddonGuard = (addonOrName): boolean => {
29     return isAddonType(addonOrName, SCRIBE_ADDON_PREFIX);
32 export function hasScribeAddon(subscriptionOrPlanIds: SubscriptionModel | FreeSubscription | undefined): boolean {
33     const subscription = subscriptionOrPlanIds;
35     if (!subscription || isFreeSubscription(subscription)) {
36         return false;
37     }
39     const plans = subscription.Plans;
40     return plans.some((plan) => isScribeAddon(plan.Name));
43 export type AddonGuard = (addonOrName: AddonOrName) => boolean;
45 const ORG_SIZE_ADDONS = [
46     ADDON_NAMES.MEMBER_VPN_BUSINESS,
47     ADDON_NAMES.MEMBER_VPN_PRO,
48     ADDON_NAMES.MEMBER_PASS_BUSINESS,
49     ADDON_NAMES.MEMBER_PASS_PRO,
52 const DRIVE_ORG_SIZE_ADDONS = [ADDON_NAMES.MEMBER_DRIVE_PRO, ADDON_NAMES.MEMBER_DRIVE_BUSINESS];
54 export const isDriveOrgSizeAddon: AddonGuard = (addonOrName): boolean => {
55     return DRIVE_ORG_SIZE_ADDONS.some((name) => addonOrName === name);
58 export const isOrgSizeAddon: AddonGuard = (addonOrName): boolean => {
59     return ORG_SIZE_ADDONS.some((name) => addonOrName === name);
62 export const isMemberAddon: AddonGuard = (addonOrName): boolean => {
63     return isAddonType(addonOrName, MEMBER_ADDON_PREFIX);
66 export const isDomainAddon: AddonGuard = (addonOrName): boolean => {
67     return isAddonType(addonOrName, DOMAIN_ADDON_PREFIX);
70 export const isIpAddon: AddonGuard = (addonOrName): boolean => {
71     return isAddonType(addonOrName, IP_ADDON_PREFIX);
74 export const removeAddon = (originalPlanIDs: PlanIDs, addonGuard: AddonGuard): PlanIDs => {
75     const planIDs: PlanIDs = { ...originalPlanIDs };
77     // if guard returns true, it means that the addon should be removed
78     for (const addonName of Object.keys(planIDs) as (ADDON_NAMES | PLANS)[]) {
79         if (addonGuard(addonName)) {
80             delete planIDs[addonName];
81         }
82     }
84     return planIDs;
87 export const countAddonsByType = (planIDs: PlanIDs, addonGuard: AddonGuard): number => {
88     return Object.keys(planIDs).reduce((acc, key) => {
89         const addonName = key as ADDON_NAMES | PLANS;
91         if (addonGuard(addonName)) {
92             return acc + (planIDs[addonName] ?? 0);
93         }
94         return acc;
95     }, 0);
98 const {
99     MAIL,
100     DRIVE,
101     MAIL_BUSINESS,
102     ENTERPRISE,
103     BUNDLE,
104     BUNDLE_PRO,
105     BUNDLE_PRO_2024,
106     MAIL_PRO,
107     DRIVE_PRO,
108     DRIVE_BUSINESS,
109     VPN_PRO,
110     VPN_BUSINESS,
111     PASS_PRO,
112     PASS_BUSINESS,
113 } = PLANS;
115 export type SupportedAddons = Partial<Record<ADDON_NAMES, boolean>>;
117 export function getSupportedB2CAddons(planIDs: PlanIDs): SupportedAddons {
118     const supported: SupportedAddons = {};
120     // Re-enable the scribe addons when/if B2C plans trully support them
122     if (planIDs[MAIL]) {
123         // supported[ADDON_NAMES.MEMBER_SCRIBE_MAILPLUS] = true;
124     }
126     if (planIDs[DRIVE]) {
127         // supported[ADDON_NAMES.MEMBER_SCRIBE_DRIVEPLUS] = true;
128     }
130     if (planIDs[BUNDLE]) {
131         // supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE] = true;
132     }
134     return supported;
137 export function getSupportedB2BAddons(planIDs: PlanIDs): SupportedAddons {
138     const supported: SupportedAddons = {};
140     if (planIDs[MAIL_PRO]) {
141         supported[ADDON_NAMES.MEMBER_MAIL_PRO] = true;
142         supported[ADDON_NAMES.MEMBER_SCRIBE_MAIL_PRO] = true;
143     }
145     if (planIDs[MAIL_BUSINESS]) {
146         supported[ADDON_NAMES.MEMBER_MAIL_BUSINESS] = true;
147         supported[ADDON_NAMES.MEMBER_SCRIBE_MAIL_BUSINESS] = true;
148     }
150     if (planIDs[DRIVE_PRO]) {
151         supported[ADDON_NAMES.MEMBER_DRIVE_PRO] = true;
152     }
154     if (planIDs[DRIVE_BUSINESS]) {
155         supported[ADDON_NAMES.MEMBER_DRIVE_BUSINESS] = true;
156     }
158     if (planIDs[BUNDLE_PRO]) {
159         supported[ADDON_NAMES.MEMBER_BUNDLE_PRO] = true;
160         supported[ADDON_NAMES.DOMAIN_BUNDLE_PRO] = true;
161         supported[ADDON_NAMES.IP_BUNDLE_PRO] = true;
162         supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE_PRO] = true;
163     }
165     if (planIDs[BUNDLE_PRO_2024]) {
166         supported[ADDON_NAMES.MEMBER_BUNDLE_PRO_2024] = true;
167         supported[ADDON_NAMES.DOMAIN_BUNDLE_PRO_2024] = true;
168         supported[ADDON_NAMES.IP_BUNDLE_PRO_2024] = true;
169         supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE_PRO_2024] = true;
170     }
172     if (planIDs[ENTERPRISE]) {
173         supported[ADDON_NAMES.MEMBER_ENTERPRISE] = true;
174         supported[ADDON_NAMES.DOMAIN_ENTERPRISE] = true;
175     }
177     if (planIDs[VPN_PRO]) {
178         supported[ADDON_NAMES.MEMBER_VPN_PRO] = true;
179     }
181     if (planIDs[VPN_BUSINESS]) {
182         supported[ADDON_NAMES.MEMBER_VPN_BUSINESS] = true;
183         supported[ADDON_NAMES.IP_VPN_BUSINESS] = true;
184     }
186     if (planIDs[PASS_PRO]) {
187         supported[ADDON_NAMES.MEMBER_PASS_PRO] = true;
188     }
190     if (planIDs[PASS_BUSINESS]) {
191         supported[ADDON_NAMES.MEMBER_PASS_BUSINESS] = true;
192     }
194     return supported;
197 export const getSupportedAddons = (planIDs: PlanIDs): SupportedAddons => {
198     const supported: SupportedAddons = {
199         ...getSupportedB2CAddons(planIDs),
200         ...getSupportedB2BAddons(planIDs),
201     };
203     return supported;