Refactor API types and sync logic
Rename and refactor the types passed over the API to be based on an entity that's either living or a tombstone. A living entity has a deleted property that's either undefined or false, while a tombstone has a deleted property set to true. All entities have a numeric id and an updatedAt timestamp. To sync entities, an array of replacements are passed around. Living entities are replaced with tombstones when they're deleted. And tombstones are replaced with living entities when restored.
This commit is contained in:
parent
251e83f640
commit
fe06d0d6bd
36 changed files with 1242 additions and 834 deletions
|
@ -1,24 +1,61 @@
|
|||
import { Account } from '~/shared/types/account';
|
||||
import { Schedule } from '~/shared/types/schedule';
|
||||
import { readSchedule, writeSchedule } from '~/server/database';
|
||||
import { broadcastUpdate } from '~/server/streams';
|
||||
import { broadcastEvent } from '~/server/streams';
|
||||
import type { ApiAccount, ApiSchedule } from '~/shared/types/api';
|
||||
|
||||
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);
|
||||
export async function updateScheduleInterestedCounts(accounts: ApiAccount[]) {
|
||||
const eventCounts = new Map<number, number>();
|
||||
const eventSlotCounts = new Map<number, number>();
|
||||
for (const account of accounts) {
|
||||
if (account.interestedEventIds)
|
||||
for (const id of account.interestedEventIds)
|
||||
eventCounts.set(id, (eventCounts.get(id) ?? 0) + 1);
|
||||
if (account.interestedEventSlotIds)
|
||||
for (const id of account.interestedEventSlotIds)
|
||||
eventSlotCounts.set(id, (eventSlotCounts.get(id) ?? 0) + 1);
|
||||
}
|
||||
|
||||
const schedule = await readSchedule();
|
||||
for (const event of schedule.events) {
|
||||
event.interested = counts.get(event.id);
|
||||
if (schedule.deleted) {
|
||||
throw new Error("Deleted schedule not implemented");
|
||||
}
|
||||
const update: ApiSchedule = {
|
||||
id: schedule.id,
|
||||
updatedAt: new Date().toISOString(),
|
||||
events: [],
|
||||
};
|
||||
const updatedFrom = schedule.updatedAt;
|
||||
for (const event of schedule.events ?? []) {
|
||||
let modified = false;
|
||||
if (event.deleted)
|
||||
continue;
|
||||
|
||||
let count = eventCounts.get(event.id);
|
||||
if (count !== event.interested) {
|
||||
event.interested = eventCounts.get(event.id);
|
||||
modified = true;
|
||||
}
|
||||
for (const slot of event.slots) {
|
||||
slot.interested = counts.get(slot.id);
|
||||
let slotCount = eventSlotCounts.get(slot.id);
|
||||
if (slotCount !== slot.interested) {
|
||||
slot.interested = slotCount;
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (modified) {
|
||||
event.updatedAt = update.updatedAt;
|
||||
update.events!.push(event);
|
||||
}
|
||||
}
|
||||
if (!update.events!.length) {
|
||||
return; // No changes
|
||||
}
|
||||
schedule.updatedAt = updatedFrom;
|
||||
await writeSchedule(schedule);
|
||||
await broadcastUpdate(schedule);
|
||||
await broadcastEvent({
|
||||
type: "schedule-update",
|
||||
updatedFrom,
|
||||
data: update,
|
||||
});
|
||||
}
|
||||
|
||||
export function canSeeCrew(accountType: string | undefined) {
|
||||
|
@ -26,17 +63,28 @@ export function canSeeCrew(accountType: string | undefined) {
|
|||
}
|
||||
|
||||
/** Filters out crew visible only parts of schedule */
|
||||
export function filterSchedule(schedule: Schedule): Schedule {
|
||||
export function filterSchedule(schedule: ApiSchedule): ApiSchedule {
|
||||
if (schedule.deleted) {
|
||||
return schedule;
|
||||
}
|
||||
return {
|
||||
id: schedule.id,
|
||||
updatedAt: schedule.updatedAt,
|
||||
locations: schedule.locations,
|
||||
events: schedule.events
|
||||
.filter(event => !event.crew)
|
||||
.map(event => ({
|
||||
...event,
|
||||
slots: event.slots.map(slot => ({
|
||||
...slot,
|
||||
assigned: undefined,
|
||||
})),
|
||||
events: (schedule.events ?? [])
|
||||
.map(event => (
|
||||
event.deleted
|
||||
? event
|
||||
: event.crew
|
||||
// Pretend crew events are deleted.
|
||||
? { id: event.id, deleted: true, updatedAt: event.updatedAt }
|
||||
: {
|
||||
...event,
|
||||
slots: event.slots.map(slot => ({
|
||||
...slot,
|
||||
assigned: undefined,
|
||||
}
|
||||
)),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import type { H3Event } from "h3";
|
||||
import { nextSessionId, readSessions, readSubscriptions, ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
|
||||
import { nextSessionId, readSessions, readSubscriptions, type ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
|
||||
|
||||
const oneYearSeconds = 365 * 24 * 60 * 60;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue