From e51902a03b8e2863119a0883b9965f0f8b3fb98a Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:56:20 +0100 Subject: [PATCH] fix(reservations): drop a stay id from another trip when a booking is created Create now checks that the linked stay belongs to the trip, as update already does, instead of storing a link to a stay that does not exist there. --- server/src/nest/reservations/reservations.service.ts | 4 ++++ server/tests/unit/nest/reservations.service.test.ts | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/server/src/nest/reservations/reservations.service.ts b/server/src/nest/reservations/reservations.service.ts index 7791122ca..aa596ca46 100644 --- a/server/src/nest/reservations/reservations.service.ts +++ b/server/src/nest/reservations/reservations.service.ts @@ -614,6 +614,10 @@ export class ReservationsService { // `toRowId` the way update() reads it: a `'0x1'` or an `'abc'` used to // be stored as `'0x1.0'`, which Postgres cannot cast back for the join. let resolvedAccommodationId: number | null = toRowId(accommodation_id) || null; + // A stay that is not on this trip is dropped, as update() drops it. + if (resolvedAccommodationId && !(await this.dayAccommodationsRepo.existsInTrip(resolvedAccommodationId, this.rowIdNum(tripId)))) { + resolvedAccommodationId = null; + } if (type === 'hotel' && !resolvedAccommodationId && create_accommodation) { const { place_id: accPlaceId, start_day_id, end_day_id, check_in, check_out, confirmation: accConf } = create_accommodation; if (start_day_id && end_day_id) { diff --git a/server/tests/unit/nest/reservations.service.test.ts b/server/tests/unit/nest/reservations.service.test.ts index b62bdce6a..4253761db 100644 --- a/server/tests/unit/nest/reservations.service.test.ts +++ b/server/tests/unit/nest/reservations.service.test.ts @@ -1542,6 +1542,18 @@ describe('ReservationsService — referenced ids stay inside the trip', () => { expect(await findRow(orm, Reservations, { id: reservation.id })).toMatchObject({ place_id: null }); }); + it('RESV-SCOPE-014: a create naming a stay that is not on the trip stores no link, as an update does', async () => { + const { mine, theirs } = twoTrips(); + const foreignPlace = createPlace(testDb, theirs.id); + const foreignDay = await findRow(orm, Days, { trip: theirs.id }) as { id: number }; + const foreignAcc = createDayAccommodation(testDb, theirs.id, foreignPlace.id, foreignDay.id, foreignDay.id); + + for (const accommodation_id of [3000000000, foreignAcc.id]) { + const { reservation } = await svc.create(String(mine.id), { title: 'Hotel', type: 'hotel', accommodation_id }); + expect(await findRow(orm, Reservations, { id: reservation.id })).toMatchObject({ accommodation_id: null }); + } + }); + it('RESV-SCOPE-011: a stay whose refs resolve to nothing is refused, never written and never dropped in silence', async () => { const { mine } = twoTrips(); const place = createPlace(testDb, mine.id);