Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / calendar / crypto / keys / resetHelper.ts
blobb6fca4affdcb7cf164e424f8f2a89748bdd11379
1 import { c } from 'ttag';
3 import type { useGetAddressKeys, useGetAddresses, useGetCalendars } from '@proton/components';
4 import { CacheType } from '@proton/redux-utilities';
6 import { getSilentApi } from '../../../api/helpers/customConfig';
7 import getHasSharedCalendars from '../../../calendar/sharing/getHasSharedCalendars';
8 import type { Api } from '../../../interfaces';
9 import type { VisualCalendar } from '../../../interfaces/calendar';
10 import { getIsOwnedCalendar } from '../../calendar';
11 import { reactivateCalendarsKeys } from './reactivateCalendarKeys';
12 import { resetCalendarKeys } from './resetCalendarKeys';
14 interface ProcessArguments {
15     api: Api;
16     getCalendars: ReturnType<typeof useGetCalendars>;
17     getAddresses: ReturnType<typeof useGetAddresses>;
18     getAddressKeys: ReturnType<typeof useGetAddressKeys>;
19     calendarsToReset?: VisualCalendar[];
20     calendarsToReactivate?: VisualCalendar[];
21     calendarsToClean?: VisualCalendar[];
24 export const process = async ({
25     api,
26     getCalendars,
27     getAddresses,
28     getAddressKeys,
29     calendarsToReset = [],
30     calendarsToReactivate = [],
31     calendarsToClean = [],
32 }: ProcessArguments) => {
33     const addresses = await getAddresses();
35     if (!addresses.length) {
36         throw new Error(c('Error').t`Please create an address first.`);
37     }
39     let hasSharedCalendars = false;
40     if (calendarsToReset.length > 0 || calendarsToClean.length > 0) {
41         // Non-owners can't reset calendar keys
42         // Even if calendarsToReset is empty, we want to call the reset endpoint in order to clean shared/holidays calendars
43         const calendars = calendarsToReset.filter((calendar) => getIsOwnedCalendar(calendar));
44         const [hasShared] = await Promise.all([
45             getHasSharedCalendars({
46                 calendars,
47                 api: getSilentApi(api),
48                 catchErrors: true,
49             }),
50             resetCalendarKeys({
51                 calendars,
52                 api,
53                 getAddressKeys,
54             }),
55         ]);
57         hasSharedCalendars = hasShared;
58     }
60     if (calendarsToReactivate.length > 0) {
61         await reactivateCalendarsKeys({
62             api,
63             calendars: calendarsToReactivate,
64             getAddressKeys,
65             addresses,
66         });
67     }
69     // Refresh the calendar model to be able to get the new flags since it's not updated through the event manager
70     await getCalendars({ cache: CacheType.None });
72     return hasSharedCalendars;