mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(db): match a stay-linked booking on Postgres in the public stay check
publicStayExists cast reservations.accommodation_id straight to integer, which Postgres refuses for the stored '14.0', so the calendar feed and the share links failed for every trip once any booking was linked to a stay. The correlation now goes through accommodationIdMatches, which compares through the new dialect helper integerTextEqualsKysely, and the Wallet pass context uses the same helper instead of its own numeric cast.
This commit is contained in:
@@ -167,6 +167,23 @@ export function castIntegerKysely<DB, TB extends keyof DB>(
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* `<text ref> = <integer ref>` for a TEXT column that holds an integer id,
|
||||
* written either bare or the way the services store it (`'14'`, `'14.0'`):
|
||||
* the text goes through {@link castIntegerKysely}, so both engines match
|
||||
* `'14'`, `'14.0'` and a stored 14 against the id 14 alike. A bare cast to
|
||||
* integer would do on SQLite, but Postgres refuses `CAST('14.0' AS integer)`
|
||||
* (22P02), and a bare text-to-integer comparison (42883).
|
||||
*/
|
||||
export function integerTextEqualsKysely<DB, TB extends keyof DB>(
|
||||
platform: Platform,
|
||||
eb: ExpressionBuilder<DB, TB>,
|
||||
text: ReferenceExpression<DB, TB>,
|
||||
integer: StringReference<DB, TB>,
|
||||
): ExpressionWrapper<DB, TB, SqlBool> {
|
||||
return eb(castIntegerKysely(platform, eb, text), '=', eb.ref(integer).$castTo<number>());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Plan 3g Task 0 (R1) — no consumer yet: Task 1 (`getJourneyFull`'s gallery
|
||||
// read) and Task 3 (`getPublicJourney`'s gallery read) each fold this into
|
||||
|
||||
@@ -350,7 +350,9 @@ type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_en
|
||||
|
||||
export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
// For its Wallet pass (_shared/reservation-pass-context.ts).
|
||||
findPassContext(id: number, tripId: number) { return passContextQuery(this.kysely<PassContextDB>(), id, tripId); }
|
||||
findPassContext(id: number, tripId: number) {
|
||||
return passContextQuery(this.kysely<PassContextDB>(), this.getEntityManager().getPlatform(), id, tripId);
|
||||
}
|
||||
|
||||
private joinedQuery() {
|
||||
return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());
|
||||
@@ -928,7 +930,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<CalendarStayKyselyDB>()
|
||||
.selectFrom('day_accommodations as a')
|
||||
.where('a.trip_id', '=', trip_id as number)
|
||||
.where((eb) => publicStayExists(eb))
|
||||
.where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb))
|
||||
.leftJoin('days as sd', 'sd.id', 'a.start_day_id')
|
||||
.leftJoin('days as ed', 'ed.id', 'a.end_day_id')
|
||||
.leftJoin('places as p', 'p.id', 'a.place_id')
|
||||
@@ -1164,7 +1166,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<SharePublicAccommodationKyselyDB>()
|
||||
.selectFrom('day_accommodations as a')
|
||||
.where('a.trip_id', '=', trip_id as number)
|
||||
.where((eb) => publicStayExists(eb))
|
||||
.where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb))
|
||||
.innerJoin('places as p', 'p.id', 'a.place_id')
|
||||
.select([
|
||||
'a.id', 'a.trip_id', 'a.place_id', 'a.start_day_id', 'a.end_day_id',
|
||||
@@ -1185,7 +1187,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
const rows = await this.kysely<SharePublicAccommodationKyselyDB>()
|
||||
.selectFrom('day_accommodations as a')
|
||||
.where('a.trip_id', '=', trip_id as number)
|
||||
.where((eb) => publicStayExists(eb))
|
||||
.where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb))
|
||||
.select('a.place_id')
|
||||
.distinct()
|
||||
.execute();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { castIntegerKysely } from '../../dialect/kysely-functions';
|
||||
import { integerTextEqualsKysely } from '../../dialect/kysely-functions';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } from 'kysely';
|
||||
@@ -7,10 +7,11 @@ import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } f
|
||||
* The join condition between a reservation and the stay it is booked against.
|
||||
*
|
||||
* `reservations.accommodation_id` is TEXT with no foreign key, while
|
||||
* `day_accommodations.id` is INTEGER, so the comparison is written as
|
||||
* `CAST(<reservation ref> AS INTEGER) = <stay ref>`. SQLite coerces a bare
|
||||
* text-to-integer comparison and Postgres refuses it (42883), so the cast is
|
||||
* needed on both engines. A stored `'14.0'` casts to 14 on both.
|
||||
* `day_accommodations.id` is INTEGER, and the services store the id as
|
||||
* `'14.0'` (`legacyBoundIntegerText`). The dialect helper
|
||||
* `integerTextEqualsKysely` compares the two so that `'14'`, `'14.0'` and 14
|
||||
* match the stay 14 on both engines. Every join and correlation on this pair
|
||||
* goes through here, so the comparison has one spelling.
|
||||
*/
|
||||
export function accommodationIdMatches<DB, TB extends keyof DB>(
|
||||
platform: Platform,
|
||||
@@ -18,5 +19,5 @@ export function accommodationIdMatches<DB, TB extends keyof DB>(
|
||||
reservationAccommodationId: StringReference<DB, TB>,
|
||||
stayId: StringReference<DB, TB>,
|
||||
): ExpressionWrapper<DB, TB, SqlBool> {
|
||||
return eb(castIntegerKysely(platform, eb, reservationAccommodationId), '=', eb.ref(stayId).$castTo<number>());
|
||||
return integerTextEqualsKysely(platform, eb, reservationAccommodationId, stayId);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { DB } from '../../kysely/db';
|
||||
import { accommodationIdMatches } from './reservation-accommodation';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
|
||||
@@ -21,6 +23,7 @@ export interface ReservationPassContext {
|
||||
*/
|
||||
export async function passContextQuery(
|
||||
kysely: Kysely<PassContextDB>,
|
||||
platform: Platform,
|
||||
id: number,
|
||||
trip_id: number,
|
||||
): Promise<ReservationPassContext | undefined> {
|
||||
@@ -28,9 +31,8 @@ export async function passContextQuery(
|
||||
.selectFrom('reservations as r')
|
||||
.innerJoin('trips as t', 't.id', 'r.trip_id')
|
||||
.leftJoin('places as p', 'p.id', 'r.place_id')
|
||||
// accommodation_id is TEXT; compared as a number so Postgres takes it as well as SQLite ("14" and "14.0" alike).
|
||||
.leftJoin('day_accommodations as ap', (join) =>
|
||||
join.on((eb) => eb(eb.cast<number>('r.accommodation_id', 'numeric'), '=', eb.ref('ap.id'))),
|
||||
join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),
|
||||
)
|
||||
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
|
||||
.where('r.id', '=', id)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { coalesceParam } from '../../dialect/sql-functions';
|
||||
import { accommodationIdMatches } from './reservation-accommodation';
|
||||
import type { DB } from '../../kysely/db';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
@@ -137,8 +138,10 @@ export type ReservationVisibilityKyselyDB = Pick<DB, 'day_accommodations' | 'res
|
||||
|
||||
/**
|
||||
* RV2 as a Kysely predicate: `(NOT EXISTS (…) OR EXISTS (… AND <RV1>))`,
|
||||
* correlated on `CAST(vr.accommodation_id AS INTEGER) = a.id`. Call inside
|
||||
* a `.where((eb) => publicStayExists(eb))` on a query already
|
||||
* correlated through `accommodationIdMatches` (the stored `'14.0'` matches
|
||||
* the stay 14 on both engines; Postgres refuses a bare cast of it to
|
||||
* integer). Call inside a `.where((eb) => publicStayExists(platform, eb))`
|
||||
* on a query already
|
||||
* `.selectFrom('day_accommodations as a')` against a `Kysely<DB>` whose
|
||||
* `DB` satisfies `ReservationVisibilityKyselyDB`. The parameter type is
|
||||
* fixed to exactly the shape `.selectFrom('day_accommodations as a')`
|
||||
@@ -151,11 +154,14 @@ export type ReservationVisibilityKyselyDB = Pick<DB, 'day_accommodations' | 'res
|
||||
* structurally"); this function accepts exactly one contract instead,
|
||||
* proven end-to-end by this file's own parity-harness test.
|
||||
*/
|
||||
export function publicStayExists(eb: ExpressionBuilder<ReservationVisibilityKyselyDB & { a: { id: number } }, 'a'>) {
|
||||
export function publicStayExists(
|
||||
platform: Platform,
|
||||
eb: ExpressionBuilder<ReservationVisibilityKyselyDB & { a: { id: number } }, 'a'>,
|
||||
) {
|
||||
const linkedTo = eb
|
||||
.selectFrom('reservations as vr')
|
||||
.select('vr.id')
|
||||
.where((eb2) => eb2(eb2.cast(eb2.ref('vr.accommodation_id'), 'integer'), '=', eb2.ref('a.id')));
|
||||
.where((eb2) => accommodationIdMatches(platform, eb2, 'vr.accommodation_id', 'a.id'));
|
||||
|
||||
return eb.or([
|
||||
eb.not(eb.exists(linkedTo)),
|
||||
|
||||
Reference in New Issue
Block a user