Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / calendar / sharing / getHasSharedCalendars.ts
blobfe1a991ba676376857dd4ab920e8bccd0c01c575
1 import { getAllMembers, getCalendarInvitations, getPublicLinks } from '@proton/shared/lib/api/calendars';
2 import { getApiWithAbort } from '@proton/shared/lib/api/helpers/customConfig';
3 import { filterOutDeclinedInvitations } from '@proton/shared/lib/calendar/sharing/shareProton/shareProton';
5 import type { Api } from '../../interfaces';
6 import type {
7     CalendarUrlsResponse,
8     CalendarWithOwnMembers,
9     GetAllMembersApiResponse,
10     GetCalendarInvitationsResponse,
11 } from '../../interfaces/calendar';
12 import { getIsOwnedCalendar } from '../calendar';
14 const getHasSharedCalendars = async ({
15     calendars,
16     api,
17     catchErrors,
18 }: {
19     calendars: CalendarWithOwnMembers[];
20     api: Api;
21     catchErrors?: boolean;
22 }) => {
23     const { signal, abort } = new AbortController();
24     const apiWithAbort = getApiWithAbort(api, signal);
26     let hasSharedCalendars = false;
28     await Promise.all(
29         calendars
30             .filter((calendar) => getIsOwnedCalendar(calendar))
31             .map(async ({ ID }) => {
32                 try {
33                     const [{ CalendarUrls: links }, { Members }, { Invitations }] = await Promise.all([
34                         apiWithAbort<CalendarUrlsResponse>(getPublicLinks(ID)),
35                         apiWithAbort<GetAllMembersApiResponse>(getAllMembers(ID)),
36                         apiWithAbort<GetCalendarInvitationsResponse>(getCalendarInvitations(ID)),
37                     ]);
39                     const pendingOrAcceptedInvitations = filterOutDeclinedInvitations(Invitations);
40                     if (links.length || Members.length > 1 || pendingOrAcceptedInvitations.length) {
41                         hasSharedCalendars = true;
42                         abort();
43                     }
44                 } catch (e: any) {
45                     if (!catchErrors) {
46                         const error =
47                             e instanceof Error ? e : new Error('Unknown error getting calendar links or members');
49                         throw error;
50                     }
51                 }
52             })
53     );
55     return hasSharedCalendars;
58 export default getHasSharedCalendars;