1 import { isBefore } from 'date-fns';
2 import { c } from 'ttag';
4 import { END_TYPE, FREQUENCY, MAX_NOTIFICATIONS } from '@proton/shared/lib/calendar/constants';
5 import type { EventModel, EventModelErrors } from '@proton/shared/lib/interfaces/calendar';
7 import { getTimeInUtc } from './time';
9 // returns array of ids exceeding MAX_NOTIFICATIONS
10 export const getExcessiveNotificationsIndices = (fields: object[], limit = MAX_NOTIFICATIONS) =>
11 fields.map((_, idx) => (idx + 1 > limit ? idx : undefined)).filter((idx): idx is number => idx !== undefined);
13 const validateEventModel = ({ start, end, isAllDay, frequencyModel }: EventModel) => {
14 const errors: EventModelErrors = {};
16 const utcStart = getTimeInUtc(start, isAllDay);
17 const utcEnd = getTimeInUtc(end, isAllDay);
19 if (utcStart > utcEnd) {
20 errors.end = c('Error').t`Start time must be before end time`;
23 if (frequencyModel.type === FREQUENCY.CUSTOM) {
24 if (!frequencyModel.interval) {
25 errors.interval = c('Error').t`Interval cannot be empty`;
28 if (frequencyModel.ends.type === END_TYPE.UNTIL) {
29 if (!frequencyModel.ends.until) {
30 errors.until = c('Error').t`Ends on date cannot be empty`;
32 if (frequencyModel.ends.until && isBefore(frequencyModel.ends.until, start.date)) {
33 errors.until = c('Error').t`Ends on date must be after start time`;
37 if (frequencyModel.ends.type === END_TYPE.AFTER_N_TIMES) {
38 if (!frequencyModel.ends.count) {
39 errors.count = c('Error').t`Number of occurrences cannot be empty`;
47 export default validateEventModel;