diff --git a/client/src/api/wsEventPolicy.ts b/client/src/api/wsEventPolicy.ts index 6bf279493..162aeeb77 100644 --- a/client/src/api/wsEventPolicy.ts +++ b/client/src/api/wsEventPolicy.ts @@ -120,7 +120,6 @@ export const IGNORED_WS_EVENTS = [ 'budget:settlement-created', 'budget:settlement-updated', 'budget:settlement-deleted', - 'reservation:positions', // Accommodations live in page-local planner state; the client refetches // them off trip:updated date changes, never off these events. 'accommodation:created', diff --git a/client/src/store/slices/remoteEventHandler.ts b/client/src/store/slices/remoteEventHandler.ts index f719bbc06..0185f0d69 100644 --- a/client/src/store/slices/remoteEventHandler.ts +++ b/client/src/store/slices/remoteEventHandler.ts @@ -7,6 +7,7 @@ import { useAuthStore } from '../authStore' import { mergeAssignmentPlace } from './placesSlice' import { withoutDay } from './daysSlice' import { orderBudgetByCategories } from './budgetOrder' +import { keepDayPositions, withTransportPositions } from '../../utils/mergedOrder' type SetState = StoreApi['setState'] type GetState = StoreApi['getState'] @@ -67,12 +68,21 @@ const putCanonicalBudgetItem: DexieWriter = async (payload, state) => { const item = state.budgetItems.find(i => i.id === (payload.itemId as number)) if (item) await offlineDb.budgetItems.put(item) } -const putReservation: DexieWriter = async payload => { +const putReservation: DexieWriter = async (payload, state) => { // The same empty ping the appliers above handle; without this the write // throws and only the swallowed catch keeps it quiet. if (!payload.reservation) return - await offlineDb.reservations.put(payload.reservation as Reservation) + // The applied row, which kept its day slots when the payload had none. + const id = (payload.reservation as Reservation).id + await offlineDb.reservations.put(state.reservations.find(r => r.id === id) ?? payload.reservation as Reservation) } +/** The moves of a `reservation:positions` event, the ones without a position left out. */ +function positionsOf(payload: Record): { id: number; day_plan_position: number }[] { + if (!Array.isArray(payload.positions)) return [] + return (payload.positions as { id?: unknown; day_plan_position?: unknown }[]) + .filter((p): p is { id: number; day_plan_position: number } => typeof p?.id === 'number' && typeof p.day_plan_position === 'number') +} + const putTripFile: DexieWriter = async payload => { await offlineDb.tripFiles.put(payload.file as TripFile) } @@ -150,6 +160,10 @@ export const DEXIE_WRITERS: Partial> = 'reservation:deleted': async payload => { await offlineDb.reservations.delete(payload.reservationId as number) }, + 'reservation:positions': async (payload, state) => { + const ids = new Set(positionsOf(payload).map(p => p.id)) + await offlineDb.reservations.bulkPut(state.reservations.filter(r => ids.has(r.id))) + }, // ── Trip ───────────────────────────────────────────────────────────────── 'trip:updated': async payload => { @@ -468,7 +482,22 @@ export const STATE_APPLIERS: Partial> 'reservation:updated': (payload, state) => { const reservation = payload.reservation as Reservation | undefined if (!reservation) return {} - return { reservations: state.reservations.map(r => r.id === reservation.id ? reservation : r) } + return { reservations: state.reservations.map(r => r.id === reservation.id ? keepDayPositions(reservation, r) : r) } + }, + // A collaborator moved bookings on a day. Applied the way the mover's own list + // wrote them, so their stops and bookings keep one order here too (#2631). REST + // names the day `day_id`, the MCP tool `dayId`; without one the move is global. + 'reservation:positions': (payload, state) => { + const positions = positionsOf(payload) + if (positions.length === 0) return {} + const dayId = (payload.day_id ?? payload.dayId) as number | null | undefined + if (dayId != null) return { reservations: withTransportPositions(state.reservations, positions, dayId) } + return { + reservations: state.reservations.map(r => { + const p = positions.find(x => x.id === r.id) + return p ? { ...r, day_plan_position: p.day_plan_position } : r + }), + } }, 'reservation:travelers-updated': (payload, state) => ({ reservations: state.reservations.map(r => r.id === (payload.reservationId as number) diff --git a/client/src/store/slices/reservationsSlice.test.ts b/client/src/store/slices/reservationsSlice.test.ts index 27e4b055b..4499899ed 100644 --- a/client/src/store/slices/reservationsSlice.test.ts +++ b/client/src/store/slices/reservationsSlice.test.ts @@ -1,4 +1,4 @@ -// FE-STORE-RESERVATIONS-001 to FE-STORE-RESERVATIONS-005 +// FE-STORE-RESERVATIONS-001 to FE-STORE-RESERVATIONS-006 import { http, HttpResponse } from 'msw'; import { server } from '../../../tests/helpers/msw/server'; import { resetAllStores, seedStore } from '../../../tests/helpers/store'; @@ -88,4 +88,19 @@ describe('reservationsSlice', () => { expect(useTripStore.getState().reservations).toHaveLength(1); expect(useTripStore.getState().budgetItems).toHaveLength(1); }); + + // #2631: the list puts a booking moved on a day by its day slot, so an edit answered + // without the slots put it back by its time until the next reload. + it('FE-STORE-RESERVATIONS-006: an edit answered without day slots keeps the ones the booking had', async () => { + seedStore(useTripStore, { reservations: [buildReservation({ id: 5, trip_id: 1, day_positions: { 3: 2.5 } })] }); + server.use( + http.put('/api/trips/1/reservations/5', () => + HttpResponse.json({ reservation: buildReservation({ id: 5, trip_id: 1, notes: 'aisle' }) }) + ) + ); + await useTripStore.getState().updateReservation(1, 5, { notes: 'aisle' }); + const stored = useTripStore.getState().reservations.find(r => r.id === 5); + expect(stored?.notes).toBe('aisle'); + expect(stored?.day_positions).toEqual({ 3: 2.5 }); + }); }); diff --git a/client/src/store/slices/reservationsSlice.ts b/client/src/store/slices/reservationsSlice.ts index 8f375b8be..e36ed0f88 100644 --- a/client/src/store/slices/reservationsSlice.ts +++ b/client/src/store/slices/reservationsSlice.ts @@ -4,6 +4,7 @@ import type { StoreApi } from 'zustand' import type { TripStoreState } from '../tripStore' import type { Reservation } from '../../types' import { getApiErrorMessage } from '../../types' +import { keepDayPositions } from '../../utils/mergedOrder' type SetState = StoreApi['setState'] type GetState = StoreApi['getState'] @@ -41,7 +42,7 @@ export const createReservationsSlice = (set: SetState, get: GetState): Reservati try { const result = await reservationsApi.update(tripId, id, data) set(state => ({ - reservations: state.reservations.map(r => r.id === id ? result.reservation : r) + reservations: state.reservations.map(r => r.id === id ? keepDayPositions(result.reservation, r) : r) })) return result.reservation } catch (err: unknown) { diff --git a/client/src/utils/mergedOrder.test.ts b/client/src/utils/mergedOrder.test.ts index b7e7ff652..f66433582 100644 --- a/client/src/utils/mergedOrder.test.ts +++ b/client/src/utils/mergedOrder.test.ts @@ -1,8 +1,14 @@ -// FE-UTIL-MERGEDORDER-001 to -008: the reorder writes the desktop day plan and the +// FE-UTIL-MERGEDORDER-001 to -009: the reorder writes the desktop day plan and the // phone's plan timeline both derive from a day's new timeline order. import { describe, expect, it } from 'vitest'; -import { metadataWithLegPositions, moveRow, planMergedOrder, withTransportPositions } from './mergedOrder'; +import { + keepDayPositions, + metadataWithLegPositions, + moveRow, + planMergedOrder, + withTransportPositions, +} from './mergedOrder'; const place = (id: number) => ({ type: 'place', data: { id } }); const note = (id: number) => ({ type: 'note', data: { id } }); @@ -91,3 +97,12 @@ describe('moveRow', () => { expect(rows).toEqual(['a', 'b', 'c', 'd']); }); }); + +describe('keepDayPositions', () => { + it('FE-UTIL-MERGEDORDER-009: keeps the day slots of the replaced copy only when the new one has none', () => { + const prev: { id: number; day_positions?: Record | null } = { id: 1, day_positions: { 4: 1.5 } }; + expect(keepDayPositions({ id: 1 }, prev)).toEqual({ id: 1, day_positions: { 4: 1.5 } }); + expect(keepDayPositions({ id: 1, day_positions: null }, prev)).toEqual({ id: 1, day_positions: null }); + expect(keepDayPositions({ id: 1 }, undefined)).toEqual({ id: 1 }); + }); +}); diff --git a/client/src/utils/mergedOrder.ts b/client/src/utils/mergedOrder.ts index 2724566b0..99e388ee8 100644 --- a/client/src/utils/mergedOrder.ts +++ b/client/src/utils/mergedOrder.ts @@ -95,6 +95,16 @@ export function withTransportPositions( }); } +/** + * A booking as an edit or a collaborator's event hands it back, with the day slots of + * the copy it replaces when it came without any. A server that did not send them yet + * put a moved booking back by its time on every edit until the next reload (#2631). + */ +export function keepDayPositions(next: R, prev: R | undefined): R { + if (!prev || next.day_positions !== undefined) return next; + return { ...next, day_positions: prev.day_positions }; +} + /** * A booking's metadata with each moved leg's position for the day written into * metadata.legs[i].day_positions. Null when the metadata holds no legs to move. diff --git a/client/tests/unit/remoteEventHandler/reservations.test.ts b/client/tests/unit/remoteEventHandler/reservations.test.ts index 6c4ee2770..ce668d666 100644 --- a/client/tests/unit/remoteEventHandler/reservations.test.ts +++ b/client/tests/unit/remoteEventHandler/reservations.test.ts @@ -97,4 +97,63 @@ describe('remoteEventHandler > reservations', () => { }); expect(useTripStore.getState().reservations[0].travelers).toBeUndefined(); }); + + // #2631: a collaborator moving a booking on a day sends only the new slots. Ignored, + // their stops arrived in the new order and the bookings stayed between the old ones. + it('FE-WSEVT-RESERV-009: reservation:positions with day_id writes the slot for that day', () => { + useTripStore.setState({ + reservations: [buildReservation({ id: 1, day_positions: { 7: 0.5 } }), buildReservation({ id: 2 })], + }); + useTripStore.getState().handleRemoteEvent({ + type: 'reservation:positions', + positions: [{ id: 1, day_plan_position: 2.5 }], + day_id: 8, + }); + const [moved, other] = useTripStore.getState().reservations; + expect(moved.day_positions).toEqual({ 7: 0.5, 8: 2.5 }); + expect(moved.day_plan_position).toBe(2.5); + expect(other.day_positions).toBeUndefined(); + }); + + it('FE-WSEVT-RESERV-010: reservation:positions from the MCP tool names the day dayId', () => { + useTripStore.setState({ reservations: [buildReservation({ id: 1 })] }); + useTripStore.getState().handleRemoteEvent({ + type: 'reservation:positions', + positions: [{ id: 1, day_plan_position: 3 }], + dayId: 4, + }); + expect(useTripStore.getState().reservations[0].day_positions).toEqual({ 4: 3 }); + }); + + it('FE-WSEVT-RESERV-011: reservation:positions without a day moves the global slot only', () => { + useTripStore.setState({ reservations: [buildReservation({ id: 1, day_positions: { 4: 1 } })] }); + useTripStore.getState().handleRemoteEvent({ + type: 'reservation:positions', + positions: [{ id: 1, day_plan_position: 6.5 }, { id: 1 }], + day_id: undefined, + }); + const [r] = useTripStore.getState().reservations; + expect(r.day_plan_position).toBe(6.5); + expect(r.day_positions).toEqual({ 4: 1 }); + }); + + it('FE-WSEVT-RESERV-012: reservation:updated without day slots keeps the ones the booking had', () => { + useTripStore.setState({ reservations: [buildReservation({ id: 1, day_positions: { 4: 1.5 } })] }); + useTripStore.getState().handleRemoteEvent({ + type: 'reservation:updated', + reservation: buildReservation({ id: 1, title: 'Train (rebooked)' }), + }); + const [r] = useTripStore.getState().reservations; + expect(r.title).toBe('Train (rebooked)'); + expect(r.day_positions).toEqual({ 4: 1.5 }); + }); + + it('FE-WSEVT-RESERV-013: reservation:updated with day slots takes them, null included', () => { + useTripStore.setState({ reservations: [buildReservation({ id: 1, day_positions: { 4: 1.5 } })] }); + useTripStore.getState().handleRemoteEvent({ + type: 'reservation:updated', + reservation: buildReservation({ id: 1, day_positions: null }), + }); + expect(useTripStore.getState().reservations[0].day_positions).toBeNull(); + }); });