feat(server): type RealtimeService against the event registry

broadcast/broadcastToUser gain registry-typed overloads (event name
must be a TREK_WS_EVENTS key, payload must match its schema) with the
plugin: namespace as the deliberate escape hatch; the 13 service
wrappers forward the generic signature. Compile-time only — the
runtime pass-through and all call sites are unchanged.
This commit is contained in:
jubnl
2026-07-29 00:16:08 +02:00
parent c0baaeb898
commit f66f8d8bd9
18 changed files with 97 additions and 25 deletions
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import { readEnv } from '../../../app-config';
import { RealtimeService } from '../../realtime/realtime.service';
import { isUpdateConflict } from '../../../services/conflictResult';
@@ -72,7 +73,7 @@ function packingViewersOf(item: PackingPrivacy | null | undefined): number[] | n
}
/** CREATE/DELETE fan-out: whole room for a Common item, else owner + recipients only. */
function emitPackingToViewers(realtime: RealtimeService, tripId: number, event: string, payload: Record<string, unknown>, item: PackingPrivacy): void {
function emitPackingToViewers<E extends TrekWsTripEventName>(realtime: RealtimeService, tripId: number, event: E, payload: TrekWsPayload<E>, item: PackingPrivacy): void {
const viewers = packingViewersOf(item);
if (viewers === null) {
realtime.broadcast(tripId, event, payload, undefined);
@@ -82,7 +83,7 @@ function emitPackingToViewers(realtime: RealtimeService, tripId: number, event:
}
/** An item event delivered owner-only when the item is private (else to the room). */
function broadcastPackingItem(realtime: RealtimeService, tripId: number, event: string, payload: Record<string, unknown>, item: PackingPrivacy): void {
function broadcastPackingItem<E extends TrekWsTripEventName>(realtime: RealtimeService, tripId: number, event: E, payload: TrekWsPayload<E>, item: PackingPrivacy): void {
const onlyUserId = item?.is_private && item.owner_id != null ? item.owner_id : undefined;
realtime.broadcast(tripId, event, payload, undefined, onlyUserId);
}