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:
parent
6336ccdb96
commit
3be7f8be05
24 changed files with 331 additions and 182 deletions
|
@ -1,45 +1,49 @@
|
|||
import {
|
||||
readAccounts, readSessions, readSubscriptions,
|
||||
writeAccounts, writeSessions, writeSubscriptions,
|
||||
readUsers, readSessions, readSubscriptions,
|
||||
writeUsers, writeSessions, writeSubscriptions,
|
||||
} from "~/server/database";
|
||||
import { cancelAccountStreams } from "~/server/streams";
|
||||
import { broadcastEvent, cancelAccountStreams } from "~/server/streams";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const serverSession = await requireServerSession(event);
|
||||
let users = await readUsers();
|
||||
|
||||
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
|
||||
// Remove sessions for this user
|
||||
const removedSessionIds = new Set<number>();
|
||||
let sessions = await readSessions();
|
||||
sessions = sessions.filter(session => {
|
||||
if (session.accountId === serverSession.accountId) {
|
||||
if (session.account.id === serverSession.account.id) {
|
||||
removedSessionIds.add(session.id);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
cancelAccountStreams(serverSession.accountId);
|
||||
cancelAccountStreams(serverSession.account.id);
|
||||
await writeSessions(sessions);
|
||||
await deleteCookie(event, "session");
|
||||
|
||||
// Remove subscriptions for this account
|
||||
// Remove subscriptions for this user
|
||||
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);
|
||||
// Remove the user
|
||||
const account = users.find(user => user.id === serverSession.account.id)!;
|
||||
const now = new Date().toISOString();
|
||||
account.deleted = true;
|
||||
account.updatedAt = now;
|
||||
await writeUsers(users);
|
||||
await broadcastEvent({
|
||||
type: "user-update",
|
||||
data: {
|
||||
id: account.id,
|
||||
updatedAt: now,
|
||||
deleted: true,
|
||||
}
|
||||
});
|
||||
|
||||
// Update Schedule counts.
|
||||
await updateScheduleInterestedCounts(accounts);
|
||||
await updateScheduleInterestedCounts(users);
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue