From 0da99f0d078a65140ab1a7a94f06d7ee3a0b3eda Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Tue, 29 Jun 2021 22:35:13 +0000 Subject: [PATCH] Removed code bloat, fixed tests --- .babelrc | 3 +++ components/ui/modal/DateOverrideModal.tsx | 7 ------ lib/slots.ts | 15 +++++++------ test/lib/slots.test.ts | 27 +++++------------------ 4 files changed, 16 insertions(+), 36 deletions(-) create mode 100644 .babelrc delete mode 100644 components/ui/modal/DateOverrideModal.tsx diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000000..e49a7e6569 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["next/babel"] +} \ No newline at end of file diff --git a/components/ui/modal/DateOverrideModal.tsx b/components/ui/modal/DateOverrideModal.tsx deleted file mode 100644 index 282f612590..0000000000 --- a/components/ui/modal/DateOverrideModal.tsx +++ /dev/null @@ -1,7 +0,0 @@ - - -/*export default function DateOverrideModal(props) { - return ( - - ); -}*/ \ No newline at end of file diff --git a/lib/slots.ts b/lib/slots.ts index 7d3a777373..c82913b597 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -4,20 +4,20 @@ import timezone from "dayjs/plugin/timezone"; dayjs.extend(utc); dayjs.extend(timezone); -interface GetSlotsType { +type GetSlots = { inviteeDate: Dayjs; frequency: number; workingHours: []; minimumBookingNotice?: number; - organizerUtcOffset: number; -} + organizerTimeZone: string; +}; -interface Boundary { +type Boundary = { lowerBound: number; upperBound: number; -} +}; -const freqApply: number = (cb, value: number, frequency: number): number => cb(value / frequency) * frequency; +const freqApply = (cb, value: number, frequency: number): number => cb(value / frequency) * frequency; const intersectBoundary = (a: Boundary, b: Boundary) => { if (a.upperBound < b.lowerBound || a.lowerBound > b.upperBound) { @@ -108,12 +108,13 @@ const getSlots = ({ minimumBookingNotice, workingHours, organizerTimeZone, -}: GetSlotsType): Dayjs[] => { +}: GetSlots): Dayjs[] => { const startTime = dayjs.utc().isSame(dayjs(inviteeDate), "day") ? inviteeDate.hour() * 60 + inviteeDate.minute() + minimumBookingNotice : 0; const inviteeBounds = inviteeBoundary(startTime, inviteeDate.utcOffset(), frequency); + return getOverlaps( inviteeBounds, organizerBoundaries(workingHours, inviteeDate, inviteeBounds, organizerTimeZone) diff --git a/test/lib/slots.test.ts b/test/lib/slots.test.ts index 90bec4be3b..8278837173 100644 --- a/test/lib/slots.test.ts +++ b/test/lib/slots.test.ts @@ -13,27 +13,10 @@ it('can fit 24 hourly slots for an empty day', async () => { // 24h in a day. expect(getSlots({ inviteeDate: dayjs().add(1, 'day'), - length: 60, + frequency: 60, + workingHours: [ + { days: [...Array(7).keys()], startTime: 0, endTime: 1440 } + ], + organizerTimeZone: 'Europe/London' })).toHaveLength(24); -}); - -it('has slots that be in the same timezone as the invitee', async() => { - expect(getSlots({ - inviteeDate: dayjs().add(1, 'day'), - length: 60 - })[0].utcOffset()).toBe(-0); - - expect(getSlots({ - inviteeDate: dayjs().tz('Europe/London').add(1, 'day'), - length: 60 - })[0].utcOffset()).toBe(dayjs().tz('Europe/London').utcOffset()); -}) - -it('excludes slots that have already passed when invitee day equals today', async () => { - expect(getSlots({ inviteeDate: dayjs(), length: 60 })).toHaveLength(12); -}); - -it('supports having slots in different utc offset than the invitee', async () => { - expect(getSlots({ inviteeDate: dayjs(), length: 60 })).toHaveLength(12); - expect(getSlots({ inviteeDate: dayjs().tz('Europe/Brussels'), length: 60 })).toHaveLength(14); }); \ No newline at end of file