Add delete account function

This commit is contained in:
Hornwitser 2025-03-07 22:28:55 +01:00
parent 3535105744
commit 598b9fd7d6
5 changed files with 96 additions and 19 deletions

View file

@ -0,0 +1,43 @@
import {
readAccounts, readSessions, readSubscriptions,
writeAccounts, writeSessions, writeSubscriptions,
} from "~/server/database";
export default defineEventHandler(async (event) => {
const accountSession = await requireAccountSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === accountSession.accountId
);
if (!sessionAccount) {
throw Error("Account does not exist");
}
// Remove sessions for this account
const removedSessionIds = new Set<number>();
let sessions = await readSessions();
sessions = sessions.filter(session => {
if (session.accountId === accountSession.accountId) {
removedSessionIds.add(session.id);
return false;
}
return true;
});
await writeSessions(sessions);
await deleteCookie(event, "session");
// Remove subscriptions for this account
let subscriptions = await readSubscriptions();
subscriptions = subscriptions.filter(
subscription => !removedSessionIds.has(subscription.sessionId)
);
await writeSubscriptions(subscriptions);
// Remove the account
accounts = accounts.filter(account => account.id !== accountSession.accountId);
await writeAccounts(accounts);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})

View file

@ -1,6 +1,5 @@
import { Account } from "~/shared/types/account";
import { readAccounts, readSchedule, writeAccounts, writeSchedule } from "~/server/database";
import { broadcastUpdate } from "~/server/streams";
import { readAccounts, writeAccounts } from "~/server/database";
export default defineEventHandler(async (event) => {
const session = await requireAccountSession(event);
@ -28,19 +27,6 @@ export default defineEventHandler(async (event) => {
}
await writeAccounts(accounts);
const counts = new Map();
for (const account of accounts)
if (account.interestedIds)
for (const id of account.interestedIds)
counts.set(id, (counts.get(id) ?? 0) + 1);
const schedule = await readSchedule();
for (const event of schedule.events) {
event.interested = counts.get(event.id);
for (const slot of event.slots) {
slot.interested = counts.get(slot.id);
}
}
await writeSchedule(schedule);
broadcastUpdate(schedule);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})