refactor(reservations): one reservation type catalog in shared feeds the server's type lists

RESERVATION_TYPES lists every type with its flags (transport, carrier,
rental, stay, legs, creatable through MCP), and the named subsets derive
from it in picker order. The MCP transport and booking enums, the share
view's travel filter, the road trip carrier query and the shared
CARRIER_TYPES now read it. The reservation response types type and status
as the known values or any stored string, so rows outside the catalog still
pass; requests stay as tolerant as before.
This commit is contained in:
Maurice
2026-10-10 14:58:28 +02:00
parent 5e797463fa
commit 15db688586
9 changed files with 305 additions and 26 deletions
+1
View File
@@ -32,6 +32,7 @@ export * from './todo/todo.schema';
export * from './budget/budget.schema';
export * from './budget/receipt-scan.schema';
export * from './reservation/reservation.schema';
export * from './reservation/reservation-types';
export * from './reservation/ki-reservation.schema';
export * from './datetime/datetime-normalize';
export * from './airtrail/airtrail.schema';
@@ -0,0 +1,110 @@
import { CARRIER_TYPES, isCarrierType } from '../roadtrip/carriers';
import {
BOOKING_RESERVATION_TYPES,
CARRIER_RESERVATION_TYPES,
LEG_RESERVATION_TYPES,
MCP_CREATABLE_TRANSPORT_TYPES,
RENTAL_RESERVATION_TYPES,
RESERVATION_STATUSES,
RESERVATION_TYPE_KEYS,
ROADTRIP_RESERVATION_TYPES,
TRANSPORT_RESERVATION_TYPES,
TRAVEL_RESERVATION_TYPES,
isTransportReservationType,
reservationTypeInfo,
} from './reservation-types';
import { reservationSchema, reservationStatusSchema, reservationTypeSchema } from './reservation.schema';
import { describe, expect, it } from 'vitest';
// The subsets pin the hand-written lists they replaced, value for value and in
// order: the MCP tool enums are built from them and list their values as here.
describe('reservation type catalog', () => {
it('lists every type once', () => {
expect(new Set(RESERVATION_TYPE_KEYS).size).toBe(RESERVATION_TYPE_KEYS.length);
});
it('keeps the transport set of the update_transport gate', () => {
expect(TRANSPORT_RESERVATION_TYPES).toEqual([
'flight',
'train',
'bus',
'car',
'taxi',
'bicycle',
'cruise',
'ferry',
'cable_car',
'transit',
'transport_other',
]);
});
it('keeps the transport picker an assistant may ask for, without transit', () => {
expect(MCP_CREATABLE_TRANSPORT_TYPES).toEqual([
'flight',
'train',
'bus',
'car',
'taxi',
'bicycle',
'cruise',
'ferry',
'cable_car',
'transport_other',
]);
});
it('keeps the booking picker', () => {
expect(BOOKING_RESERVATION_TYPES).toEqual(['hotel', 'restaurant', 'event', 'tour', 'activity', 'parking', 'other']);
});
it('gives legs to flights and trains only', () => {
expect(LEG_RESERVATION_TYPES).toEqual(['flight', 'train']);
});
it('calls the same rides carriers as the road trip did, without the hire car', () => {
expect([...CARRIER_RESERVATION_TYPES].sort()).toEqual(['bus', 'cruise', 'ferry', 'flight', 'train']);
expect(CARRIER_TYPES).toBe(CARRIER_RESERVATION_TYPES);
expect(isCarrierType('car')).toBe(false);
expect(RENTAL_RESERVATION_TYPES).toEqual(['car']);
expect([...ROADTRIP_RESERVATION_TYPES].sort()).toEqual(['bus', 'car', 'cruise', 'ferry', 'flight', 'train']);
});
it('counts every transport and the hotel as travel', () => {
expect(TRAVEL_RESERVATION_TYPES).toEqual([...TRANSPORT_RESERVATION_TYPES, 'hotel']);
});
it('looks a stored type up, and knows nothing about a value outside the catalog', () => {
expect(reservationTypeInfo('ferry')).toMatchObject({ isTransport: true, isCarrier: true, creatableViaMcp: true });
expect(reservationTypeInfo('transit')).toMatchObject({
isTransport: true,
isCarrier: false,
creatableViaMcp: false,
});
expect(reservationTypeInfo('spaceship')).toBeUndefined();
expect(reservationTypeInfo(null)).toBeUndefined();
expect(isTransportReservationType('cable_car')).toBe(true);
expect(isTransportReservationType('hotel')).toBe(false);
expect(isTransportReservationType(undefined)).toBe(false);
});
it('keeps the three statuses', () => {
expect(RESERVATION_STATUSES).toEqual(['pending', 'confirmed', 'cancelled']);
});
});
describe('reservation response type and status', () => {
it('accepts the catalog values and passes stored free text through', () => {
expect(reservationTypeSchema.parse('flight')).toBe('flight');
expect(reservationTypeSchema.parse('legacy_kind')).toBe('legacy_kind');
expect(reservationStatusSchema.parse('confirmed')).toBe('confirmed');
expect(reservationStatusSchema.parse('waitlisted')).toBe('waitlisted');
expect(reservationTypeSchema.safeParse(3).success).toBe(false);
});
it('still parses a row whose type is outside the catalog', () => {
const row = { id: 1, trip_id: 2, title: 'Old row', status: 'pending', type: 'balloon' };
expect(reservationSchema.parse(row)).toMatchObject({ type: 'balloon' });
});
});
+137
View File
@@ -0,0 +1,137 @@
/**
* The reservation types TREK knows, with what each one is. One table instead of
* the type lists the server and the client used to keep side by side (the MCP
* transport gate, the share view's "travel only" filter, the day plan's
* carriers), so a new type is added once and every named subset follows.
*
* The order is the transport form's picker, then the booking form's; every
* subset below keeps it, so an enum built from one lists its values in the
* order the UI offers them.
*
* The `type` column itself stays free text: rows written by older versions or
* by an import may hold a value outside this table, and the API returns them
* as they are (`reservationTypeSchema` accepts any string).
*/
export interface ReservationTypeInfo {
readonly type: string;
/** Getting from A to B; rendered with a transport icon and edited through the transport tools. */
readonly isTransport: boolean;
/**
* A ride the traveller takes and leaves behind (the day plan's carrier, the
* road trip's seam). A hire car, a taxi or a transit hop is not one.
*/
readonly isCarrier: boolean;
/** A hire car: driven by the traveller, its desks are stops on the drive. */
readonly isRental: boolean;
/** Where the trip sleeps. */
readonly isStay: boolean;
/** Carries per-segment detail (metadata.legs). */
readonly hasLegs: boolean;
/**
* An assistant may create one through the MCP tools. `transit` is not: its
* provider itinerary is written by create_transit_journey alone.
*/
readonly creatableViaMcp: boolean;
}
const transport = {
isTransport: true,
isCarrier: false,
isRental: false,
isStay: false,
hasLegs: false,
creatableViaMcp: true,
} as const;
const booking = {
isTransport: false,
isCarrier: false,
isRental: false,
isStay: false,
hasLegs: false,
creatableViaMcp: true,
} as const;
export const RESERVATION_TYPES = [
{ type: 'flight', ...transport, isCarrier: true, hasLegs: true },
{ type: 'train', ...transport, isCarrier: true, hasLegs: true },
{ type: 'bus', ...transport, isCarrier: true },
{ type: 'car', ...transport, isRental: true },
{ type: 'taxi', ...transport },
{ type: 'bicycle', ...transport },
{ type: 'cruise', ...transport, isCarrier: true },
{ type: 'ferry', ...transport, isCarrier: true },
{ type: 'cable_car', ...transport },
{ type: 'transit', ...transport, creatableViaMcp: false },
{ type: 'transport_other', ...transport },
{ type: 'hotel', ...booking, isStay: true },
{ type: 'restaurant', ...booking },
{ type: 'event', ...booking },
{ type: 'tour', ...booking },
{ type: 'activity', ...booking },
{ type: 'parking', ...booking },
{ type: 'other', ...booking },
] as const satisfies readonly ReservationTypeInfo[];
type Entry = (typeof RESERVATION_TYPES)[number];
/** A reservation type from the catalog. */
export type ReservationType = Entry['type'];
type Flag = Exclude<keyof ReservationTypeInfo, 'type'>;
/** The catalog's types whose flags match all of `match`, in catalog order. */
function typesWhere<const M extends Partial<Record<Flag, boolean>>>(match: M): readonly Extract<Entry, M>['type'][] {
const flags = Object.entries(match) as [Flag, boolean][];
return RESERVATION_TYPES.filter((e) => flags.every(([flag, value]) => e[flag] === value)).map(
(e) => e.type,
) as Extract<Entry, M>['type'][];
}
/** Every type in the catalog, in picker order. */
export const RESERVATION_TYPE_KEYS: readonly ReservationType[] = RESERVATION_TYPES.map((e) => e.type);
/** Every transport type, `transit` included. */
export const TRANSPORT_RESERVATION_TYPES = typesWhere({ isTransport: true });
/** The transport types an assistant may ask for: the transport picker, without `transit`. */
export const MCP_CREATABLE_TRANSPORT_TYPES = typesWhere({ isTransport: true, creatableViaMcp: true });
/** The non-transport types an assistant may ask for: what create_reservation offers. */
export const BOOKING_RESERVATION_TYPES = typesWhere({ isTransport: false, creatableViaMcp: true });
/** The transport types that carry per-segment legs. */
export const LEG_RESERVATION_TYPES = typesWhere({ hasLegs: true });
/** The rides the traveller leaves behind (the day plan's carriers, the road trip's seams). */
export const CARRIER_RESERVATION_TYPES = typesWhere({ isCarrier: true });
/** The rental types (a hire car). */
export const RENTAL_RESERVATION_TYPES = typesWhere({ isRental: true });
/** What a road trip places on its route: the carriers it breaks at, and the hire cars whose desks are stops. */
export const ROADTRIP_RESERVATION_TYPES: readonly ReservationType[] = [
...CARRIER_RESERVATION_TYPES,
...RENTAL_RESERVATION_TYPES,
];
/** Getting there and sleeping there: every transport type, then the stays. */
export const TRAVEL_RESERVATION_TYPES: readonly ReservationType[] = [
...TRANSPORT_RESERVATION_TYPES,
...typesWhere({ isStay: true }),
];
const BY_TYPE: ReadonlyMap<string, ReservationTypeInfo> = new Map(RESERVATION_TYPES.map((e) => [e.type, e]));
/** The catalog entry for a stored type, or undefined for a value outside the catalog. */
export function reservationTypeInfo(type: string | null | undefined): ReservationTypeInfo | undefined {
return type == null ? undefined : BY_TYPE.get(type);
}
/** Whether a stored type is a transport booking. False for anything outside the catalog. */
export function isTransportReservationType(type: string | null | undefined): boolean {
return reservationTypeInfo(type)?.isTransport === true;
}
/** The statuses a reservation can be in. A row without one reads as `pending`. */
export const RESERVATION_STATUSES = ['pending', 'confirmed', 'cancelled'] as const;
export type ReservationStatus = (typeof RESERVATION_STATUSES)[number];
+20 -2
View File
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { RESERVATION_STATUSES, RESERVATION_TYPE_KEYS } from './reservation-types';
/**
* Reservation + accommodation API contract — single source of truth for the
@@ -15,6 +16,23 @@ import { z } from 'zod';
const open = z.record(z.string(), z.unknown());
/**
* A reservation's `type` as the API returns it: one of the catalog's types
* (`RESERVATION_TYPES`), or whatever free text an older row or an import
* stored, which is passed through as it is.
*/
export const reservationTypeSchema = z.enum(RESERVATION_TYPE_KEYS).or(z.string());
/** A reservation's `status` as the API returns it: a known status, or a stored value outside them. */
export const reservationStatusSchema = z.enum(RESERVATION_STATUSES).or(z.string());
// Inside reservationSchema the two fields stay z.string(), described by the
// catalog. Zod 4 types a union field as optional for a consumer compiled
// without strictNullChecks (the client), so the union there would loosen the
// inferred Reservation type; the runtime answer is the same, any string.
const reservationTypeField = z.string().describe('One of RESERVATION_TYPES, or free text an older row stored');
const reservationStatusField = z.string().describe('One of RESERVATION_STATUSES, or a value an older row stored');
/**
* A reservation endpoint (flight/train leg terminal) — row of the
* reservation_endpoints table (server/src/services/reservationService.ts).
@@ -148,8 +166,8 @@ export const reservationSchema = z.object({
confirmation_number: z.string().nullable().optional(),
notes: z.string().nullable().optional(),
url: z.string().nullable().optional(),
status: z.string(),
type: z.string(),
status: reservationStatusField,
type: reservationTypeField,
accommodation_id: z.union([z.number(), z.string()]).nullable().optional(),
metadata: z.string().nullable().optional(),
needs_review: z.number().optional(),
+4 -1
View File
@@ -1,4 +1,5 @@
import { chronoOrder } from '../day/chrono-order';
import { CARRIER_RESERVATION_TYPES } from '../reservation/reservation-types';
import { haversineKm, type LatLng } from './corridor';
import type { CarrierTerminal, RoadtripStop, RoutedLeg } from './planning-types';
import { formatClock, formatDurationShort, parseClock } from './roadtripModel';
@@ -12,8 +13,10 @@ import { formatClock, formatDurationShort, parseClock } from './roadtripModel';
* On the road trip such a booking is a seam: the road ends at the terminal it leaves
* from and starts again at the one it lands at. Routing straight across it drew a
* two-thousand-kilometre drive where a flight was booked (#2428).
*
* The set is the reservation catalog's `isCarrier` types (`reservation-types.ts`).
*/
export const CARRIER_TYPES = ['flight', 'train', 'ferry', 'cruise', 'bus'] as const;
export const CARRIER_TYPES = CARRIER_RESERVATION_TYPES;
export function isCarrierType(type: string | null | undefined): boolean {
return !!type && (CARRIER_TYPES as readonly string[]).includes(type);