1 import { c } from 'ttag';
3 import type { useGetAddressKeys } from '@proton/account/addressKeys/hooks';
4 import type { useGetAddresses } from '@proton/account/addresses/hooks';
5 import type { useGetCalendars } from '@proton/calendar/calendars/hooks';
6 import { CacheType } from '@proton/redux-utilities';
8 import { getSilentApi } from '../../../api/helpers/customConfig';
9 import getHasSharedCalendars from '../../../calendar/sharing/getHasSharedCalendars';
10 import type { Api } from '../../../interfaces';
11 import type { VisualCalendar } from '../../../interfaces/calendar';
12 import { getIsOwnedCalendar } from '../../calendar';
13 import { reactivateCalendarsKeys } from './reactivateCalendarKeys';
14 import { resetCalendarKeys } from './resetCalendarKeys';
16 interface ProcessArguments {
18 getCalendars: ReturnType<typeof useGetCalendars>;
19 getAddresses: ReturnType<typeof useGetAddresses>;
20 getAddressKeys: ReturnType<typeof useGetAddressKeys>;
21 calendarsToReset?: VisualCalendar[];
22 calendarsToReactivate?: VisualCalendar[];
23 calendarsToClean?: VisualCalendar[];
26 export const process = async ({
31 calendarsToReset = [],
32 calendarsToReactivate = [],
33 calendarsToClean = [],
34 }: ProcessArguments) => {
35 const addresses = await getAddresses();
37 if (!addresses.length) {
38 throw new Error(c('Error').t`Please create an address first.`);
41 let hasSharedCalendars = false;
42 if (calendarsToReset.length > 0 || calendarsToClean.length > 0) {
43 // Non-owners can't reset calendar keys
44 // Even if calendarsToReset is empty, we want to call the reset endpoint in order to clean shared/holidays calendars
45 const calendars = calendarsToReset.filter((calendar) => getIsOwnedCalendar(calendar));
46 const [hasShared] = await Promise.all([
47 getHasSharedCalendars({
49 api: getSilentApi(api),
59 hasSharedCalendars = hasShared;
62 if (calendarsToReactivate.length > 0) {
63 await reactivateCalendarsKeys({
65 calendars: calendarsToReactivate,
71 // Refresh the calendar model to be able to get the new flags since it's not updated through the event manager
72 await getCalendars({ cache: CacheType.None });
74 return hasSharedCalendars;