Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / assistant / assistantUpsellConfig.ts
blobb17eeca75a95ae4526de16f3cd61968285941506
1 import type { OpenCallbackProps } from '@proton/components/containers/payments/subscription/SubscriptionModalProvider';
2 import { SUBSCRIPTION_STEPS } from '@proton/components/containers/payments/subscription/constants';
3 import type { SelectedPlan } from '@proton/payments';
4 import { type ADDON_NAMES, type PLANS, type PlanIDs, getScribeAddonNameByPlan } from '@proton/payments';
5 import { CYCLE } from '@proton/shared/lib/constants';
6 import { isScribeAddon, removeAddon } from '@proton/shared/lib/helpers/addons';
7 import type { UserModel } from '@proton/shared/lib/interfaces';
9 const getUpgradeCycles = (currentCycle = CYCLE.MONTHLY) => ({
10     cycle: currentCycle,
11     minimumCycle: currentCycle,
12     maximumCycle: currentCycle === CYCLE.MONTHLY ? CYCLE.YEARLY : currentCycle,
13 });
15 const paidSingleUserUpsellConfig = (
16     upsellRef: string,
17     planName: PLANS,
18     addonName: ADDON_NAMES | undefined,
19     cycle?: CYCLE
20 ): OpenCallbackProps => {
21     const cycles = getUpgradeCycles(cycle);
23     const planIDs: PlanIDs = {
24         [planName]: 1,
25     };
27     if (addonName) {
28         planIDs[addonName] = 1;
29     }
31     return {
32         mode: 'upsell-modal',
33         planIDs,
34         step: SUBSCRIPTION_STEPS.CHECKOUT,
35         disablePlanSelection: true,
36         upsellRef,
37         ...cycles,
38         metrics: {
39             source: 'upsells',
40         },
41     };
44 const paidMultipleUserUpsellConfig = (
45     upsellRef: string,
46     addonName: ADDON_NAMES | undefined,
47     selectedPlan: SelectedPlan
48 ): OpenCallbackProps => {
49     const cycles = getUpgradeCycles(selectedPlan.cycle);
51     // if we already have scribe addons, then we will use the current number of scribes as starting addon number
52     // in the upsell
53     // if we don't, then we will use the number of members as starting number for scribe addons
54     const addonsValue = selectedPlan.getTotalScribes() || selectedPlan.getTotalMembers();
56     const planIDs: PlanIDs = {
57         ...selectedPlan.planIDs,
58     };
59     if (addonName) {
60         planIDs[addonName] = addonsValue;
61     }
63     return {
64         mode: 'upsell-modal',
65         planIDs,
66         step: SUBSCRIPTION_STEPS.CHECKOUT,
67         disablePlanSelection: true,
68         upsellRef,
69         ...cycles,
70         metrics: {
71             source: 'upsells',
72         },
73     };
76 export const getAssistantUpsellConfig = (
77     upsellRef: string,
78     user: UserModel,
79     isOrgAdmin: boolean,
80     selectedPlan: SelectedPlan
81 ): OpenCallbackProps | undefined => {
82     if (user.isSubUser) {
83         return undefined;
84     }
86     if (isOrgAdmin) {
87         const addonName = getScribeAddonNameByPlan(selectedPlan.name);
88         return paidMultipleUserUpsellConfig(upsellRef, addonName, selectedPlan);
89     }
91     if (user.isPaid) {
92         const addonName = getScribeAddonNameByPlan(selectedPlan.name);
93         return paidSingleUserUpsellConfig(upsellRef, selectedPlan.name, addonName, selectedPlan.cycle);
94     }
96     return undefined;
99 export const getAssistantDowngradeConfig = (
100     upsellRef: string,
101     selectedPlan: SelectedPlan
102 ): OpenCallbackProps | undefined => {
103     return {
104         mode: 'upsell-modal',
105         /**
106          * Removes only Scribe addons and keep all others
107          */
108         planIDs: removeAddon(selectedPlan.planIDs, isScribeAddon),
109         step: SUBSCRIPTION_STEPS.CHECKOUT,
110         disablePlanSelection: true,
111         cycle: selectedPlan.cycle,
112         maximumCycle: selectedPlan.cycle,
113         minimumCycle: selectedPlan.cycle,
114         upsellRef,
115         metrics: {
116             source: 'upsells',
117         },
118     };