Rename accounts to users to be consistent with the new naming scheme where account only referes to the logged in user of the session and implement live updates of users via a user store which listens for updates from the event stream.
73 lines
2 KiB
TypeScript
73 lines
2 KiB
TypeScript
import { readUsers, writeUsers } from "~/server/database";
|
|
import { DateTime, Info } from "~/shared/utils/luxon";
|
|
import { apiAccountPatchSchema } from "~/shared/types/api";
|
|
import { z } from "zod/v4-mini";
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const session = await requireServerSession(event);
|
|
const { success, error, data: patch } = apiAccountPatchSchema.safeParse(await readBody(event));
|
|
if (!success) {
|
|
throw createError({
|
|
status: 400,
|
|
statusText: "Bad Request",
|
|
message: z.prettifyError(error),
|
|
});
|
|
}
|
|
|
|
if (patch.timezone?.length) {
|
|
const zone = Info.normalizeZone(patch.timezone);
|
|
if (!zone.isValid) {
|
|
throw createError({
|
|
status: 400,
|
|
message: `Invalid timezone: the zone "${patch.timezone} is not supported`,
|
|
});
|
|
}
|
|
}
|
|
if (patch.locale?.length) {
|
|
const locale = DateTime.local({ locale: patch.locale }).resolvedLocaleOptions().locale;
|
|
if (locale !== patch.locale) {
|
|
throw createError({
|
|
status: 400,
|
|
message: `Invalid locale: the locale "${patch.locale}" is not supported (was treated as "${locale}")`
|
|
});
|
|
}
|
|
}
|
|
|
|
const users = await readUsers();
|
|
const account = users.find(user => user.id === session.account.id);
|
|
if (!account) {
|
|
throw Error("Account does not exist");
|
|
}
|
|
|
|
if (patch.interestedEventIds !== undefined) {
|
|
if (patch.interestedEventIds.length) {
|
|
account.interestedEventIds = patch.interestedEventIds;
|
|
} else {
|
|
delete account.interestedEventIds;
|
|
}
|
|
}
|
|
if (patch.interestedEventSlotIds !== undefined) {
|
|
if (patch.interestedEventSlotIds.length) {
|
|
account.interestedEventSlotIds = patch.interestedEventSlotIds;
|
|
} else {
|
|
delete account.interestedEventSlotIds;
|
|
}
|
|
}
|
|
if (patch.timezone !== undefined) {
|
|
if (patch.timezone)
|
|
account.timezone = patch.timezone;
|
|
else
|
|
delete account.timezone;
|
|
}
|
|
if (patch.locale !== undefined) {
|
|
if (patch.locale)
|
|
account.locale = patch.locale;
|
|
else
|
|
delete account.locale;
|
|
}
|
|
await writeUsers(users);
|
|
|
|
// Update Schedule counts.
|
|
await updateScheduleInterestedCounts(users);
|
|
})
|