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
25
shared/types/account.d.ts
vendored
25
shared/types/account.d.ts
vendored
|
@ -1,25 +0,0 @@
|
|||
export interface Account {
|
||||
id: number,
|
||||
type: "anonymous" | "regular" | "crew" | "admin",
|
||||
/** Name of the account. Not present on anonymous accounts */
|
||||
name?: string,
|
||||
interestedIds?: string[],
|
||||
timezone?: string,
|
||||
}
|
||||
|
||||
export interface Subscription {
|
||||
type: "push",
|
||||
sessionId: number,
|
||||
push: PushSubscriptionJSON,
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
id: number,
|
||||
accountId: number,
|
||||
}
|
||||
|
||||
export interface AccountSession {
|
||||
id: number,
|
||||
account: Account,
|
||||
push: boolean,
|
||||
}
|
111
shared/types/api.ts
Normal file
111
shared/types/api.ts
Normal file
|
@ -0,0 +1,111 @@
|
|||
import { z } from "zod/v4-mini";
|
||||
import { defineEntity, idSchema, type Id } from "~/shared/types/common";
|
||||
|
||||
export interface ApiAccount {
|
||||
id: Id,
|
||||
type: "anonymous" | "regular" | "crew" | "admin",
|
||||
/** Name of the account. Not present on anonymous accounts */
|
||||
name?: string,
|
||||
interestedEventIds?: number[],
|
||||
interestedEventSlotIds?: number[],
|
||||
timezone?: string,
|
||||
}
|
||||
|
||||
export const apiAccountPatchSchema = z.object({
|
||||
name: z.optional(z.string()),
|
||||
interestedEventIds: z.optional(z.array(z.number())),
|
||||
interestedEventSlotIds: z.optional(z.array(z.number())),
|
||||
timezone: z.optional(z.string()),
|
||||
});
|
||||
export type ApiAccountPatch = z.infer<typeof apiAccountPatchSchema>;
|
||||
|
||||
export const apiSubscriptionSchema = z.object({
|
||||
type: z.literal("push"),
|
||||
sessionId: z.number(),
|
||||
push: z.object({
|
||||
endpoint: z.optional(z.string()),
|
||||
expirationTime: z.nullish(z.number()),
|
||||
keys: z.record(z.string(), z.string()),
|
||||
}),
|
||||
});
|
||||
export type ApiSubscription = z.infer<typeof apiSubscriptionSchema>;
|
||||
|
||||
export interface ApiSession {
|
||||
id: Id,
|
||||
account?: ApiAccount,
|
||||
push: boolean,
|
||||
}
|
||||
|
||||
export const apiScheduleLocationSchema = defineEntity({
|
||||
name: z.string(),
|
||||
description: z.optional(z.string()),
|
||||
});
|
||||
export type ApiScheduleLocation = z.infer<typeof apiScheduleLocationSchema>;
|
||||
|
||||
export const apiScheduleEventSlotSchema = z.object({
|
||||
id: idSchema,
|
||||
start: z.string(),
|
||||
end: z.string(),
|
||||
locationIds: z.array(idSchema),
|
||||
assigned: z.optional(z.array(z.number())),
|
||||
interested: z.optional(z.number()),
|
||||
});
|
||||
export type ApiScheduleEventSlot = z.infer<typeof apiScheduleEventSlotSchema>;
|
||||
|
||||
export const apiScheduleEventSchema = defineEntity({
|
||||
name: z.string(),
|
||||
crew: z.optional(z.boolean()),
|
||||
host: z.optional(z.string()),
|
||||
cancelled: z.optional(z.boolean()),
|
||||
description: z.optional(z.string()),
|
||||
interested: z.optional(z.number()),
|
||||
slots: z.array(apiScheduleEventSlotSchema),
|
||||
});
|
||||
export type ApiScheduleEvent = z.infer<typeof apiScheduleEventSchema>;
|
||||
|
||||
export const apiScheduleRoleSchema = defineEntity({
|
||||
name: z.string(),
|
||||
description: z.optional(z.string()),
|
||||
});
|
||||
export type ApiScheduleRole = z.infer<typeof apiScheduleRoleSchema>;
|
||||
|
||||
export const apiScheduleShiftSlotSchema = z.object({
|
||||
id: idSchema,
|
||||
start: z.string(),
|
||||
end: z.string(),
|
||||
assigned: z.optional(z.array(z.number())),
|
||||
});
|
||||
export type ApiScheduleShiftSlot = z.infer<typeof apiScheduleShiftSlotSchema>;
|
||||
|
||||
export const apiScheduleShiftSchema = defineEntity({
|
||||
roleId: idSchema,
|
||||
name: z.string(),
|
||||
description: z.optional(z.string()),
|
||||
slots: z.array(apiScheduleShiftSlotSchema),
|
||||
});
|
||||
export type ApiScheduleShift = z.infer<typeof apiScheduleShiftSchema>;
|
||||
|
||||
export const apiScheduleSchema = defineEntity({
|
||||
id: z.literal(111),
|
||||
locations: z.optional(z.array(apiScheduleLocationSchema)),
|
||||
events: z.optional(z.array(apiScheduleEventSchema)),
|
||||
roles: z.optional(z.array(apiScheduleRoleSchema)),
|
||||
shifts: z.optional(z.array(apiScheduleShiftSchema)),
|
||||
});
|
||||
export type ApiSchedule = z.infer<typeof apiScheduleSchema>;
|
||||
|
||||
export interface ApiAccountUpdate {
|
||||
type: "account-update",
|
||||
data: ApiAccount,
|
||||
}
|
||||
|
||||
export interface ApiScheduleUpdate {
|
||||
type: "schedule-update",
|
||||
updatedFrom?: string,
|
||||
data: ApiSchedule,
|
||||
}
|
||||
|
||||
export type ApiEvent =
|
||||
| ApiAccountUpdate
|
||||
| ApiScheduleUpdate
|
||||
;
|
25
shared/types/common.ts
Normal file
25
shared/types/common.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { z } from "zod/v4-mini";
|
||||
|
||||
export const idSchema = z.number();
|
||||
export type Id = z.infer<typeof idSchema>;
|
||||
|
||||
export const entityLivingSchema = z.object({
|
||||
id: idSchema,
|
||||
updatedAt: z.string(),
|
||||
deleted: z.optional(z.literal(false)),
|
||||
});
|
||||
export type EnityLiving = z.infer<typeof entityLivingSchema>;
|
||||
|
||||
export const entityToombstoneSchema = z.object({
|
||||
id: idSchema,
|
||||
updatedAt: z.string(),
|
||||
deleted: z.literal(true),
|
||||
});
|
||||
export type EntityToombstone = z.infer<typeof entityToombstoneSchema>;
|
||||
|
||||
export const entitySchema = z.discriminatedUnion("deleted", [entityLivingSchema, entityToombstoneSchema]);
|
||||
export type Entity = z.infer<typeof entitySchema>;
|
||||
|
||||
export function defineEntity<T extends {}>(fields: T) {
|
||||
return z.discriminatedUnion("deleted", [z.extend(entityLivingSchema, fields), entityToombstoneSchema]);
|
||||
}
|
65
shared/types/schedule.d.ts
vendored
65
shared/types/schedule.d.ts
vendored
|
@ -1,65 +0,0 @@
|
|||
export interface ScheduleEvent {
|
||||
name: string,
|
||||
id: string,
|
||||
crew?: boolean,
|
||||
host?: string,
|
||||
cancelled?: boolean,
|
||||
description?: string,
|
||||
interested?: number,
|
||||
slots: TimeSlot[],
|
||||
}
|
||||
|
||||
export interface ScheduleLocation {
|
||||
name: string,
|
||||
id: string,
|
||||
description?: string,
|
||||
}
|
||||
|
||||
export interface TimeSlot {
|
||||
id: string,
|
||||
start: string,
|
||||
end: string,
|
||||
locations: string[],
|
||||
assigned?: number[],
|
||||
interested?: number,
|
||||
}
|
||||
|
||||
export interface Shift {
|
||||
name: string,
|
||||
id: string,
|
||||
role: string,
|
||||
description?: string,
|
||||
slots: ShiftSlot[],
|
||||
}
|
||||
|
||||
export interface Role {
|
||||
name: string,
|
||||
id: string,
|
||||
description?: string,
|
||||
}
|
||||
|
||||
export interface ShiftSlot {
|
||||
id: string,
|
||||
start: string,
|
||||
end: string,
|
||||
assigned?: number[],
|
||||
}
|
||||
|
||||
export interface Schedule {
|
||||
locations: ScheduleLocation[],
|
||||
events: ScheduleEvent[],
|
||||
roles?: Role[],
|
||||
rota?: Shift[],
|
||||
}
|
||||
|
||||
export type ChangeRecord<T extends { id: string }> =
|
||||
| { op: "set", data: T }
|
||||
| { op: "del", data: { id: string }}
|
||||
;
|
||||
|
||||
export interface SchedulePatch {
|
||||
locations?: ChangeRecord<ScheduleLocation>[],
|
||||
events?: ChangeRecord<ScheduleEvent>[],
|
||||
roles?: ChangeRecord<Role>[],
|
||||
rota?: ChangeRecord<Shift>[],
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue