2025-03-07 20:15:41 +01:00
|
|
|
import { Account } from "~/shared/types/account";
|
2025-03-07 22:28:55 +01:00
|
|
|
import { readAccounts, writeAccounts } from "~/server/database";
|
2025-03-07 20:15:41 +01:00
|
|
|
|
|
|
|
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);
|
2025-03-07 20:15:41 +01:00
|
|
|
})
|