owltide/server/api/auth/account.delete.ts
Hornwitser 251e83f640
All checks were successful
/ build (push) Successful in 1m12s
/ deploy (push) Successful in 16s
Rename AcountSession to ServerSession
Start the work of clearly distingushing client side types, server side
types and types shared over the API by renaming "AccountSession" and
"Session" names used on the server to "ServerSession".
2025-06-09 16:51:05 +02:00

45 lines
1.3 KiB
TypeScript

import {
readAccounts, readSessions, readSubscriptions,
writeAccounts, writeSessions, writeSubscriptions,
} from "~/server/database";
import { cancelAccountStreams } from "~/server/streams";
export default defineEventHandler(async (event) => {
const serverSession = await requireServerSession(event);
let accounts = await readAccounts();
const sessionAccount = accounts.find(
account => account.id === serverSession.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 === serverSession.accountId) {
removedSessionIds.add(session.id);
return false;
}
return true;
});
cancelAccountStreams(serverSession.accountId);
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 !== serverSession.accountId);
await writeAccounts(accounts);
// Update Schedule counts.
await updateScheduleInterestedCounts(accounts);
})