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;
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];
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.
30 return [weeks, NOTIFICATION_UNITS.WEEK];
32 // Finally just return it as a day notification.
33 return [days + +modifyNegativeDay, NOTIFICATION_UNITS.DAY];
46 const partDayTriggerToModel = ({ type, when, weeks, days, hours, minutes }: TriggerToModelShared) => {
47 const [value, unit] = (() => {
49 return [weeks, NOTIFICATION_UNITS.WEEK];
52 return [days, NOTIFICATION_UNITS.DAY];
55 return [hours, NOTIFICATION_UNITS.HOUR];
57 return [minutes, NOTIFICATION_UNITS.MINUTE];
69 interface TriggerToModel {
71 type: NOTIFICATION_TYPE_API;
72 trigger: Partial<VcalDurationValue>;
75 export const triggerToModel = ({
78 trigger: { weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, isNegative = false },
79 }: TriggerToModel): Omit<NotificationModel, 'id'> => {
80 const parsedTrigger = {
84 minutes: getInt(minutes),
85 seconds: getInt(seconds),
88 const normalizedTrigger = normalizeRelativeTrigger(parsedTrigger, isAllDay);
89 const when = isNegative ? NOTIFICATION_WHEN.BEFORE : NOTIFICATION_WHEN.AFTER;
91 return allDayTriggerToModel({ type, when, ...normalizedTrigger });
93 return partDayTriggerToModel({ type, when, ...normalizedTrigger });
96 export const getDeviceNotifications = (notifications: NotificationModel[]) => {
97 return notifications.filter(({ type }) => type === NOTIFICATION_TYPE_API.DEVICE);