10 } from '../constants';
11 import type { Addon, PlanIDs, SubscriptionModel } from '../interfaces';
13 type AddonOrName = Addon | ADDON_NAMES | PLANS;
15 function isAddonType(addonOrName: AddonOrName, addonPrefix: string): boolean {
16 let addonName: ADDON_NAMES | PLANS;
17 if (typeof addonOrName === 'string') {
18 addonName = addonOrName;
20 addonName = addonOrName.Name;
23 return addonName.startsWith(addonPrefix);
26 export const isScribeAddon: AddonGuard = (addonOrName): boolean => {
27 return isAddonType(addonOrName, SCRIBE_ADDON_PREFIX);
30 export function hasScribeAddon(subscriptionOrPlanIds: SubscriptionModel | FreeSubscription | undefined): boolean {
31 const subscription = subscriptionOrPlanIds;
33 if (!subscription || isFreeSubscription(subscription)) {
37 const plans = subscription.Plans;
38 return plans.some((plan) => isScribeAddon(plan.Name));
41 export type AddonGuard = (addonOrName: AddonOrName) => boolean;
43 const ORG_SIZE_ADDONS = [
44 ADDON_NAMES.MEMBER_VPN_BUSINESS,
45 ADDON_NAMES.MEMBER_VPN_PRO,
46 ADDON_NAMES.MEMBER_PASS_BUSINESS,
47 ADDON_NAMES.MEMBER_PASS_PRO,
50 export const isOrgSizeAddon: AddonGuard = (addonOrName): boolean => {
51 return ORG_SIZE_ADDONS.some((name) => addonOrName === name);
54 export const isMemberAddon: AddonGuard = (addonOrName): boolean => {
55 return isAddonType(addonOrName, MEMBER_ADDON_PREFIX);
58 export const isDomainAddon: AddonGuard = (addonOrName): boolean => {
59 return isAddonType(addonOrName, DOMAIN_ADDON_PREFIX);
62 export const isIpAddon: AddonGuard = (addonOrName): boolean => {
63 return isAddonType(addonOrName, IP_ADDON_PREFIX);
66 export const removeAddon = (originalPlanIDs: PlanIDs, addonGuard: AddonGuard): PlanIDs => {
67 const planIDs: PlanIDs = { ...originalPlanIDs };
69 // if guard returns true, it means that the addon should be removed
70 for (const addonName of Object.keys(planIDs) as (ADDON_NAMES | PLANS)[]) {
71 if (addonGuard(addonName)) {
72 delete planIDs[addonName];
79 export const countAddonsByType = (planIDs: PlanIDs, addonGuard: AddonGuard): number => {
80 return Object.keys(planIDs).reduce((acc, key) => {
81 const addonName = key as ADDON_NAMES | PLANS;
83 if (addonGuard(addonName)) {
84 return acc + (planIDs[addonName] ?? 0);
106 export type SupportedAddons = Partial<Record<ADDON_NAMES, boolean>>;
108 export function getSupportedB2CAddons(planIDs: PlanIDs): SupportedAddons {
109 const supported: SupportedAddons = {};
111 // Re-enable the scribe addons when/if B2C plans trully support them
114 // supported[ADDON_NAMES.MEMBER_SCRIBE_MAILPLUS] = true;
117 if (planIDs[DRIVE]) {
118 // supported[ADDON_NAMES.MEMBER_SCRIBE_DRIVEPLUS] = true;
121 if (planIDs[BUNDLE]) {
122 // supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE] = true;
128 export function getSupportedB2BAddons(planIDs: PlanIDs): SupportedAddons {
129 const supported: SupportedAddons = {};
131 if (planIDs[MAIL_PRO]) {
132 supported[ADDON_NAMES.MEMBER_MAIL_PRO] = true;
133 supported[ADDON_NAMES.MEMBER_SCRIBE_MAIL_PRO] = true;
136 if (planIDs[MAIL_BUSINESS]) {
137 supported[ADDON_NAMES.MEMBER_MAIL_BUSINESS] = true;
138 supported[ADDON_NAMES.MEMBER_SCRIBE_MAIL_BUSINESS] = true;
141 if (planIDs[DRIVE_PRO]) {
142 supported[ADDON_NAMES.MEMBER_DRIVE_PRO] = true;
145 if (planIDs[BUNDLE_PRO]) {
146 supported[ADDON_NAMES.MEMBER_BUNDLE_PRO] = true;
147 supported[ADDON_NAMES.DOMAIN_BUNDLE_PRO] = true;
148 supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE_PRO] = true;
151 if (planIDs[BUNDLE_PRO_2024]) {
152 supported[ADDON_NAMES.MEMBER_BUNDLE_PRO_2024] = true;
153 supported[ADDON_NAMES.DOMAIN_BUNDLE_PRO_2024] = true;
154 supported[ADDON_NAMES.MEMBER_SCRIBE_BUNDLE_PRO_2024] = true;
157 if (planIDs[ENTERPRISE]) {
158 supported[ADDON_NAMES.MEMBER_ENTERPRISE] = true;
159 supported[ADDON_NAMES.DOMAIN_ENTERPRISE] = true;
162 if (planIDs[VPN_PRO]) {
163 supported[ADDON_NAMES.MEMBER_VPN_PRO] = true;
166 if (planIDs[VPN_BUSINESS]) {
167 supported[ADDON_NAMES.MEMBER_VPN_BUSINESS] = true;
168 supported[ADDON_NAMES.IP_VPN_BUSINESS] = true;
171 if (planIDs[PASS_PRO]) {
172 supported[ADDON_NAMES.MEMBER_PASS_PRO] = true;
175 if (planIDs[PASS_BUSINESS]) {
176 supported[ADDON_NAMES.MEMBER_PASS_BUSINESS] = true;
182 export const getSupportedAddons = (planIDs: PlanIDs): SupportedAddons => {
183 const supported: SupportedAddons = {
184 ...getSupportedB2CAddons(planIDs),
185 ...getSupportedB2BAddons(planIDs),