Skip to content

Commit

Permalink
Simplify migrateMeetingTime
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Aheichyk <[email protected]>
  • Loading branch information
Mikhail Aheichyk committed Oct 18, 2023
1 parent e8d714e commit 8ff6bbe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 353 deletions.

This file was deleted.

This file was deleted.

32 changes: 0 additions & 32 deletions matrix-meetings-bot/src/util/migrateMeetingTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { CalendarEntryDto } from '../dto/CalendarEntryDto';
import { mockCalendarEntry } from '../testUtils';
import { migrateMeetingTime } from './migrateMeetingTime';

Expand Down Expand Up @@ -250,35 +249,4 @@ describe('migrateMeetingTime', () => {
}),
]);
});

it.each<{
calendar: CalendarEntryDto[] | undefined;
externalRrule: string | undefined;
}>([
{ calendar: [], externalRrule: undefined },
{ calendar: [], externalRrule: 'FREQ=DAILY;COUNT=1' },
{
calendar: [
{
uid: 'some-id',
dtstart: { tzid: 'UTC', value: '20220101T000000' },
dtend: { tzid: 'UTC', value: '20220103T000000' },
rrule: 'FREQ=DAILY;COUNT=1',
},
],
externalRrule: undefined,
},
])('should fail with %s', ({ calendar, externalRrule }) => {
const meetingTime = {
start_time: '2022-01-01T00:00:00.000Z',
end_time: '2022-01-03T00:00:00.000Z',
calendar,
};

expect(() =>
migrateMeetingTime(meetingTime, externalRrule, undefined),
).toThrowError(
'Unexpected input: either start_time with end_time or calendar should be provided',
);
});
});
36 changes: 11 additions & 25 deletions matrix-meetings-bot/src/util/migrateMeetingTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import { DateTime } from 'luxon';
import { v4 as uuiv4 } from 'uuid';
import { CalendarEntryDto } from '../dto/CalendarEntryDto';
import { formatICalDate } from '../shared';
import { isRRuleOverrideEntry } from '../shared/calendarUtils/helpers';
import { overrideCalendarEntries } from '../shared/calendarUtils/overrideCalendarEntries';

export interface IMeetingTime {
start_time?: string;
Expand All @@ -35,31 +33,19 @@ export interface IMeetingTime {
*/
export function migrateMeetingTime(
meetingTime: IMeetingTime,
externalRrule?: string,
externalRrule?: string | undefined,
existingCalendar?: CalendarEntryDto[],
): CalendarEntryDto[] {
if (meetingTime.start_time && meetingTime.end_time && !meetingTime.calendar) {
const uid = existingCalendar?.find((e) => !isRRuleOverrideEntry(e))?.uid;

const calendarEntry = {
uid: uid ?? uuiv4(),
dtstart: formatICalDate(DateTime.fromISO(meetingTime.start_time)),
dtend: formatICalDate(DateTime.fromISO(meetingTime.end_time)),
rrule: externalRrule,
};

return existingCalendar && uid
? overrideCalendarEntries(existingCalendar, calendarEntry)
: [calendarEntry];
} else if (
!meetingTime.start_time &&
!meetingTime.end_time &&
meetingTime.calendar
) {
return meetingTime.calendar;
} else {
throw new Error(
'Unexpected input: either start_time with end_time or calendar should be provided',
);
return [
{
uid: existingCalendar?.[0]?.uid ?? uuiv4(),
dtstart: formatICalDate(DateTime.fromISO(meetingTime.start_time)),
dtend: formatICalDate(DateTime.fromISO(meetingTime.end_time)),
rrule: externalRrule,
},
];
}

return meetingTime.calendar ?? [];
}

0 comments on commit 8ff6bbe

Please sign in to comment.