Refactor base types for entities and tombstones

Rename the base Entity type to ApiEntity, and the base EntityToombstone
to ApiTombstone to better reflect the reality that its only used in the
API interface and that the client and server types uses its own base if
any.

Remove EntityLiving and pull EntityTombstone out of of the base entity
type so that the types based on ApiEntity are always living entities and
if it's possible for it to contain tombstone this will be explicitly
told with the type including a union with ApiTombstone.

Refactor the types of the ClientEntity and ClientMap to better reflect
the types of the entities it stores and converts to/from.
This commit is contained in:
Hornwitser 2025-06-24 15:19:11 +02:00
parent e3ff872b5c
commit 985b8e0950
13 changed files with 107 additions and 102 deletions

View file

@ -1,23 +1,25 @@
import { DateTime, FixedOffsetZone, Zone } from "~/shared/utils/luxon";
import type {
ApiEntity,
ApiSchedule,
ApiScheduleEvent,
ApiScheduleEventSlot,
ApiScheduleLocation,
ApiScheduleRole,
ApiScheduleShift,
ApiScheduleShiftSlot
ApiScheduleShiftSlot,
ApiTombstone
} from "~/shared/types/api";
import type { Entity, Id, Living, Tombstone } from "~/shared/types/common";
import { mapEquals, setEquals } from "~/shared/utils/functions";
import type { Id } from "~/shared/types/common";
import { setEquals } from "~/shared/utils/functions";
import { ClientEntity } from "~/utils/client-entity";
function filterAlive<T extends Entity>(entities?: T[]) {
return (entities ?? []).filter((entity) => !entity.deleted) as Living<T>[];
function filterEntity<T extends ApiEntity>(entities?: (T | ApiTombstone)[]) {
return (entities ?? []).filter((entity) => !entity.deleted) as T[];
}
function filterTombstone<T extends Entity>(entities?: T[]) {
return (entities ?? []).filter((entity) => entity.deleted) as Tombstone<T>[];
function filterTombstone<T extends ApiEntity>(entities?: (T | ApiTombstone)[]) {
return (entities ?? []).filter((entity) => entity.deleted) as ApiTombstone[];
}
export function toIso(timestamp: DateTime) {
@ -37,7 +39,7 @@ function mapWithout<K, V>(map: Map<K, V>, key: K) {
}
export class ClientScheduleLocation extends ClientEntity {
export class ClientScheduleLocation extends ClientEntity<ApiScheduleLocation> {
serverName: string;
serverDescription: string;
@ -86,7 +88,7 @@ export class ClientScheduleLocation extends ClientEntity {
);
}
static fromApi(api: Living<ApiScheduleLocation>, opts: { zone: Zone, locale: string }) {
static fromApi(api: ApiScheduleLocation, opts: { zone: Zone, locale: string }) {
return new this(
api.id,
DateTime.fromISO(api.updatedAt, opts),
@ -96,7 +98,7 @@ export class ClientScheduleLocation extends ClientEntity {
);
}
override apiUpdate(api: Living<ApiScheduleLocation>, opts: { zone: Zone, locale: string }) {
override apiUpdate(api: ApiScheduleLocation, opts: { zone: Zone, locale: string }) {
const wasModified = this.isModified();
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
this.serverDeleted = false;
@ -107,7 +109,7 @@ export class ClientScheduleLocation extends ClientEntity {
}
}
toApi(): ApiScheduleLocation {
toApi(): ApiScheduleLocation | ApiTombstone {
if (this.deleted) {
return {
id: this.id,
@ -124,7 +126,7 @@ export class ClientScheduleLocation extends ClientEntity {
}
}
export class ClientScheduleEvent extends ClientEntity {
export class ClientScheduleEvent extends ClientEntity<ApiScheduleEvent> {
schedule!: ClientSchedule;
serverName: string;
serverCrew: boolean;
@ -237,7 +239,7 @@ export class ClientScheduleEvent extends ClientEntity {
}
static fromApi(
api: Living<ApiScheduleEvent>,
api: ApiScheduleEvent,
opts: { zone: Zone, locale: string },
) {
return new this(
@ -255,7 +257,7 @@ export class ClientScheduleEvent extends ClientEntity {
}
override apiUpdate(
api: Living<ApiScheduleEvent>,
api: ApiScheduleEvent,
opts: { zone: Zone, locale: string },
) {
const wasModified = this.isModified();
@ -273,7 +275,7 @@ export class ClientScheduleEvent extends ClientEntity {
}
}
toApi(): ApiScheduleEvent {
toApi(): ApiScheduleEvent | ApiTombstone {
if (this.deleted) {
return {
id: this.id,
@ -437,7 +439,7 @@ export class ClientScheduleEventSlot {
}
}
export class ClientScheduleRole extends ClientEntity {
export class ClientScheduleRole extends ClientEntity<ApiScheduleRole> {
serverName: string;
serverDescription: string;
@ -486,7 +488,7 @@ export class ClientScheduleRole extends ClientEntity {
);
}
static fromApi(api: Living<ApiScheduleRole>, opts: { zone: Zone, locale: string }) {
static fromApi(api: ApiScheduleRole, opts: { zone: Zone, locale: string }) {
return new this(
api.id,
DateTime.fromISO(api.updatedAt, opts),
@ -496,7 +498,7 @@ export class ClientScheduleRole extends ClientEntity {
);
}
override apiUpdate(api: Living<ApiScheduleRole>, opts: { zone: Zone, locale: string }) {
override apiUpdate(api: ApiScheduleRole, opts: { zone: Zone, locale: string }) {
const wasModified = this.isModified();
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
this.serverDeleted = false;
@ -507,7 +509,7 @@ export class ClientScheduleRole extends ClientEntity {
}
}
toApi(): ApiScheduleRole {
toApi(): ApiScheduleRole | ApiTombstone {
if (this.deleted) {
return {
id: this.id,
@ -524,7 +526,7 @@ export class ClientScheduleRole extends ClientEntity {
}
}
export class ClientScheduleShift extends ClientEntity {
export class ClientScheduleShift extends ClientEntity<ApiScheduleShift> {
schedule!: ClientSchedule;
serverRoleId: Id;
serverName: string;
@ -616,7 +618,7 @@ export class ClientScheduleShift extends ClientEntity {
}
static fromApi(
api: Living<ApiScheduleShift>,
api: ApiScheduleShift,
opts: { zone: Zone, locale: string },
) {
return new this(
@ -631,7 +633,7 @@ export class ClientScheduleShift extends ClientEntity {
}
override apiUpdate(
api: Living<ApiScheduleShift>,
api: ApiScheduleShift,
opts: { zone: Zone, locale: string },
) {
const wasModified = this.isModified();
@ -646,7 +648,7 @@ export class ClientScheduleShift extends ClientEntity {
}
}
toApi(): ApiScheduleShift {
toApi(): ApiScheduleShift | ApiTombstone {
if (this.deleted) {
return {
id: this.id,
@ -787,7 +789,7 @@ export class ClientScheduleShiftSlot {
}
}
export class ClientSchedule extends ClientEntity {
export class ClientSchedule extends ClientEntity<ApiSchedule> {
nextClientId = -1;
constructor(
@ -854,7 +856,7 @@ export class ClientSchedule extends ClientEntity {
}
}
static fromApi(api: Living<ApiSchedule>, opts: { zone: Zone, locale: string }) {
static fromApi(api: ApiSchedule, opts: { zone: Zone, locale: string }) {
const eventSlots = idMap((api.events ?? [])
.filter(event => !event.deleted)
.flatMap(event => event.slots.map(slot => ClientScheduleEventSlot.fromApi(slot, event.id, opts)))
@ -890,7 +892,7 @@ export class ClientSchedule extends ClientEntity {
return schedule;
}
toApi(diff = false): ApiSchedule {
toApi(diff = false): ApiSchedule | ApiTombstone {
if (this.deleted) {
return {
id: this.id,
@ -908,7 +910,7 @@ export class ClientSchedule extends ClientEntity {
}
}
override apiUpdate(update: Living<ApiSchedule>, opts: { zone: Zone, locale: string }) {
override apiUpdate(update: ApiSchedule, opts: { zone: Zone, locale: string }) {
if (update.deleted)
throw new Error("ClientSchedule.apiUpdate: Unexpected deletion");
if (update.id !== this.id)