2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-06-23 00:17:22 +02:00
|
|
|
import { readUsers, writeUsers } from "~/server/database";
|
2025-06-14 19:26:58 +02:00
|
|
|
import { DateTime, Info } from "~/shared/utils/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) {
|
2025-06-14 19:26:58 +02:00
|
|
|
const zone = Info.normalizeZone(patch.timezone);
|
|
|
|
if (!zone.isValid) {
|
2025-03-09 15:53:51 +01:00
|
|
|
throw createError({
|
|
|
|
status: 400,
|
2025-06-14 19:26:58 +02:00
|
|
|
message: `Invalid timezone: the zone "${patch.timezone} is not supported`,
|
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-06-23 00:17:22 +02:00
|
|
|
const users = await readUsers();
|
|
|
|
const account = users.find(user => user.id === session.account.id);
|
|
|
|
if (!account) {
|
2025-03-07 20:15:41 +01:00
|
|
|
throw Error("Account does not exist");
|
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
if (patch.interestedEventIds !== undefined) {
|
|
|
|
if (patch.interestedEventIds.length) {
|
2025-06-23 00:17:22 +02:00
|
|
|
account.interestedEventIds = patch.interestedEventIds;
|
2025-06-11 21:05:17 +02:00
|
|
|
} else {
|
2025-06-23 00:17:22 +02:00
|
|
|
delete account.interestedEventIds;
|
2025-06-11 21:05:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (patch.interestedEventSlotIds !== undefined) {
|
|
|
|
if (patch.interestedEventSlotIds.length) {
|
2025-06-23 00:17:22 +02:00
|
|
|
account.interestedEventSlotIds = patch.interestedEventSlotIds;
|
2025-03-09 15:53:51 +01:00
|
|
|
} else {
|
2025-06-23 00:17:22 +02:00
|
|
|
delete account.interestedEventSlotIds;
|
2025-03-09 15:53:51 +01:00
|
|
|
}
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
if (patch.timezone !== undefined) {
|
|
|
|
if (patch.timezone)
|
2025-06-23 00:17:22 +02:00
|
|
|
account.timezone = patch.timezone;
|
2025-03-09 15:53:51 +01:00
|
|
|
else
|
2025-06-23 00:17:22 +02:00
|
|
|
delete account.timezone;
|
2025-03-07 20:15:41 +01:00
|
|
|
}
|
2025-06-13 21:50:22 +02:00
|
|
|
if (patch.locale !== undefined) {
|
|
|
|
if (patch.locale)
|
2025-06-23 00:17:22 +02:00
|
|
|
account.locale = patch.locale;
|
2025-06-13 21:50:22 +02:00
|
|
|
else
|
2025-06-23 00:17:22 +02:00
|
|
|
delete account.locale;
|
2025-06-13 21:50:22 +02:00
|
|
|
}
|
2025-06-23 00:17:22 +02:00
|
|
|
await writeUsers(users);
|
2025-03-07 20:15:41 +01:00
|
|
|
|
2025-03-07 22:28:55 +01:00
|
|
|
// Update Schedule counts.
|
2025-06-23 00:17:22 +02:00
|
|
|
await updateScheduleInterestedCounts(users);
|
2025-03-07 20:15:41 +01:00
|
|
|
})
|