Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / calendar / src / app / components / eventModal / eventForm / modelToFrequencyProperties.ts
blobd3c63459ed9810a9abf58a0c3491c68ee5f588ba
1 import { END_TYPE, FREQUENCY, MONTHLY_TYPE } from '@proton/shared/lib/calendar/constants';
2 import { getNegativeSetpos, getPositiveSetpos } from '@proton/shared/lib/calendar/recurrence/rrule';
3 import { getUntilProperty, numericDayToDay } from '@proton/shared/lib/calendar/vcalConverter';
4 import { fromLocalDate, toUTCDate } from '@proton/shared/lib/date/timezone';
5 import type { EventModel } from '@proton/shared/lib/interfaces/calendar';
6 import type { VcalRruleProperty } from '@proton/shared/lib/interfaces/calendar/VcalModel';
7 import unique from '@proton/utils/unique';
9 const modelToFrequencyProperties = ({ frequencyModel, start, isAllDay, isAttendee }: EventModel) => {
10     const { type, frequency, interval, weekly, monthly, ends, vcalRruleValue } = frequencyModel;
11     const { date: startDate, tzid } = start;
13     if ((isAttendee || type === FREQUENCY.OTHER) && vcalRruleValue) {
14         return {
15             rrule: {
16                 value: vcalRruleValue,
17             },
18         };
19     }
21     if ([FREQUENCY.DAILY, FREQUENCY.WEEKLY, FREQUENCY.MONTHLY, FREQUENCY.YEARLY].includes(type)) {
22         const rrule = {
23             value: { freq: type },
24         };
25         return {
26             rrule,
27         };
28     }
30     if (type === FREQUENCY.CUSTOM) {
31         if (ends.type === END_TYPE.AFTER_N_TIMES && ends.count && ends.count < 1) {
32             return;
33         }
34         const rrule = {
35             value: {
36                 freq: frequency,
37             },
38         } as VcalRruleProperty;
40         if (interval && interval > 1) {
41             rrule.value.interval = interval;
42         }
43         if (frequency === FREQUENCY.WEEKLY) {
44             // weekly.days may include repeated days (cf. function getFrequencyModelChange)
45             const weeklyDays = unique(weekly.days).sort();
46             if (!weeklyDays.length || !weeklyDays.includes(startDate.getDay())) {
47                 throw new Error('Inconsistent weekly rrule');
48             }
49             if (weeklyDays.length > 1) {
50                 rrule.value.byday = weeklyDays.map(numericDayToDay);
51             }
52         }
53         if (frequency === FREQUENCY.MONTHLY) {
54             const startFakeUtcDate = toUTCDate(fromLocalDate(startDate));
55             if (monthly.type === MONTHLY_TYPE.ON_NTH_DAY) {
56                 rrule.value.byday = numericDayToDay(startFakeUtcDate.getUTCDay());
57                 rrule.value.bysetpos = getPositiveSetpos(startFakeUtcDate);
58             }
59             if (monthly.type === MONTHLY_TYPE.ON_MINUS_NTH_DAY) {
60                 rrule.value.byday = numericDayToDay(startFakeUtcDate.getUTCDay());
61                 rrule.value.bysetpos = getNegativeSetpos(startFakeUtcDate);
62             }
63         }
64         if (frequency === FREQUENCY.YEARLY) {
65             // rrule.value.bymonthday = startDate.getDate();
66             // rrule.value.bymonth = startDate.getMonth() + 1;
67         }
68         if (ends.type === END_TYPE.AFTER_N_TIMES) {
69             rrule.value.count = ends.count;
70         }
71         if (ends.type === END_TYPE.UNTIL && ends.until) {
72             rrule.value.until = getUntilProperty(fromLocalDate(ends.until), isAllDay, tzid);
73         }
74         return {
75             rrule,
76         };
77     }
80 export default modelToFrequencyProperties;