Fixes user availability to be contextual to the user timezone (#1166)
* WIP, WIP, WIP, WIP * Adds missing types * Type fixes for useSlots * Type fixes * Fixes periodType 500 error when updating * Adds missing dayjs plugin and type fixes * An attempt was made to fix tests * Save work in progress * Added UTC overflow to days * Update lib/availability.ts Co-authored-by: Alex Johansson <[email protected]> * No more magic numbers * Fixed slots.test & added getWorkingHours.test * Tests pass, simpler logic, profit? * Timezone shifting! * Forgot to unskip tests * Updated the user page * Added American seed user, some fixes * tmp fix so to continue testing availability * Removed timeZone parameter, fix defaultValue auto-scroll Co-authored-by: Omar López <[email protected]> Co-authored-by: Alex Johansson <[email protected]>
This commit is contained in:
23 files changed
+587
-333
No files matched your search
@@ -0,0 +1,159 @@
|
||||
import { expect, it } from "@jest/globals";
|
||||
import dayjs from "dayjs";
|
||||
import timezone from "dayjs/plugin/timezone";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
import MockDate from "mockdate";
|
||||
|
||||
import { getWorkingHours } from "@lib/availability";
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
MockDate.set("2021-06-20T11:59:59Z");
|
||||
|
||||
it("correctly translates Availability (UTC+0) to UTC workingHours", async () => {
|
||||
expect(
|
||||
getWorkingHours({ timeZone: "GMT" }, [
|
||||
{
|
||||
days: [0],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 23)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
{
|
||||
days: [0],
|
||||
endTime: 1439,
|
||||
startTime: 1380,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("correctly translates Availability in a positive UTC offset (Pacific/Auckland) to UTC workingHours", async () => {
|
||||
// Take note that (Pacific/Auckland) is UTC+12 on 2021-06-20, NOT +13 like the other half of the year.
|
||||
expect(
|
||||
getWorkingHours({ timeZone: "Pacific/Auckland" }, [
|
||||
{
|
||||
days: [1],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
{
|
||||
days: [1],
|
||||
endTime: 719,
|
||||
startTime: 0,
|
||||
},
|
||||
{
|
||||
days: [0],
|
||||
endTime: 1439,
|
||||
startTime: 720, // 0 (midnight) - 12 * 60 (DST)
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("correctly translates Availability in a negative UTC offset (Pacific/Midway) to UTC workingHours", async () => {
|
||||
// Take note that (Pacific/Midway) is UTC-12 on 2021-06-20, NOT +13 like the other half of the year.
|
||||
expect(
|
||||
getWorkingHours({ timeZone: "Pacific/Midway" }, [
|
||||
{
|
||||
days: [1],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
{
|
||||
days: [2],
|
||||
endTime: 659,
|
||||
startTime: 0,
|
||||
},
|
||||
{
|
||||
days: [1],
|
||||
endTime: 1439,
|
||||
startTime: 660,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("can do the same with UTC offsets", async () => {
|
||||
// Take note that (Pacific/Midway) is UTC-12 on 2021-06-20, NOT +13 like the other half of the year.
|
||||
expect(
|
||||
getWorkingHours({ utcOffset: dayjs().tz("Pacific/Midway").utcOffset() }, [
|
||||
{
|
||||
days: [1],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
{
|
||||
days: [2],
|
||||
endTime: 659,
|
||||
startTime: 0,
|
||||
},
|
||||
{
|
||||
days: [1],
|
||||
endTime: 1439,
|
||||
startTime: 660,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("can also shift UTC into other timeZones", async () => {
|
||||
// UTC+0 time with 23:00 - 23:59 (Sunday) and 00:00 - 16:00 (Monday) when cast into UTC+1 should become 00:00 = 17:00 (Monday)
|
||||
expect(
|
||||
getWorkingHours({ utcOffset: -60 }, [
|
||||
{
|
||||
days: [0],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 23)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
{
|
||||
days: [1],
|
||||
startTime: new Date(Date.UTC(2021, 11, 17, 0)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 17, 16)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
// TODO: Maybe the desired result is 0-1020 as a single entry, but this requires some post-processing to merge. It may work as is so leaving this as now.
|
||||
{
|
||||
days: [1],
|
||||
endTime: 59,
|
||||
startTime: 0,
|
||||
},
|
||||
{
|
||||
days: [1],
|
||||
endTime: 1020,
|
||||
startTime: 60,
|
||||
},
|
||||
]);
|
||||
// And the other way around; UTC+0 time with 00:00 - 1:00 (Monday) and 21:00 - 24:00 (Sunday) when cast into UTC-1 should become 20:00 = 24:00 (Sunday)
|
||||
expect(
|
||||
getWorkingHours({ utcOffset: 60 }, [
|
||||
{
|
||||
days: [0],
|
||||
startTime: new Date(Date.UTC(2021, 11, 16, 21)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
|
||||
},
|
||||
{
|
||||
days: [1],
|
||||
startTime: new Date(Date.UTC(2021, 11, 17, 0)),
|
||||
endTime: new Date(Date.UTC(2021, 11, 17, 1)),
|
||||
},
|
||||
])
|
||||
).toStrictEqual([
|
||||
// TODO: Maybe the desired result is 1200-1439 as a single entry, but this requires some post-processing to merge. It may work as is so leaving this as now.
|
||||
{
|
||||
days: [0],
|
||||
endTime: 1379,
|
||||
startTime: 1200,
|
||||
},
|
||||
{
|
||||
days: [0],
|
||||
endTime: 1439,
|
||||
startTime: 1380,
|
||||
},
|
||||
]);
|
||||
});
|
||||
+34
-8
@@ -4,6 +4,7 @@ import timezone from "dayjs/plugin/timezone";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
import MockDate from "mockdate";
|
||||
|
||||
import { MINUTES_DAY_END, MINUTES_DAY_START } from "@lib/availability";
|
||||
import getSlots from "@lib/slots";
|
||||
|
||||
dayjs.extend(utc);
|
||||
@@ -17,8 +18,14 @@ it("can fit 24 hourly slots for an empty day", async () => {
|
||||
getSlots({
|
||||
inviteeDate: dayjs().add(1, "day"),
|
||||
frequency: 60,
|
||||
workingHours: [{ days: Array.from(Array(7).keys()), startTime: 0, endTime: 1440 }],
|
||||
organizerTimeZone: "Europe/London",
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
})
|
||||
).toHaveLength(24);
|
||||
});
|
||||
@@ -29,8 +36,14 @@ it.skip("only shows future booking slots on the same day", async () => {
|
||||
getSlots({
|
||||
inviteeDate: dayjs(),
|
||||
frequency: 60,
|
||||
workingHours: [{ days: Array.from(Array(7).keys()), startTime: 0, endTime: 1440 }],
|
||||
organizerTimeZone: "GMT",
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
})
|
||||
).toHaveLength(12);
|
||||
});
|
||||
@@ -40,19 +53,32 @@ it("can cut off dates that due to invitee timezone differences fall on the next
|
||||
getSlots({
|
||||
inviteeDate: dayjs().tz("Europe/Amsterdam").startOf("day"), // time translation +01:00
|
||||
frequency: 60,
|
||||
workingHours: [{ days: [0], startTime: 1380, endTime: 1440 }],
|
||||
organizerTimeZone: "Europe/London",
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
days: [0],
|
||||
startTime: 23 * 60, // 23h
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
})
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it.skip("can cut off dates that due to invitee timezone differences fall on the previous day", async () => {
|
||||
const workingHours = [
|
||||
{
|
||||
days: [0],
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: 1 * 60, // 1h
|
||||
},
|
||||
];
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs().startOf("day"), // time translation -01:00
|
||||
frequency: 60,
|
||||
workingHours: [{ days: [0], startTime: 0, endTime: 60 }],
|
||||
organizerTimeZone: "Europe/London",
|
||||
minimumBookingNotice: 0,
|
||||
workingHours,
|
||||
})
|
||||
).toHaveLength(0);
|
||||
});
|
||||
Reference in new issue
Block a user