mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
fix(db): find a stay's bookings on Postgres
reservations.accommodation_id is TEXT and holds '14.0'. Compared with a bare number, Postgres binds the number as the text '14' and finds nothing, so deleting a stay, a day or a place left its bookings behind with no error. The three filters read the column as an integer, as the joins already do.
This commit is contained in:
@@ -3,7 +3,7 @@ import { DayAssignments } from '../entities/DayAssignments.entity';
|
||||
import { Days } from '../entities/Days.entity';
|
||||
import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functions';
|
||||
import { concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
|
||||
import { accommodationIdMatches } from './_shared/reservation-accommodation';
|
||||
import { accommodationIdMatches, reservationStayIs } from './_shared/reservation-accommodation';
|
||||
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
|
||||
import type { DayAssignmentRow } from './day-assignment-rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
@@ -609,7 +609,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
.else(concatKysely(platform, eb, { value: date }, { expression: substringKysely(platform, eb, 'reservation_time', 11) }))
|
||||
.end(),
|
||||
}))
|
||||
.where('accommodation_id', '=', accommodation_id)
|
||||
.where((eb) => reservationStayIs(this.getEntityManager().getPlatform(), eb, 'accommodation_id', accommodation_id))
|
||||
.where('type', '=', 'hotel')
|
||||
.execute();
|
||||
}
|
||||
@@ -715,7 +715,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<ReservationsByAccommodationKyselyDB>()
|
||||
.selectFrom('reservations')
|
||||
.select(['id', 'metadata'])
|
||||
.where('accommodation_id', '=', accommodation_id)
|
||||
.where((eb) => reservationStayIs(this.getEntityManager().getPlatform(), eb, 'accommodation_id', accommodation_id))
|
||||
.execute();
|
||||
return rows as { id: number; metadata: string | null }[];
|
||||
}
|
||||
@@ -759,7 +759,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<ReservationsByAccommodationKyselyDB>()
|
||||
.selectFrom('reservations')
|
||||
.select(['id'])
|
||||
.where('accommodation_id', '=', accommodation_id)
|
||||
.where((eb) => reservationStayIs(this.getEntityManager().getPlatform(), eb, 'accommodation_id', accommodation_id))
|
||||
.execute();
|
||||
return rows as { id: number }[];
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { integerTextEqualsKysely } from '../../dialect/kysely-functions';
|
||||
import { castIntegerKysely, integerTextEqualsKysely } from '../../dialect/kysely-functions';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } from 'kysely';
|
||||
@@ -21,3 +21,19 @@ export function accommodationIdMatches<DB, TB extends keyof DB>(
|
||||
): ExpressionWrapper<DB, TB, SqlBool> {
|
||||
return integerTextEqualsKysely(platform, eb, reservationAccommodationId, stayId);
|
||||
}
|
||||
|
||||
/**
|
||||
* A reservation booked against the stay `stayId`, the filter form of
|
||||
* {@link accommodationIdMatches}: the TEXT column read as an integer, so the
|
||||
* stored `'14.0'` matches 14 on Postgres too. Compared as written (`= 14`),
|
||||
* Postgres binds the number as the text `'14'` and finds no row at all, which
|
||||
* no error reports.
|
||||
*/
|
||||
export function reservationStayIs<DB, TB extends keyof DB>(
|
||||
platform: Platform,
|
||||
eb: ExpressionBuilder<DB, TB>,
|
||||
accommodationId: StringReference<DB, TB>,
|
||||
stayId: number,
|
||||
): ExpressionWrapper<DB, TB, SqlBool> {
|
||||
return eb(castIntegerKysely(platform, eb, accommodationId), '=', stayId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user