Add delete account function
This commit is contained in:
parent
3535105744
commit
598b9fd7d6
5 changed files with 96 additions and 19 deletions
|
@ -58,6 +58,10 @@ button {
|
||||||
font-size: inherit;
|
font-size: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fieldset {
|
||||||
|
padding-inline: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
label {
|
label {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,13 @@
|
||||||
<p>
|
<p>
|
||||||
<PushNotification />
|
<PushNotification />
|
||||||
</p>
|
</p>
|
||||||
|
<fieldset>
|
||||||
|
<legend>Danger Zone</legend>
|
||||||
|
Delete my account and all data associated with it
|
||||||
|
<button @click="deleteAccount">
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</fieldset>
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -15,8 +22,24 @@ definePageMeta({
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data: session } = useAccountSession();
|
const { data: session } = useAccountSession();
|
||||||
|
const { refresh: sessionRefresh } = useAccountSession();
|
||||||
|
|
||||||
|
async function deleteAccount() {
|
||||||
|
try {
|
||||||
|
await $fetch.raw("/api/account", {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
await sessionRefresh();
|
||||||
|
await navigateTo("/");
|
||||||
|
|
||||||
|
} catch (err: any) {
|
||||||
|
alert(`Delete account failed: ${err.statusCode} ${err.statusMessage}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style scoped>
|
||||||
|
fieldset {
|
||||||
|
width: fit-content;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
43
server/api/account.delete.ts
Normal file
43
server/api/account.delete.ts
Normal 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);
|
||||||
|
})
|
|
@ -1,6 +1,5 @@
|
||||||
import { Account } from "~/shared/types/account";
|
import { Account } from "~/shared/types/account";
|
||||||
import { readAccounts, readSchedule, writeAccounts, writeSchedule } from "~/server/database";
|
import { readAccounts, writeAccounts } from "~/server/database";
|
||||||
import { broadcastUpdate } from "~/server/streams";
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const session = await requireAccountSession(event);
|
const session = await requireAccountSession(event);
|
||||||
|
@ -28,19 +27,6 @@ export default defineEventHandler(async (event) => {
|
||||||
}
|
}
|
||||||
await writeAccounts(accounts);
|
await writeAccounts(accounts);
|
||||||
|
|
||||||
const counts = new Map();
|
// Update Schedule counts.
|
||||||
for (const account of accounts)
|
await updateScheduleInterestedCounts(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);
|
|
||||||
})
|
})
|
||||||
|
|
21
server/utils/schedule.ts
Normal file
21
server/utils/schedule.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import { Account } from '~/shared/types/account';
|
||||||
|
import { readSchedule, writeSchedule } from '~/server/database';
|
||||||
|
import { broadcastUpdate } from '~/server/streams';
|
||||||
|
|
||||||
|
export async function updateScheduleInterestedCounts(accounts: Account[]) {
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue