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,5 +1,10 @@
|
|||
<template>
|
||||
<figure class="timetable">
|
||||
<figure class="timetable" v-if="schedule.deleted">
|
||||
<p>
|
||||
Error: Schedule is deleted.
|
||||
</p>
|
||||
</figure>
|
||||
<figure class="timetable" v-else>
|
||||
<details>
|
||||
<summary>Debug</summary>
|
||||
<details>
|
||||
|
@ -56,7 +61,7 @@
|
|||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template v-for="location in schedule.locations" :key="location.id">
|
||||
<template v-for="location in schedule.locations?.filter(l => !l.deleted)" :key="location.id">
|
||||
<tr v-if="locationRows.has(location.id)">
|
||||
<th>{{ location.name }}</th>
|
||||
<td
|
||||
|
@ -75,7 +80,7 @@
|
|||
<th>Shifts</th>
|
||||
<td :colSpan="totalColumns"></td>
|
||||
</tr>
|
||||
<template v-for="role in schedule.roles" :key="role.id">
|
||||
<template v-for="role in schedule.roles?.filter(r => !r.deleted)" :key="role.id">
|
||||
<tr v-if="roleRows.has(role.id)">
|
||||
<th>{{ role.name }}</th>
|
||||
<td
|
||||
|
@ -97,7 +102,8 @@
|
|||
|
||||
<script setup lang="ts">
|
||||
import { DateTime } from "luxon";
|
||||
import type { Role, Schedule, ScheduleEvent, ScheduleLocation, Shift, ShiftSlot, TimeSlot } from "~/shared/types/schedule";
|
||||
import type { ApiSchedule, ApiScheduleEvent, ApiScheduleEventSlot, ApiScheduleLocation, ApiScheduleRole, ApiScheduleShift, ApiScheduleShiftSlot } from "~/shared/types/api";
|
||||
import type { Id } from "~/shared/types/common";
|
||||
import { pairs, setEquals } from "~/shared/utils/functions";
|
||||
|
||||
const oneHourMs = 60 * 60 * 1000;
|
||||
|
@ -107,8 +113,8 @@ const oneMinMs = 60 * 1000;
|
|||
|
||||
/** Point in time where a time slots starts or ends. */
|
||||
type Edge =
|
||||
| { type: "start" | "end", source: "event", slot: TimeSlot }
|
||||
| { type: "start" | "end", source: "shift", role: string, slot: ShiftSlot }
|
||||
| { type: "start" | "end", source: "event", slot: ApiScheduleEventSlot }
|
||||
| { type: "start" | "end", source: "shift", roleId: Id, slot: ApiScheduleShiftSlot }
|
||||
;
|
||||
|
||||
/** Point in time where multiple edges meet. */
|
||||
|
@ -118,8 +124,8 @@ type Junction = { ts: number, edges: Edge[] };
|
|||
type Span = {
|
||||
start: Junction;
|
||||
end: Junction,
|
||||
locations: Map<string, Set<TimeSlot>>,
|
||||
roles: Map<string, Set<ShiftSlot>>,
|
||||
locations: Map<number, Set<ApiScheduleEventSlot>>,
|
||||
roles: Map<number, Set<ApiScheduleShiftSlot>>,
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -133,7 +139,10 @@ type Stretch = {
|
|||
spans: Span[];
|
||||
}
|
||||
|
||||
function* edgesFromEvents(events: Iterable<ScheduleEvent>, filter = (slot: TimeSlot) => true): Generator<Edge> {
|
||||
function* edgesFromEvents(
|
||||
events: Iterable<Extract<ApiScheduleEvent, { deleted?: false }>>,
|
||||
filter = (slot: ApiScheduleEventSlot) => true,
|
||||
): Generator<Edge> {
|
||||
for (const event of events) {
|
||||
for (const slot of event.slots.filter(filter)) {
|
||||
if (slot.start > slot.end) {
|
||||
|
@ -145,14 +154,17 @@ function* edgesFromEvents(events: Iterable<ScheduleEvent>, filter = (slot: TimeS
|
|||
}
|
||||
}
|
||||
|
||||
function* edgesFromShifts(shifts: Iterable<Shift>, filter = (slot: ShiftSlot) => true): Generator<Edge> {
|
||||
function* edgesFromShifts(
|
||||
shifts: Iterable<Extract<ApiScheduleShift, { deleted?: false }>>,
|
||||
filter = (slot: ApiScheduleShiftSlot) => true,
|
||||
): Generator<Edge> {
|
||||
for (const shift of shifts) {
|
||||
for (const slot of shift.slots.filter(filter)) {
|
||||
if (slot.start > slot.end) {
|
||||
throw new Error(`Slot ${slot.id} ends before it starts.`);
|
||||
}
|
||||
yield { type: "start", source: "shift", role: shift.role, slot };
|
||||
yield { type: "end", source: "shift", role: shift.role, slot };
|
||||
yield { type: "start", source: "shift", roleId: shift.roleId, slot };
|
||||
yield { type: "end", source: "shift", roleId: shift.roleId, slot };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -173,23 +185,25 @@ function junctionsFromEdges(edges: Iterable<Edge>) {
|
|||
}
|
||||
|
||||
function* spansFromJunctions(
|
||||
junctions: Iterable<Junction>, locations: ScheduleLocation[], roles: Role[] | undefined,
|
||||
junctions: Iterable<Junction>,
|
||||
locations: Extract<ApiScheduleLocation, { deleted?: false }>[],
|
||||
roles: Extract<ApiScheduleRole, { deleted?: false }>[],
|
||||
): Generator<Span> {
|
||||
const activeLocations = new Map(
|
||||
locations.map(location => [location.id, new Set<TimeSlot>()])
|
||||
locations.map(location => [location.id, new Set<ApiScheduleEventSlot>()])
|
||||
);
|
||||
const activeRoles = new Map(
|
||||
roles?.map(role => [role.id, new Set<ShiftSlot>()])
|
||||
roles?.map(role => [role.id, new Set<ApiScheduleShiftSlot>()])
|
||||
);
|
||||
for (const [start, end] of pairs(junctions)) {
|
||||
for (const edge of start.edges) {
|
||||
if (edge.type === "start") {
|
||||
if (edge.source === "event") {
|
||||
for (const location of edge.slot.locations) {
|
||||
activeLocations.get(location)?.add(edge.slot)
|
||||
for (const id of edge.slot.locationIds) {
|
||||
activeLocations.get(id)?.add(edge.slot)
|
||||
}
|
||||
} else if (edge.source === "shift") {
|
||||
activeRoles.get(edge.role)?.add(edge.slot)
|
||||
activeRoles.get(edge.roleId)?.add(edge.slot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,11 +224,11 @@ function* spansFromJunctions(
|
|||
for (const edge of end.edges) {
|
||||
if (edge.type === "end") {
|
||||
if (edge.source === "event") {
|
||||
for (const location of edge.slot.locations) {
|
||||
activeLocations.get(location)?.delete(edge.slot)
|
||||
for (const id of edge.slot.locationIds) {
|
||||
activeLocations.get(id)?.delete(edge.slot)
|
||||
}
|
||||
} else if (edge.source === "shift") {
|
||||
activeRoles.get(edge.role)?.delete(edge.slot);
|
||||
activeRoles.get(edge.roleId)?.delete(edge.slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -325,24 +339,24 @@ function padStretch(stretch: Stretch, timezone: string): Stretch {
|
|||
|
||||
function tableElementsFromStretches(
|
||||
stretches: Iterable<Stretch>,
|
||||
events: ScheduleEvent[],
|
||||
locations: ScheduleLocation[],
|
||||
rota: Shift[] | undefined,
|
||||
roles: Role[] | undefined,
|
||||
events: Extract<ApiScheduleEvent, { deleted?: false }>[],
|
||||
locations: Extract<ApiScheduleLocation, { deleted?: false }>[],
|
||||
shifts: Extract<ApiScheduleShift, { deleted?: false }>[],
|
||||
roles: Extract<ApiScheduleRole, { deleted?: false }>[],
|
||||
timezone: string,
|
||||
) {
|
||||
type Col = { minutes?: number };
|
||||
type DayHead = { span: number, content?: string }
|
||||
type HourHead = { span: number, content?: string }
|
||||
type LocationCell = { span: number, slots: Set<TimeSlot>, title: string, crew?: boolean }
|
||||
type RoleCell = { span: number, slots: Set<ShiftSlot>, title: string };
|
||||
type LocationCell = { span: number, slots: Set<ApiScheduleEventSlot>, title: string, crew?: boolean }
|
||||
type RoleCell = { span: number, slots: Set<ApiScheduleShiftSlot>, title: string };
|
||||
const columnGroups: { className?: string, cols: Col[] }[] = [];
|
||||
const dayHeaders: DayHead[] = [];
|
||||
const hourHeaders: HourHead[]= [];
|
||||
const locationRows = new Map<string, LocationCell[]>(locations.map(location => [location.id, []]));
|
||||
const roleRows = new Map<string, RoleCell[]>(roles?.map?.(role => [role.id, []]));
|
||||
const locationRows = new Map<number, LocationCell[]>(locations.map(location => [location.id, []]));
|
||||
const roleRows = new Map<number, RoleCell[]>(roles.map(role => [role.id, []]));
|
||||
const eventBySlotId = new Map(events.flatMap(event => event.slots.map(slot => [slot.id, event])));
|
||||
const shiftBySlotId = new Map(rota?.flatMap?.(shift => shift.slots.map(slot =>[slot.id, shift])))
|
||||
const shiftBySlotId = new Map(shifts?.flatMap?.(shift => shift.slots.map(slot =>[slot.id, shift])))
|
||||
let totalColumns = 0;
|
||||
|
||||
function startColumnGroup(className?: string) {
|
||||
|
@ -354,7 +368,7 @@ function tableElementsFromStretches(
|
|||
function startHour(content?: string) {
|
||||
hourHeaders.push({ span: 0, content })
|
||||
}
|
||||
function startLocation(id: string, slots = new Set<TimeSlot>()) {
|
||||
function startLocation(id: number, slots = new Set<ApiScheduleEventSlot>()) {
|
||||
const rows = locationRows.get(id)!;
|
||||
if (rows.length) {
|
||||
const row = rows[rows.length - 1];
|
||||
|
@ -363,7 +377,7 @@ function tableElementsFromStretches(
|
|||
}
|
||||
rows.push({ span: 0, slots, title: "" });
|
||||
}
|
||||
function startRole(id: string, slots = new Set<ShiftSlot>()) {
|
||||
function startRole(id: number, slots = new Set<ApiScheduleShiftSlot>()) {
|
||||
const rows = roleRows.get(id)!;
|
||||
if (rows.length) {
|
||||
const row = rows[rows.length - 1];
|
||||
|
@ -487,21 +501,35 @@ function tableElementsFromStretches(
|
|||
}
|
||||
|
||||
const props = defineProps<{
|
||||
schedule: Schedule,
|
||||
eventSlotFilter?: (slot: TimeSlot) => boolean,
|
||||
shiftSlotFilter?: (slot: ShiftSlot) => boolean,
|
||||
schedule: ApiSchedule,
|
||||
eventSlotFilter?: (slot: ApiScheduleEventSlot) => boolean,
|
||||
shiftSlotFilter?: (slot: ApiScheduleShiftSlot) => boolean,
|
||||
}>();
|
||||
const schedule = computed(() => props.schedule);
|
||||
const junctions = computed(() => junctionsFromEdges([
|
||||
...edgesFromEvents(schedule.value.events, props.eventSlotFilter),
|
||||
...edgesFromShifts(schedule.value.rota ?? [], props.shiftSlotFilter),
|
||||
]));
|
||||
const stretches = computed(() => [
|
||||
...stretchesFromSpans(
|
||||
spansFromJunctions(junctions.value, schedule.value.locations, schedule.value.roles),
|
||||
oneHourMs * 5
|
||||
)
|
||||
])
|
||||
const junctions = computed(() => {
|
||||
if (schedule.value.deleted) {
|
||||
throw Error("Unhandled deleted schedule");
|
||||
}
|
||||
return junctionsFromEdges([
|
||||
...edgesFromEvents(schedule.value.events?.filter(e => !e.deleted) ?? [], props.eventSlotFilter),
|
||||
...edgesFromShifts(schedule.value.shifts?.filter(s => !s.deleted) ?? [], props.shiftSlotFilter),
|
||||
])
|
||||
});
|
||||
const stretches = computed(() => {
|
||||
if (schedule.value.deleted) {
|
||||
throw Error("Unhandled deleted schedule");
|
||||
}
|
||||
return [
|
||||
...stretchesFromSpans(
|
||||
spansFromJunctions(
|
||||
junctions.value,
|
||||
schedule.value.locations?.filter(l => !l.deleted) ?? [],
|
||||
schedule.value.roles?.filter(r => !r.deleted) ?? [],
|
||||
),
|
||||
oneHourMs * 5
|
||||
)
|
||||
]
|
||||
})
|
||||
|
||||
const accountStore = useAccountStore();
|
||||
const timezone = computed({
|
||||
|
@ -509,9 +537,19 @@ const timezone = computed({
|
|||
set: (value: string) => { accountStore.timezone = value },
|
||||
});
|
||||
|
||||
const elements = computed(() => tableElementsFromStretches(
|
||||
stretches.value, schedule.value.events, schedule.value.locations, schedule.value.rota, schedule.value.roles, accountStore.activeTimezone
|
||||
));
|
||||
const elements = computed(() => {
|
||||
if (schedule.value.deleted) {
|
||||
throw Error("Unhandled deleted schedule");
|
||||
}
|
||||
return tableElementsFromStretches(
|
||||
stretches.value,
|
||||
schedule.value.events?.filter(e => !e.deleted) ?? [],
|
||||
schedule.value.locations?.filter(l => !l.deleted) ?? [],
|
||||
schedule.value.shifts?.filter(s => !s.deleted) ?? [],
|
||||
schedule.value.roles?.filter(r => !r.deleted) ?? [],
|
||||
accountStore.activeTimezone
|
||||
);
|
||||
});
|
||||
const totalColumns = computed(() => elements.value.totalColumns);
|
||||
const columnGroups = computed(() => elements.value.columnGroups);
|
||||
const dayHeaders = computed(() => elements.value.dayHeaders);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue