Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / calendar / alarms / notificationModel.ts
blob3268e8514d3aa491aa5c1d12dfedbe4c536580b4
1 import type { NotificationModel, VcalDurationValue } from '../../interfaces/calendar';
2 import { normalizeRelativeTrigger, transformBeforeAt } from '../alarms/trigger';
3 import { NOTIFICATION_TYPE_API, NOTIFICATION_UNITS, NOTIFICATION_WHEN } from '../constants';
5 const getInt = (value: any) => parseInt(value, 10) || 0;
7 interface TriggerToModelShared {
8     when: NOTIFICATION_WHEN;
9     type: NOTIFICATION_TYPE_API;
10     weeks: number;
11     days: number;
12     hours: number;
13     minutes: number;
16 const allDayTriggerToModel = ({ type, when, weeks, days, hours, minutes }: TriggerToModelShared) => {
17     const isNegative = when === NOTIFICATION_WHEN.BEFORE;
19     const at = new Date(2000, 0, 1, hours, minutes);
20     const modifiedAt = isNegative ? transformBeforeAt(at) : at;
21     const modifyNegativeDay = isNegative && (modifiedAt.getHours() > 0 || modifiedAt.getMinutes() > 0);
23     const [value, unit] = (() => {
24         // Transform for example -P1W6DT10H10M into 2 weeks at...
25         if (weeks >= 0 && days === 6 && modifyNegativeDay) {
26             return [weeks + 1, NOTIFICATION_UNITS.WEEK];
27         }
28         // Otherwise, if there is something in the week, and even if there are days in the trigger, the client will truncate this into a week notification since the selector is not more advanced than that.
29         if (weeks > 0) {
30             return [weeks, NOTIFICATION_UNITS.WEEK];
31         }
32         // Finally just return it as a day notification.
33         return [days + +modifyNegativeDay, NOTIFICATION_UNITS.DAY];
34     })();
36     return {
37         unit,
38         type,
39         when,
40         value,
41         at: modifiedAt,
42         isAllDay: true,
43     };
46 const partDayTriggerToModel = ({ type, when, weeks, days, hours, minutes }: TriggerToModelShared) => {
47     const [value, unit] = (() => {
48         if (weeks) {
49             return [weeks, NOTIFICATION_UNITS.WEEK];
50         }
51         if (days) {
52             return [days, NOTIFICATION_UNITS.DAY];
53         }
54         if (hours) {
55             return [hours, NOTIFICATION_UNITS.HOUR];
56         }
57         return [minutes, NOTIFICATION_UNITS.MINUTE];
58     })();
60     return {
61         unit,
62         type,
63         when,
64         value,
65         isAllDay: false,
66     };
69 interface TriggerToModel {
70     isAllDay: boolean;
71     type: NOTIFICATION_TYPE_API;
72     trigger: Partial<VcalDurationValue>;
75 export const triggerToModel = ({
76     isAllDay,
77     type,
78     trigger: { weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, isNegative = false },
79 }: TriggerToModel): Omit<NotificationModel, 'id'> => {
80     const parsedTrigger = {
81         weeks: getInt(weeks),
82         days: getInt(days),
83         hours: getInt(hours),
84         minutes: getInt(minutes),
85         seconds: getInt(seconds),
86         isNegative,
87     };
88     const normalizedTrigger = normalizeRelativeTrigger(parsedTrigger, isAllDay);
89     const when = isNegative ? NOTIFICATION_WHEN.BEFORE : NOTIFICATION_WHEN.AFTER;
90     if (isAllDay) {
91         return allDayTriggerToModel({ type, when, ...normalizedTrigger });
92     }
93     return partDayTriggerToModel({ type, when, ...normalizedTrigger });
96 export const getDeviceNotifications = (notifications: NotificationModel[]) => {
97     return notifications.filter(({ type }) => type === NOTIFICATION_TYPE_API.DEVICE);