Refactor user storage and update

Rename accounts to users to be consistent with the new naming scheme
where account only referes to the logged in user of the session and
implement live updates of users via a user store which listens for
updates from the event stream.
This commit is contained in:
Hornwitser 2025-06-23 00:17:22 +02:00
parent 6336ccdb96
commit 3be7f8be05
24 changed files with 331 additions and 182 deletions

View file

@ -1,4 +1,4 @@
import { readAccounts, writeAccounts } from "~/server/database";
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";
@ -34,40 +34,40 @@ export default defineEventHandler(async (event) => {
}
}
const accounts = await readAccounts();
const sessionAccount = accounts.find(account => account.id === session.accountId);
if (!sessionAccount) {
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) {
sessionAccount.interestedEventIds = patch.interestedEventIds;
account.interestedEventIds = patch.interestedEventIds;
} else {
delete sessionAccount.interestedEventIds;
delete account.interestedEventIds;
}
}
if (patch.interestedEventSlotIds !== undefined) {
if (patch.interestedEventSlotIds.length) {
sessionAccount.interestedEventSlotIds = patch.interestedEventSlotIds;
account.interestedEventSlotIds = patch.interestedEventSlotIds;
} else {
delete sessionAccount.interestedEventSlotIds;
delete account.interestedEventSlotIds;
}
}
if (patch.timezone !== undefined) {
if (patch.timezone)
sessionAccount.timezone = patch.timezone;
account.timezone = patch.timezone;
else
delete sessionAccount.timezone;
delete account.timezone;
}
if (patch.locale !== undefined) {
if (patch.locale)
sessionAccount.locale = patch.locale;
account.locale = patch.locale;
else
delete sessionAccount.locale;
delete account.locale;
}
await writeAccounts(accounts);
await writeUsers(users);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
await updateScheduleInterestedCounts(users);
})