Removed code bloat, fixed tests
parent
5d30586a24
commit
0da99f0d07
|
@ -1,7 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
/*export default function DateOverrideModal(props) {
|
|
||||||
return (
|
|
||||||
|
|
||||||
);
|
|
||||||
}*/
|
|
15
lib/slots.ts
15
lib/slots.ts
|
@ -4,20 +4,20 @@ import timezone from "dayjs/plugin/timezone";
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
dayjs.extend(timezone);
|
dayjs.extend(timezone);
|
||||||
|
|
||||||
interface GetSlotsType {
|
type GetSlots = {
|
||||||
inviteeDate: Dayjs;
|
inviteeDate: Dayjs;
|
||||||
frequency: number;
|
frequency: number;
|
||||||
workingHours: [];
|
workingHours: [];
|
||||||
minimumBookingNotice?: number;
|
minimumBookingNotice?: number;
|
||||||
organizerUtcOffset: number;
|
organizerTimeZone: string;
|
||||||
}
|
};
|
||||||
|
|
||||||
interface Boundary {
|
type Boundary = {
|
||||||
lowerBound: number;
|
lowerBound: number;
|
||||||
upperBound: 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) => {
|
const intersectBoundary = (a: Boundary, b: Boundary) => {
|
||||||
if (a.upperBound < b.lowerBound || a.lowerBound > b.upperBound) {
|
if (a.upperBound < b.lowerBound || a.lowerBound > b.upperBound) {
|
||||||
|
@ -108,12 +108,13 @@ const getSlots = ({
|
||||||
minimumBookingNotice,
|
minimumBookingNotice,
|
||||||
workingHours,
|
workingHours,
|
||||||
organizerTimeZone,
|
organizerTimeZone,
|
||||||
}: GetSlotsType): Dayjs[] => {
|
}: GetSlots): Dayjs[] => {
|
||||||
const startTime = dayjs.utc().isSame(dayjs(inviteeDate), "day")
|
const startTime = dayjs.utc().isSame(dayjs(inviteeDate), "day")
|
||||||
? inviteeDate.hour() * 60 + inviteeDate.minute() + minimumBookingNotice
|
? inviteeDate.hour() * 60 + inviteeDate.minute() + minimumBookingNotice
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
const inviteeBounds = inviteeBoundary(startTime, inviteeDate.utcOffset(), frequency);
|
const inviteeBounds = inviteeBoundary(startTime, inviteeDate.utcOffset(), frequency);
|
||||||
|
|
||||||
return getOverlaps(
|
return getOverlaps(
|
||||||
inviteeBounds,
|
inviteeBounds,
|
||||||
organizerBoundaries(workingHours, inviteeDate, inviteeBounds, organizerTimeZone)
|
organizerBoundaries(workingHours, inviteeDate, inviteeBounds, organizerTimeZone)
|
||||||
|
|
|
@ -13,27 +13,10 @@ it('can fit 24 hourly slots for an empty day', async () => {
|
||||||
// 24h in a day.
|
// 24h in a day.
|
||||||
expect(getSlots({
|
expect(getSlots({
|
||||||
inviteeDate: dayjs().add(1, 'day'),
|
inviteeDate: dayjs().add(1, 'day'),
|
||||||
length: 60,
|
frequency: 60,
|
||||||
|
workingHours: [
|
||||||
|
{ days: [...Array(7).keys()], startTime: 0, endTime: 1440 }
|
||||||
|
],
|
||||||
|
organizerTimeZone: 'Europe/London'
|
||||||
})).toHaveLength(24);
|
})).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);
|
|
||||||
});
|
|
Loading…
Reference in New Issue