To make it possible to render the timetable in the user's local time we need to know the timezone to render it in on the server. Otherwise there will be hydration errors and paint flashing as the client renders a different timezone. Add a server global default timezone that can be overriden on a per-account bases to prepare for timezone handling the timetable.
62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import { Account } from "~/shared/types/account";
|
|
import { readAccounts, writeAccounts } from "~/server/database";
|
|
import { DateTime } from "luxon";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await requireAccountSession(event);
|
|
const body: Pick<Account, "interestedIds" | "timezone"> = await readBody(event);
|
|
if (
|
|
body.interestedIds !== undefined
|
|
&& !(
|
|
!(body.interestedIds instanceof Array)
|
|
|| !body.interestedIds.every(id => typeof id === "string")
|
|
)
|
|
) {
|
|
throw createError({
|
|
status: 400,
|
|
message: "Invalid interestedIds",
|
|
});
|
|
}
|
|
|
|
if (body.timezone !== undefined) {
|
|
if (typeof body.timezone !== "string") {
|
|
throw createError({
|
|
status: 400,
|
|
message: "Invalid timezone",
|
|
});
|
|
}
|
|
if (body.timezone.length) {
|
|
const zonedTime = DateTime.local().setZone(body.timezone);
|
|
if (!zonedTime.isValid) {
|
|
throw createError({
|
|
status: 400,
|
|
message: "Invalid timezone: " + zonedTime.invalidExplanation,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const accounts = await readAccounts();
|
|
const sessionAccount = accounts.find(account => account.id === session.accountId);
|
|
if (!sessionAccount) {
|
|
throw Error("Account does not exist");
|
|
}
|
|
|
|
if (body.interestedIds !== undefined) {
|
|
if (body.interestedIds.length) {
|
|
sessionAccount.interestedIds = body.interestedIds;
|
|
} else {
|
|
delete sessionAccount.interestedIds;
|
|
}
|
|
}
|
|
if (body.timezone !== undefined) {
|
|
if (body.timezone)
|
|
sessionAccount.timezone = body.timezone;
|
|
else
|
|
delete sessionAccount.timezone;
|
|
}
|
|
await writeAccounts(accounts);
|
|
|
|
// Update Schedule counts.
|
|
await updateScheduleInterestedCounts(accounts);
|
|
})
|