I firmly believe in free software. The application I'm making here have capabilities that I've not seen in any system. It presents itself as an opportunity to collaborate on a tool that serves the people rather than corporations. Whose incentives are to help people rather, not make the most money. And whose terms ensure that these freedoms and incentives cannot be taken back or subverted. I license this software under the AGPL.
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
/*
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
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);
|
|
})
|