2025-03-07 22:28:55 +01:00
|
|
|
import { readAccounts, writeAccounts } from "~/server/database";
|
2025-03-09 15:53:51 +01:00
|
|
|
import { DateTime } from "luxon";
|
2025-06-11 21:05:17 +02:00
|
|
|
import { apiAccountPatchSchema } from "~/shared/types/api";
|
|
|
|
import { z } from "zod/v4-mini";
|
|
|
|
|
2025-03-07 20:15:41 +01:00
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
2025-06-09 16:51:05 +02:00
|
|
|
const session = await requireServerSession(event);
|
2025-06-11 21:05:17 +02:00
|
|
|
const { success, error, data: patch } = apiAccountPatchSchema.safeParse(await readBody(event));
|
|
|
|
if (!success) {
|
2025-03-07 20:15:41 +01:00
|
|
|
throw createError({
|
|
|
|
status: 400,
|
2025-06-11 21:05:17 +02:00
|
|
|
statusText: "Bad Request",
|
|
|
|
message: z.prettifyError(error),
|
2025-03-07 20:15:41 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
if (patch.timezone?.length) {
|
|
|
|
const zonedTime = DateTime.local({ locale: "en-US" }).setZone(patch.timezone);
|
|
|
|
if (!zonedTime.isValid) {
|
2025-03-09 15:53:51 +01:00
|
|
|
throw createError({
|
|
|
|
status: 400,
|
2025-06-11 21:05:17 +02:00
|
|
|
message: "Invalid timezone: " + zonedTime.invalidExplanation,
|
2025-03-09 15:53:51 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2025-06-13 21:50:22 +02:00
|
|
|
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}")`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2025-03-09 15:53:51 +01:00
|
|
|
|
2025-03-07 20:15:41 +01:00
|
|
|
const accounts = await readAccounts();
|
|
|
|
const sessionAccount = accounts.find(account => account.id === session.accountId);
|
|
|
|
if (!sessionAccount) {
|
|
|
|
throw Error("Account does not exist");
|
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
if (patch.interestedEventIds !== undefined) {
|
|
|
|
if (patch.interestedEventIds.length) {
|
|
|
|
sessionAccount.interestedEventIds = patch.interestedEventIds;
|
|
|
|
} else {
|
|
|
|
delete sessionAccount.interestedEventIds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (patch.interestedEventSlotIds !== undefined) {
|
|
|
|
if (patch.interestedEventSlotIds.length) {
|
|
|
|
sessionAccount.interestedEventSlotIds = patch.interestedEventSlotIds;
|
2025-03-09 15:53:51 +01:00
|
|
|
} else {
|
2025-06-11 21:05:17 +02:00
|
|
|
delete sessionAccount.interestedEventSlotIds;
|
2025-03-09 15:53:51 +01:00
|
|
|
}
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
if (patch.timezone !== undefined) {
|
|
|
|
if (patch.timezone)
|
|
|
|
sessionAccount.timezone = patch.timezone;
|
2025-03-09 15:53:51 +01:00
|
|
|
else
|
|
|
|
delete sessionAccount.timezone;
|
2025-03-07 20:15:41 +01:00
|
|
|
}
|
2025-06-13 21:50:22 +02:00
|
|
|
if (patch.locale !== undefined) {
|
|
|
|
if (patch.locale)
|
|
|
|
sessionAccount.locale = patch.locale;
|
|
|
|
else
|
|
|
|
delete sessionAccount.locale;
|
|
|
|
}
|
2025-03-07 20:15:41 +01:00
|
|
|
await writeAccounts(accounts);
|
|
|
|
|
2025-03-07 22:28:55 +01:00
|
|
|
// Update Schedule counts.
|
|
|
|
await updateScheduleInterestedCounts(accounts);
|
2025-03-07 20:15:41 +01:00
|
|
|
})
|