Removed code bloat, fixed tests

This commit is contained in:
Alex van Andel 2021-06-29 22:35:13 +00:00
parent 5d30586a24
commit 0da99f0d07
4 changed files with 16 additions and 36 deletions

3
.babelrc Normal file
View File

@ -0,0 +1,3 @@
{
"presets": ["next/babel"]
}

View File

@ -1,7 +0,0 @@
/*export default function DateOverrideModal(props) {
return (
);
}*/

View File

@ -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)

View File

@ -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);
});