Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / propertiesToNotificationModel.ts
blob12d0a23c583a0b55112d343494646029a3180bae
1 import { filterFutureNotifications, sortNotificationsByAscendingTrigger } from '@proton/shared/lib/calendar/alarms';
2 import { triggerToModel } from '@proton/shared/lib/calendar/alarms/notificationModel';
3 import { ICAL_ALARM_ACTION, NOTIFICATION_TYPE_API } from '@proton/shared/lib/calendar/constants';
4 import { getSupportedAlarmAction } from '@proton/shared/lib/calendar/icsSurgery/valarm';
5 import { getIsAlarmComponent } from '@proton/shared/lib/calendar/vcalHelper';
6 import type { NotificationModel } from '@proton/shared/lib/interfaces/calendar/Notification';
7 import type { VcalVeventComponent } from '@proton/shared/lib/interfaces/calendar/VcalModel';
8 import generateUID from '@proton/utils/generateUID';
9 import unary from '@proton/utils/unary';
11 export const propertiesToNotificationModel = (
12     { components = [] }: Partial<VcalVeventComponent> = {},
13     isAllDay: boolean
14 ): NotificationModel[] => {
15     const modelNotifications = components.filter(unary(getIsAlarmComponent)).map(({ trigger, action }) => {
16         const type =
17             getSupportedAlarmAction(action).value === ICAL_ALARM_ACTION.EMAIL
18                 ? NOTIFICATION_TYPE_API.EMAIL
19                 : NOTIFICATION_TYPE_API.DEVICE;
21         return {
22             id: generateUID('notification'),
23             ...triggerToModel({
24                 trigger: trigger ? trigger.value : {},
25                 type,
26                 isAllDay,
27             }),
28         };
29     });
30     // Filter out future alarms
31     return sortNotificationsByAscendingTrigger(filterFutureNotifications(modelNotifications));