Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(bookings): count a multi-segment rail ticket's fare once

Maurice23b7f6d71c27
diff --git a/server/src/db/repositories/BudgetItems.repository.ts b/server/src/db/repositories/BudgetItems.repository.ts
index 4946d6e1a..899a10beb 100644
--- a/server/src/db/repositories/BudgetItems.repository.ts
+++ b/server/src/db/repositories/BudgetItems.repository.ts
@@ -456,6 +456,41 @@ export class BudgetItemsRepository extends TrekRepository<BudgetItems> {
.execute();
}
+ /**
+ * Every expense of the trip that is linked to a booking carrying a booking
+ * reference, with that booking's reference and title and the trip's currency:
+ * what a new booking's price is compared against to tell a ticket's fare
+ * booked a second time (#2895).
+ */
+ async listLinkedToReferencedReservations(trip_id: number): Promise<
+ {
+ reservation_id: number;
+ title: string;
+ confirmation_number: string | null;
+ total_price: number;
+ currency: string | null;
+ trip_currency: string | null;
+ }[]
+ > {
+ return await this.kysely<Pick<DB, 'budget_items' | 'reservations' | 'trips'>>()
+ .selectFrom('budget_items as b')
+ .innerJoin('reservations as r', 'r.id', 'b.reservation_id')
+ .innerJoin('trips as t', 't.id', 'b.trip_id')
+ .select([
+ 'r.id as reservation_id',
+ 'r.title',
+ 'r.confirmation_number',
+ 'b.total_price',
+ 'b.currency',
+ 't.currency as trip_currency',
+ ])
+ .where('b.trip_id', '=', trip_id)
+ .where('r.trip_id', '=', trip_id)
+ .where('r.confirmation_number', 'is not', null)
+ .orderBy('b.id', 'asc')
+ .execute();
+ }
+
/** BG17's `budget_items` half — `UPDATE budget_items SET currency = ? WHERE trip_id = ? AND (currency IS NULL OR currency = '')`. */
async pinCurrency(trip_id: number | string, prev_currency: string): Promise<void> {
await this.kysely<BudgetKyselyDB>()
diff --git a/server/src/nest/booking-import/booking-import.service.ts b/server/src/nest/booking-import/booking-import.service.ts
index 394f4e78c..307793811 100644
--- a/server/src/nest/booking-import/booking-import.service.ts
+++ b/server/src/nest/booking-import/booking-import.service.ts
@@ -230,7 +230,7 @@ export class BookingImportService {
if (kiItems.length === 0) {
allWarnings.push(`${file.originalname}: no reservations found`);
} else {
- const { items, warnings } = mapReservations(kiItems, file.originalname);
+ const { items, warnings } = mapReservations(kiItems, file.originalname, aiUsed ? 'ai' : 'kitinerary');
// LLM extraction is less certain than kitinerary — always flag for review.
if (aiUsed) for (const it of items) it.needs_review = true;
diff --git a/server/src/nest/booking-import/kitinerary-mapper.ts b/server/src/nest/booking-import/kitinerary-mapper.ts
index cc5c7f3b5..63353c2c0 100644
--- a/server/src/nest/booking-import/kitinerary-mapper.ts
+++ b/server/src/nest/booking-import/kitinerary-mapper.ts
@@ -2,6 +2,7 @@ import { findByIata } from '../airports/airports.data';
import type {
KiReservation,
KiFlight,
+ KiStation,
KiTrainTrip,
KiBusTrip,
KiBoatTrip,
@@ -16,7 +17,14 @@ import type {
ParsedEndpoint,
ParsedVenue,
} from './kitinerary.types';
-import { normalizeLocalDateTime, splitLocalDateTime } from '@trek/shared';
+import {
+ haversineMetres,
+ LEG_RESERVATION_TYPES,
+ normalizeLocalDateTime,
+ splitLocalDateTime,
+ sumMinor,
+ toMinor,
+} from '@trek/shared';
// ---------------------------------------------------------------------------
// Helpers
@@ -172,43 +180,70 @@ function mapFlight(r: KiReservation, source: ParsedBookingItem['source']): Parse
/** The longest layover that still makes one journey, as AirTrail's import joins legs. */
const MAX_LAYOVER_MS = 24 * 3600 * 1000;
+/** Two stations this close are one place, however each segment of the ticket spells its name. */
+const SAME_STATION_METRES = 500;
+
/** Epoch ms of a kitinerary time, NaN when it is missing or cannot be read. */
function instantMs(dt: KiDateTimeish): number {
const iso = toIsoString(dt);
return iso ? new Date(iso).getTime() : Number.NaN;
}
+/** Where a segment leaves from or arrives at: an airport's IATA code, a station's name, and its position when known. */
+interface HopPlace {
+ key: string | null;
+ geo: { lat: number; lng: number } | null;
+}
+
+/** One segment of a journey, reduced to what tells whether the next one continues it. */
+interface Hop {
+ from: HopPlace;
+ to: HopPlace;
+ departureTime: KiDateTimeish;
+ arrivalTime: KiDateTimeish;
+}
+
+function samePlace(a: HopPlace, b: HopPlace): boolean {
+ if (a.key && a.key === b.key) return true;
+ return !!a.geo && !!b.geo && haversineMetres(a.geo.lat, a.geo.lng, b.geo.lat, b.geo.lng) <= SAME_STATION_METRES;
+}
+
/**
- * True when flight `b` continues the `chain` of connecting flights. It has to
- * leave from where the chain's last flight landed, must not land anywhere the
- * chain has already been (that is a return, however soon it flies), and has to
- * leave within a day. The day is counted from the last flight's arrival, or
+ * True when segment `next` continues the `chain` of connecting segments. It has
+ * to leave from where the chain's last segment arrived, must not arrive anywhere
+ * the chain has already been (that is a return, however soon it runs), and has
+ * to leave within a day. The day is counted from the last segment's arrival, or
* from its departure when no arrival is known (an AI import often has none).
- * A missing or unreadable departure fails both comparisons: two flights whose
+ * A missing or unreadable departure fails both comparisons: two segments whose
* order cannot be told apart stay two bookings, where a guess had folded a
* return days earlier into the outbound one (#2653).
*/
-function sameConnection(chain: KiReservation[], b: KiReservation): boolean {
- const fa = chain[chain.length - 1].reservationFor as KiFlight | undefined;
- const fb = b.reservationFor as KiFlight | undefined;
- if (!fa || !fb) return false;
- const arrIata = fa.arrivalAirport?.iataCode?.toUpperCase();
- const depIata = fb.departureAirport?.iataCode?.toUpperCase();
- if (!arrIata || !depIata || arrIata !== depIata) return false; // must connect at the same airport
- const visited = new Set<string>();
- chain.forEach((leg, i) => {
- const f = leg.reservationFor as KiFlight | undefined;
- if (i === 0 && f?.departureAirport?.iataCode) visited.add(f.departureAirport.iataCode.toUpperCase());
- if (f?.arrivalAirport?.iataCode) visited.add(f.arrivalAirport.iataCode.toUpperCase());
- });
- const nextArr = fb.arrivalAirport?.iataCode?.toUpperCase();
- if (nextArr && visited.has(nextArr)) return false;
- const arrived = instantMs(fa.arrivalTime);
- const from = Number.isNaN(arrived) ? instantMs(fa.departureTime) : arrived;
- const gapMs = instantMs(fb.departureTime) - from;
+function continuesChain(chain: Hop[], next: Hop): boolean {
+ const last = chain[chain.length - 1];
+ if (!samePlace(last.to, next.from)) return false;
+ const visited = [chain[0].from, ...chain.map((hop) => hop.to)];
+ if (visited.some((place) => samePlace(place, next.to))) return false;
+ const arrived = instantMs(last.arrivalTime);
+ const from = Number.isNaN(arrived) ? instantMs(last.departureTime) : arrived;
+ const gapMs = instantMs(next.departureTime) - from;
return gapMs >= 0 && gapMs <= MAX_LAYOVER_MS;
}
+function flightHop(r: KiReservation): Hop | null {
+ const f = r.reservationFor as KiFlight | undefined;
+ if (!f) return null;
+ const airport = (ap: KiFlight['departureAirport']): HopPlace => ({
+ key: ap?.iataCode?.toUpperCase() ?? null,
+ geo: null,
+ });
+ return {
+ from: airport(f.departureAirport),
+ to: airport(f.arrivalAirport),
+ departureTime: f.departureTime,
+ arrivalTime: f.arrivalTime,
+ };
+}
+
/** Collapse several connecting flight legs (same PNR) into one multi-leg booking. */
function mapFlightGroup(legs: KiReservation[], source: ParsedBookingItem['source']): ParsedBookingItem | null {
const flights = legs.map((l) => l.reservationFor as KiFlight | undefined);
@@ -260,10 +295,13 @@ function mapFlightGroup(legs: KiReservation[], source: ParsedBookingItem['source
const originEp = makeEndpoint(first.departureAirport, 'from', firstDep.time, firstDep.date);
if (originEp) endpoints.push(originEp);
+ // A stop carries the onward departure, as the planner form saves one; the
+ // landing there stays in the leg's arr_time.
fs.forEach((f, i) => {
const isLast = i === fs.length - 1;
const arr = splitIso(f.arrivalTime);
- const arrEp = makeEndpoint(f.arrivalAirport, isLast ? 'to' : 'stop', arr.time, arr.date);
+ const onward = isLast ? arr : splitIso(fs[i + 1].departureTime);
+ const arrEp = makeEndpoint(f.arrivalAirport, isLast ? 'to' : 'stop', onward.time, onward.date);
if (arrEp) endpoints.push(arrEp);
const airline = f.airline?.name ?? f.airline?.iataCode ?? '';
metaLegs.push({
@@ -303,160 +341,198 @@ function mapFlightGroup(legs: KiReservation[], source: ParsedBookingItem['source
};
}
-function mapTrain(r: KiReservation, source: ParsedBookingItem['source']): ParsedBookingItem | null {
- const t = r.reservationFor as KiTrainTrip | undefined;
- if (!t) return null;
+/** A train, bus or boat segment under the names all three share. */
+interface GroundSegment {
+ from?: KiStation;
+ to?: KiStation;
+ departureTime?: KiDateTimeish;
+ arrivalTime?: KiDateTimeish;
+ /** The train or bus number, or a boat's name. */
+ id: string;
+}
- const depName = t.departureStation?.name ?? 'Unknown';
- const arrName = t.arrivalStation?.name ?? 'Unknown';
- const trainId = t.trainNumber ?? t.trainName ?? '';
- const title = trainId ? `${trainId} (${depName} → ${arrName})` : `Train ${depName} → ${arrName}`;
+interface GroundKind {
+ type: 'train' | 'bus' | 'cruise';
+ read: (trip: Record<string, unknown>) => GroundSegment;
+ title: (id: string, route: string) => string;
+ /** Where the segment's id is kept in the metadata; a boat's name is its title instead. */
+ idKey?: 'train_number' | 'bus_number';
+}
- const { date: depDate, time: depTime } = splitIso(t.departureTime);
- const { date: arrDate, time: arrTime } = splitIso(t.arrivalTime);
+// A Map, not an object literal: the key is the document's own @type, and
+// "constructor" must not find anything.
+const GROUND_KINDS = new Map<string, GroundKind>([
+ [
+ 'TrainReservation',
+ {
+ type: 'train',
+ read: (trip) => {
+ const t = trip as KiTrainTrip;
+ return {
+ from: t.departureStation,
+ to: t.arrivalStation,
+ departureTime: t.departureTime,
+ arrivalTime: t.arrivalTime,
+ id: t.trainNumber ?? t.trainName ?? '',
+ };
+ },
+ title: (id, route) => (id ? `${id} (${route})` : `Train ${route}`),
+ idKey: 'train_number',
+ },
+ ],
+ [
+ 'BusReservation',
+ {
+ type: 'bus',
+ read: (trip) => {
+ const b = trip as KiBusTrip;
+ return {
+ from: b.departureBusStop,
+ to: b.arrivalBusStop,
+ departureTime: b.departureTime,
+ arrivalTime: b.arrivalTime,
+ id: b.busNumber ?? b.busName ?? '',
+ };
+ },
+ title: (id, route) => (id ? `${id} (${route})` : `Bus ${route}`),
+ idKey: 'bus_number',
+ },
+ ],
+ [
+ 'BoatReservation',
+ {
+ type: 'cruise',
+ read: (trip) => {
+ const b = trip as KiBoatTrip;
+ return {
+ from: b.departureBoatTerminal,
+ to: b.arrivalBoatTerminal,
+ departureTime: b.departureTime,
+ arrivalTime: b.arrivalTime,
+ id: b.name ?? '',
+ };
+ },
+ title: (id, route) => id || `Cruise ${route}`,
+ },
+ ],
+]);
- const endpoints: ParsedEndpoint[] = [];
- const dc = coords(t.departureStation?.geo);
- const ac = coords(t.arrivalStation?.geo);
- // Push named endpoints even without coords — preview() geocodes them (#1969).
- if (t.departureStation?.name)
- endpoints.push({
- role: 'from',
- sequence: 0,
- name: depName,
- code: null,
- lat: dc?.lat ?? null,
- lng: dc?.lng ?? null,
- timezone: null,
- local_time: depTime,
- local_date: depDate,
- });
- if (t.arrivalStation?.name)
- endpoints.push({
- role: 'to',
- sequence: 1,
- name: arrName,
- code: null,
- lat: ac?.lat ?? null,
- lng: ac?.lng ?? null,
- timezone: null,
- local_time: arrTime,
- local_date: arrDate,
- });
+/** The types whose bookings carry one entry per segment in metadata.legs. */
+const LEG_TYPES: readonly string[] = LEG_RESERVATION_TYPES;
+function stationPlace(stop: KiStation | undefined): HopPlace {
+ return { key: stop?.name?.trim().toLowerCase() || null, geo: coords(stop?.geo) };
+}
+
+function groundHop(kind: GroundKind, r: KiReservation): Hop | null {
+ if (!r.reservationFor) return null;
+ const s = kind.read(r.reservationFor);
return {
- type: 'train',
- title,
- reservation_time: toIsoString(t.departureTime),
- reservation_end_time: toIsoString(t.arrivalTime),
- confirmation_number: r.reservationNumber ?? null,
- metadata: trainId ? { train_number: trainId } : undefined,
- endpoints,
- needs_review: endpoints.length < 2,
- source,
+ from: stationPlace(s.from),
+ to: stationPlace(s.to),
+ departureTime: s.departureTime,
+ arrivalTime: s.arrivalTime,
};
}
-function mapBus(r: KiReservation, source: ParsedBookingItem['source']): ParsedBookingItem | null {
- const b = r.reservationFor as KiBusTrip | undefined;
- if (!b) return null;
+/** A named stop as an endpoint, with or without coordinates: preview() geocodes the rest (#1969). */
+function stationEndpoint(
+ stop: KiStation | undefined,
+ role: ParsedEndpoint['role'],
+ sequence: number,
+ dt: KiDateTimeish,
+): ParsedEndpoint | null {
+ if (!stop?.name) return null;
+ const c = coords(stop.geo);
+ const { date, time } = splitIso(dt);
+ return {
+ role,
+ sequence,
+ name: stop.name,
+ code: null,
+ lat: c?.lat ?? null,
+ lng: c?.lng ?? null,
+ timezone: null,
+ local_time: time,
+ local_date: date,
+ };
+}
- const depName = b.departureBusStop?.name ?? 'Unknown';
- const arrName = b.arrivalBusStop?.name ?? 'Unknown';
- const busId = b.busNumber ?? b.busName ?? '';
- const title = busId ? `${busId} (${depName} → ${arrName})` : `Bus ${depName} → ${arrName}`;
+const isEndpoint = (e: ParsedEndpoint | null): e is ParsedEndpoint => e !== null;
- const { date: depDate, time: depTime } = splitIso(b.departureTime);
- const { date: arrDate, time: arrTime } = splitIso(b.arrivalTime);
-
- const endpoints: ParsedEndpoint[] = [];
- const dc = coords(b.departureBusStop?.geo);
- const ac = coords(b.arrivalBusStop?.geo);
- if (b.departureBusStop?.name)
- endpoints.push({
- role: 'from',
- sequence: 0,
- name: depName,
- code: null,
- lat: dc?.lat ?? null,
- lng: dc?.lng ?? null,
- timezone: null,
- local_time: depTime,
- local_date: depDate,
- });
- if (b.arrivalBusStop?.name)
- endpoints.push({
- role: 'to',
- sequence: 1,
- name: arrName,
- code: null,
- lat: ac?.lat ?? null,
- lng: ac?.lng ?? null,
- timezone: null,
- local_time: arrTime,
- local_date: arrDate,
- });
+function mapGround(r: KiReservation, kind: GroundKind, source: ParsedBookingItem['source']): ParsedBookingItem | null {
+ if (!r.reservationFor) return null;
+ const s = kind.read(r.reservationFor);
+ const route = `${s.from?.name ?? 'Unknown'} → ${s.to?.name ?? 'Unknown'}`;
+ const endpoints = [
+ stationEndpoint(s.from, 'from', 0, s.departureTime),
+ stationEndpoint(s.to, 'to', 1, s.arrivalTime),
+ ].filter(isEndpoint);
return {
- type: 'bus',
- title,
- reservation_time: toIsoString(b.departureTime),
- reservation_end_time: toIsoString(b.arrivalTime),
+ type: kind.type,
+ title: kind.title(s.id, route),
+ reservation_time: toIsoString(s.departureTime),
+ reservation_end_time: toIsoString(s.arrivalTime),
confirmation_number: r.reservationNumber ?? null,
- metadata: busId ? { bus_number: busId } : undefined,
+ ...(kind.idKey && s.id ? { metadata: { [kind.idKey]: s.id } } : {}),
endpoints,
needs_review: endpoints.length < 2,
source,
};
}
-function mapBoat(r: KiReservation, source: ParsedBookingItem['source']): ParsedBookingItem | null {
- const b = r.reservationFor as KiBoatTrip | undefined;
- if (!b) return null;
-
- const depName = b.departureBoatTerminal?.name ?? 'Unknown';
- const arrName = b.arrivalBoatTerminal?.name ?? 'Unknown';
- const title = (b as any).name ?? `Cruise ${depName} → ${arrName}`;
-
- const { date: depDate, time: depTime } = splitIso(b.departureTime);
- const { date: arrDate, time: arrTime } = splitIso(b.arrivalTime);
+/**
+ * Several connecting segments of one ticket as one booking with stops, the
+ * shape the planner form saves a train with changes in (#2895). A stop's
+ * endpoint carries the onward departure, as the form writes it; each segment's
+ * own times, number, platform and seat go into metadata.legs.
+ */
+function mapGroundGroup(
+ nodes: KiReservation[],
+ kind: GroundKind,
+ source: ParsedBookingItem['source'],
+): ParsedBookingItem | null {
+ const segments = nodes.map((node) => kind.read(node.reservationFor ?? {}));
+ const first = segments[0];
+ const last = segments[segments.length - 1];
+ const endpoints = [
+ stationEndpoint(first.from, 'from', 0, first.departureTime),
+ ...segments.map((s, i) =>
+ s === last
+ ? stationEndpoint(s.to, 'to', i + 1, s.arrivalTime)
+ : stationEndpoint(s.to, 'stop', i + 1, segments[i + 1].departureTime),
+ ),
+ ].filter(isEndpoint);
+ endpoints.forEach((e, i) => {
+ e.sequence = i;
+ });
- const endpoints: ParsedEndpoint[] = [];
- const dc = coords(b.departureBoatTerminal?.geo);
- const ac = coords(b.arrivalBoatTerminal?.geo);
- if (b.departureBoatTerminal?.name)
- endpoints.push({
- role: 'from',
- sequence: 0,
- name: depName,
- code: null,
- lat: dc?.lat ?? null,
- lng: dc?.lng ?? null,
- timezone: null,
- local_time: depTime,
- local_date: depDate,
- });
- if (b.arrivalBoatTerminal?.name)
- endpoints.push({
- role: 'to',
- sequence: 1,
- name: arrName,
- code: null,
- lat: ac?.lat ?? null,
- lng: ac?.lng ?? null,
- timezone: null,
- local_time: arrTime,
- local_date: arrDate,
- });
+ const legs = segments.map((s, i) => {
+ const detail = segmentDetail(nodes[i]);
+ return {
+ from: s.from?.name ?? null,
+ to: s.to?.name ?? null,
+ ...(kind.idKey && s.id ? { [kind.idKey]: s.id } : {}),
+ ...(detail.platform != null ? { platform: detail.platform } : {}),
+ ...(detail.seat != null ? { seat: detail.seat } : {}),
+ dep_time: splitIso(s.departureTime).time,
+ arr_time: splitIso(s.arrivalTime).time,
+ };
+ });
return {
- type: 'cruise',
- title,
- reservation_time: toIsoString(b.departureTime),
- reservation_end_time: toIsoString(b.arrivalTime),
- confirmation_number: r.reservationNumber ?? null,
+ type: kind.type,
+ title: kind.title('', `${first.from?.name ?? 'Unknown'} → ${last.to?.name ?? 'Unknown'}`),
+ reservation_time: toIsoString(first.departureTime),
+ reservation_end_time: toIsoString(last.arrivalTime),
+ confirmation_number: nodes[0].reservationNumber ?? null,
+ metadata: {
+ ...(kind.idKey && first.id ? { [kind.idKey]: first.id } : {}),
+ legs,
+ },
endpoints,
- needs_review: endpoints.length < 2,
+ needs_review: endpoints.length < segments.length + 1,
source,
};
}
@@ -612,10 +688,16 @@ function mapEvent(r: KiReservation, source: ParsedBookingItem['source']): Parsed
// Public
// ---------------------------------------------------------------------------
-/** Merge seat/class/platform/price into an item's metadata (type-agnostic).
+/** Seat, class, platform and price of one node (type-agnostic).
* Models name these inconsistently and sometimes nest them under reservationFor,
- * so check both levels and common aliases. The item's own metadata wins. */
-function applyCommonMeta(item: ParsedBookingItem, r: KiReservation): ParsedBookingItem {
+ * so check both levels and common aliases. */
+function segmentDetail(r: KiReservation): {
+ seat?: string;
+ class?: string;
+ platform?: string;
+ price?: unknown;
+ priceCurrency?: string;
+} {
const rf = (r.reservationFor && typeof r.reservationFor === 'object' ? r.reservationFor : {}) as Record<
string,
unknown
@@ -627,21 +709,173 @@ function applyCommonMeta(item: ParsedBookingItem, r: KiReservation): ParsedBooki
}
return undefined;
};
- const m: Record<string, unknown> = {};
const seat = pick('seat', 'seatNumber');
- if (seat != null) m.seat = String(seat);
const cls = pick('class', 'bookingClass', 'fareClass', 'serviceClass', 'seatingType');
- if (cls != null) m.class = String(cls);
const platform = pick('platform', 'departurePlatform');
- if (platform != null) m.platform = String(platform);
const price = pick('price', 'priceAmount', 'totalPrice', 'total');
- if (price != null) m.price = price;
const cur = pick('priceCurrency', 'priceCurrencyISO4217Code', 'currency');
- if (cur != null) m.priceCurrency = String(cur);
- if (Object.keys(m).length) item.metadata = { ...m, ...(item.metadata ?? {}) };
+ return {
+ ...(seat != null ? { seat: String(seat) } : {}),
+ ...(cls != null ? { class: String(cls) } : {}),
+ ...(platform != null ? { platform: String(platform) } : {}),
+ ...(price != null ? { price } : {}),
+ ...(cur != null ? { priceCurrency: String(cur) } : {}),
+ };
+}
+
+/** Merge a node's seat/class/platform/price into its item's metadata. The item's own metadata wins. */
+function applyCommonMeta(item: ParsedBookingItem, r: KiReservation): ParsedBookingItem {
+ const detail = segmentDetail(r);
+ if (Object.keys(detail).length) item.metadata = { ...detail, ...(item.metadata ?? {}) };
return item;
}
+interface Fare {
+ price?: unknown;
+ priceCurrency?: string;
+}
+
+const currencyOf = (fare: Fare) => fare.priceCurrency?.trim().toUpperCase();
+
+/** Whether every price is a number, all in one currency. */
+function summable(fares: Fare[]): boolean {
+ return fares.every((fare) => Number.isFinite(Number(fare.price)) && currencyOf(fare) === currencyOf(fares[0]));
+}
+
+/** Whether every price is the same amount, to the cent, in the same currency. */
+function oneAmount(fares: Fare[]): boolean {
+ return summable(fares) && fares.every((fare) => toMinor(Number(fare.price)) === toMinor(Number(fares[0].price)));
+}
+
+/**
+ * What one ticket covering several segments cost. KItinerary and the AI import
+ * both print the ticket's total on every segment, so the same amount on each is
+ * that total once, not once per segment (#2895). Amounts that differ are fares
+ * per segment and add up, in whole cents, for flights as for trains. Mixed
+ * currencies, or an amount that is not a number, keep the first segment's price.
+ */
+function ticketFare(nodes: KiReservation[]): Fare | null {
+ const priced = nodes.map(segmentDetail).filter((detail) => detail.price != null);
+ if (priced.length === 0) return null;
+ const [first] = priced;
+ const currency = first.priceCurrency != null ? { priceCurrency: first.priceCurrency } : {};
+ if (!summable(priced) || oneAmount(priced)) return { price: first.price, ...currency };
+ return { price: sumMinor(priced.map((detail) => Number(detail.price))) / 100, ...currency };
+}
+
+function withoutPrice(item: ParsedBookingItem): ParsedBookingItem {
+ if (!item.metadata) return item;
+ const { price: _price, priceCurrency: _currency, ...rest } = item.metadata;
+ return { ...item, metadata: Object.keys(rest).length ? rest : undefined };
+}
+
+function hopOf(r: KiReservation): Hop | null {
+ if (r['@type'] === 'FlightReservation') return flightHop(r);
+ const kind = GROUND_KINDS.get(r['@type']);
+ return kind ? groundHop(kind, r) : null;
+}
+
+/**
+ * The segments one ticket covers, from `start` on: consecutive nodes of the same
+ * type under the same booking reference, each leaving from where the one before
+ * it arrived (`continuesChain`). A node without a reference is a ticket of its own.
+ */
+function ticketChain(kiItems: KiReservation[], start: number): KiReservation[] {
+ const first = kiItems[start];
+ const firstHop = hopOf(first);
+ const ref = first.reservationNumber ?? null;
+ const chain = [first];
+ if (ref == null || !firstHop) return chain;
+ const hops = [firstHop];
+ for (let j = start + 1; j < kiItems.length; j++) {
+ const next = kiItems[j];
+ const hop = next['@type'] === first['@type'] && (next.reservationNumber ?? null) === ref ? hopOf(next) : null;
+ if (!hop || !continuesChain(hops, hop)) break;
+ chain.push(next);
+ hops.push(hop);
+ }
+ return chain;
+}
+
+/**
+ * The bookings one chain of connecting segments becomes. A flight or a train
+ * with connections is one booking with stops and legs, priced once. A bus or a
+ * boat has no legs to carry, so its segments stay bookings of their own, and
+ * `priceRepeatedTotalOnce` keeps a total repeated on each to one of them.
+ */
+function mapTicket(
+ chain: KiReservation[],
+ fileName: string,
+ start: number,
+ out: { items: ParsedBookingItem[]; warnings: string[] },
+): void {
+ const [first] = chain;
+ const kind = GROUND_KINDS.get(first['@type']);
+ const source = { fileName, index: start };
+
+ if (kind && chain.length > 1 && !LEG_TYPES.includes(kind.type)) {
+ chain.forEach((node, k) => {
+ const item = mapGround(node, kind, { fileName, index: start + k });
+ if (item) out.items.push(applyCommonMeta(item, node));
+ else out.warnings.push(unmappableWarning(node, fileName, start + k));
+ });
+ return;
+ }
+
+ const grouped = chain.length > 1;
+ let item: ParsedBookingItem | null;
+ if (kind) item = grouped ? mapGroundGroup(chain, kind, source) : mapGround(first, kind, source);
+ else item = grouped ? mapFlightGroup(chain, source) : mapFlight(first, source);
+ if (!item) {
+ out.warnings.push(unmappableWarning(first, fileName, start));
+ return;
+ }
+ const fare = grouped ? ticketFare(chain) : null;
+ if (fare) item.metadata = { ...(item.metadata ?? {}), ...fare };
+ out.items.push(applyCommonMeta(item, first));
+}
+
+/** The booking types a transport reservation maps to, whichever kind of ticket it came from. */
+const TRANSPORT_TYPES = new Set<string>(['flight', ...[...GROUND_KINDS.values()].map((kind) => kind.type)]);
+
+const departureMs = (item: ParsedBookingItem) =>
+ item.reservation_time ? new Date(item.reservation_time).getTime() : Number.NaN;
+
+const fareOf = (item: ParsedBookingItem): Fare => {
+ const currency = item.metadata?.priceCurrency;
+ return { price: item.metadata?.price, priceCurrency: typeof currency === 'string' ? currency : undefined };
+};
+
+/**
+ * KItinerary prints an order's total on every segment, also on segments that do
+ * not join into one booking: an outward and a return trip under one order
+ * number, a gap between two segments, a change of station whose names differ
+ * where no position is known, a bus or boat ticket with changes, a flight and a
+ * train on one Rail&Fly reference. When every priced transport booking of one
+ * reference carries the same amount in the same currency, that amount is the
+ * order's total once, so the first of them to depart keeps it and the others
+ * lose it (#2895). Amounts that differ are fares of their own and stay.
+ *
+ * Only for what KItinerary extracted. A model can read fares the document
+ * states per segment that happen to be equal; `collapseRepeatedBookingTotal`
+ * tells the two apart for the AI import by the document's own total line.
+ */
+function priceRepeatedTotalOnce(items: ParsedBookingItem[]): ParsedBookingItem[] {
+ const byReference = new Map<string, ParsedBookingItem[]>();
+ for (const item of items) {
+ const reference = item.confirmation_number?.trim().toUpperCase();
+ if (!reference || !TRANSPORT_TYPES.has(item.type) || item.metadata?.price == null) continue;
+ byReference.set(reference, [...(byReference.get(reference) ?? []), item]);
+ }
+ const repeats = new Set<ParsedBookingItem>();
+ for (const priced of byReference.values()) {
+ if (priced.length < 2 || !oneAmount(priced.map(fareOf))) continue;
+ const keeper = priced.reduce((first, item) => (departureMs(item) < departureMs(first) ? item : first));
+ priced.filter((item) => item !== keeper).forEach((item) => repeats.add(item));
+ }
+ return repeats.size ? items.map((item) => (repeats.has(item) ? withoutPrice(item) : item)) : items;
+}
+
/**
* Why a mapper answered null, in the words of the node it refused.
*
@@ -660,6 +894,7 @@ function unmappableWarning(r: KiReservation, fileName: string, index: number): s
export function mapReservations(
kiItems: KiReservation[],
fileName: string,
+ extractedBy: 'kitinerary' | 'ai' = 'kitinerary',
): { items: ParsedBookingItem[]; warnings: string[] } {
const items: ParsedBookingItem[] = [];
const warnings: string[] = [];
@@ -669,37 +904,14 @@ export function mapReservations(
const source = { fileName, index: i };
let item: ParsedBookingItem | null;
- // Group consecutive connecting flight legs that share a PNR into one booking.
- if (r['@type'] === 'FlightReservation') {
- const pnr = r.reservationNumber ?? null;
- const group = [r];
- while (
- i + 1 < kiItems.length &&
- kiItems[i + 1]['@type'] === 'FlightReservation' &&
- pnr != null &&
- (kiItems[i + 1].reservationNumber ?? null) === pnr &&
- sameConnection(group, kiItems[i + 1])
- ) {
- group.push(kiItems[++i]);
- }
- item = group.length > 1 ? mapFlightGroup(group, source) : mapFlight(r, source);
- // `i` sits on the last leg the group swallowed, so the index comes from
- // `source` — the node the warning is actually describing.
- if (item) items.push(applyCommonMeta(item, r));
- else warnings.push(unmappableWarning(r, fileName, source.index));
+ if (r['@type'] === 'FlightReservation' || GROUND_KINDS.has(r['@type'])) {
+ const chain = ticketChain(kiItems, i);
+ mapTicket(chain, fileName, i, { items, warnings });
+ i += chain.length - 1;
continue;
}
switch (r['@type']) {
- case 'TrainReservation':
- item = mapTrain(r, source);
- break;
- case 'BusReservation':
- item = mapBus(r, source);
- break;
- case 'BoatReservation':
- item = mapBoat(r, source);
- break;
case 'LodgingReservation':
item = mapLodging(r, source);
break;
@@ -722,5 +934,5 @@ export function mapReservations(
else warnings.push(unmappableWarning(r, fileName, i));
}
- return { items, warnings };
+ return { items: extractedBy === 'kitinerary' ? priceRepeatedTotalOnce(items) : items, warnings };
}
diff --git a/server/src/nest/llm-parse/booking-total.ts b/server/src/nest/llm-parse/booking-total.ts
index 68560b204..c7307b68a 100644
--- a/server/src/nest/llm-parse/booking-total.ts
+++ b/server/src/nest/llm-parse/booking-total.ts
@@ -43,22 +43,28 @@ const priceOf = (node: Node): number | null => {
return amount != null && amount > 0 ? toMinor(amount) : null;
};
+/** The reservations a ticket with several segments is read as, one node per segment. */
+const SEGMENT_TYPES = new Set(['FlightReservation', 'TrainReservation', 'BusReservation', 'BoatReservation']);
+
/**
* A model reading a round trip with one total for the whole booking puts that
* total on every flight, and saving both legs then books the purchase twice
- * (#2651). When the flights plainly share it, the price stays on the first
- * flight to depart and leaves the others.
+ * (#2651). A rail, bus or ferry ticket with changes reads the same way, one
+ * node per segment with the ticket's total on each (#2895). When the segments
+ * plainly share it, the price stays on the first segment to depart and leaves
+ * the others.
*
- * Plainly means: only flights, all on one booking reference, at least two of
+ * Plainly means: transport segments only, of any mix of kinds (Rail&Fly, a
+ * train with a replacement bus), all on one booking reference, at least two of
* them priced, every price the same amount in the same currency, and the
* document printing that amount on a total line, on fewer lines than there are
- * priced flights. Fares that differ, separate references, mixed currencies,
+ * priced segments. Fares that differ, separate references, mixed currencies,
* other booking types, or no document text to check against (a PDF read
* natively, a photo) leave the nodes as they are.
*/
export function collapseRepeatedBookingTotal(nodes: Node[], documentText: string | undefined): Node[] {
if (!documentText || nodes.length < 2) return nodes;
- if (!nodes.every((node) => node['@type'] === 'FlightReservation')) return nodes;
+ if (!nodes.every((node) => typeof node['@type'] === 'string' && SEGMENT_TYPES.has(node['@type']))) return nodes;
const ref = reference(nodes[0]);
if (!ref || !nodes.every((node) => reference(node) === ref)) return nodes;
diff --git a/server/src/nest/reservations/reservation-writes.service.ts b/server/src/nest/reservations/reservation-writes.service.ts
index 769dc6d60..e8b685f5a 100644
--- a/server/src/nest/reservations/reservation-writes.service.ts
+++ b/server/src/nest/reservations/reservation-writes.service.ts
@@ -1,3 +1,5 @@
+import { BudgetItems } from '../../db/entities/BudgetItems.entity';
+import type { BudgetItemsRepository } from '../../db/repositories/BudgetItems.repository';
import type { Reservation, User } from '../../types';
import { DomainError } from '../common/domain-error';
import { restTripWriter, type TripWriter } from '../common/trip-writer';
@@ -9,7 +11,9 @@ import {
type CreateReservationData,
type UpdateReservationData,
} from './reservations.service';
+import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
+import { toMinor } from '@trek/shared';
/** The linked cost a booking write takes, as ReservationsService.createWithCost reads it. */
type BudgetEntry = Parameters<ReservationsService['createWithCost']>[2];
@@ -41,6 +45,7 @@ export class ReservationWritesService {
private readonly reservations: ReservationsService,
private readonly airtrailLink: AirtrailLinkService,
private readonly realtime: RealtimeService,
+ @InjectRepository(BudgetItems) private readonly budgetItems: BudgetItemsRepository,
) {}
/** The REST caller of a booking write: skips the sender's socket, as the routes always did. */
@@ -62,6 +67,35 @@ export class ReservationWritesService {
return { reservation };
}
+ /**
+ * The booking of the trip whose linked cost already holds `price` under the
+ * same booking reference, or null. One ticket covering several trains or
+ * flights is one booking with legs, priced once; its fare again under its
+ * reference usually means a segment was saved as a booking of its own at the
+ * full fare, and the trip counts the ticket twice (#2895). The reference is
+ * compared trimmed and case-insensitive, the amount in whole cents. A bare
+ * price is in the trip's currency, so the earlier cost has to be in it too:
+ * its own currency, or the trip's when it names none.
+ */
+ async bookingWithSameFare(
+ tripId: number,
+ confirmationNumber: string | undefined,
+ price: number | undefined,
+ ): Promise<{ id: number; title: string } | null> {
+ const reference = confirmationNumber?.trim().toUpperCase();
+ if (!reference || price == null || !(price > 0)) return null;
+ const minor = toMinor(price);
+ const linked = await this.budgetItems.listLinkedToReferencedReservations(tripId);
+ const tripCurrency = (cost: (typeof linked)[number]) => (cost.trip_currency?.trim() || 'EUR').toUpperCase();
+ const earlier = linked.find(
+ (cost) =>
+ cost.confirmation_number?.trim().toUpperCase() === reference &&
+ toMinor(cost.total_price) === minor &&
+ (cost.currency?.trim().toUpperCase() || tripCurrency(cost)) === tripCurrency(cost),
+ );
+ return earlier ? { id: earlier.reservation_id, title: earlier.title } : null;
+ }
+
/**
* An edit of a booking the caller already read (`current`), with the costs it
* re-files. A stay the edit wrote for the first time is `accommodation:created`,
diff --git a/server/src/nest/reservations/reservations.mcp.ts b/server/src/nest/reservations/reservations.mcp.ts
index a58a0949d..43f258ae9 100644
--- a/server/src/nest/reservations/reservations.mcp.ts
+++ b/server/src/nest/reservations/reservations.mcp.ts
@@ -117,6 +117,16 @@ function linkedCost(price: number | null | undefined, category: string | undefin
return price != null && price > 0 ? { total_price: price, category } : undefined;
}
+/** What a price field means: the whole booking's cost, written once. */
+const PRICE_FIELD =
+ 'Total cost of THIS booking, shown on it and linked in the Budget tab. A ticket covering several segments under one confirmation_number is priced once, never repeated on each segment.';
+
+/** The booking is saved either way: two tickets can cost the same under one order. The caller decides. */
+const sameFareWarning = (earlier: { id: number; title: string }) =>
+ `Booking ${earlier.id} ("${earlier.title}") already carries this price under the same confirmation_number, so the Budget tab now counts it twice. `
+ + 'If both belong to one ticket or booking, its total belongs on one of them only: segments of one journey become one booking with update_transport (endpoints[] + legs[]) and the other is deleted; '
+ + 'otherwise remove this cost with delete_budget_item. Two separate tickets at the same price need no change.';
+
// ---------------------------------------------------------------------------
// Multi-leg bookings (#1914)
//
@@ -356,7 +366,7 @@ export class ReservationsMcp {
check_in: z.string().max(10).optional().describe('Check-in time (e.g. "15:00", hotel type only)'),
check_out: z.string().max(10).optional().describe('Check-out time (e.g. "11:00", hotel type only)'),
assignment_id: idSchema.optional().describe('Link to a day assignment (restaurant, train, car, cruise, event, tour, activity, other)'),
- price: z.number().nonnegative().optional().describe('Reservation cost — shown on the booking and linked in the Budget tab'),
+ price: z.number().nonnegative().optional().describe(PRICE_FIELD),
budget_category: z.string().max(100).optional().describe('Budget category for the price entry (defaults to reservation type)'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
@@ -391,6 +401,7 @@ export class ReservationsMcp {
: undefined;
const metadata = price != null ? { price: String(price) } : undefined;
+ const sameFare = await this.writes.bookingWithSameFare(tripId, confirmation_number, price);
// The booking and its linked cost are one write, the same use case REST runs.
const { reservation } = await this.writes.createBooking(String(tripId), {
@@ -399,7 +410,7 @@ export class ReservationsMcp {
create_accommodation: createAccommodation,
metadata,
}, linkedCost(price, budget_category || type), await this.guards.tripWriter(tripId, ctx.userId));
- return ok({ reservation });
+ return ok(sameFare ? { reservation, warning: sameFareWarning(sameFare) } : { reservation });
}
@Tool({
@@ -643,7 +654,9 @@ export class ReservationsMcp {
@Tool({
name: 'create_transport',
- description: 'Create a transport booking for a trip: flight, train, bus, car, taxi, bicycle, cruise, ferry or transport_other. For scheduled public transit use create_transit_journey, which attaches the provider itinerary. Use endpoints[] to record origin/destination and intermediate stops; for flights, set code to the IATA airport code (use search_airports first). For a booking WITH STOPOVERS also pass legs[] (one entry per segment, one fewer than endpoints[]), otherwise every segment inherits the stop time as both its arrival and its departure. The top-level confirmation_number is the booking reference; when a single segment was booked under its own reference, put that one on the leg instead. Created as pending, so confirm it with update_transport. Set price to record the cost; it will appear on the booking and in the Budget tab.',
+ description: 'Create a transport booking for a trip: flight, train, bus, car, taxi, bicycle, cruise, ferry or transport_other. For scheduled public transit use create_transit_journey, which attaches the provider itinerary. Use endpoints[] to record origin/destination and intermediate stops; for flights, set code to the IATA airport code (use search_airports first). For a booking WITH STOPOVERS also pass legs[] (one entry per segment, one fewer than endpoints[]), otherwise every segment inherits the stop time as both its arrival and its departure. The top-level confirmation_number is the booking reference; when a single segment was booked under its own reference, put that one on the leg instead. '
+ + 'A ticket covering several flights or trains under one reference is ONE create_transport with endpoints[] + legs[], never one call per segment: each call books its price as a cost of its own, so a fare repeated per segment is counted once per segment. '
+ + 'Created as pending, so confirm it with update_transport. Set price to record the cost; it will appear on the booking and in the Budget tab.',
inputSchema: {
tripId: idSchema,
type: z.enum(CREATABLE_TRANSPORT_TYPES),
@@ -660,7 +673,7 @@ export class ReservationsMcp {
endpoints: endpointSchema,
legs: legsSchema,
needs_review: z.boolean().optional(),
- price: z.number().nonnegative().optional().describe('Transport cost — shown on the booking and linked in the Budget tab'),
+ price: z.number().nonnegative().optional().describe(PRICE_FIELD),
budget_category: z.string().max(100).optional().describe('Budget category for the price entry (defaults to transport type)'),
},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
@@ -719,6 +732,7 @@ export class ReservationsMcp {
}
if (price != null) meta.price = String(price);
+ const sameFare = await this.writes.bookingWithSameFare(tripId, confirmation_number, price);
const { reservation } = await this.writes.createBooking(String(tripId), {
title,
@@ -736,7 +750,7 @@ export class ReservationsMcp {
endpoints: transportEndpoints,
needs_review,
}, linkedCost(price, budget_category || type), await this.guards.tripWriter(tripId, ctx.userId));
- return ok({ reservation });
+ return ok(sameFare ? { reservation, warning: sameFareWarning(sameFare) } : { reservation });
}
@Tool({
diff --git a/server/tests/unit/mcp/tools-transports.test.ts b/server/tests/unit/mcp/tools-transports.test.ts
index 6254979a4..5e7ffd953 100644
--- a/server/tests/unit/mcp/tools-transports.test.ts
+++ b/server/tests/unit/mcp/tools-transports.test.ts
@@ -9,6 +9,7 @@ import { BudgetItems } from '../../../src/db/entities/BudgetItems.entity';
import { DayAccommodations } from '../../../src/db/entities/DayAccommodations.entity';
import { ReservationEndpoints } from '../../../src/db/entities/ReservationEndpoints.entity';
import { Reservations } from '../../../src/db/entities/Reservations.entity';
+import { Trips } from '../../../src/db/entities/Trips.entity';
import { AirtrailLinkService } from '../../../src/nest/integrations/airtrail-link.service';
import { ReservationsService } from '../../../src/nest/reservations/reservations.service';
import { createUser, createTrip, createDay, createPlace } from '../../helpers/factories';
@@ -368,6 +369,123 @@ describe('Transport tools: the price link', () => {
});
});
+ it('books a ticket with changes as one booking with legs and one cost (#2895)', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ await withHarness(user.id, async (h) => {
+ const result = await h.client.callTool({
+ name: 'create_transport',
+ arguments: {
+ tripId: trip.id,
+ type: 'train',
+ title: 'Klagenfurt → Ludwigshafen',
+ confirmation_number: '297199016704',
+ price: 71.67,
+ endpoints: [
+ { role: 'from', sequence: 0, name: 'Klagenfurt West', lat: 46.63, lng: 14.28 },
+ { role: 'stop', sequence: 1, name: 'Villach Hbf', lat: 46.62, lng: 13.85 },
+ { role: 'to', sequence: 2, name: 'Heidelberg Hbf', lat: 49.4, lng: 8.67 },
+ ],
+ legs: [
+ { train_number: 'S1', dep_time: '06:12', arr_time: '06:30' },
+ { train_number: 'ICE 116', dep_time: '06:40', arr_time: '12:50' },
+ ],
+ },
+ });
+ const data = parseToolResult(result) as { reservation: { id: number }; warning?: string };
+
+ expect(data.warning).toBeUndefined();
+ expect(await countRows(orm, BudgetItems, { trip: trip.id })).toBe(1);
+ expect(await findRow(orm, BudgetItems, { reservation: data.reservation.id })).toMatchObject({
+ total_price: 71.67,
+ });
+ });
+ });
+
+ it('warns when a segment repeats the fare an earlier booking of the same reference carries (#2895)', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ await withHarness(user.id, async (h) => {
+ const segment = (title: string, extra: Record<string, unknown> = {}) =>
+ h.client.callTool({
+ name: 'create_transport',
+ arguments: {
+ tripId: trip.id,
+ type: 'train',
+ title,
+ confirmation_number: '297199016704',
+ price: 71.67,
+ ...extra,
+ },
+ });
+ const first = parseToolResult(await segment('S1 Klagenfurt West → Villach')) as {
+ reservation: { id: number };
+ warning?: string;
+ };
+ expect(first.warning).toBeUndefined();
+
+ const second = parseToolResult(
+ await segment('ICE 116 Villach → Heidelberg', { confirmation_number: ' 297199016704 ' }),
+ ) as { reservation: { id: number }; warning?: string };
+ expect(second.warning).toContain(`Booking ${first.reservation.id} ("S1 Klagenfurt West → Villach")`);
+ expect(second.warning).toContain('update_transport');
+ // Still saved with its cost: two tickets can cost the same under one order.
+ expect(await findRow(orm, BudgetItems, { reservation: second.reservation.id })).toMatchObject({
+ total_price: 71.67,
+ });
+
+ const otherFare = parseToolResult(await segment('S1 Heidelberg → Ludwigshafen', { price: 12.4 })) as {
+ warning?: string;
+ };
+ expect(otherFare.warning).toBeUndefined();
+ const otherReference = parseToolResult(await segment('STR 7', { confirmation_number: 'OTHER-1' })) as {
+ warning?: string;
+ };
+ expect(otherReference.warning).toBeUndefined();
+ const unpriced = parseToolResult(await segment('Bus', { price: undefined })) as { warning?: string };
+ expect(unpriced.warning).toBeUndefined();
+ });
+ });
+
+ it('compares an earlier cost in its own currency against the trip currency after a switch', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ await withHarness(user.id, async (h) => {
+ const segment = (title: string) =>
+ h.client.callTool({
+ name: 'create_transport',
+ arguments: { tripId: trip.id, type: 'train', title, confirmation_number: 'SBB-9', price: 48 },
+ });
+ const first = parseToolResult(await segment('IC 1 Zürich → Bern')) as { reservation: { id: number } };
+
+ // A switch to CHF pins the earlier cost to the outgoing EUR: a bare 48 now is francs.
+ await updateRows(orm, Trips, { id: trip.id }, { currency: 'CHF' });
+ await updateRows(orm, BudgetItems, { reservation: first.reservation.id }, { currency: 'EUR' });
+ expect((parseToolResult(await segment('IC 8 Bern → Brig')) as { warning?: string }).warning).toBeUndefined();
+
+ // The same cost written in the trip's own currency is the same fare, and as the
+ // oldest it is the one named ahead of the bare-priced IC 8.
+ await updateRows(orm, BudgetItems, { reservation: first.reservation.id }, { currency: 'chf' });
+ expect((parseToolResult(await segment('IR 90 Brig → Sion')) as { warning?: string }).warning).toContain(
+ `Booking ${first.reservation.id} ("IC 1 Zürich → Bern")`,
+ );
+ });
+ });
+
+ it('create_reservation warns about a repeated booking total too', async () => {
+ const { user } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ await withHarness(user.id, async (h) => {
+ const room = (title: string) =>
+ h.client.callTool({
+ name: 'create_reservation',
+ arguments: { tripId: trip.id, type: 'other', title, confirmation_number: 'HX-77', price: 300 },
+ });
+ expect((parseToolResult(await room('Room 1')) as { warning?: string }).warning).toBeUndefined();
+ expect((parseToolResult(await room('Room 2')) as { warning?: string }).warning).toContain('delete_budget_item');
+ });
+ });
+
it('create_transport with price 0 records no budget item', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
diff --git a/server/tests/unit/nest/booking-import/booking-import.service.test.ts b/server/tests/unit/nest/booking-import/booking-import.service.test.ts
index fa4ec90f4..232a995bf 100644
--- a/server/tests/unit/nest/booking-import/booking-import.service.test.ts
+++ b/server/tests/unit/nest/booking-import/booking-import.service.test.ts
@@ -393,6 +393,44 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
]);
});
+ it('BOOKING-IMPORT-2895: a rail ticket with changes is one booking with one cost for its fare', async () => {
+ const segment = (from: string, to: string, departureTime: string, arrivalTime: string, trainNumber: string) => ({
+ '@type': 'TrainReservation',
+ reservationNumber: '297199016704',
+ totalPrice: 71.67,
+ priceCurrency: 'EUR',
+ reservationFor: {
+ trainNumber,
+ departureStation: { name: from },
+ arrivalStation: { name: to },
+ departureTime,
+ arrivalTime,
+ },
+ });
+ const { svc: importer, maps } = make({
+ kit: true,
+ extract: async () => [
+ segment('Klagenfurt West', 'Villach Hbf', '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ segment('Villach Hbf', 'Heidelberg Hbf', '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
+ segment('Heidelberg Hbf', 'Ludwigshafen (Rhein) Mitte', '2026-07-04T13:05:00', '2026-07-04T13:30:00', 'S1'),
+ segment('Ludwigshafen (Rhein) Mitte', 'Rheingönheim', '2026-07-04T13:41:00', '2026-07-04T13:55:00', 'STR 7'),
+ ],
+ });
+ maps.geocodeQuery.mockResolvedValue({ lat: 49, lng: 8 });
+ const preview = await importer.preview([file('ticket.pdf')], 'no-ai', 1);
+ expect(preview.items).toHaveLength(1);
+
+ const { svc, reservations } = makeConfirm({ freeze: async () => {} });
+ await svc.confirm('5', preview.items as never, undefined);
+
+ expect(reservations.createWithCostInTx).toHaveBeenCalledTimes(1);
+ expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
+ '5',
+ expect.objectContaining({ type: 'train', confirmation_number: '297199016704' }),
+ { total_price: 71.67, category: 'transport', currency: 'EUR' },
+ );
+ });
+
it('BOOKING-IMPORT-TX-002: no cost without the Costs addon or without a price', async () => {
const off = makeConfirm({ budget: false });
await off.svc.confirm('5', [priced as never], undefined);
diff --git a/server/tests/unit/nest/booking-import/kitineraryMapper.test.ts b/server/tests/unit/nest/booking-import/kitineraryMapper.test.ts
index 26c514ae9..3cc5c7c4f 100644
--- a/server/tests/unit/nest/booking-import/kitineraryMapper.test.ts
+++ b/server/tests/unit/nest/booking-import/kitineraryMapper.test.ts
@@ -59,6 +59,8 @@ describe('kitinerary mapper — multi-leg flight grouping', () => {
expect(booking.endpoints).toHaveLength(3);
expect(booking.endpoints!.map((e) => e.role)).toEqual(['from', 'stop', 'to']);
expect(booking.endpoints!.map((e) => e.sequence)).toEqual([0, 1, 2]);
+ // The stop carries the onward departure, as the planner form and the MCP tools write it.
+ expect(booking.endpoints!.map((e) => e.local_time)).toEqual(['10:00', '14:30', '23:30']);
const meta = booking.metadata as any;
expect(meta.legs).toHaveLength(2);
expect(meta.legs[0]).toMatchObject({ from: 'FRA', to: 'BER', flight_number: 'LH 100' });
@@ -181,6 +183,38 @@ describe('kitinerary mapper — multi-leg flight grouping', () => {
expect((items[0].endpoints ?? []).map((e) => e.role)).toEqual(['from', 'stop', 'stop', 'to']);
});
+ it('prices a connection once when every leg repeats the booking total', () => {
+ const priced = (node: ReturnType<typeof flight>) => ({ ...node, totalPrice: 412.5, priceCurrency: 'EUR' });
+ const { items } = mapReservations(
+ legs(
+ priced(flight('PX1', FRA, BER, '2026-06-11T08:00:00', '2026-06-11T09:00:00', 'LH 60')),
+ priced(flight('PX1', BER, HND, '2026-06-11T11:00:00', '2026-06-12T06:00:00', 'LH 61')),
+ ),
+ 'test.json',
+ );
+
+ expect(items).toHaveLength(1);
+ expect(items[0].metadata).toMatchObject({ price: 412.5, priceCurrency: 'EUR' });
+ });
+
+ it('adds the fares of a connection that are priced per leg', () => {
+ const priced = (node: ReturnType<typeof flight>, totalPrice: number) => ({
+ ...node,
+ totalPrice,
+ priceCurrency: 'EUR',
+ });
+ const { items } = mapReservations(
+ legs(
+ priced(flight('PX2', FRA, BER, '2026-06-11T08:00:00', '2026-06-11T09:00:00', 'LH 62'), 89.9),
+ priced(flight('PX2', BER, HND, '2026-06-11T11:00:00', '2026-06-12T06:00:00', 'LH 63'), 612.35),
+ ),
+ 'test.json',
+ );
+
+ expect(items).toHaveLength(1);
+ expect(items[0].metadata).toMatchObject({ price: 702.25, priceCurrency: 'EUR' });
+ });
+
it('leaves a single flight unchanged (two endpoints, no legs array)', () => {
const { items } = mapReservations(
[flight('S1', FRA, BER, '2026-06-11T10:00:00', '2026-06-11T12:00:00', 'LH 1')] as any,
@@ -193,6 +227,267 @@ describe('kitinerary mapper — multi-leg flight grouping', () => {
});
});
+const station = (name: string, lat?: number, lng?: number) => ({
+ name,
+ ...(lat === undefined ? {} : { geo: { latitude: lat, longitude: lng } }),
+});
+
+const KLU = station('Klagenfurt West', 46.63, 14.28);
+const VIL = station('Villach Hbf', 46.62, 13.85);
+const HD = station('Heidelberg Hbf', 49.4, 8.67);
+const LU = station('Ludwigshafen (Rhein) Mitte', 49.48, 8.43);
+const RHG = station('Rheingönheim', 49.44, 8.42);
+
+const train = (
+ ref: string,
+ dep: ReturnType<typeof station>,
+ arr: ReturnType<typeof station>,
+ depTime: string,
+ arrTime: string,
+ trainNumber: string,
+ extra: Record<string, unknown> = { totalPrice: 71.67, priceCurrency: 'EUR' },
+) => ({
+ '@type': 'TrainReservation',
+ reservationNumber: ref,
+ ...extra,
+ reservationFor: {
+ trainNumber,
+ departureStation: dep,
+ arrivalStation: arr,
+ departureTime: depTime,
+ arrivalTime: arrTime,
+ },
+});
+
+/** The ticket from the report: four segments under one reference, the fare repeated on each. */
+const ticket = (fares?: Record<string, unknown>[]) =>
+ [
+ train('297199016704', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1', fares?.[0]),
+ train('297199016704', VIL, HD, '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116', fares?.[1]),
+ train('297199016704', HD, LU, '2026-07-04T13:05:00', '2026-07-04T13:30:00', 'S1', fares?.[2]),
+ train('297199016704', LU, RHG, '2026-07-04T13:41:00', '2026-07-04T13:55:00', 'STR 7', fares?.[3]),
+ ] as unknown as KiReservation[];
+
+describe('kitinerary mapper: a rail ticket with changes (#2895)', () => {
+ it('maps the connecting segments of one ticket to one train booking priced once', () => {
+ const { items, warnings } = mapReservations(ticket(), 'db-ticket.pdf');
+
+ expect(warnings).toEqual([]);
+ expect(items).toHaveLength(1);
+ const [booking] = items;
+ expect(booking).toMatchObject({
+ type: 'train',
+ title: 'Train Klagenfurt West → Rheingönheim',
+ confirmation_number: '297199016704',
+ reservation_time: '2026-07-04T06:12:00',
+ reservation_end_time: '2026-07-04T13:55:00',
+ needs_review: false,
+ source: { fileName: 'db-ticket.pdf', index: 0 },
+ });
+ expect(booking.endpoints!.map((e) => [e.role, e.sequence, e.name, e.local_time])).toEqual([
+ ['from', 0, 'Klagenfurt West', '06:12'],
+ ['stop', 1, 'Villach Hbf', '06:40'],
+ ['stop', 2, 'Heidelberg Hbf', '13:05'],
+ ['stop', 3, 'Ludwigshafen (Rhein) Mitte', '13:41'],
+ ['to', 4, 'Rheingönheim', '13:55'],
+ ]);
+ const metadata = booking.metadata as { legs: Record<string, unknown>[]; price: unknown; train_number: string };
+ expect(metadata.price).toBe(71.67);
+ expect(metadata.train_number).toBe('S1');
+ expect(metadata.legs).toHaveLength(4);
+ expect(metadata.legs[1]).toEqual({
+ from: 'Villach Hbf',
+ to: 'Heidelberg Hbf',
+ train_number: 'ICE 116',
+ dep_time: '06:40',
+ arr_time: '12:50',
+ });
+ });
+
+ it('adds fares that differ per segment in whole cents', () => {
+ const fare = (totalPrice: number) => ({ totalPrice, priceCurrency: 'EUR' });
+ const { items } = mapReservations(ticket([fare(0.1), fare(0.2), fare(50), fare(21.37)]), 'db-ticket.pdf');
+
+ expect(items).toHaveLength(1);
+ expect((items[0].metadata as { price: unknown }).price).toBe(71.67);
+ });
+
+ it('keeps the first price when the segments are priced in different currencies', () => {
+ const { items } = mapReservations(
+ ticket([{ totalPrice: 20, priceCurrency: 'EUR' }, { totalPrice: 30, priceCurrency: 'CHF' }, {}, {}]),
+ 'mixed.pdf',
+ );
+
+ expect(items[0].metadata).toMatchObject({ price: 20, priceCurrency: 'EUR' });
+ });
+
+ it('joins a change of station spelling at the same place by its position', () => {
+ const villachArrival = station('Villach Hauptbahnhof', 46.6205, 13.8505);
+ const { items } = mapReservations(
+ [
+ train('R1', KLU, villachArrival, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('R1', VIL, HD, '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
+ ] as unknown as KiReservation[],
+ 'spelling.pdf',
+ );
+
+ expect(items).toHaveLength(1);
+ });
+
+ it('keeps segments under different references, days apart, or out and back as separate bookings', () => {
+ const separate = (nodes: ReturnType<typeof train>[]) =>
+ mapReservations(nodes as unknown as KiReservation[], 'x.pdf').items.length;
+
+ expect(
+ separate([
+ train('A1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('B2', VIL, HD, '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
+ ]),
+ ).toBe(2);
+ expect(
+ separate([
+ train('A1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('A1', VIL, HD, '2026-07-08T06:40:00', '2026-07-08T12:50:00', 'ICE 116'),
+ ]),
+ ).toBe(2);
+ expect(
+ separate([
+ train('A1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('A1', VIL, KLU, '2026-07-04T18:00:00', '2026-07-04T18:20:00', 'S1'),
+ ]),
+ ).toBe(2);
+ expect(
+ separate([
+ train('A1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('A1', HD, LU, '2026-07-04T13:05:00', '2026-07-04T13:30:00', 'S1'),
+ ]),
+ ).toBe(2);
+ });
+
+ it('books the total of an outward and a return trip under one order number once', () => {
+ const { items } = mapReservations(
+ [
+ train('ORDER1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('ORDER1', VIL, HD, '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
+ train('ORDER1', HD, VIL, '2026-07-11T07:10:00', '2026-07-11T13:20:00', 'ICE 117'),
+ train('ORDER1', VIL, KLU, '2026-07-11T13:35:00', '2026-07-11T13:53:00', 'S1'),
+ ] as unknown as KiReservation[],
+ 'return.pdf',
+ );
+
+ expect(items).toHaveLength(2);
+ expect(items.map((item) => item.metadata?.price ?? null)).toEqual([71.67, null]);
+ expect(items[1].metadata).not.toHaveProperty('priceCurrency');
+ });
+
+ it('books the total once where a change of station names it two ways and gives no position', () => {
+ const { items } = mapReservations(
+ [
+ train('297199016704', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('297199016704', VIL, HD, '2026-07-04T06:40:00', '2026-07-04T12:50:00', 'ICE 116'),
+ train(
+ '297199016704',
+ HD,
+ station('Ludwigshafen(Rhein) Mitte'),
+ '2026-07-04T13:05:00',
+ '2026-07-04T13:30:00',
+ 'S1',
+ ),
+ train('297199016704', station('LU Mitte'), RHG, '2026-07-04T13:41:00', '2026-07-04T13:55:00', 'STR 7'),
+ ] as unknown as KiReservation[],
+ 'db-ticket.pdf',
+ );
+
+ expect(items.map((item) => (item.metadata?.legs as unknown[] | undefined)?.length ?? 1)).toEqual([3, 1]);
+ expect(items.map((item) => item.metadata?.price ?? null)).toEqual([71.67, null]);
+ });
+
+ it('leaves equal fares on separate bookings to the AI import, whose own net reads the document', () => {
+ const { items } = mapReservations(
+ [
+ train('ORDER1', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ train('ORDER1', VIL, KLU, '2026-07-11T13:35:00', '2026-07-11T13:53:00', 'S1'),
+ ] as unknown as KiReservation[],
+ 'return.pdf',
+ 'ai',
+ );
+
+ expect(items.map((item) => item.metadata?.price)).toEqual([71.67, 71.67]);
+ });
+
+ it('keeps the price on the first to depart when the document lists the return first', () => {
+ const { items } = mapReservations(
+ [
+ train('ORDER2', VIL, KLU, '2026-07-11T13:35:00', '2026-07-11T13:53:00', 'S1'),
+ train('ORDER2', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1'),
+ ] as unknown as KiReservation[],
+ 'return.pdf',
+ );
+
+ expect(items.map((item) => item.metadata?.price ?? null)).toEqual([null, 71.67]);
+ });
+
+ it('leaves fares that differ between the bookings of one order alone', () => {
+ const fare = (totalPrice: number) => ({ totalPrice, priceCurrency: 'EUR' });
+ const { items } = mapReservations(
+ [
+ train('ORDER3', KLU, VIL, '2026-07-04T06:12:00', '2026-07-04T06:30:00', 'S1', fare(9.8)),
+ train('ORDER3', VIL, KLU, '2026-07-11T13:35:00', '2026-07-11T13:53:00', 'S1', fare(12.4)),
+ ] as unknown as KiReservation[],
+ 'return.pdf',
+ );
+
+ expect(items.map((item) => item.metadata?.price)).toEqual([9.8, 12.4]);
+ });
+
+ it('leaves a single train as it was, with two endpoints and no legs', () => {
+ const { items } = mapReservations(ticket().slice(1, 2), 'one.pdf');
+
+ expect(items).toHaveLength(1);
+ expect(items[0]).toMatchObject({ title: 'ICE 116 (Villach Hbf → Heidelberg Hbf)' });
+ expect(items[0].endpoints).toHaveLength(2);
+ expect(items[0].metadata).toEqual({ train_number: 'ICE 116', price: 71.67, priceCurrency: 'EUR' });
+ });
+
+ it('keeps the segments of a bus or boat ticket apart but books the repeated total once', () => {
+ const asType = (type: string, nodes: KiReservation[]) =>
+ nodes.map((node) => {
+ const trip = node.reservationFor as Record<string, unknown>;
+ const reservationFor =
+ type === 'BusReservation'
+ ? {
+ ...trip,
+ departureBusStop: trip.departureStation,
+ arrivalBusStop: trip.arrivalStation,
+ busNumber: 'FlixBus',
+ }
+ : { ...trip, departureBoatTerminal: trip.departureStation, arrivalBoatTerminal: trip.arrivalStation };
+ return { ...node, '@type': type, reservationFor };
+ });
+
+ for (const type of ['BusReservation', 'BoatReservation']) {
+ const { items } = mapReservations(asType(type, ticket().slice(0, 2)), 'coach.pdf');
+ expect(items).toHaveLength(2);
+ expect(items.map((item) => item.metadata?.price ?? null)).toEqual([71.67, null]);
+ expect(items.map((item) => item.source.index)).toEqual([0, 1]);
+ }
+
+ const fares = ticket([{ totalPrice: 10 }, { totalPrice: 25 }]).slice(0, 2);
+ const { items } = mapReservations(asType('BusReservation', fares), 'coach.pdf');
+ expect(items.map((item) => item.metadata?.price)).toEqual([10, 25]);
+ });
+
+ it('does not read a type named after an object property as transport', () => {
+ const { items, warnings } = mapReservations(
+ [{ '@type': 'constructor', reservationFor: {} }] as unknown as KiReservation[],
+ 'odd.json',
+ );
+
+ expect(items).toEqual([]);
+ expect(warnings).toEqual(['Unknown type "constructor" in odd.json[0] — skipped']);
+ });
+});
+
describe('kitinerary mapper — printed 12-hour clocks (#2094)', () => {
it('reads a PM arrival as the afternoon instead of slicing the meridiem off', () => {
// Before the fix the endpoint carried '01:11', because splitIso sliced
diff --git a/server/tests/unit/nest/llm-parse/booking-total.test.ts b/server/tests/unit/nest/llm-parse/booking-total.test.ts
index 8f374ff07..054089cfa 100644
--- a/server/tests/unit/nest/llm-parse/booking-total.test.ts
+++ b/server/tests/unit/nest/llm-parse/booking-total.test.ts
@@ -88,3 +88,44 @@ describe('collapseRepeatedBookingTotal (#2651)', () => {
expect(collapseRepeatedBookingTotal([nodes[0]], TOTAL)).toEqual([nodes[0]]);
});
});
+
+describe('collapseRepeatedBookingTotal on a ticket with changes (#2895)', () => {
+ const rail = (train: string, departureTime: string, price?: unknown, type = 'TrainReservation') =>
+ leg(train, departureTime, price, { currency: 'EUR', ref: '297199016704', type });
+ const TICKET = 'Ihre Fahrkarte\nGesamtpreis 71,67 EUR';
+
+ it('keeps the total a rail ticket repeats on every segment on the first train only', () => {
+ const nodes = [
+ rail('S1', '2026-07-04T06:12', 71.67),
+ rail('ICE 116', '2026-07-04T06:40', 71.67),
+ rail('S1', '2026-07-04T13:05', 71.67),
+ rail('STR 7', '2026-07-04T13:41', 71.67),
+ ];
+ expect(prices(collapseRepeatedBookingTotal(nodes, TICKET))).toEqual([71.67, null, null, null]);
+ });
+
+ it('does the same for a bus or a ferry ticket', () => {
+ for (const type of ['BusReservation', 'BoatReservation']) {
+ const nodes = [rail('A', '2026-07-04T06:12', 71.67, type), rail('B', '2026-07-04T09:00', 71.67, type)];
+ expect(prices(collapseRepeatedBookingTotal(nodes, TICKET))).toEqual([71.67, null]);
+ }
+ });
+
+ it('books a total once across a ticket that mixes kinds of transport', () => {
+ const railAndFly = [
+ rail('LH 1', '2026-07-04T13:05', 71.67, 'FlightReservation'),
+ rail('ICE 116', '2026-07-04T06:40', 71.67),
+ ];
+ expect(prices(collapseRepeatedBookingTotal(railAndFly, TICKET))).toEqual([null, 71.67]);
+ const replacementBus = [
+ rail('ICE 116', '2026-07-04T06:40', 71.67),
+ rail('SEV', '2026-07-04T13:05', 71.67, 'BusReservation'),
+ ];
+ expect(prices(collapseRepeatedBookingTotal(replacementBus, TICKET))).toEqual([71.67, null]);
+ });
+
+ it('leaves fares that differ alone', () => {
+ const fares = [rail('S1', '2026-07-04T06:12', 10), rail('ICE 116', '2026-07-04T06:40', 61.67)];
+ expect(collapseRepeatedBookingTotal(fares, TICKET)).toBe(fares);
+ });
+});