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:
authored and GitHub committed 2021-11-18 01:03:19 +00:00
1 parent f3c95fa3de
commit ffdf0b9217
23 files changed
+587 -333

No files matched your search

+6 -17
View File
@@ -6,6 +6,7 @@ import utc from "dayjs/plugin/utc";
import type { NextApiRequest, NextApiResponse } from "next";
import { asStringOrNull } from "@lib/asStringOrNull";
import { getWorkingHours } from "@lib/availability";
import { getBusyCalendarTimes } from "@lib/calendarClient";
import prisma from "@lib/prisma";
@@ -76,26 +77,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}));
const timeZone = eventType?.timeZone || currentUser.timeZone;
const workingHours = eventType?.availability.length ? eventType.availability : currentUser.availability;
// FIXME: Currently the organizer timezone is used for the logic
// refactor to be organizerTimezone unaware, use UTC instead.
const workingHours = getWorkingHours(
{ timeZone },
eventType?.availability.length ? eventType.availability : currentUser.availability
);
res.status(200).json({
busy: bufferedBusyTimes,
timeZone,
workingHours: workingHours
// FIXME: Currently the organizer timezone is used for the logic
// refactor to be organizerTimezone unaware, use UTC instead.
.map((workingHour) => ({
days: workingHour.days,
startTime: dayjs(workingHour.startTime).tz(timeZone).toDate(),
endTime: dayjs(workingHour.endTime).tz(timeZone).toDate(),
}))
.map((workingHour) => ({
days: workingHour.days,
startTime: workingHour.startTime.getHours() * 60 + workingHour.startTime.getMinutes(),
endTime: workingHour.endTime.getHours() * 60 + workingHour.endTime.getMinutes(),
})),
workingHours,
});
}
+8 -4
View File
@@ -1,9 +1,13 @@
import { EventTypeCustomInput, MembershipRole, Prisma } from "@prisma/client";
import { EventTypeCustomInput, MembershipRole, Prisma, PeriodType } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "@lib/auth";
import prisma from "@lib/prisma";
import { OpeningHours } from "@lib/types/event-type";
import { WorkingHours } from "@lib/types/schedule";
function handlePeriodType(periodType: string): PeriodType {
return PeriodType[periodType.toUpperCase()];
}
function handleCustomInputs(customInputs: EventTypeCustomInput[], eventTypeId: number) {
if (!customInputs || !customInputs?.length) return undefined;
@@ -112,7 +116,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
locations: req.body.locations,
eventName: req.body.eventName,
customInputs: handleCustomInputs(req.body.customInputs as EventTypeCustomInput[], req.body.id),
periodType: req.body.periodType,
periodType: req.body.periodType ? handlePeriodType(req.body.periodType) : undefined,
periodDays: req.body.periodDays,
periodStartDate: req.body.periodStartDate,
periodEndDate: req.body.periodEndDate,
@@ -161,7 +165,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
if (req.body.availability) {
const openingHours: OpeningHours[] = req.body.availability.openingHours || [];
const openingHours: WorkingHours[] = req.body.availability.openingHours || [];
// const overrides = req.body.availability.dateOverrides || [];
const eventTypeId = +req.body.id;