mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 14:17:31 +00:00
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.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user