fix(roadtrip): keep the stay on each visit, not on the place (#3092)

A nullable day_assignments.stay_minutes holds the stay of one visit; a visit without one keeps reading the place's duration, so existing trips drive as before. REST PUT /trips/:tripId/assignments/:id/stay and the MCP tool set_assignment_stay write it with day_edit.
This commit is contained in:
Maurice
2026-11-14 17:48:57 +01:00
parent 535761dbd5
commit bac4599d64
40 changed files with 510 additions and 96 deletions
@@ -60,7 +60,7 @@ export function stopSpan(assignment: Assignment): DaySpan | null {
if (start === null) return null;
const end = clockMinutes(assignment.assignment_end_time ?? assignment.place.end_time);
if (end !== null && end > start) return { start, end };
const duration = assignment.place.duration_minutes ?? 0;
const duration = assignment.stay_minutes ?? assignment.place.duration_minutes ?? 0;
return { start, end: duration > 0 ? Math.min(start + duration, MINUTES_PER_DAY) : null };
}
@@ -3,6 +3,7 @@ import type { AssistantChangeEffect } from '@trek/shared';
import { dayPoints, type LatLng } from '../../store/assistantMapModel';
import type { Assignment, AssignmentsMap, Place } from '../../types';
import { haversineKm } from '../../utils/geo';
import { stayOf } from '../Roadtrip/stayReading';
/**
* A day as it would read once the waiting changes of a card ran: the stops it
@@ -91,7 +92,7 @@ function draftOf(visit: Assignment, status: StopStatus, toolCallId: string | nul
const time = visit.assignment_time ?? place?.place_time ?? null;
const start = minutesOf(time);
const explicitEnd = minutesOf(visit.assignment_end_time ?? place?.end_time ?? null);
const duration = place?.duration_minutes;
const duration = stayOf(visit);
const end = explicitEnd ?? (start !== null && typeof duration === 'number' && duration > 0 ? start + duration : null);
return {
stop: { key, name: place?.name ?? '', status, toolCallId, time: start === null ? null : time },
@@ -105,7 +105,7 @@ describe('RoadtripStayModal', () => {
fireEvent.change(slider(), { target: { value: '45' } })
fireEvent.click(screen.getByRole('button', { name: /^save$/i }))
await waitFor(() => expect(onSave).toHaveBeenCalledWith(9, 45))
await waitFor(() => expect(onSave).toHaveBeenCalledWith(9, 45, undefined))
await waitFor(() => expect(onClose).toHaveBeenCalled())
})
@@ -116,7 +116,7 @@ describe('RoadtripStayModal', () => {
fireEvent.click(screen.getByRole('button', { name: /no stay|none/i }))
await waitFor(() => expect(onSave).toHaveBeenCalledWith(9, 0))
await waitFor(() => expect(onSave).toHaveBeenCalledWith(9, 0, undefined))
expect(onSave.mock.calls[0][1]).not.toBeNull()
})
@@ -29,8 +29,11 @@ export interface StayDraft {
interface RoadtripStayModalProps {
stop: StayDraft | null
onClose: () => void
/** Zero clears it: the rail reads "no stay" and "nothing planned" as the same thing. */
onSave: (placeId: number, minutes: number) => Promise<void> | void
/**
* Zero clears it: the rail reads "no stay" and "nothing planned" as the same thing.
* The visit rides along, so the stay lands on it and not on every visit of the place.
*/
onSave: (placeId: number, minutes: number, assignmentId?: number) => Promise<void> | void
}
/** The round minus and plus beside the number. */
@@ -59,10 +62,11 @@ const SLIDER = [
* question the number is actually being chosen for: given when the drive arrives, when
* does it leave again. Typing minutes into a box could never show that.
*
* The value is `places.duration_minutes`, which the schedule has read for years and
* nothing has ever been able to write. Cleared as a zero rather than a null, because the
* update statement folds a null into "leave it alone" — a null could give a stop a stay
* but never take one away.
* The value is the visit's own stay (`day_assignments.stay_minutes`, #3092), so two
* visits of one place can last differently; a stop without a saved visit writes the
* place's `duration_minutes`, which a visit without a stay of its own reads. Cleared as a
* zero rather than a null, because a null would hand the visit back to the place's stay
* instead of taking the stay away.
*/
export default function RoadtripStayModal({ stop, onClose, onSave }: RoadtripStayModalProps): React.ReactElement | null {
const { t } = useTranslation()
@@ -86,7 +90,7 @@ export default function RoadtripStayModal({ stop, onClose, onSave }: RoadtripSta
if (saving) return
setSaving(true)
try {
await onSave(stop.placeId, value)
await onSave(stop.placeId, value, stop.assignmentId)
onClose()
} finally {
setSaving(false)
@@ -29,7 +29,8 @@ import type { RoadtripStopType } from '@trek/shared'
// from the one table in stopKinds.ts.
export interface RoadtripStopDraft {
editing?: { placeId: number; dwellMinutes: number; stopType: RoadtripStopType | null; accommodationId?: number; checkIn?: string; checkOut?: string }
/** `assignmentId` is the visit being edited: its stay is its own, not every visit's of the place (#3092). */
editing?: { placeId: number; assignmentId?: number; dwellMinutes: number; stopType: RoadtripStopType | null; accommodationId?: number; checkIn?: string; checkOut?: string }
arrivalTime?: string | null
poi: CorridorPoi
dayId: number
+16 -4
View File
@@ -2,15 +2,27 @@ import { formatClock, parseClock, type ScheduleEntry } from './roadtripModel'
import type { RoadtripDay, RoadtripStop } from '@trek/shared/roadtrip'
import type { StayDraft } from './RoadtripStayModal'
import type { RoadtripStayControl } from './VisitControls'
import type { Assignment } from '../../types'
/**
* How long the drive stands at one visit: its own stay (#3092), else the one its place
* carries. A place planned twice can be stopped at once and only driven past the other
* time, and a visit nobody gave a length keeps reading the place, as every visit did
* before the stay moved onto it.
*/
export function stayOf(a: Pick<Assignment, 'stay_minutes' | 'place'>): number | null {
if (typeof a.stay_minutes === 'number') return a.stay_minutes
return typeof a.place?.duration_minutes === 'number' ? a.place.duration_minutes : null
}
/**
* How long a stop is stood at, the way a stay badge shows it.
*
* For most stops that is the stay the place carries. A stop with a time set to leave it
* is stood at from whenever the drive gets there until that time, so its stay is what
* For most stops that is the stay of the visit (`stayOf`). A stop with a time set to leave
* it is stood at from whenever the drive gets there until that time, so its stay is what
* the schedule made of it and `until` says when it ends. The number is read here and
* never written back to the place: the stay belongs to the place, the end time to one
* visit of it, and a stored copy would be wrong the moment the arrival moved.
* never written back: the end time decides it, and a stored copy would be wrong the
* moment the arrival moved.
*/
export interface StayReading {
minutes: number | null
@@ -5,6 +5,7 @@ import { avoidedClasses, calculateRouteWithLegs, routeEngineFor, RoutingRefusedE
import { resolveLegMode } from '../Planner/legMode'
import { splitIntoRuns, parseAvoid, type DriveLimits } from './roadtripModel'
import { spillChains } from './nightSpill'
import { stayOf } from './stayReading'
import { useSettingsStore } from '../../store/settingsStore'
import { useTripStore } from '../../store/tripStore'
import { useVehicleRange } from './useVehicleRange'
@@ -201,7 +202,7 @@ const asStop = (a: Assignment, ownerDayId: number, ownerIndex: number, accommoda
checkInTime: stay?.check_in ?? null,
night: stay !== undefined,
bookedNightId: a.accommodation_id ?? null,
dwellMinutes: typeof p.duration_minutes === 'number' ? p.duration_minutes : null,
dwellMinutes: stayOf(a),
endDay: a.end_day === true,
legMode: a.leg_transport_mode ?? null,
incomingLegMode: a.incoming_leg_transport_mode ?? null,