owltide/server/api/account.patch.ts

33 lines
934 B
TypeScript
Raw Normal View History

import { Account } from "~/shared/types/account";
2025-03-07 22:28:55 +01:00
import { readAccounts, writeAccounts } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
const body: Pick<Account, "interestedIds"> = await readBody(event);
if (
!(body.interestedIds instanceof Array)
|| !body.interestedIds.every(id => typeof id === "string")
) {
throw createError({
status: 400,
message: "Invalid interestedIds",
});
}
const accounts = await readAccounts();
const sessionAccount = accounts.find(account => account.id === session.accountId);
if (!sessionAccount) {
throw Error("Account does not exist");
}
if (body.interestedIds.length) {
sessionAccount.interestedIds = body.interestedIds;
} else {
delete sessionAccount.interestedIds;
}
await writeAccounts(accounts);
2025-03-07 22:28:55 +01:00
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})