merge dev into feat/postgres-support

This commit is contained in:
Maurice
2026-11-17 11:10:01 +01:00
3032 changed files with 153102 additions and 20800 deletions
@@ -0,0 +1,62 @@
import type { DB } from '../../kysely/db';
import { accommodationIdMatches } from './reservation-accommodation';
import type { Platform } from '@mikro-orm/core';
import type { Kysely } from 'kysely';
/** The tables the joined booking projection reads. */
export type ReservationJoinDB = Pick<DB, 'reservations' | 'days' | 'places' | 'day_accommodations'>;
/**
* A booking with its day number, its place's name and its stay's place and
* times: the projection `ReservationsRepository.listForTrip` and
* `findWithJoins` narrow with their own `WHERE`. Kept out of the repository
* file, which is at its size baseline. `accommodation_id` is TEXT and `ap.id`
* INTEGER, so the stay is matched through accommodationIdMatches: SQLite
* coerces the bare comparison, Postgres refuses it.
*/
export function joinedReservationQuery(kysely: Kysely<ReservationJoinDB>, platform: Platform) {
return kysely
.selectFrom('reservations as r')
.leftJoin('days as d', 'd.id', 'r.day_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
.leftJoin('day_accommodations as ap', (join) =>
join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),
)
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.selectAll('r')
.select([
'd.day_number as day_number',
'p.name as place_name',
'ap.place_id as accommodation_place_id',
'acc_p.name as accommodation_name',
'ap.start_day_id as accommodation_start_day_id',
'ap.end_day_id as accommodation_end_day_id',
'ap.check_in as accommodation_check_in',
'ap.check_in_end as accommodation_check_in_end',
'ap.check_out as accommodation_check_out',
]);
}
/**
* The booking an import already wrote into this trip under the same origin,
* or undefined: what `idx_reservations_external (external_source,
* external_id, trip_id)` would refuse, read before the write so a repeat
* import is reported instead of failing on the unique index
* (`ReservationsRepository.findIdByExternal`).
*/
export async function idByExternalQuery(
kysely: Kysely<Pick<DB, 'reservations'>>,
trip_id: number,
external_source: string,
external_id: string,
): Promise<number | undefined> {
const row = await kysely
.selectFrom('reservations')
.select(['id'])
.where('trip_id', '=', trip_id)
.where('external_source', '=', external_source)
.where('external_id', '=', external_id)
.executeTakeFirst();
return row?.id;
}
@@ -0,0 +1,21 @@
/**
* Where an imported booking came from and its key there, as the reservations
* table stores them. Named on an insert only when the caller passes them: an
* imported booking records its origin, every other write leaves the columns to
* their defaults (`sync_enabled INTEGER DEFAULT 1`). Kept out of
* Reservations.repository.ts, which is at its size baseline.
*/
export interface ReservationOriginColumns {
external_source?: string;
external_id?: string;
sync_enabled?: number;
}
/** The origin columns an insert names: only those the caller set. */
export function originColumnValues(input: ReservationOriginColumns): ReservationOriginColumns {
return {
...(input.external_source !== undefined && { external_source: input.external_source }),
...(input.external_id !== undefined && { external_id: input.external_id }),
...(input.sync_enabled !== undefined && { sync_enabled: input.sync_enabled }),
};
}
@@ -0,0 +1,46 @@
import type { DB } from '../../kysely/db';
import type { Kysely } from 'kysely';
/** The tables `passContextQuery` reads. */
export type PassContextDB = Pick<DB, 'reservations' | 'trips' | 'places' | 'day_accommodations'>;
/** What a booking's Wallet pass needs beyond the booking: where it is, and the trip's name. */
export interface ReservationPassContext {
trip_title: string | null;
place_lat: number | null;
place_lng: number | null;
place_name: string | null;
}
/**
* The trip's title and the place a booking is at (its own place, else its
* stay's), for its Apple Wallet pass (`ReservationsRepository.findPassContext`).
* Scoped to the trip, so a booking of another trip reads as none. Kept out of
* the repository file, which is at its size baseline.
*/
export async function passContextQuery(
kysely: Kysely<PassContextDB>,
id: number,
trip_id: number,
): Promise<ReservationPassContext | undefined> {
const row = await kysely
.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'))),
)
.leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id')
.where('r.id', '=', id)
.where('r.trip_id', '=', trip_id)
.select([
't.title as trip_title',
(eb) => eb.fn.coalesce('p.lat', 'acc_p.lat').as('place_lat'),
(eb) => eb.fn.coalesce('p.lng', 'acc_p.lng').as('place_lng'),
(eb) => eb.fn.coalesce('p.name', 'acc_p.name').as('place_name'),
])
.executeTakeFirst();
return row as ReservationPassContext | undefined;
}