Track which account is interested in which events
Store a list of ids of events and slots that accounts have marked as being interested in, and show aggeregate counts in the schedule.
This commit is contained in:
parent
ca51c07065
commit
db9a12250e
5 changed files with 137 additions and 0 deletions
46
server/api/account.patch.ts
Normal file
46
server/api/account.patch.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { Account } from "~/shared/types/account";
|
||||
import { readAccounts, readSchedule, writeAccounts, writeSchedule } from "~/server/database";
|
||||
import { broadcastUpdate } from "~/server/streams";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const session = await requireAccountSession(event);
|
||||
const body: Pick<Account, "interestedIds"> = await readBody(event);
|
||||
if (
|
||||
!(body.interestedIds instanceof Array)
|
||||
|| !body.interestedIds.every(id => typeof id === "string")
|
||||
) {
|
||||
throw createError({
|
||||
status: 400,
|
||||
message: "Invalid interestedIds",
|
||||
});
|
||||
}
|
||||
|
||||
const accounts = await readAccounts();
|
||||
const sessionAccount = accounts.find(account => account.id === session.accountId);
|
||||
if (!sessionAccount) {
|
||||
throw Error("Account does not exist");
|
||||
}
|
||||
|
||||
if (body.interestedIds.length) {
|
||||
sessionAccount.interestedIds = body.interestedIds;
|
||||
} else {
|
||||
delete sessionAccount.interestedIds;
|
||||
}
|
||||
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);
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue