Update all non-major dependencies
[ProtonMail-WebClient.git] / applications / calendar / src / app / helpers / invite.ts
blob910ab164e41746c3edff62fe23740974b5619844
1 import { getAttendeeEmail } from '@proton/shared/lib/calendar/attendees';
2 import type { VcalVeventComponent } from '@proton/shared/lib/interfaces/calendar';
4 import type { InviteActions } from '../interfaces/Invite';
5 import { INVITE_ACTION_TYPES } from '../interfaces/Invite';
7 const { SEND_INVITATION, SEND_UPDATE, CHANGE_PARTSTAT, DECLINE_INVITATION, CANCEL_INVITATION } = INVITE_ACTION_TYPES;
9 export const extractInviteEmails = ({
10     inviteActions,
11     vevent,
12     cancelVevent,
13 }: {
14     inviteActions: InviteActions;
15     vevent?: VcalVeventComponent;
16     cancelVevent?: VcalVeventComponent;
17 }) => {
18     const { type, addedAttendees, removedAttendees } = inviteActions;
19     const hasAddedAttendees = !!addedAttendees?.length;
20     const hasRemovedAttendees = !!removedAttendees?.length;
21     const invitedEmails = vevent?.attendee?.map((attendee) => getAttendeeEmail(attendee));
22     const addedEmails = addedAttendees?.map((attendee) => getAttendeeEmail(attendee));
23     const removedEmails = removedAttendees?.map((attendee) => getAttendeeEmail(attendee));
24     const cancelledEmails = cancelVevent?.attendee?.map((attendee) => getAttendeeEmail(attendee));
25     const organizerEmail = vevent?.organizer ? getAttendeeEmail(vevent.organizer) : undefined;
26     const emails: string[] = [];
28     if (type === SEND_INVITATION) {
29         if (!hasAddedAttendees && !hasRemovedAttendees && invitedEmails?.length) {
30             emails.push(...invitedEmails);
31         } else {
32             if (addedEmails?.length) {
33                 emails.push(...addedEmails);
34             }
35             if (removedEmails?.length) {
36                 emails.push(...removedEmails);
37             }
38         }
39     } else if (type === SEND_UPDATE) {
40         if (invitedEmails?.length) {
41             emails.push(...invitedEmails);
42         }
43         if (removedEmails?.length) {
44             emails.push(...removedEmails);
45         }
46     } else if (type === CANCEL_INVITATION) {
47         if (cancelledEmails?.length) {
48             emails.push(...cancelledEmails);
49         }
50     } else if ([CHANGE_PARTSTAT, DECLINE_INVITATION].includes(type)) {
51         if (organizerEmail) {
52             emails.push(organizerEmail);
53         }
54     }
56     return emails;