Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / helpers / attendees.ts
blob1bb1cfe0840336aea548f1ae743a9bc7764f2217
1 import { c } from 'ttag';
3 import { apiNotificationsToModel } from '@proton/shared/lib/calendar/alarms/notificationsToModel';
4 import { ICAL_ATTENDEE_STATUS } from '@proton/shared/lib/calendar/constants';
5 import { canonicalizeEmail, canonicalizeEmailByGuess } from '@proton/shared/lib/helpers/email';
6 import { getInitials } from '@proton/shared/lib/helpers/string';
7 import type { CalendarSettings, EventModel } from '@proton/shared/lib/interfaces/calendar';
8 import type { ContactEmail } from '@proton/shared/lib/interfaces/contacts';
9 import type { RequireOnly, SimpleMap } from '@proton/shared/lib/interfaces/utils';
11 import type { DisplayNameEmail } from '../containers/calendar/interface';
13 const { NEEDS_ACTION, DECLINED, ACCEPTED, TENTATIVE } = ICAL_ATTENDEE_STATUS;
15 export const modifyEventModelPartstat = (
16     model: RequireOnly<EventModel, 'isAllDay'>,
17     partstat: ICAL_ATTENDEE_STATUS,
18     calendarSettings: CalendarSettings
19 ): Partial<EventModel> => {
20     const { attendees = [], isAllDay, selfAttendeeIndex } = model;
21     const selfAttendee = selfAttendeeIndex !== undefined ? attendees[selfAttendeeIndex] : undefined;
22     let addDefaultNotifications = false;
23     const modelWithPartstat = { ...model };
24     const notificationsKey = isAllDay ? 'fullDayNotifications' : 'partDayNotifications';
25     if (modelWithPartstat.attendees && selfAttendee && selfAttendeeIndex !== undefined) {
26         addDefaultNotifications =
27             [NEEDS_ACTION, DECLINED].includes(selfAttendee.partstat) &&
28             [ACCEPTED, TENTATIVE].includes(partstat) &&
29             model[notificationsKey]?.length === 0;
30         modelWithPartstat.attendees[selfAttendeeIndex].partstat = partstat;
31     }
32     if (partstat === DECLINED) {
33         return {
34             ...modelWithPartstat,
35             hasDefaultNotifications: false,
36             partDayNotifications: [],
37             fullDayNotifications: [],
38         };
39     }
40     if (!addDefaultNotifications) {
41         return modelWithPartstat;
42     }
44     const notifications = apiNotificationsToModel({ notifications: null, isAllDay, calendarSettings });
45     return {
46         ...modelWithPartstat,
47         hasDefaultNotifications: true,
48         [notificationsKey]: notifications,
49     };
52 export const getOrganizerDisplayData = (
53     organizer = { email: '', cn: '' },
54     isSelfOrganizer: boolean,
55     contactEmailsMap: SimpleMap<ContactEmail>,
56     displayNameEmailMap: SimpleMap<DisplayNameEmail>
57 ) => {
58     const { email, cn } = organizer;
59     if (!email) {
60         // it should not happen
61         return { name: '', title: '', initials: '' };
62     }
64     const { displayName } = displayNameEmailMap[canonicalizeEmailByGuess(email)] || {};
65     const { ContactID: contactID } = contactEmailsMap[canonicalizeEmail(email)] || {};
67     if (isSelfOrganizer) {
68         return {
69             name: c('Event info. Organizer name').t`You`,
70             title: `${email}`,
71             contactID,
72             initials: getInitials(displayName || cn || email),
73         };
74     }
76     const name = displayName || cn || email;
77     const title = name === email ? email : `${name} <${email}>`;
78     const initials = getInitials(name);
80     return { name, title, contactID, initials };