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;
32 if (partstat === DECLINED) {
35 hasDefaultNotifications: false,
36 partDayNotifications: [],
37 fullDayNotifications: [],
40 if (!addDefaultNotifications) {
41 return modelWithPartstat;
44 const notifications = apiNotificationsToModel({ notifications: null, isAllDay, calendarSettings });
47 hasDefaultNotifications: true,
48 [notificationsKey]: notifications,
52 export const getOrganizerDisplayData = (
53 organizer = { email: '', cn: '' },
54 isSelfOrganizer: boolean,
55 contactEmailsMap: SimpleMap<ContactEmail>,
56 displayNameEmailMap: SimpleMap<DisplayNameEmail>
58 const { email, cn } = organizer;
60 // it should not happen
61 return { name: '', title: '', initials: '' };
64 const { displayName } = displayNameEmailMap[canonicalizeEmailByGuess(email)] || {};
65 const { ContactID: contactID } = contactEmailsMap[canonicalizeEmail(email)] || {};
67 if (isSelfOrganizer) {
69 name: c('Event info. Organizer name').t`You`,
72 initials: getInitials(displayName || cn || email),
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 };