mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
fix(planner): show each leg's own seat on a flight with a stop
A flight with a stop saves a seat per leg, but every view read the booking's flat seat, which mirrors the first leg's. A seat booked only on the second leg showed nowhere, and one on the first leg showed on both. #2701 had fixed this for trains only. - getFlightLegs reads each leg's seat. The first leg falls back to the flat seat, as the booking form does, so older bookings keep theirs. - bookingSeats lists the seat of every leg that has one with its route. The detail views (the phone's transport sheet, the booking detail) show each leg's seat under its route ("Seat IST → JFK"), and a leg opened from its row shows its own. The cards and field grids (bookings list, the phone's Bookings and Transports tabs, the day plan's transport detail) hold every seat in one Seat cell, so a card cutting its grid short no longer drops the second leg's. - The day plan's flight leg rows carry their seat, and the PDF prints a flight's seat, direct or per leg. - The watch and the Wallet pass name every leg's seat of the booking. - A legs-only MCP update sets the flat seat, airline and flight number (train number and platform) again from the new first leg, so a seat taken off the first leg does not live on in the flat seat. A flat value the stored first leg never had stays. Closes #3158
This commit is contained in:
@@ -192,6 +192,23 @@ describe('getTransportForDay', () => {
|
||||
expect(day2[0].__leg).toMatchObject({ index: 1, from: 'Frankfurt', to: 'München', train_number: 'ICE 500', platform: '9' })
|
||||
})
|
||||
|
||||
it('gives each leg of a flight with a stop its own seat, the first leg the flat one when it has none', () => {
|
||||
const flight = (legs: object[], seat?: string) => [{
|
||||
id: 42, type: 'flight', day_id: 1, end_day_id: 1,
|
||||
metadata: JSON.stringify({ ...(seat ? { seat } : {}), legs }),
|
||||
}]
|
||||
const legs = [
|
||||
{ from: 'MXP', to: 'IST', dep_day_id: 1, dep_time: '11:00', arr_day_id: 1, arr_time: '15:10' },
|
||||
{ from: 'IST', to: 'JFK', seat: '14C', dep_day_id: 1, dep_time: '17:05', arr_day_id: 1, arr_time: '20:40' },
|
||||
]
|
||||
const rows = getTransportForDay({ reservations: flight(legs), dayId: 1, dayAssignmentIds: [], days })
|
||||
expect(rows.map(r => r.__leg.seat)).toEqual([null, '14C'])
|
||||
// A booking saved before legs had seats keeps its flat seat on the first leg only.
|
||||
const unseated = [{ ...legs[0] }, { ...legs[1], seat: undefined }]
|
||||
const legacy = getTransportForDay({ reservations: flight(unseated, '3A'), dayId: 1, dayAssignmentIds: [], days })
|
||||
expect(legacy.map(r => r.__leg.seat)).toEqual(['3A', null])
|
||||
})
|
||||
|
||||
it('leaves a single-leg train untouched (no __leg)', () => {
|
||||
const reservations = [{ id: 41, type: 'train', day_id: 1, end_day_id: 1, metadata: JSON.stringify({ train_number: 'RE 1' }) }]
|
||||
const rows = getTransportForDay({ reservations, dayId: 1, dayAssignmentIds: [], days })
|
||||
|
||||
@@ -156,7 +156,12 @@ function parseMultiLegs(r: any): any[] | null {
|
||||
// Defensive: recover metadata that was accidentally double-encoded by an earlier
|
||||
// bug (a JSON string of a JSON string) so already-saved bookings heal on read.
|
||||
if (typeof meta === 'string') { try { meta = JSON.parse(meta || '{}') } catch { meta = {} } }
|
||||
if (meta && Array.isArray(meta.legs) && meta.legs.length > 1) return meta.legs
|
||||
if (meta && Array.isArray(meta.legs) && meta.legs.length > 1) {
|
||||
// The flat seat mirrors the first leg's, so a booking saved before legs had
|
||||
// seats keeps it on its first leg (flightLegs' legSeat reads it the same way).
|
||||
const flatSeat = typeof meta.seat === 'string' && meta.seat ? meta.seat : null
|
||||
return meta.legs.map((l: Record<string, unknown> | null, i: number) => (i === 0 && flatSeat && !l?.seat ? { ...l, seat: flatSeat } : l))
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -200,9 +205,10 @@ export function expandFlightLegsForDay(
|
||||
index: i, total: legs.length,
|
||||
from: leg.from ?? null, to: leg.to ?? null,
|
||||
airline: leg.airline ?? null, flight_number: leg.flight_number ?? null,
|
||||
// Train legs carry their own per-leg detail; added only for trains so a
|
||||
// flight's __leg object stays byte-identical to before.
|
||||
...(r.type === 'train' ? { train_number: leg.train_number ?? null, platform: leg.platform ?? null, seat: leg.seat ?? null } : {}),
|
||||
// Each leg's own seat, a flight's too: it is saved per leg, and reading the
|
||||
// booking's flat seat showed the first leg's on every leg.
|
||||
seat: leg.seat ?? null,
|
||||
...(r.type === 'train' ? { train_number: leg.train_number ?? null, platform: leg.platform ?? null } : {}),
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { getFlightLegs, getTrainLegs, isMultiLegTrain, stripAirportCode, usesStationRoute } from './flightLegs'
|
||||
import { bookingSeats, getFlightLegs, getTrainLegs, isMultiLegTrain, seatCells, seatLabel, stripAirportCode, usesStationRoute } from './flightLegs'
|
||||
import type { Reservation } from '../types'
|
||||
|
||||
function res(partial: Partial<Reservation>): Reservation {
|
||||
@@ -97,6 +97,42 @@ describe('per-segment booking references (#1943)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('the seats of a booking with a stop', () => {
|
||||
const stopover = (legs: object[], extra: Record<string, unknown> = {}) =>
|
||||
res({ type: 'flight', metadata: JSON.stringify({ ...extra, legs }) })
|
||||
|
||||
it('reads each flight leg its own seat, so one booked only on the second leg is not lost', () => {
|
||||
const r = stopover([{ from: 'MXP', to: 'IST' }, { from: 'IST', to: 'JFK', seat: '14C' }])
|
||||
expect(getFlightLegs(r).map(l => l.seat)).toEqual([undefined, '14C'])
|
||||
expect(bookingSeats(r)).toEqual([{ route: 'IST → JFK', seat: '14C' }])
|
||||
})
|
||||
|
||||
it('gives the first leg the flat seat when it has none of its own, as the booking form does', () => {
|
||||
const r = stopover([{ from: 'MXP', to: 'IST' }, { from: 'IST', to: 'JFK' }], { seat: '3A' })
|
||||
expect(getFlightLegs(r).map(l => l.seat)).toEqual(['3A', undefined])
|
||||
expect(bookingSeats(r)).toEqual([{ route: 'MXP → IST', seat: '3A' }])
|
||||
})
|
||||
|
||||
it('lists a train with a stop leg by leg too', () => {
|
||||
const r = res({ metadata: JSON.stringify({ legs: [{ from: 'Takayama', to: 'Nagoya', seat: '10-4C' }, { from: 'Nagoya', to: 'Kyoto', seat: '4-5A' }] }) })
|
||||
expect(bookingSeats(r)).toEqual([{ route: 'Takayama → Nagoya', seat: '10-4C' }, { route: 'Nagoya → Kyoto', seat: '4-5A' }])
|
||||
})
|
||||
|
||||
it('gives a booking of one leg, of any type, its own seat without a route', () => {
|
||||
expect(bookingSeats(res({ type: 'flight', metadata: JSON.stringify({ seat: '31A' }) }))).toEqual([{ route: null, seat: '31A' }])
|
||||
expect(bookingSeats(res({ type: 'event', metadata: JSON.stringify({ seat: 'Row 4' }) }))).toEqual([{ route: null, seat: 'Row 4' }])
|
||||
expect(bookingSeats(res({ type: 'flight', metadata: null }))).toEqual([])
|
||||
})
|
||||
|
||||
it('labels a leg seat with its route and a booking seat plainly', () => {
|
||||
expect(seatLabel('Seat', 'IST → JFK')).toBe('Seat IST → JFK')
|
||||
expect(seatLabel('Seat', null)).toBe('Seat')
|
||||
const r = stopover([{ from: 'MXP', to: 'IST', seat: '3A' }, { from: 'IST', to: 'JFK', seat: '14C' }])
|
||||
expect(seatCells(r, 'Seat', ' · Economy')).toEqual([{ label: 'Seat', value: '3A, 14C · Economy' }])
|
||||
expect(seatCells(res({ type: 'flight', metadata: null }), 'Seat')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('stripAirportCode', () => {
|
||||
it('drops the code the forms and the airport backfill append, and nothing else', () => {
|
||||
expect(stripAirportCode('Paris Charles de Gaulle (CDG)')).toBe('Paris Charles de Gaulle')
|
||||
|
||||
@@ -19,6 +19,8 @@ export interface FlightLeg {
|
||||
to: string | null
|
||||
airline?: string
|
||||
flight_number?: string
|
||||
/** This leg's own seat; the first leg falls back to the booking's flat seat (see legSeat). */
|
||||
seat?: string
|
||||
/** This segment's own booking reference, when it was booked separately (#1943). */
|
||||
confirmation_number?: string
|
||||
dep_day_id?: number | null
|
||||
@@ -67,11 +69,12 @@ export function stripAirportCode(name: string): string {
|
||||
export function getFlightLegs(r: Reservation): FlightLeg[] {
|
||||
const meta = parseReservationMetadata(r)
|
||||
if (Array.isArray(meta.legs) && meta.legs.length > 0) {
|
||||
return meta.legs.map((l: any): FlightLeg => ({
|
||||
return meta.legs.map((l: any, i: number): FlightLeg => ({
|
||||
from: l.from ?? null,
|
||||
to: l.to ?? null,
|
||||
airline: l.airline || undefined,
|
||||
flight_number: l.flight_number || undefined,
|
||||
seat: legSeat(l, i, meta),
|
||||
confirmation_number: l.confirmation_number || undefined,
|
||||
dep_day_id: l.dep_day_id ?? null,
|
||||
dep_time: l.dep_time ?? null,
|
||||
@@ -91,6 +94,7 @@ export function getFlightLegs(r: Reservation): FlightLeg[] {
|
||||
to: toCode,
|
||||
airline: meta.airline || undefined,
|
||||
flight_number: meta.flight_number || undefined,
|
||||
seat: meta.seat || undefined,
|
||||
// The single segment IS the booking, so its reference is the booking's.
|
||||
confirmation_number: r.confirmation_number || undefined,
|
||||
dep_day_id: r.day_id ?? null,
|
||||
@@ -127,12 +131,12 @@ export interface TrainLeg {
|
||||
export function getTrainLegs(r: Reservation): TrainLeg[] {
|
||||
const meta = parseReservationMetadata(r)
|
||||
if (Array.isArray(meta.legs) && meta.legs.length > 0) {
|
||||
return meta.legs.map((l: any): TrainLeg => ({
|
||||
return meta.legs.map((l: any, i: number): TrainLeg => ({
|
||||
from: l.from ?? null,
|
||||
to: l.to ?? null,
|
||||
train_number: l.train_number || undefined,
|
||||
platform: l.platform || undefined,
|
||||
seat: l.seat || undefined,
|
||||
seat: legSeat(l, i, meta),
|
||||
confirmation_number: l.confirmation_number || undefined,
|
||||
dep_day_id: l.dep_day_id ?? null,
|
||||
dep_time: l.dep_time ?? null,
|
||||
@@ -161,6 +165,48 @@ export function getTrainLegs(r: Reservation): TrainLeg[] {
|
||||
}]
|
||||
}
|
||||
|
||||
/**
|
||||
* A leg's own seat. The flat `metadata.seat` mirrors the first leg's, so a
|
||||
* booking saved before legs carried seats, or one an import gave only the flat
|
||||
* seat, still shows it on its first leg, as the booking form reads it.
|
||||
*/
|
||||
function legSeat(leg: { seat?: unknown } | null | undefined, index: number, meta: { seat?: unknown }): string | undefined {
|
||||
if (typeof leg?.seat === 'string' && leg.seat) return leg.seat
|
||||
return index === 0 && typeof meta.seat === 'string' && meta.seat ? meta.seat : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* The seats of a booking, each with the route of the leg it belongs to. A flight
|
||||
* or train with a stop lists one per leg that has a seat, so a seat booked only
|
||||
* on the second leg is not lost behind the first leg's empty one; any other
|
||||
* booking gives its own seat with no route.
|
||||
*/
|
||||
export function bookingSeats(r: Reservation): { route: string | null; seat: string }[] {
|
||||
const legs: (FlightLeg | TrainLeg)[] =
|
||||
r.type === 'flight' ? getFlightLegs(r) : usesStationRoute(r.type) ? getTrainLegs(r) : []
|
||||
if (legs.length > 1) {
|
||||
return legs.flatMap((l) => (l.seat ? [{ route: [l.from, l.to].filter(Boolean).join(' → ') || null, seat: l.seat }] : []))
|
||||
}
|
||||
const seat = parseReservationMetadata(r).seat
|
||||
return typeof seat === 'string' && seat ? [{ route: null, seat }] : []
|
||||
}
|
||||
|
||||
/** The label of one of bookingSeats' seats: "Seat", or "Seat FRA → IST" for a leg. */
|
||||
export function seatLabel(seatWord: string, route: string | null): string {
|
||||
return route ? `${seatWord} ${route}` : seatWord
|
||||
}
|
||||
|
||||
/**
|
||||
* bookingSeats as the one Seat cell of a booking's field grid: every leg's seat
|
||||
* in route order, then `suffix` (its class). One cell, because a card cuts its
|
||||
* grid after a few cells and a route in the label does not fit a narrow one;
|
||||
* the detail views name each leg's seat under its route.
|
||||
*/
|
||||
export function seatCells(r: Reservation, seatWord: string, suffix = ''): { label: string; value: string }[] {
|
||||
const seats = bookingSeats(r)
|
||||
return seats.length ? [{ label: seatWord, value: seats.map((s) => s.seat).join(', ') + suffix }] : []
|
||||
}
|
||||
|
||||
/** Number of flight segments. 1 for a simple from -> to booking. */
|
||||
export function legCount(r: Reservation): number {
|
||||
return getFlightLegs(r).length
|
||||
|
||||
Reference in New Issue
Block a user