From cd32f07bf55058fd59dcdbeffae4ea7b9b9501fe Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:05:45 +0100 Subject: [PATCH] fix(roadtrip): offer and save a visit's stay under the day plan right on the desktop The rail and the inspector offered the stay to anyone who may edit places, while a saved visit's stay needs day_edit, so the dialog closed without saving and the stop popup wrote half its change before a 403. Refusals now say so before anything is written. --- client/src/pages/TripPlannerPage.tsx | 2 +- .../tripPlanner/useRoadtripStopEdits.test.ts | 24 +++++++++++++- .../pages/tripPlanner/useRoadtripStopEdits.ts | 32 +++++++++++++------ .../src/pages/tripPlanner/useTripPlanner.ts | 4 +-- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/client/src/pages/TripPlannerPage.tsx b/client/src/pages/TripPlannerPage.tsx index ffb838de3..4fda0d993 100644 --- a/client/src/pages/TripPlannerPage.tsx +++ b/client/src/pages/TripPlannerPage.tsx @@ -319,7 +319,7 @@ function TripPlannerPageDesktop(): React.ReactElement | null { onMoveStopToDay={can('day_edit', trip) ? moveRoadtripStopToDay : undefined} onAskAlternatives={can('day_edit', trip) ? askRouteAlternatives : undefined} openAlternatives={routeAlternatives.open} - onEditStay={can('place_edit', trip) ? editRoadtripStay : undefined} + onEditStay={can('day_edit', trip) ? editRoadtripStay : undefined} onSetStopKind={can('place_edit', trip) ? setRoadtripStopKind : undefined} onSetStopFill={can('place_edit', trip) ? setRoadtripStopFill : undefined} onFollowTrack={can('day_edit', trip) && followTrack.available ? followTrack.open : undefined} diff --git a/client/src/pages/tripPlanner/useRoadtripStopEdits.test.ts b/client/src/pages/tripPlanner/useRoadtripStopEdits.test.ts index 65a81cd0f..38171550f 100644 --- a/client/src/pages/tripPlanner/useRoadtripStopEdits.test.ts +++ b/client/src/pages/tripPlanner/useRoadtripStopEdits.test.ts @@ -1,4 +1,4 @@ -// FE-TP-STOPEDITS-001 to FE-TP-STOPEDITS-018 +// FE-TP-STOPEDITS-001 to FE-TP-STOPEDITS-019 // // 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 @@ -529,5 +529,27 @@ describe('useRoadtripStopEdits', () => { expect(stayOf(first)).toBe(90) expect(stayOf(second)).toBe(45) }) + + it('FE-TP-STOPEDITS-019: the stay of a visit needs the day plan right, and a refusal says so before anything is written', async () => { + const tripActions = seed() + vi.mocked(can).mockImplementation(action => action === 'place_edit') + 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 }) + expect(result.current.mayEditStay(52)).toBe(false) + expect(result.current.mayEditStay(undefined)).toBe(true) + + await act(async () => { await result.current.setRoadtripStay(70, 0, 52) }) + expect(assignmentRepo.setStay).not.toHaveBeenCalled() + expect(toast.error).toHaveBeenLastCalledWith('trip.journal.errorForbidden') + + await act(async () => { await result.current.saveStopDraft({ stopType: 'rest_area', dwellMinutes: 90 }) }) + expect(actions.updatePlace).not.toHaveBeenCalled() + expect(toast.error).toHaveBeenCalledTimes(2) + + await act(async () => { await result.current.saveStopDraft({ stopType: 'rest_area', dwellMinutes: 45 }) }) + expect(actions.updatePlace).toHaveBeenCalledWith(42, 70, { stop_type: 'rest_area' }) + expect(assignmentRepo.setStay).not.toHaveBeenCalled() + vi.mocked(can).mockImplementation(() => allowed) + }) }) }) diff --git a/client/src/pages/tripPlanner/useRoadtripStopEdits.ts b/client/src/pages/tripPlanner/useRoadtripStopEdits.ts index d153e2915..a7cd7d223 100644 --- a/client/src/pages/tripPlanner/useRoadtripStopEdits.ts +++ b/client/src/pages/tripPlanner/useRoadtripStopEdits.ts @@ -40,22 +40,26 @@ function savedVisitDayOf(assignments: TripStoreState['assignments'], assignmentI * 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. + * place, as before. The visit's stay is part of the day plan: without the right to edit + * it a changed stay is refused before anything is written, and an unchanged one is left + * alone, so the kind still saves. */ async function writeEditedStop( tripActions: TripStoreState, tripId: PlannerBase['tripId'], assignments: TripStoreState['assignments'], - { placeId, assignmentId }: { placeId: number; assignmentId?: number }, + { placeId, assignmentId, dwellMinutes: before }: { placeId: number; assignmentId?: number; dwellMinutes?: number }, { stopType, dwellMinutes }: { stopType: RoadtripStopType | null; dwellMinutes: number }, + { mayEditVisit, refused }: { mayEditVisit: boolean; refused: string }, ): Promise { const visitDayId = savedVisitDayOf(assignments, assignmentId) if (assignmentId == null || visitDayId === null) { await tripActions.updatePlace(tripId, placeId, { stop_type: stopType, duration_minutes: dwellMinutes }) return } + if (!mayEditVisit && dwellMinutes !== before) throw new Error(refused) await tripActions.updatePlace(tripId, placeId, { stop_type: stopType }) - await tripActions.setAssignmentStay(tripId, visitDayId, assignmentId, dwellMinutes) + if (mayEditVisit) await tripActions.setAssignmentStay(tripId, visitDayId, assignmentId, dwellMinutes) } /** @@ -97,7 +101,10 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) { } try { if (stopDraft.editing) { - await writeEditedStop(tripActions, tripId, assignments, stopDraft.editing, { stopType, dwellMinutes }) + await writeEditedStop(tripActions, tripId, assignments, stopDraft.editing, { stopType, dwellMinutes }, { + mayEditVisit: can('day_edit', trip), + refused: t('trip.journal.errorForbidden'), + }) 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. @@ -140,7 +147,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, assignments]) + }, [stopDraft, tripId, tripActions, updateRouteForDay, toast, t, roadtripVias, viaLiesBefore, loadAccommodations, tripAccommodations, reservations, setStayRelease, setStopDraft, assignments, can, trip]) /** The yes to the question above: the same save, this time allowed to drop the night. */ const confirmStayRelease = useCallback(async () => { @@ -361,20 +368,27 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) { * it back to the place's stay instead of taking the stay away. The rail reads zero and * absent as the same thing. */ + const mayEditStay = useCallback((assignmentId?: number) => + can(savedVisitDayOf(assignments, assignmentId) === null ? 'place_edit' : 'day_edit', trip), + [assignments, can, trip]) + const setRoadtripStay = useCallback(async (placeId: number, minutes: number, assignmentId?: number) => { const visitDayId = savedVisitDayOf(assignments, assignmentId) + // The dialog closes once this returns, so a refusal says so rather than looking saved. + if (!mayEditStay(assignmentId)) { + toast.error(t('trip.journal.errorForbidden')) + return + } 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')) } - }, [assignments, tripId, tripActions, toast, t, can, trip]) + }, [assignments, tripId, tripActions, toast, t, mayEditStay]) /** * Moves a stop onto another day, from the road trip rail. @@ -443,7 +457,7 @@ export function useRoadtripStopEdits(options: RoadtripStopEditsOptions) { return { saveStopDraft, confirmStayRelease, saveStopDraftAsNight, setRoadtripStopKind, setRoadtripStopFill, reorderRoadtripStop, stayDraft, setStayDraft, roadtripEndsDayAt, setRoadtripEndDay, setRoadtripStay, - moveRoadtripStopToDay, editRoadtripStay, + moveRoadtripStopToDay, editRoadtripStay, mayEditStay, } } diff --git a/client/src/pages/tripPlanner/useTripPlanner.ts b/client/src/pages/tripPlanner/useTripPlanner.ts index 5d62632b9..d0bfd516d 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.ts +++ b/client/src/pages/tripPlanner/useTripPlanner.ts @@ -556,7 +556,7 @@ export function useTripPlanner() { }, []) const { - stayDraft, setStayDraft, editRoadtripStay, setRoadtripStay, roadtripEndsDayAt, setRoadtripEndDay, + stayDraft, setStayDraft, editRoadtripStay, setRoadtripStay, roadtripEndsDayAt, setRoadtripEndDay, mayEditStay, saveStopDraft, saveStopDraftAsNight, confirmStayRelease, reorderRoadtripStop, moveRoadtripStopToDay, setRoadtripStopKind, setRoadtripStopFill, } = useRoadtripStopEdits({ @@ -694,7 +694,7 @@ export function useTripPlanner() { ? { active: roadtripEndsDayAt(endDayStop), onToggle: () => setRoadtripEndDay(endDayStop) } : undefined const roadtripStay = roadtripActive && selectedPlace - ? inspectorStay(roadtripRoutes.days, endDayStop, selectedPlace, can('place_edit', trip) ? editRoadtripStay : undefined) + ? inspectorStay(roadtripRoutes.days, endDayStop, selectedPlace, mayEditStay(endDayStop?.assignmentId) ? editRoadtripStay : undefined) : undefined const mapTileUrl = useTileUrl(OFM_POSITRON)