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 {
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 ({
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.`);
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({
47 api: getSilentApi(api),
57 hasSharedCalendars = hasShared;
60 if (calendarsToReactivate.length > 0) {
61 await reactivateCalendarsKeys({
63 calendars: calendarsToReactivate,
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;