mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
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:
@@ -87,6 +87,8 @@ export interface PlaceVisit {
|
||||
endTime: string | null;
|
||||
notes: string | null;
|
||||
endDay: boolean;
|
||||
/** The visit's own stay (#3092), or null where it read the place's. */
|
||||
stayMinutes: number | null;
|
||||
routeExcluded: boolean;
|
||||
legMode: string | null;
|
||||
incomingLegMode: string | null;
|
||||
@@ -103,6 +105,7 @@ export function visitOf(dayId: number, a: Assignment): PlaceVisit {
|
||||
endTime: a.assignment_end_time ?? null,
|
||||
notes: a.notes ?? null,
|
||||
endDay: a.end_day === true,
|
||||
stayMinutes: a.stay_minutes ?? null,
|
||||
routeExcluded: a.route_excluded === true,
|
||||
legMode: a.leg_transport_mode ?? null,
|
||||
incomingLegMode: a.incoming_leg_transport_mode ?? null,
|
||||
@@ -123,6 +126,7 @@ export type RestoreActions = Pick<
|
||||
| 'setAssignmentTimes'
|
||||
| 'setAssignmentNotes'
|
||||
| 'setAssignmentEndDay'
|
||||
| 'setAssignmentStay'
|
||||
| 'setAssignmentRouteExcluded'
|
||||
| 'setAssignmentParticipants'
|
||||
| 'setAssignmentLegMode'
|
||||
@@ -153,7 +157,7 @@ export async function restorePlace(
|
||||
return restored;
|
||||
}
|
||||
|
||||
/** Gives a visit planned again the times, note, end of day, travellers and legs it had. */
|
||||
/** Gives a visit planned again the times, note, end of day, stay, travellers and legs it had. */
|
||||
export async function restoreVisit(
|
||||
tripId: number | string,
|
||||
actions: Omit<RestoreActions, 'addPlace' | 'assignPlaceToDay' | 'ratePlace'>,
|
||||
@@ -166,6 +170,7 @@ export async function restoreVisit(
|
||||
}
|
||||
if (visit.notes) await actions.setAssignmentNotes(tripId, dayId, assignmentId, visit.notes);
|
||||
if (visit.endDay) await actions.setAssignmentEndDay(tripId, dayId, assignmentId, true);
|
||||
if (visit.stayMinutes !== null) await actions.setAssignmentStay(tripId, dayId, assignmentId, visit.stayMinutes);
|
||||
if (visit.routeExcluded) await actions.setAssignmentRouteExcluded(tripId, dayId, assignmentId, true);
|
||||
if (visit.participantIds.length > 0)
|
||||
await actions.setAssignmentParticipants(tripId, dayId, assignmentId, visit.participantIds);
|
||||
|
||||
@@ -306,7 +306,7 @@ export function usePlaceFormOpeners(options: PlaceFormOpenersOptions) {
|
||||
setStopDraft({
|
||||
poi: { osm_id: place.osm_id ?? '', name: place.name, lat: place.lat, lng: place.lng, category, poi_type: category, address: place.address ?? null, website: place.website ?? null, phone: place.phone ?? null, opening_hours: null, cuisine: null, source: 'trek', offRouteKm: 0, alongKm: 0 },
|
||||
arrivalTime, dayId, dayNumber: days.find(day => day.id === dayId)?.day_number ?? 0, position: visit.order_index ?? 0,
|
||||
editing: { placeId: place.id, stopType: place.stop_type ?? (stay ? 'hotel' : null), dwellMinutes: place.duration_minutes ?? 30, accommodationId: stay?.id, checkIn: stay?.check_in ?? '', checkOut: stay?.check_out ?? '' },
|
||||
editing: { placeId: place.id, assignmentId: visit.id, stopType: place.stop_type ?? (stay ? 'hotel' : null), dwellMinutes: visit.stay_minutes ?? place.duration_minutes ?? 30, accommodationId: stay?.id, checkIn: stay?.check_in ?? '', checkOut: stay?.check_out ?? '' },
|
||||
...(isOvernightCategory(category) ? { overnight: { ...overnightOptions(dayId), ...(stay ? { defaultEndDayId: stay.end_day_id } : {}) } } : {}),
|
||||
})
|
||||
return
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-TP-STOPEDITS-001 to FE-TP-STOPEDITS-016
|
||||
// FE-TP-STOPEDITS-001 to FE-TP-STOPEDITS-018
|
||||
//
|
||||
// Edits to one stop on the drive, driven straight through useRoadtripStopEdits. The
|
||||
// stop popup's draft and the stay release question are real state in the harness, so
|
||||
@@ -8,6 +8,7 @@ import { act, renderHook } from '@testing-library/react'
|
||||
import { accommodationsApi } from '../../api/client'
|
||||
import type { RoadtripStopDraft } from '../../components/Roadtrip/RoadtripStopPopup'
|
||||
import { reanchorAfterInsert, reanchorAfterRemove, reanchorAfterReorder } from '../../components/Roadtrip/roadtripModel'
|
||||
import { stayOf } from '../../components/Roadtrip/stayReading'
|
||||
import type { CorridorPoi } from '../../components/Roadtrip/useCorridorPois'
|
||||
import type { RoadtripStop } from '../../components/Roadtrip/useRoadtripRoutes'
|
||||
import { useTripStore, type TripStoreState } from '../../store/tripStore'
|
||||
@@ -17,6 +18,7 @@ import type { Accommodation, AssignmentsMap } from '../../types'
|
||||
import type { Can, Translate } from './plannerTypes'
|
||||
import type { PlannerDialogs } from './usePlannerDialogs'
|
||||
import type { RoadtripFeed } from './useRoadtripFeed'
|
||||
import { assignmentRepo } from '../../repo/assignmentRepo'
|
||||
import { useRoadtripStopEdits } from './useRoadtripStopEdits'
|
||||
|
||||
type Options = Parameters<typeof useRoadtripStopEdits>[0]
|
||||
@@ -485,4 +487,47 @@ describe('useRoadtripStopEdits', () => {
|
||||
'common.unknownError', 'common.unknownError',
|
||||
])
|
||||
})
|
||||
|
||||
// #3092: the same place planned twice on a day, once to stop and once only to steer the
|
||||
// route past it. The stay belongs to the visit, so changing one leaves the other alone.
|
||||
describe('a stay per visit', () => {
|
||||
const viewpoint = buildPlace({ id: 70, lat: 50, lng: 13, duration_minutes: 45 })
|
||||
const twice: AssignmentsMap = {
|
||||
5: [
|
||||
buildAssignment({ id: 51, day_id: 5, place: viewpoint, place_id: 70, order_index: 0 }),
|
||||
buildAssignment({ id: 52, day_id: 5, place: viewpoint, place_id: 70, order_index: 1 }),
|
||||
],
|
||||
}
|
||||
const seed = () => {
|
||||
useTripStore.setState({ assignments: twice })
|
||||
vi.spyOn(assignmentRepo, 'setStay').mockImplementation(async (_tripId, visit, minutes) => ({ ...visit, stay_minutes: minutes }))
|
||||
return { ...useTripStore.getState(), ...actions, setAssignmentStay: useTripStore.getState().setAssignmentStay } as unknown as TripStoreState
|
||||
}
|
||||
|
||||
it('FE-TP-STOPEDITS-017: the stay dialog changes one visit of a place and leaves the other visit of it alone', async () => {
|
||||
const tripActions = seed()
|
||||
const { result } = renderEdits(null, { tripActions, assignments: twice })
|
||||
|
||||
await act(async () => { await result.current.setRoadtripStay(70, 0, 52) })
|
||||
|
||||
expect(actions.updatePlace).not.toHaveBeenCalled()
|
||||
expect(assignmentRepo.setStay).toHaveBeenCalledWith(42, expect.objectContaining({ id: 52 }), 0)
|
||||
const [first, second] = useTripStore.getState().assignments['5']
|
||||
expect(stayOf(first)).toBe(45)
|
||||
expect(stayOf(second)).toBe(0)
|
||||
})
|
||||
|
||||
it('FE-TP-STOPEDITS-018: the stop popup writes the kind on the place and the stay on the visit it was opened on', async () => {
|
||||
const tripActions = seed()
|
||||
const editing = { placeId: 70, assignmentId: 51, dwellMinutes: 45, stopType: null }
|
||||
const { result } = renderEdits({ poi: hit(), dayId: 5, position: 0, dayNumber: 5, editing }, { tripActions, assignments: twice })
|
||||
|
||||
await act(async () => { await result.current.saveStopDraft({ stopType: 'rest_area', dwellMinutes: 90 }) })
|
||||
|
||||
expect(actions.updatePlace).toHaveBeenCalledWith(42, 70, { stop_type: 'rest_area' })
|
||||
const [first, second] = useTripStore.getState().assignments['5']
|
||||
expect(stayOf(first)).toBe(90)
|
||||
expect(stayOf(second)).toBe(45)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,6 +24,40 @@ interface RoadtripStopEditsOptions
|
||||
updateRouteForDay: ReturnType<typeof useRouteCalculation>['updateRouteForDay']
|
||||
}
|
||||
|
||||
/**
|
||||
* The day a saved visit is stored on, or null for none: no visit named, the optimistic
|
||||
* row of one still being saved (a negative id the server never issued), or one no longer
|
||||
* planned. A stay is written onto such a visit (#3092); without one it falls back to the
|
||||
* place.
|
||||
*/
|
||||
function savedVisitDayOf(assignments: TripStoreState['assignments'], assignmentId: number | undefined): number | null {
|
||||
if (assignmentId == null || assignmentId <= 0) return null
|
||||
const day = Object.entries(assignments).find(([, visits]) => visits.some(visit => visit.id === assignmentId))
|
||||
return day ? Number(day[0]) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the stop popup over a stop already on the drive. The kind is the place's, the stay
|
||||
* the visit's (#3092): a place planned twice keeps a stay per visit, so the popup on one
|
||||
* of them leaves the other one's alone. A stop with no saved visit writes both onto the
|
||||
* place, as before.
|
||||
*/
|
||||
async function writeEditedStop(
|
||||
tripActions: TripStoreState,
|
||||
tripId: PlannerBase['tripId'],
|
||||
assignments: TripStoreState['assignments'],
|
||||
{ placeId, assignmentId }: { placeId: number; assignmentId?: number },
|
||||
{ stopType, dwellMinutes }: { stopType: RoadtripStopType | null; dwellMinutes: number },
|
||||
): Promise<void> {
|
||||
const visitDayId = savedVisitDayOf(assignments, assignmentId)
|
||||
if (assignmentId == null || visitDayId === null) {
|
||||
await tripActions.updatePlace(tripId, placeId, { stop_type: stopType, duration_minutes: dwellMinutes })
|
||||
return
|
||||
}
|
||||
await tripActions.updatePlace(tripId, placeId, { stop_type: stopType })
|
||||
await tripActions.setAssignmentStay(tripId, visitDayId, assignmentId, dwellMinutes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits to one stop on the drive: saving the stop popup as a pause or as a night,
|
||||
* releasing a booked night, the kind, fill and stay of a stop, moving it within its day
|
||||
@@ -63,7 +97,7 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) {
|
||||
}
|
||||
try {
|
||||
if (stopDraft.editing) {
|
||||
await tripActions.updatePlace(tripId, stopDraft.editing.placeId, { stop_type: stopType, duration_minutes: dwellMinutes })
|
||||
await writeEditedStop(tripActions, tripId, assignments, stopDraft.editing, { stopType, dwellMinutes })
|
||||
if (accommodationId) {
|
||||
// The night is what was switched off, not the stop: it stays where it is
|
||||
// in the drive and becomes an ordinary pause.
|
||||
@@ -106,7 +140,7 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) {
|
||||
} catch (err: unknown) {
|
||||
toast.error(err instanceof Error ? err.message : t('common.unknownError'))
|
||||
}
|
||||
}, [stopDraft, tripId, tripActions, updateRouteForDay, toast, t, roadtripVias, viaLiesBefore, loadAccommodations, tripAccommodations, reservations, setStayRelease, setStopDraft])
|
||||
}, [stopDraft, tripId, tripActions, updateRouteForDay, toast, t, roadtripVias, viaLiesBefore, loadAccommodations, tripAccommodations, reservations, setStayRelease, setStopDraft, assignments])
|
||||
|
||||
/** The yes to the question above: the same save, this time allowed to drop the night. */
|
||||
const confirmStayRelease = useCallback(async () => {
|
||||
@@ -315,22 +349,32 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) {
|
||||
/**
|
||||
* How long the traveller stays at one stop.
|
||||
*
|
||||
* Writes `places.duration_minutes`, which the schedule has read since it was written
|
||||
* and which nothing in TREK has ever been able to set: the road trip is the only place
|
||||
* the value means anything, so it is the only place that edits it.
|
||||
* The stay belongs to the visit (#3092): the same place planned twice on a day can be
|
||||
* stopped at once and only driven past the other time, so a stop the rail names by its
|
||||
* assignment writes `day_assignments.stay_minutes` and leaves the place and its other
|
||||
* visits alone. Only a stop without a saved visit of its own (no assignment, or the
|
||||
* optimistic row of one still on its way) falls back to the place's
|
||||
* `duration_minutes`, which every visit without a stay of its own reads.
|
||||
*
|
||||
* Zero rather than null to clear: the update statement folds a null into "leave it
|
||||
* alone" (`COALESCE(?, duration_minutes)`), so a null could give a stop a stay but
|
||||
* never take one away. The rail reads zero and absent as the same thing.
|
||||
* Zero rather than null to clear: on the place the update statement folds a null into
|
||||
* "leave it alone" (`COALESCE(?, duration_minutes)`), and on a visit a null would hand
|
||||
* it back to the place's stay instead of taking the stay away. The rail reads zero and
|
||||
* absent as the same thing.
|
||||
*/
|
||||
const setRoadtripStay = useCallback(async (placeId: number, minutes: number) => {
|
||||
if (!can('place_edit', trip)) return
|
||||
const setRoadtripStay = useCallback(async (placeId: number, minutes: number, assignmentId?: number) => {
|
||||
const visitDayId = savedVisitDayOf(assignments, assignmentId)
|
||||
try {
|
||||
if (assignmentId != null && visitDayId !== null) {
|
||||
if (!can('day_edit', trip)) return
|
||||
await tripActions.setAssignmentStay(tripId, visitDayId, assignmentId, minutes)
|
||||
return
|
||||
}
|
||||
if (!can('place_edit', trip)) return
|
||||
await tripActions.updatePlace(tripId, placeId, { duration_minutes: minutes })
|
||||
} catch (err: unknown) {
|
||||
toast.error(err instanceof Error ? err.message : t('common.unknownError'))
|
||||
}
|
||||
}, [tripId, tripActions, toast, t, can, trip])
|
||||
}, [assignments, tripId, tripActions, toast, t, can, trip])
|
||||
|
||||
/**
|
||||
* Moves a stop onto another day, from the road trip rail.
|
||||
|
||||
Reference in New Issue
Block a user