fix(planner): bookings a collaborator moves on a day take their new slots in every open list

reservation:positions was on the ignored list, while the stop order the same
reorder sends was applied, so in other tabs and for other members the bookings
sat between the wrong stops until a reload. The event now writes the slots the
way the mover's own list did, for the day it names (day_id from REST, dayId
from the MCP tool) or as the global slot without one, and caches them offline.

A booking handed back by an edit or a reservation:updated event without day
slots keeps the ones it had, in the store and in the offline cache.

Refs #2631
This commit is contained in:
Maurice
2026-10-12 12:33:19 +02:00
parent 12f08e3646
commit 93173b1573
7 changed files with 136 additions and 8 deletions
-1
View File
@@ -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',
+32 -3
View File
@@ -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<TripStoreState>['setState']
type GetState = StoreApi<TripStoreState>['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<string, unknown>): { 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<Record<TrekWsTripEventName, DexieWriter>> =
'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<Record<TrekWsTripEventName, StateApplier>>
'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)
@@ -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 });
});
});
+2 -1
View File
@@ -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<TripStoreState>['setState']
type GetState = StoreApi<TripStoreState>['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) {
+17 -2
View File
@@ -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<string, number> | 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 });
});
});
+10
View File
@@ -95,6 +95,16 @@ export function withTransportPositions<R extends PositionedBooking>(
});
}
/**
* 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<R extends PositionedBooking>(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.
@@ -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();
});
});