Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(reservations): drop a stay id from another trip when a booking is created

Mauricee51902a03b8e
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);