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.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
import type { Reservations } from '../entities/Reservations.entity';
import { DayAssignments } from '../entities/DayAssignments.entity';
import { Days } from '../entities/Days.entity';
import { coalesceOverride, columnRef, dayDistance, isCalendarDate } from '../dialect/sql-functions';
import { concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions';
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';
import { passContextQuery } from './_shared/reservation-pass-context';
import { originColumnValues, type ReservationOriginColumns } from './_shared/reservation-origin-columns';
import { idByExternalQuery, joinedReservationQuery, type ReservationJoinDB } from './_shared/reservation-joined-query';
import type { DB } from '../kysely/db';
import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared';
import type {
PublicApiAccommodationKyselyDB,
PublicApiAccommodationRow,
PublicApiReservationKyselyDB,
PublicApiScheduledReservationRow,
PublicApiUnplannedPlaceKyselyDB,
PublicApiUnplannedPlaceRow,
PublicApiUnscheduledReservationRow,
} from './reservation-public-api-rows';
import type { ReservationAllColumnsRow, ReservationJoinRow } from './reservation-rows';
// The reservation row shapes live in their own module; re-exported so callers keep one import path.
export type { ReservationAllColumnsRow, ReservationJoinRow } from './reservation-rows';
// The public API row types moved to their own module; re-exported so callers keep one import path.
export type {
PublicApiAccommodationRow,
PublicApiScheduledReservationRow,
PublicApiUnplannedPlaceRow,
PublicApiUnscheduledReservationRow,
};
/**
* `reservations` as the two aliases below bind it: `accommodation_id` is a
* TEXT column, widened to `string | number | null` so the plain `number`
* their statements bind against it type-checks (R2's ruling).
*/
type ReservationsNumericAccommodationBind = Omit<DB['reservations'], 'accommodation_id'> & { accommodation_id: string | number | null };
/**
* Kysely typing for `restampLinkedReservation` (DY23). `accommodation_id:
* string | number | null` — R2's ruling: the legacy statement binds `accId`
* (a plain JS `number`, `stay.id`) straight into the `:accId` named param
* compared against the TEXT column, so this method's caller passes the SAME
* number, unconverted, and the interface is widened only so that bind
* type-checks (SQLite applies its own TEXT-affinity conversion at compare
* time regardless of which JS type bound the value — see the method's own
* docstring).
*/
type ReservationRestampKyselyDB = { reservations: ReservationsNumericAccommodationBind };
/**
* Kysely typing for AC37/AC40 (`listIdMetadataByStay`/`listIdsByStay`).
*
* **Revert to the REAL-bound compare (Plan 3d Task 7 whole-plan review, M3;
* corrects this docstring's earlier claim).** A bound-parameter `WHERE
* accommodation_id = ?` with a plain JS `number` is EXACTLY the comparison
* the legacy statement's own `Number(id)` bind produced: `better-sqlite3`
* binds every plain JS number as SQLite REAL, integer-valued or not
* (`typeof(?)` on a bound `14` reads back `'real'`) — including through
* MikroORM's Kysely dialect — and SQLite's TEXT-affinity conversion of a
* REAL for a comparison renders its decimal form (`14` → `'14.0'`), so a
* bound `WHERE accommodation_id = ?` matches `'14.0'` ONLY, same as the
* legacy statement — never `'14'`. The earlier version of this docstring
* claimed the opposite (that `em.insert()`/`nativeUpdate()` calls in this
* cluster store the bare `'14'` shape and a REAL-bound compare would
* therefore miss them): that was true only because RS28/RS41
* (`reservations.service.ts`, outside this repository) used to write
* `String(n)` instead of the legacy's REAL-bound shape (H1, the same
* review). H1 fixes every write in this cluster to store `'<id>.0'`
* (`legacyBoundIntegerText`), so the widening below has no remaining
* reason to exist, and parity is law once it doesn't: `CAST(accommodation_id
* AS INTEGER) = ?` (via {@link castIntegerKysely}) matches `'14'`, `'14.0'`,
* `'14 '` and `'14abc'` alike — a WIDER row set than the legacy's own
* `'14.0'`-only compare (measured, task-7-review.md M3) — which is an
* unpinned parity break, not a fix, on a read the legacy also bound
* `Number(id)` for. `castIntegerKysely` stays in use elsewhere in this file
* (RS20/RV2's correlated subqueries) where the legacy statement's OWN
* comparison used a `CAST`, not a bound number — this interface's two
* consumers are not that case.
*/
type ReservationsByAccommodationKyselyDB = { reservations: ReservationsNumericAccommodationBind };
// ---------------------------------------------------------------------------
// Plan 3d Task 4 (calendar, `listUpcoming`, the visibility-predicate
// consumers incl. `share`, the `public-api` pickup) — additive. `calendar/**`
// and `share/**`'s reads on `day_accommodations`/`reservation_endpoints`/
// `reservation_day_positions` land here rather than on those tables' own
// repositories: `RoadtripVias`/`RoadtripDayTracks`/`RoadtripPreferences`/
// `RoadtripDayBoundaries`/`DayAccommodations`/`ReservationEndpoints`/
// `ReservationTravelers` repositories are Task 6's file set for this window
// (trip copy's additive methods), so a cross-table Kysely read — the SAME
// escape hatch `joinedQuery()`/`getAssignmentTripId`/`findNearestDayId`
// above already use to reach `days`/`places`/`day_accommodations` from this
// file — lands the read here instead of opening a second implementer's file.
// `DayAssignments.repository.ts` is not in Task 6's set, but CL4's read is
// kept alongside its RS20/CL2/CL7 siblings for the same "one file, one
// diff" reason.
// ---------------------------------------------------------------------------
/**
* CL2 (`CalendarService.buildTripCalendar`'s reservation read) — `SELECT
* r.*, pl.lat AS place_lat, pl.lng AS place_lng, sd.date AS stay_start_date,
* ed.date AS stay_end_date, a.check_in AS stay_check_in, a.check_out AS
* stay_check_out, (SELECT MIN(r2.id) FROM reservations r2 WHERE
* r2.accommodation_id = a.id) AS stay_first_reservation_id, rd.date AS
* day_date, red.date AS end_day_date FROM reservations r LEFT JOIN places
* pl … LEFT JOIN day_accommodations a ON r.accommodation_id = a.id LEFT
* JOIN days sd/ed/rd/red … WHERE r.trip_id = ? AND <RV1('r')>`, no `ORDER
* BY`. `r.accommodation_id = a.id` and `r2.accommodation_id = a.id` are
* plain column-to-column joins (no `CAST`) — SQLite applies numeric
* affinity to the TEXT side against a genuine INTEGER column on its own
* (§18.1), the same reasoning `joinedQuery()`'s `ap`/`acc_p` joins above
* already rely on; a `CAST` is only needed where one side is a BOUND value
* (RV2, RS20's correlated title subquery). The `stay_first_reservation_id`
* subquery is deliberately **not** filtered by RV1 — inventory §18.2's
* documented, unfixed hole (a staged sibling booking with a lower id can
* win the slot and suppress a public booking's all-day block); kept
* byte-for-byte, pinned by CAL-CL2-HOLE-001 in the repository test.
*/
export interface CalendarReservationRow extends ReservationAllColumnsRow {
place_lat: number | null;
place_lng: number | null;
stay_start_date: string | null;
stay_end_date: string | null;
stay_check_in: string | null;
stay_check_out: string | null;
stay_first_reservation_id: number | null;
day_date: string | null;
end_day_date: string | null;
}
type CalendarReservationKyselyDB = Pick<DB, 'reservations' | 'places' | 'day_accommodations' | 'days'>;
/**
* CL4 (`CalendarService.buildTripCalendar`'s assignment read, for every day
* of the trip at once, undated ones for a linked booking's zone) — `SELECT da.*, p.name as place_name, p.address as
* place_address, p.lat as place_lat, p.lng as place_lng, COALESCE(da.stay_minutes,
* p.duration_minutes) as place_duration_minutes, COALESCE(da.assignment_time, p.place_time) as
* effective_time, COALESCE(da.assignment_end_time, p.end_time) as
* effective_end_time FROM
* day_assignments da JOIN places p ON da.place_id = p.id WHERE da.day_id IN
* (...) AND da.accommodation_id IS NULL ORDER BY da.order_index, da.created_at,
* da.id` — the booked-night stop (its own accommodation-linked row) is
* excluded: it is the booking, and reading it here would put the hotel on the
* day a second time. Each day's rows keep the per-day order; the id settles a tie.
*/
export interface CalendarStopRow extends DayAssignmentRow {
place_name: string;
place_address: string | null;
place_lat: number | null;
place_lng: number | null;
place_duration_minutes: number | null;
effective_time: string | null;
effective_end_time: string | null;
}
type CalendarStopsKyselyDB = Pick<DB, 'day_assignments' | 'places'>;
/**
* CL7 (`CalendarService.buildTripCalendar`'s check-in/check-out stay read)
* — `SELECT a.id, a.check_in, a.check_in_end, a.check_out, sd.date AS
* start_date, ed.date AS end_date, p.name AS place_name, p.address AS
* place_address, p.lat AS place_lat, p.lng AS place_lng, (SELECT r.title
* FROM reservations r WHERE r.accommodation_id = a.id AND <RV1('r')> ORDER
* BY r.id ASC LIMIT 1) AS reservation_title FROM day_accommodations a LEFT
* JOIN days sd … LEFT JOIN days ed … LEFT JOIN places p … WHERE a.trip_id =
* ? AND <RV2('a')> ORDER BY a.id ASC`. The `reservation_title` subquery is
* a plain column-to-column compare too (no `CAST`), same reasoning as CL2.
* `.where('a.trip_id', ...).where(publicStayExists)` are chained BEFORE the
* `sd`/`ed`/`p` joins below (Kysely renders WHERE after JOIN regardless of
* call order — the AST, not the call sequence, decides clause order) so
* `publicStayExists`'s narrow `ExpressionBuilder<…, 'a'>` signature sees
* exactly the `a` alias it requires (Task 0 review's own finding: a
* differently-joined outer query typechecks as long as the alias stays
* `a`).
*/
export interface CalendarStayRow {
id: number;
check_in: string | null;
check_in_end: string | null;
check_out: string | null;
start_date: string | null;
end_date: string | null;
place_name: string | null;
place_address: string | null;
place_lat: number | null;
place_lng: number | null;
reservation_title: string | null;
}
type CalendarStayKyselyDB = ReservationVisibilityKyselyDB & Pick<DB, 'day_accommodations' | 'reservations' | 'days' | 'places'>;
/**
* RS20 (`ReservationsService.listUpcoming`) — the CTE + `UNION ALL`
* statement, inventory §2e. Ruling (ii): ONE fully typed Kysely statement
* (no `connection.execute()` exception, no JS merge/re-sort — the reviewer's
* prototype matched legacy rows across 5 `(today, now, limit)` settings
* incl. the cross-table `id` tiebreak, and the plan adopted that). GLOB
* through `startsWithIsoDateKysely`; `CAST` through `castIntegerKysely`
* (the correlated title subquery compares a BOUND `a.id` — an outer-query
* value, not a plain column-to-column join, unlike CL2/CL7's `MIN`/title
* subqueries — so the legacy statement casts here too:
* `CAST(res.accommodation_id AS INTEGER) = a.id`); `||` through
* `concatKysely`; `substr` through `substringKysely`. `LIMIT` stays inside
* SQL; the SQLite collation `ORDER BY at_date ASC, COALESCE(at_time,
* '00:00') ASC, id ASC` (the cross-table `id` tiebreak — a stay id and a
* reservation id can collide) is reproduced exactly, never a JS `.sort()`.
*
* Each of the three `entries` arms is `$castTo<EntriesRow>()`'d before the
* `unionAll` chain: Kysely infers a literal type (`'checkin'`) for
* `eb.val('checkin').as('type')` in arms 2/3 against `r.type: string |
* null` in arm 1, and the three arms' `title`/`reservation_time` sources
* differ in nullability shape too — the union of literal/nullable variants
* doesn't collapse into one column type on its own, the same
* `DayStopRow`/`AssignmentTimeSortKyselyDB` precedent
* (`DayAssignments.repository.ts::listForTimeSort`) uses for its own
* `eb.and([...])`-inferred boolean column.
*/
export interface UpcomingReservationRow {
id: number;
trip_id: number;
title: string;
type: string | null;
status: string | null;
location: string | null;
reservation_time: string | null;
confirmation_number: string | null;
trip_title: string;
trip_cover: string | null;
day_date: string | null;
place_name: string | null;
place_image: string | null;
}
/** RS20's `entries` CTE row shape — see {@link UpcomingReservationRow}'s docstring for why every arm is cast to this. */
interface EntriesRow extends UpcomingReservationRow {
at_date: string | null;
at_time: string | null;
}
type UpcomingReservationsKyselyDB = Pick<DB, 'trips' | 'trip_members' | 'reservations' | 'days' | 'places' | 'day_accommodations'>;
/**
* `share.service.ts:251` (`publicEndpointsByReservation`) — `SELECT
* e.reservation_id, e.role, e.sequence, e.name, e.code, e.lat, e.lng,
* e.timezone, e.local_date, e.local_time FROM reservation_endpoints e JOIN
* reservations r ON r.id = e.reservation_id WHERE r.trip_id = ? ORDER BY
* e.reservation_id ASC, e.sequence ASC`. Not filtered by RV1 — the legacy
* statement loads every reservation's endpoints regardless of
* `ingest_state`, and the caller only ever looks one up for a reservation
* `share.service.ts`'s OWN filtered read already returned (parity, matching
* `CalendarService.loadEndpointsByTrip`'s identical shape, §4's CL6 note).
*/
export interface ShareEndpointRow {
reservation_id: number;
role: string;
sequence: number;
name: string;
code: string | null;
lat: number;
lng: number;
timezone: string | null;
local_date: string | null;
local_time: string | null;
}
type ShareEndpointsKyselyDB = Pick<DB, 'reservation_endpoints' | 'reservations'>;
/**
* `share.service.ts:368` (`getSharedTripData`'s day-position read) —
* `SELECT rdp.reservation_id, rdp.day_id, rdp.position FROM
* reservation_day_positions rdp JOIN reservations r ON rdp.reservation_id =
* r.id WHERE r.trip_id = ?`, no `ORDER BY`, no RV1 filter (same reasoning
* as the endpoints read above — the caller only consults `posMap` for a
* reservation its own filtered read already returned).
*/
export interface ShareDayPositionRow {
reservation_id: number;
day_id: number;
position: number;
}
type ShareDayPositionsKyselyDB = Pick<DB, 'reservation_day_positions' | 'reservations'>;
/**
* `share.service.ts:387` (`getSharedTripData`'s public booking read) —
* `SELECT ${PUBLIC_RESERVATION_COLUMNS} FROM reservations r WHERE r.trip_id
* = ? AND <RV1('r')> ORDER BY r.reservation_time ASC` —
* `PUBLIC_RESERVATION_COLUMNS` is `id, trip_id, day_id, end_day_id,
* place_id, accommodation_id, title, type, status, location,
* reservation_time, reservation_end_time, notes, url, metadata,
* created_at`, the allow-list a public link may see (never the
* confirmation number, the import trail or who is travelling).
*/
export interface SharePublicReservationRow {
id: number;
trip_id: number;
day_id: number | null;
end_day_id: number | null;
place_id: number | null;
accommodation_id: string | null;
title: string;
type: string | null;
status: string | null;
location: string | null;
reservation_time: string | null;
reservation_end_time: string | null;
notes: string | null;
url: string | null;
metadata: string | null;
created_at: string | null;
}
type SharePublicReservationKyselyDB = Pick<DB, 'reservations'>;
/**
* `share.service.ts:401` (`getSharedTripData`'s public stay read) —
* `SELECT ${PUBLIC_ACCOMMODATION_COLUMNS}, p.name as place_name, p.address
* as place_address, p.lat as place_lat, p.lng as place_lng FROM
* day_accommodations a JOIN places p ON a.place_id = p.id WHERE a.trip_id =
* ? AND <RV2('a')>` — an INNER `JOIN`, not `LEFT`, unlike CL7: a stay with
* no linked place is silently excluded from a public link (parity, kept
* exactly). `PUBLIC_ACCOMMODATION_COLUMNS` is `id, trip_id, place_id,
* start_day_id, end_day_id, check_in, check_in_end, check_out, notes`.
*/
export interface SharePublicAccommodationRow {
id: number;
trip_id: number;
place_id: number | null;
start_day_id: number | null;
end_day_id: number | null;
check_in: string | null;
check_in_end: string | null;
check_out: string | null;
notes: string | null;
place_name: string | null;
place_address: string | null;
place_lat: number | null;
place_lng: number | null;
}
type SharePublicAccommodationKyselyDB = ReservationVisibilityKyselyDB & Pick<DB, 'day_accommodations' | 'places'>;
/** `AirportsService.backfillFlightEndpoints` (AIR1): the tables its `NOT EXISTS` guard reads. */
type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_endpoints'>;
export class ReservationsRepository extends TrekRepository<Reservations> {
// For its Wallet pass (_shared/reservation-pass-context.ts).
findPassContext(id: number, tripId: number) { return passContextQuery(this.getEntityManager(), id, tripId); }
private joinedQuery() {
return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());
}
/**
* RS18 (`ReservationsService.list`) — the joined projection for a whole
* trip, `ORDER BY r.reservation_time ASC, r.created_at ASC`.
*/
async listForTrip(trip_id: number | string): Promise<ReservationJoinRow[]> {
const rows = await this.joinedQuery()
.where('r.trip_id', '=', trip_id as number)
.orderBy('r.reservation_time', 'asc')
.orderBy('r.created_at', 'asc')
.execute();
return rows as ReservationJoinRow[];
}
/**
* RR1 (`ReservationsReadService.getReservationWithJoins`) — the SAME
* projection for one reservation, no `ORDER BY` (a single row), and only
* when it belongs to `trip_id`: every caller holds the trip it checked, so a
* booking id from another trip reads as missing rather than leaking its row.
*/
async findWithJoins(id: number | string, trip_id: number | string): Promise<ReservationJoinRow | undefined> {
const row = await this.joinedQuery()
.where('r.id', '=', id as number)
.where('r.trip_id', '=', trip_id as number)
.executeTakeFirst();
return row as ReservationJoinRow | undefined;
}
/**
* RS35 (`ReservationsService.getReservation`) — `SELECT * FROM
* reservations WHERE id = ? AND trip_id = ?`, the trip-scoping guard every
* write path re-reads through. `id`/`trip_id: number` (rule 21 — the
* SERVICE parses both ONCE via `toRowId` before calling this, and every
* later write in the same method reuses that same parsed value, never a
* second, independent conversion).
*/
async findInTrip(id: number, trip_id: number): Promise<ReservationAllColumnsRow | undefined> {
return this.qb('r')
.select(['r.*'])
.where({ id, trip: trip_id })
.execute<ReservationAllColumnsRow | undefined>('get', false);
}
/** RS45 (`ReservationsService.remove`'s pre-image read) — `SELECT id, title, type, accommodation_id FROM reservations WHERE id = ? AND trip_id = ?`. */
async findHeaderInTrip(id: number, trip_id: number): Promise<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined> {
return this.qb('r')
.select(['r.id', 'r.title', 'r.type', 'r.accommodation_id'])
.where({ id, trip: trip_id })
.execute<{ id: number; title: string; type: string | null; accommodation_id: string | null } | undefined>('get', false);
}
/**
* RS12 (`ReservationsService.resyncReservationDays`) — `SELECT id,
* reservation_time, reservation_end_time, day_id, end_day_id FROM
* reservations WHERE trip_id = ? AND (type != 'hotel' OR accommodation_id
* IS NULL) AND reservation_time IS NOT NULL`.
*/
async listResyncCandidates(trip_id: number): Promise<{
id: number; reservation_time: string | null; reservation_end_time: string | null;
day_id: number | null; end_day_id: number | null;
}[]> {
const platform = this.getEntityManager().getPlatform();
return this.qb('r')
.select(['r.id', 'r.reservation_time', 'r.reservation_end_time', columnRef(platform, 'r.day_id').as('day_id'), columnRef(platform, 'r.end_day_id').as('end_day_id')])
.where({
trip: trip_id,
$or: [{ type: { $ne: 'hotel' } }, { accommodation_id: null }],
reservation_time: { $ne: null },
})
.execute<{ id: number; reservation_time: string | null; reservation_end_time: string | null; day_id: number | null; end_day_id: number | null }[]>('all', false);
}
/** RS13 (`resyncReservationDays`'s per-row write) — `UPDATE reservations SET day_id = ?, end_day_id = ? WHERE id = ?`. */
async setDays(id: number, day_id: number | null, end_day_id: number | null): Promise<void> {
await this.nativeUpdate({ id }, { day: day_id, endDay: end_day_id });
}
/**
* RS28 (`ReservationsService.createInTx`) — `INSERT INTO reservations
* (trip_id, day_id, end_day_id, place_id, assignment_id, title,
* reservation_time, reservation_end_time, location, confirmation_number,
* notes, url, status, type, accommodation_id, metadata, needs_review)
* VALUES (?×17)`. `ingest_state`/`day_plan_position` deliberately NOT
* named, so the column's own DB-level `DEFAULT` applies (`ingest_state`
* `.default('live')`) — the caller has already resolved every value (the
* `|| null` coercions, `resolvedOrNull`, the numeric `accommodation_id`
* stringified for the TEXT column, R2).
*
* `external_source`/`external_id`/`sync_enabled` are named only when the
* caller passes them: an imported booking records where it came from, every
* other write leaves them to the column defaults as before (`sync_enabled
* INTEGER DEFAULT 1`, `Migration20200101021800_…`).
*/
async insertReservation(input: {
trip_id: number;
day_id: number | null;
end_day_id: number | null;
place_id: number | null;
assignment_id: number | null;
title: string;
reservation_time: string | null;
reservation_end_time: string | null;
location: string | null;
confirmation_number: string | null;
notes: string | null;
url: string | null;
status: string;
type: string;
accommodation_id: string | null;
metadata: string | null;
needs_review: number;
} & ReservationOriginColumns): Promise<number> {
return await this.insert({
trip: input.trip_id,
day: input.day_id,
endDay: input.end_day_id,
place: input.place_id,
assignment: input.assignment_id,
title: input.title,
reservation_time: input.reservation_time,
reservation_end_time: input.reservation_end_time,
location: input.location,
confirmation_number: input.confirmation_number,
notes: input.notes,
url: input.url,
status: input.status,
type: input.type,
accommodation_id: input.accommodation_id,
metadata: input.metadata,
needs_review: input.needs_review,
...originColumnValues(input),
});
}
/** The booking an import already wrote into this trip under the same origin (_shared/reservation-joined-query.ts). */
findIdByExternal(trip_id: number, external_source: string, external_id: string): Promise<number | undefined> {
return idByExternalQuery(this.kysely<DB>(), trip_id, external_source, external_id);
}
/**
* RS41 (`ReservationsService.updateInTx`) — the 16-column `UPDATE
* reservations SET … WHERE id = ?`. The `PlacesRepository.updatePlace`/PL11
* precedent: the four legacy `COALESCE(?, col)` keep-if-null columns
* (`title`, `status`, `type`, `needs_review`) are resolved to their FINAL
* value by the SERVICE first (it already holds the pre-image via the
* `current` parameter every caller passes), so this writes exactly what it
* is handed — no SQL-side COALESCE. `id: number` (rule 21 — the SAME
* `toRowId`-parsed value `findInTrip`'s gate read used).
*/
async updateReservation(id: number, write: {
title: string;
reservation_time: string | null;
reservation_end_time: string | null;
location: string | null;
confirmation_number: string | null;
notes: string | null;
url: string | null;
day_id: number | null;
end_day_id: number | null;
place_id: number | null;
assignment_id: number | null;
status: string;
type: string;
accommodation_id: string | null;
metadata: string | null;
needs_review: number;
}): Promise<void> {
await this.nativeUpdate({ id }, {
title: write.title,
reservation_time: write.reservation_time,
reservation_end_time: write.reservation_end_time,
location: write.location,
confirmation_number: write.confirmation_number,
notes: write.notes,
url: write.url,
day: write.day_id,
endDay: write.end_day_id,
place: write.place_id,
assignment: write.assignment_id,
status: write.status,
type: write.type,
accommodation_id: write.accommodation_id,
metadata: write.metadata,
needs_review: write.needs_review,
});
}
/**
* RS33 (`updatePositions`'s legacy/global branch) — `UPDATE reservations
* SET day_plan_position = ? WHERE id = ? AND trip_id = ?`. `position:
* number | null`, never `undefined` (R8 — the SERVICE maps an absent
* `day_plan_position` to `null` explicitly before calling, so
* `nativeUpdate` always WRITES the column instead of silently skipping it).
*/
async setDayPlanPosition(id: number, trip_id: number, position: number | null): Promise<void> {
await this.nativeUpdate({ id, trip: trip_id }, { day_plan_position: position });
}
/** RS50 (`remove`) — `DELETE FROM reservations WHERE id = ?`, unscoped (the caller already proved trip access via `findHeaderInTrip`). */
async deleteById(id: number): Promise<void> {
await this.nativeDelete({ id });
}
/** DY14 (`DaysService.restampReservationDates`) — `SELECT id, day_id, end_day_id, reservation_time, reservation_end_time FROM reservations WHERE trip_id = ?`. */
async listForRestamp(trip_id: number): Promise<{
id: number; day_id: number | null; end_day_id: number | null;
reservation_time: string | null; reservation_end_time: string | null;
}[]> {
const platform = this.getEntityManager().getPlatform();
return this.qb('r')
.select(['r.id', columnRef(platform, 'r.day_id').as('day_id'), columnRef(platform, 'r.end_day_id').as('end_day_id'), 'r.reservation_time', 'r.reservation_end_time'])
.where({ trip: trip_id })
.execute<{ id: number; day_id: number | null; end_day_id: number | null; reservation_time: string | null; reservation_end_time: string | null }[]>('all', false);
}
/** DY15 — `UPDATE reservations SET reservation_time = ? WHERE id = ?`. */
async setReservationTime(id: number, reservation_time: string): Promise<void> {
await this.nativeUpdate({ id }, { reservation_time });
}
/** DY16 — `UPDATE reservations SET reservation_end_time = ? WHERE id = ?`. */
async setReservationEndTime(id: number, reservation_end_time: string): Promise<void> {
await this.nativeUpdate({ id }, { reservation_end_time });
}
/**
* DY23 (`DaysService.resyncAccommodationDays`) — the named-param `UPDATE
* reservations SET day_id = :dayId, reservation_time = CASE WHEN
* reservation_time IS NULL THEN :date ELSE :date || SUBSTR(reservation_time,
* 11) END WHERE accommodation_id = :accId AND type = 'hotel'`. Kysely (the
* `CASE WHEN … ELSE … || SUBSTR(...)` shape has no typed-filter
* equivalent — the T6 tier per the inventory's own ruling), through
* `concatKysely`/`substringKysely` (`kysely-functions.ts`, the Kysely-expression
* twins of `concat`/`substring` — the MikroORM `RawQueryFragment` forms
* render as a literal `?` token inside a Kysely statement and throw at
* execution, Task 0 review H1). `accommodation_id` (TEXT) compared against
* `accId` (a bound NUMBER, exactly as the legacy statement bound it — R2,
* never stringified here, so the SAME SQLite TEXT-affinity conversion the
* legacy statement triggered still applies, including its own miss on a
* `"14.0"`-shaped row — parity, not a fix).
*/
async restampLinkedReservation(accommodation_id: number, day_id: number, date: string): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.kysely<ReservationRestampKyselyDB>()
.updateTable('reservations')
.set((eb) => ({
day_id,
reservation_time: eb
.case()
.when('reservation_time', 'is', null)
.then(eb.val(date))
.else(concatKysely(platform, eb, { value: date }, { expression: substringKysely(platform, eb, 'reservation_time', 11) }))
.end(),
}))
.where((eb) => reservationStayIs(eb, 'accommodation_id', accommodation_id))
.where('type', '=', 'hotel')
.execute();
}
/**
* RS22's dispatch (`ReservationsService.referencesOutsideTrip`'s
* `assignment_id` branch) — `SELECT d.trip_id FROM day_assignments da JOIN
* days d ON da.day_id = d.id WHERE da.id = ?`. Reached through
* `DayAssignmentsRepository`'s own relation join (`da.day` — the same
* cross-repository-read shape `TripMembersRepository.rosterUserIds`
* already uses for a table this repository does not own), never a new
* method on that file
* (`DayAssignmentsRepository`/`DaysRepository` are Task 1's
* additive-method territory per the file-ownership split).
*/
async getAssignmentTripId(assignment_id: number): Promise<number | undefined> {
const platform = this.getEntityManager().getPlatform();
const dayAssignments = this.getEntityManager().getRepository(DayAssignments);
const row = await dayAssignments
.qb('da')
.join('da.day', 'd')
.select([columnRef(platform, 'd.trip_id').as('trip_id')])
.where({ id: assignment_id })
.execute<{ trip_id: number } | undefined>('get', false);
return row?.trip_id;
}
/**
* RS11 (`resolveDayIdFromTime`'s nearest-day fallback) — `SELECT id FROM
* days WHERE trip_id = ? ORDER BY ABS(JULIANDAY(date) - JULIANDAY(?)) ASC,
* date ASC LIMIT 1`, through `DaysRepository`'s own QB. A date the calendar does
* not have (`2024-02-30`) finds no day on either engine (`isCalendarDate`).
*/
async findNearestDayId(trip_id: number, iso_date: string): Promise<number | undefined> {
if (!isCalendarDate(iso_date)) return undefined;
const platform = this.getEntityManager().getPlatform();
const days = this.getEntityManager().getRepository(Days);
const row = await days
.qb('d')
.select(['d.id'])
.where({ trip: trip_id })
.orderBy([{ [dayDistance(platform, 'd.date', iso_date)]: 'asc' }, { date: 'asc' }])
.limit(1)
.execute<{ id: number } | undefined>('get', false);
return row?.id;
}
// ---------------------------------------------------------------------------
// Plan 3d Task 3 (`AccommodationsService`) — additive, per this task's own
// file-ownership rule ("additive methods on Days/Places/DayAssignments/
// Reservations repositories where a read belongs there").
// ---------------------------------------------------------------------------
/**
* AC33 (`AccommodationsService.createAccommodation`) — `INSERT INTO
* reservations (trip_id, day_id, title, reservation_time, location,
* confirmation_number, notes, status, type, accommodation_id, metadata)
* VALUES (?, ?, ?, ?, ?, ?, ?, 'confirmed', 'hotel', ?, ?)` — the
* auto-created partner hotel booking. `status`/`type` literals, `location`
* always `null` (matching the legacy statement exactly — it never binds a
* value there). `accommodation_id: string` — R2: the caller stringifies
* the new stay's numeric id (`String(newId)`) the same way `insertReservation`'s
* own `accommodation_id` caller already does, so SQLite's TEXT-affinity
* storage is identical to the legacy raw bind of the bare number.
*/
async insertHotelPartner(input: {
trip_id: number | string;
day_id: number;
title: string;
reservation_time: string | null;
confirmation_number: string | null;
notes: string | null;
accommodation_id: string;
metadata: string | null;
}): Promise<number> {
return await this.insert({
trip: input.trip_id,
day: input.day_id,
title: input.title,
reservation_time: input.reservation_time,
location: null,
confirmation_number: input.confirmation_number,
notes: input.notes,
// A stay without a confirmation number is not a confirmed booking yet.
status: input.confirmation_number ? 'confirmed' : 'pending',
type: 'hotel',
accommodation_id: input.accommodation_id,
metadata: input.metadata,
});
}
/**
* AC37 (`AccommodationsService.updateAccommodation`) — `SELECT id,
* metadata FROM reservations WHERE accommodation_id = ?`, every linked
* booking (no unique constraint on the column — more than one booking can
* point at the same stay). Bound as a plain `number`, matching the
* legacy's own `Number(id)` bind byte-for-byte — see
* {@link ReservationsByAccommodationKyselyDB}'s docstring (M3, reverted
* from the `castIntegerKysely` widening).
*/
async listIdMetadataByStay(accommodation_id: number): Promise<{ id: number; metadata: string | null }[]> {
const rows = await this.kysely<ReservationsByAccommodationKyselyDB>()
.selectFrom('reservations')
.select(['id', 'metadata'])
.where((eb) => reservationStayIs(eb, 'accommodation_id', accommodation_id))
.execute();
return rows as { id: number; metadata: string | null }[];
}
/**
* AC38 (`AccommodationsService.updateAccommodation`) — `UPDATE
* reservations SET metadata = ?, confirmation_number = COALESCE(?,
* confirmation_number) WHERE id = ?`. Runs AFTER `updateAccommodation`'s
* own transaction commits (§18.6 — R5 class, flagged not fixed).
* `coalesceOverride` for the value-side COALESCE — the bound `?` FIRST,
* the column the fallback, so the new value wins
* (`DayAccommodationsRepository.patchConfirmation`'s precedent).
*/
async setMetadataAndConfirmation(id: number, metadata: string, confirmation: string | null): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.qb()
.update({
metadata,
confirmation_number: coalesceOverride(platform, confirmation, 'confirmation_number'),
})
.where({ id })
.execute('run');
}
/** A pending hotel booking turns confirmed once its stay gets a confirmation number; other statuses stay. */
async confirmIfPending(id: number): Promise<void> {
await this.kysely<ReservationsByAccommodationKyselyDB>()
.updateTable('reservations')
.set({ status: 'confirmed' })
.where('id', '=', id)
.where('status', '=', 'pending')
.execute();
}
/**
* AC40 (`AccommodationsService.deleteAccommodation`) — `SELECT id FROM
* reservations WHERE accommodation_id = ?`, ALL linked bookings. Same
* REAL-bound compare as {@link listIdMetadataByStay} (M3).
*/
async listIdsByStay(accommodation_id: number): Promise<{ id: number }[]> {
const rows = await this.kysely<ReservationsByAccommodationKyselyDB>()
.selectFrom('reservations')
.select(['id'])
.where((eb) => reservationStayIs(eb, 'accommodation_id', accommodation_id))
.execute();
return rows as { id: number }[];
}
// ---------------------------------------------------------------------------
// Plan 3d Task 6 (`trips.service.ts::copy`) — moved here from
// `ReservationEndpoints.repository.ts` in the Task 7 whole-plan review's
// fix wave (item 8/D4, "one repository per table"): Task 4 owned this file
// for the Task 6 tree window, so TP58/TP59 (the reservations copy
// read/write) landed on `ReservationEndpointsRepository` instead at the
// time — a pure move now that the window is closed, same statements,
// same parity proof (`Reservations.repository.test.ts`). Endpoints/
// travelers/day-positions are never copied with a reservation (R7,
// `copy` never copied them — kept as-is on their own repositories).
// ---------------------------------------------------------------------------
/**
* TP58 (`trips.service.ts::copy`'s reservations read) — `SELECT * FROM
* reservations WHERE trip_id = ?`, no `ORDER BY` (matching the legacy
* statement; the copy loop's own id-remap doesn't depend on read order).
* `ReservationAllColumnsRow` (RS35's own `SELECT *` shape) rather than a
* second, differently-named copy of the same fields.
*/
async listAllForTrip(trip_id: number): Promise<ReservationAllColumnsRow[]> {
return await this.kysely<Pick<DB, 'reservations'>>()
.selectFrom('reservations')
.selectAll()
.where('trip_id', '=', trip_id)
.execute();
}
/**
* TP59 (`trips.service.ts::copy`'s reservation INSERT) — `INSERT INTO
* reservations (trip_id, day_id, end_day_id, place_id, assignment_id,
* accommodation_id, title, reservation_time, reservation_end_time,
* location, confirmation_number, notes, url, status, type, metadata,
* day_plan_position, needs_review, ingest_state) VALUES (?×19)`. The
* `external_*`/`sync_enabled`/`created_at` columns are deliberately
* omitted (the legacy statement's own column list never named them — a
* copy must not inherit the source's external sync identity).
*
* `this.insert(...)`, not `this.getEntityManager().insert(Reservations,
* …)`: this repository IS templated on `Reservations` (unlike
* `ReservationEndpointsRepository`, where this method used to live and
* needed the escape hatch to reach a table it wasn't templated on) — the
* ordinary `TrekRepository.insert` is the right call here.
*
* `accommodation_id` arrives ALREADY formatted as the legacy `'<id>.0'`
* TEXT shape (`row-id.ts`'s `legacyBoundIntegerText`, the program's
* Plan 3d Task 3 `"14.0"` finding): a plain JS number written here would
* be inlined as a SQL literal (rule 22) and stored as `'14'`, not the
* shape the legacy raw-bound statement produced.
*/
async insertReservationCopy(input: {
trip_id: number;
day_id: number | null;
end_day_id: number | null;
place_id: number | null;
assignment_id: number | null;
accommodation_id: string | null;
title: string;
reservation_time: string | null;
reservation_end_time: string | null;
location: string | null;
confirmation_number: string | null;
notes: string | null;
url: string | null;
status: string | null;
type: string | null;
metadata: string | null;
day_plan_position: number | null;
needs_review: number;
ingest_state: string;
}): Promise<number> {
return await this.insert({
trip: input.trip_id,
day: input.day_id,
endDay: input.end_day_id,
place: input.place_id,
assignment: input.assignment_id,
accommodation_id: input.accommodation_id,
title: input.title,
reservation_time: input.reservation_time,
reservation_end_time: input.reservation_end_time,
location: input.location,
confirmation_number: input.confirmation_number,
notes: input.notes,
url: input.url,
status: input.status,
type: input.type,
metadata: input.metadata,
day_plan_position: input.day_plan_position,
needs_review: input.needs_review,
ingest_state: input.ingest_state,
});
}
// ---------------------------------------------------------------------------
// Plan 3d Task 4 — additive (see the file-level comment above the interfaces
// this section's methods return).
// ---------------------------------------------------------------------------
/** CL2 — see {@link CalendarReservationRow}'s docstring. */
async listForCalendar(trip_id: number | string): Promise<CalendarReservationRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<CalendarReservationKyselyDB>()
.selectFrom('reservations as r')
.leftJoin('places as pl', 'pl.id', 'r.place_id')
.leftJoin('day_accommodations as a', (join) =>
join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'a.id')),
)
.leftJoin('days as sd', 'sd.id', 'a.start_day_id')
.leftJoin('days as ed', 'ed.id', 'a.end_day_id')
.leftJoin('days as rd', 'rd.id', 'r.day_id')
.leftJoin('days as red', 'red.id', 'r.end_day_id')
.selectAll('r')
.select((eb) => [
'pl.lat as place_lat',
'pl.lng as place_lng',
'sd.date as stay_start_date',
'ed.date as stay_end_date',
'a.check_in as stay_check_in',
'a.check_out as stay_check_out',
eb
.selectFrom('reservations as r2')
.select((eb2) => eb2.fn.min<number | null>('r2.id').as('c'))
.where((eb2) => accommodationIdMatches(platform, eb2, 'r2.accommodation_id', 'a.id'))
.as('stay_first_reservation_id'),
'rd.date as day_date',
'red.date as end_day_date',
])
.where('r.trip_id', '=', trip_id as number)
.where((eb) => publicReservationExpr(eb, 'r.ingest_state'))
.execute();
return rows as CalendarReservationRow[];
}
/** CL4 — see {@link CalendarStopRow}'s docstring. No days, no query. */
async listCalendarStopsForDays(day_ids: number[]): Promise<CalendarStopRow[]> {
if (day_ids.length === 0) return [];
const rows = await this.kysely<CalendarStopsKyselyDB>()
.selectFrom('day_assignments as da')
.innerJoin('places as p', 'p.id', 'da.place_id')
.selectAll('da')
.select((eb) => [
'p.name as place_name',
'p.address as place_address',
'p.lat as place_lat',
'p.lng as place_lng',
eb.fn.coalesce('da.stay_minutes', 'p.duration_minutes').as('place_duration_minutes'),
eb.fn.coalesce('da.assignment_time', 'p.place_time').as('effective_time'),
eb.fn.coalesce('da.assignment_end_time', 'p.end_time').as('effective_end_time'),
])
.where('da.day_id', 'in', day_ids)
.where('da.accommodation_id', 'is', null)
.orderBy('da.order_index', 'asc')
.orderBy('da.created_at', 'asc')
.orderBy('da.id', 'asc')
.execute();
return rows as CalendarStopRow[];
}
/** CL7 — see {@link CalendarStayRow}'s docstring. */
async listPublicStaysForCalendar(trip_id: number | string): Promise<CalendarStayRow[]> {
const platform = this.getEntityManager().getPlatform();
const rows = await this.kysely<CalendarStayKyselyDB>()
.selectFrom('day_accommodations as a')
.where('a.trip_id', '=', trip_id as number)
.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')
.select((eb) => [
'a.id',
'a.check_in',
'a.check_in_end',
'a.check_out',
'sd.date as start_date',
'ed.date as end_date',
'p.name as place_name',
'p.address as place_address',
'p.lat as place_lat',
'p.lng as place_lng',
eb
.selectFrom('reservations as r')
.select('r.title')
.where((eb2) => accommodationIdMatches(platform, eb2, 'r.accommodation_id', 'a.id'))
.where((eb2) => publicReservationExpr(eb2, 'r.ingest_state'))
.orderBy('r.id', 'asc')
.limit(1)
.as('reservation_title'),
])
.orderBy('a.id', 'asc')
.execute();
return rows as CalendarStayRow[];
}
/** RS20 — see {@link UpcomingReservationRow}'s docstring. */
async listUpcomingForUser(user_id: number, today: string, now_hhmm: string, limit: number): Promise<UpcomingReservationRow[]> {
const platform = this.getEntityManager().getPlatform();
// The `COALESCE(place name, nameless-stay's-linked-booking title, trip
// title)` title is identical across the check-in and check-out arms
// (both name the STAY, not the moment); it is spelled out in each rather
// than factored into a shared helper, matching `Trips.repository.ts`'s
// `activeTrip` precedent for its own repeated `CASE WHEN` — a helper
// spanning two differently-joined query builders would need the same
// kind of hand-written cross-callback type `_shared/reservation-visibility
// .ts`'s own module docstring explains doesn't hold up.
const rows = await this.kysely<UpcomingReservationsKyselyDB>()
.with('visible_trips', (qb) =>
qb
.selectFrom('trips as t')
.leftJoin('trip_members as tm', (join) => join.onRef('tm.trip_id', '=', 't.id').on('tm.user_id', '=', user_id))
.select(['t.id', 't.title', 't.cover_image'])
.where((eb) =>
eb.and([eb.or([eb('t.user_id', '=', user_id), eb('tm.user_id', 'is not', null)]), eb('t.is_archived', '=', 0)]),
),
)
.with('entries', (qb) => {
const bookings = qb
.selectFrom('reservations as r')
.innerJoin('visible_trips as tr', 'tr.id', 'r.trip_id')
.leftJoin('days as d', 'd.id', 'r.day_id')
.leftJoin('places as p', 'p.id', 'r.place_id')
.select((eb) => [
'r.id as id',
'r.trip_id as trip_id',
'r.title as title',
'r.type as type',
'r.status as status',
'r.location as location',
'r.reservation_time as reservation_time',
'r.confirmation_number as confirmation_number',
'tr.title as trip_title',
'tr.cover_image as trip_cover',
'd.date as day_date',
'p.name as place_name',
'p.image_url as place_image',
eb
.case()
.when(startsWithIsoDateKysely(platform, eb, 'r.reservation_time'))
.then(substringKysely(platform, eb, 'r.reservation_time', 1, 10))
.else(eb.ref('d.date'))
.end()
.as('at_date'),
eb
.case()
.when(startsWithIsoDateKysely(platform, eb, 'r.reservation_time'))
.then(substringKysely(platform, eb, 'r.reservation_time', 12))
.else(eb.ref('r.reservation_time'))
.end()
.as('at_time'),
])
.where('r.status', '!=', 'cancelled')
.where((eb) => eb(eb.fn.coalesce(eb.ref('r.type'), eb.val('')), 'not in', ['hotel', 'transit']))
.$castTo<EntriesRow>();
const checkins = qb
.selectFrom('day_accommodations as a')
.innerJoin('visible_trips as tr', 'tr.id', 'a.trip_id')
.innerJoin('days as d', 'd.id', 'a.start_day_id')
.leftJoin('places as p', 'p.id', 'a.place_id')
.select((eb) => [
'a.id as id',
'a.trip_id as trip_id',
eb
.fn.coalesce(
eb.ref('p.name'),
eb
.selectFrom('reservations as res')
.select('res.title')
.where((eb2) => accommodationIdMatches(platform, eb2, 'res.accommodation_id', 'a.id'))
.where('res.status', '!=', 'cancelled')
.orderBy('res.id', 'asc')
.limit(1),
eb.ref('tr.title'),
)
.as('title'),
eb.val('checkin').as('type'),
eb.val('confirmed').as('status'),
eb.val<string | null>(null).as('location'),
eb
.case()
.when('a.check_in', 'is not', null)
.then(concatKysely(platform, eb, { column: 'd.date' }, { value: 'T' }, { column: 'a.check_in' }))
.end()
.as('reservation_time'),
'a.confirmation as confirmation_number',
'tr.title as trip_title',
'tr.cover_image as trip_cover',
'd.date as day_date',
'p.name as place_name',
'p.image_url as place_image',
'd.date as at_date',
'a.check_in as at_time',
])
.$castTo<EntriesRow>();
const checkouts = qb
.selectFrom('day_accommodations as a')
.innerJoin('visible_trips as tr', 'tr.id', 'a.trip_id')
.innerJoin('days as d', 'd.id', 'a.end_day_id')
.leftJoin('places as p', 'p.id', 'a.place_id')
.select((eb) => [
'a.id as id',
'a.trip_id as trip_id',
eb
.fn.coalesce(
eb.ref('p.name'),
eb
.selectFrom('reservations as res')
.select('res.title')
.where((eb2) => accommodationIdMatches(platform, eb2, 'res.accommodation_id', 'a.id'))
.where('res.status', '!=', 'cancelled')
.orderBy('res.id', 'asc')
.limit(1),
eb.ref('tr.title'),
)
.as('title'),
eb.val('checkout').as('type'),
eb.val('confirmed').as('status'),
eb.val<string | null>(null).as('location'),
eb
.case()
.when('a.check_out', 'is not', null)
.then(concatKysely(platform, eb, { column: 'd.date' }, { value: 'T' }, { column: 'a.check_out' }))
.end()
.as('reservation_time'),
'a.confirmation as confirmation_number',
'tr.title as trip_title',
'tr.cover_image as trip_cover',
'd.date as day_date',
'p.name as place_name',
'p.image_url as place_image',
'd.date as at_date',
'a.check_out as at_time',
])
.$castTo<EntriesRow>();
return bookings.unionAll(checkins).unionAll(checkouts);
})
.selectFrom('entries')
.select(['id', 'trip_id', 'title', 'type', 'status', 'location', 'reservation_time', 'confirmation_number', 'trip_title', 'trip_cover', 'day_date', 'place_name', 'place_image'])
.where('at_date', 'is not', null)
.where((eb) =>
eb.or([
eb('at_date', '>', today),
eb.and([eb('at_date', '=', today), eb(eb.fn.coalesce(eb.ref('at_time'), eb.val('23:59')), '>=', now_hhmm)]),
]),
)
.orderBy('at_date', 'asc')
.orderBy((eb) => eb.fn.coalesce(eb.ref('at_time'), eb.val('00:00')), 'asc')
.orderBy('id', 'asc')
.limit(limit)
.execute();
return rows as UpcomingReservationRow[];
}
/** `share.service.ts:251` — see {@link ShareEndpointRow}'s docstring. */
async listEndpointsForShare(trip_id: number | string): Promise<ShareEndpointRow[]> {
const rows = await this.kysely<ShareEndpointsKyselyDB>()
.selectFrom('reservation_endpoints as e')
.innerJoin('reservations as r', 'r.id', 'e.reservation_id')
.select(['e.reservation_id', 'e.role', 'e.sequence', 'e.name', 'e.code', 'e.lat', 'e.lng', 'e.timezone', 'e.local_date', 'e.local_time'])
.where('r.trip_id', '=', trip_id as number)
.orderBy('e.reservation_id', 'asc')
.orderBy('e.sequence', 'asc')
.execute();
return rows as ShareEndpointRow[];
}
/** `share.service.ts:368` — see {@link ShareDayPositionRow}'s docstring. */
async listDayPositionsForShare(trip_id: number | string): Promise<ShareDayPositionRow[]> {
const rows = await this.kysely<ShareDayPositionsKyselyDB>()
.selectFrom('reservation_day_positions as rdp')
.innerJoin('reservations as r', 'r.id', 'rdp.reservation_id')
.select(['rdp.reservation_id', 'rdp.day_id', 'rdp.position'])
.where('r.trip_id', '=', trip_id as number)
.execute();
return rows as ShareDayPositionRow[];
}
/** `share.service.ts:387` — see {@link SharePublicReservationRow}'s docstring. */
async listPublicForShare(trip_id: number | string): Promise<SharePublicReservationRow[]> {
const rows = await this.kysely<SharePublicReservationKyselyDB>()
.selectFrom('reservations as r')
.select([
'r.id', 'r.trip_id', 'r.day_id', 'r.end_day_id', 'r.place_id', 'r.accommodation_id',
'r.title', 'r.type', 'r.status', 'r.location', 'r.reservation_time', 'r.reservation_end_time',
'r.notes', 'r.url', 'r.metadata', 'r.created_at',
])
.where('r.trip_id', '=', trip_id as number)
.where((eb) => publicReservationExpr(eb, 'r.ingest_state'))
.orderBy('r.reservation_time', 'asc')
.execute();
return rows as SharePublicReservationRow[];
}
/** `share.service.ts:401` — see {@link SharePublicAccommodationRow}'s docstring. */
async listPublicAccommodationsForShare(trip_id: number | string): Promise<SharePublicAccommodationRow[]> {
const rows = await this.kysely<SharePublicAccommodationKyselyDB>()
.selectFrom('day_accommodations as a')
.where('a.trip_id', '=', trip_id as number)
.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',
'a.check_in', 'a.check_in_end', 'a.check_out', 'a.notes',
'p.name as place_name', 'p.address as place_address', 'p.lat as place_lat', 'p.lng as place_lng',
])
.execute();
return rows as SharePublicAccommodationRow[];
}
/**
* `share.service.ts`'s travel-only read (SH19, #1712) — `SELECT DISTINCT
* a.place_id FROM day_accommodations a WHERE a.trip_id = ? AND <RV2('a')>`:
* the places the trip sleeps at, through the same public-stay predicate as
* {@link listPublicAccommodationsForShare}.
*/
async listPublicStayPlaceIdsForShare(trip_id: number | string): Promise<number[]> {
const rows = await this.kysely<SharePublicAccommodationKyselyDB>()
.selectFrom('day_accommodations as a')
.where('a.trip_id', '=', trip_id as number)
.where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb))
.select('a.place_id')
.distinct()
.execute();
return rows.map((r) => r.place_id as number);
}
/** `public-api.service.ts::reservationsByDay` (Task 5's `// Task 2` pickup) — see {@link PublicApiScheduledReservationRow}'s docstring. */
async listScheduledForPublicApi(trip_id: number): Promise<PublicApiScheduledReservationRow[]> {
const rows = await this.kysely<PublicApiReservationKyselyDB>()
.selectFrom('reservations')
.select(['day_id', 'type', 'title', 'location', 'reservation_time', 'reservation_end_time', 'status', 'notes'])
.where('trip_id', '=', trip_id)
.where('day_id', 'is not', null)
.orderBy('day_id', 'asc')
.orderBy('reservation_time', 'asc')
.execute();
return rows as PublicApiScheduledReservationRow[];
}
/** `public-api.service.ts::buildUnscheduledReservations` (Task 5's `// Task 2` pickup) — see {@link PublicApiUnscheduledReservationRow}'s docstring. */
async listUnscheduledForPublicApi(trip_id: number): Promise<PublicApiUnscheduledReservationRow[]> {
const rows = await this.kysely<PublicApiReservationKyselyDB>()
.selectFrom('reservations')
.select(['type', 'title', 'location', 'reservation_time', 'reservation_end_time', 'status', 'notes'])
.where('trip_id', '=', trip_id)
.where('day_id', 'is', null)
.orderBy('reservation_time', 'asc')
.orderBy('id', 'asc')
.execute();
return rows as PublicApiUnscheduledReservationRow[];
}
/** `public-api.service.ts::buildAccommodations` (Task 5's `// Task 3` pickup) — see {@link PublicApiAccommodationRow}'s docstring. */
async listAccommodationsForPublicApi(trip_id: number): Promise<PublicApiAccommodationRow[]> {
const rows = await this.kysely<PublicApiAccommodationKyselyDB>()
.selectFrom('day_accommodations as a')
.leftJoin('places as p', 'p.id', 'a.place_id')
.leftJoin('days as ds', 'ds.id', 'a.start_day_id')
.leftJoin('days as de', 'de.id', 'a.end_day_id')
.select(['p.name', 'p.address', 'p.lat', 'p.lng', 'ds.date as start_date', 'de.date as end_date', 'a.check_in', 'a.check_out', 'a.notes'])
.where('a.trip_id', '=', trip_id)
.orderBy('ds.date', 'asc')
.execute();
return rows as PublicApiAccommodationRow[];
}
/** `public-api.service.ts::buildUnplannedPlaces` (Task 5's `// Task 3` pickup) — see {@link PublicApiUnplannedPlaceRow}'s docstring. */
async listUnplannedPlacesForPublicApi(trip_id: number): Promise<PublicApiUnplannedPlaceRow[]> {
const rows = await this.kysely<PublicApiUnplannedPlaceKyselyDB>()
.selectFrom('places as p')
.leftJoin('categories as c', 'c.id', 'p.category_id')
.select((eb) => [
'p.name', 'p.address', 'p.lat', 'p.lng', 'p.place_time', 'p.end_time',
'p.duration_minutes', 'p.notes', 'p.transport_mode', 'c.name as category',
])
.where('p.trip_id', '=', trip_id)
.where((eb) => eb.not(eb.exists(eb.selectFrom('day_assignments as da').select('da.id').whereRef('da.place_id', '=', 'p.id'))))
.where((eb) => eb.not(eb.exists(eb.selectFrom('day_accommodations as a').select('a.id').whereRef('a.place_id', '=', 'p.id'))))
.orderBy('p.created_at', 'asc')
.orderBy('p.id', 'asc')
.execute();
return rows as PublicApiUnplannedPlaceRow[];
}
// ---------------------------------------------------------------------------
// Plan 3e Task 1 (`FilesService.findForeignLinkTarget`, R12) — additive,
// append-only per that task's own file-ownership rule.
// ---------------------------------------------------------------------------
/**
* FL1 (`FilesService.findForeignLinkTarget`'s reservation branch) —
* `SELECT 1 FROM reservations WHERE id = ? AND trip_id = ?`, re-expressed
* as "what trip does this row belong to" (R12: one method per target
* table, no dynamic identifier dispatch — the RS21/RS23/RS25 D4 precedent)
* so the caller compares the returned `trip_id` against its own, already
* `toRowId`-parsed value instead of binding two ids into the query itself.
* `trip_id` is a `persist(false)` mirror of the `trip` relation —
* `columnRef`, not a bare select (the program-wide trap).
*/
async findTripId(id: number): Promise<number | undefined> {
const platform = this.getEntityManager().getPlatform();
const row = await this.qb('r')
.select([columnRef(platform, 'r.trip_id').as('trip_id')])
.where({ id })
.execute<{ trip_id: number } | undefined>('get', false);
return row?.trip_id;
}
// ---------------------------------------------------------------------------
// Plan 3e Task 2 (budget) — additive, append-only per that task's own
// file-ownership rule: BG54/85, BG55/86, BG56/87 — the mirrored-price
// read/write/re-select `BudgetService.clearReservationPrice`/
// `syncReservationPrice` run against a reservation. Kysely, matching
// `findTripId` above's reasoning: `trip_id` is a `persist(false)` mirror.
// ---------------------------------------------------------------------------
/** BG54/BG85 — `SELECT id, metadata FROM reservations WHERE id = ? AND trip_id = ?`. */
async getIdAndMetadata(id: number | string, trip_id: number | string): Promise<{ id: number; metadata: string | null } | undefined> {
return await this.kysely<Pick<DB, 'reservations'>>()
.selectFrom('reservations')
.select(['id', 'metadata'])
.where('id', '=', id as number)
.where('trip_id', '=', trip_id as number)
.executeTakeFirst();
}
/** BG55/BG86 — `UPDATE reservations SET metadata = ? WHERE id = ?`. */
async setMetadata(id: number | string, metadata: string): Promise<void> {
await this.kysely<Pick<DB, 'reservations'>>()
.updateTable('reservations')
.set({ metadata })
.where('id', '=', id as number)
.execute();
}
/** BG56/BG87 — `SELECT * FROM reservations WHERE id = ?`, the mirrored-price re-select for the `reservation:updated` broadcast. */
async getFull(id: number | string): Promise<ReservationAllColumnsRow | undefined> {
return await this.kysely<Pick<DB, 'reservations'>>()
.selectFrom('reservations')
.selectAll()
.where('id', '=', id as number)
.executeTakeFirst();
}
// ---------------------------------------------------------------------------
// Plan 3h Task 4 (AirTrail's bookkeeping, `nest/integrations/airtrail*.ts` —
// owns zero tables of its own, R7) + airports' flight-endpoint backfill
// (`nest/airports/airports.service.ts`) — additive, per this task's own
// file-ownership rule ("additive methods on Reservations/ReservationEndpoints/
// Days/Places/Users repositories"). `trip_id` is a `persist(false)` FK
// mirror — every trip-scoped read below filters by the `trip` ref property
// (the `findInTrip`/`listResyncCandidates` precedent above), never a raw
// column name.
// ---------------------------------------------------------------------------
/** ATI1 (`airtrail-import.service.ts#importAirtrailFlights`, dedup: already-linked ids) — `SELECT external_id, metadata FROM reservations WHERE trip_id = ? AND external_source = 'airtrail'`. */
async listAirtrailLinkedForTrip(trip_id: number): Promise<{ external_id: string | null; metadata: string | null }[]> {
return this.qb('r')
.select(['r.external_id', 'r.metadata'])
.where({ trip: trip_id, external_source: 'airtrail' })
.execute<{ external_id: string | null; metadata: string | null }[]>('all', false);
}
/** ATI2 (`airtrail-import.service.ts#importAirtrailFlights`, existing-flight signature dedup) — `SELECT r.id, r.reservation_time, r.metadata FROM reservations r WHERE r.trip_id = ? AND r.type = 'flight'`. */
async listFlightReservationsForTrip(trip_id: number): Promise<{ id: number; reservation_time: string | null; metadata: string | null }[]> {
return this.qb('r')
.select(['r.id', 'r.reservation_time', 'r.metadata'])
.where({ trip: trip_id, type: 'flight' })
.execute<{ id: number; reservation_time: string | null; metadata: string | null }[]>('all', false);
}
/**
* ATI5 (`airtrail-import.service.ts#importAirtrailFlights`, multi-leg
* branch) — `UPDATE reservations SET external_source = 'airtrail',
* external_id = ?, external_owner_user_id = ?, sync_enabled = 0,
* external_synced_at = ? WHERE id = ?`. **CORRECTNESS GAP, pre-existing,
* report don't fix** (inventory §3b): neither this nor {@link
* linkAirtrailSingleFlight} is transactional with the preceding
* `ReservationsService.create` call — a failure between the two leaves an
* untracked reservation. Preserved exactly, not "fixed" with an added
* `uow.transactional` wrap.
*/
async linkAirtrailMultiLeg(id: number, external_id: string, external_owner_user_id: number, external_synced_at: string): Promise<void> {
await this.nativeUpdate({ id }, { external_source: 'airtrail', external_id, external_owner_user_id, sync_enabled: 0, external_synced_at });
}
/**
* ATI6 (`airtrail-import.service.ts#importAirtrailFlights`, single-flight
* branch) — `UPDATE reservations SET external_source = 'airtrail',
* external_id = ?, external_owner_user_id = ?, sync_enabled = 1,
* external_hash = ?, external_synced_at = ? WHERE id = ?`. Same
* pre-existing tx gap as {@link linkAirtrailMultiLeg} — report don't fix.
*/
async linkAirtrailSingleFlight(id: number, external_id: string, external_owner_user_id: number, external_hash: string, external_synced_at: string): Promise<void> {
await this.nativeUpdate({ id }, { external_source: 'airtrail', external_id, external_owner_user_id, sync_enabled: 1, external_hash, external_synced_at });
}
/** ATL2 (`airtrail-link.service.ts#detach`) — `UPDATE reservations SET sync_enabled = 0 WHERE id = ?`. */
async setAirtrailSyncDisabled(id: number): Promise<void> {
await this.nativeUpdate({ id }, { sync_enabled: 0 });
}
/**
* ATL4 (`airtrail-link.service.ts#pushReservationToAirtrail`) — `SELECT
* id, trip_id, external_id, external_owner_user_id, sync_enabled FROM
* reservations WHERE id = ? AND external_source = 'airtrail'`. `trip_id`
* via `columnRef` (the `listResyncCandidates` precedent above) — the
* mirror column, not a bare select.
*/
async findAirtrailLinked(id: number): Promise<{ id: number; trip_id: number; external_id: string | null; external_owner_user_id: number | null; sync_enabled: number | null } | undefined> {
const platform = this.getEntityManager().getPlatform();
return this.qb('r')
.select(['r.id', columnRef(platform, 'r.trip_id').as('trip_id'), 'r.external_id', 'r.external_owner_user_id', 'r.sync_enabled'])
.where({ id, external_source: 'airtrail' })
.execute<{ id: number; trip_id: number; external_id: string | null; external_owner_user_id: number | null; sync_enabled: number | null } | undefined>('get', false);
}
/**
* ATL5 (`airtrail-link.service.ts#pushReservationToAirtrail`, self-write
* suppression) / ATS2 (`airtrail-sync.service.ts#syncOwner`, byte-identical
* 2nd call site) — `UPDATE reservations SET external_hash = ?,
* external_synced_at = ? WHERE id = ?`. One method, two call sites (D4).
*/
async setAirtrailSyncStamp(id: number, external_hash: string, external_synced_at: string): Promise<void> {
await this.nativeUpdate({ id }, { external_hash, external_synced_at });
}
/**
* ATS1 (`airtrail-sync.service.ts#syncOwner`) — `SELECT id, trip_id,
* external_id, external_hash FROM reservations WHERE external_source =
* 'airtrail' AND sync_enabled = 1 AND external_owner_user_id = ?`.
*/
async listAirtrailSyncCandidatesForOwner(owner_user_id: number): Promise<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]> {
const platform = this.getEntityManager().getPlatform();
return this.qb('r')
.select(['r.id', columnRef(platform, 'r.trip_id').as('trip_id'), 'r.external_id', 'r.external_hash'])
.where({ external_source: 'airtrail', sync_enabled: 1, external_owner_user_id: owner_user_id })
.execute<{ id: number; trip_id: number; external_id: string | null; external_hash: string | null }[]>('all', false);
}
/**
* ATS3 (`airtrail-sync.service.ts#runAirtrailSync`) — `SELECT DISTINCT
* external_owner_user_id AS uid FROM reservations WHERE external_source =
* 'airtrail' AND sync_enabled = 1 AND external_owner_user_id IS NOT NULL`.
* **NON-HTTP ENTRYPOINT SOURCE** — the cron poll's owner list.
*/
async listAirtrailSyncOwners(): Promise<number[]> {
const rows = await this.qb('r')
.select(['r.external_owner_user_id'])
.distinct()
.where({ external_source: 'airtrail', sync_enabled: 1, external_owner_user_id: { $ne: null } })
.execute<{ external_owner_user_id: number }[]>('all', false);
return rows.map((row) => row.external_owner_user_id);
}
/**
* AIR1 (`airports.service.ts#backfillFlightEndpoints`) — `SELECT r.id,
* r.metadata, r.reservation_time, r.reservation_end_time FROM reservations
* r WHERE r.type = 'flight' AND NOT EXISTS (SELECT 1 FROM
* reservation_endpoints e WHERE e.reservation_id = r.id)`. **NON-HTTP
* ENTRYPOINT SOURCE** — the boot-sweep backfill; idempotent via this NOT
* EXISTS guard, same shape as `Reservations.repository.ts`'s own
* `listUnplannedPlacesForPublicApi` NOT EXISTS precedent (`eb.not(eb.exists(...))`).
*/
async listFlightsMissingEndpoints(): Promise<{ id: number; metadata: string | null; reservation_time: string | null; reservation_end_time: string | null }[]> {
const rows = await this.kysely<FlightsMissingEndpointsKyselyDB>()
.selectFrom('reservations as r')
.select(['r.id', 'r.metadata', 'r.reservation_time', 'r.reservation_end_time'])
.where('r.type', '=', 'flight')
.where((eb) => eb.not(eb.exists(eb.selectFrom('reservation_endpoints as e').select('e.id').whereRef('e.reservation_id', '=', 'r.id'))))
.execute();
return rows;
}
/** AIR3 (`airports.service.ts#backfillFlightEndpoints`, looped) — `UPDATE reservations SET needs_review = 1 WHERE id = ?`. */
async markNeedsReview(id: number): Promise<void> {
await this.nativeUpdate({ id }, { needs_review: 1 });
}
/** RPL4/RPL5's shared select list, the columns of `RoadtripCarrierRow`. */
private roadtripCarrierQuery() {
const platform = this.getEntityManager().getPlatform();
return this.qb('r')
.select([
'r.id',
'r.type',
'r.title',
columnRef(platform, 'r.day_id').as('day_id'),
columnRef(platform, 'r.end_day_id').as('end_day_id'),
'r.reservation_time',
'r.reservation_end_time',
'r.metadata',
'r.day_plan_position',
]);
}
/**
* RPL4 (`RoadtripPlanService.carriers`) — `SELECT id, type, title, day_id,
* end_day_id, reservation_time, reservation_end_time, metadata,
* day_plan_position FROM reservations WHERE trip_id = ? AND type IN
* (ROADTRIP_RESERVATION_TYPES) AND day_id IS NOT NULL`: the bookings that
* seam the drive, and the hire cars whose desks stand on it.
*/
async listRoadtripCarriers(trip_id: number): Promise<RoadtripCarrierRow[]> {
return this.roadtripCarrierQuery()
.where({ trip: trip_id, type: { $in: [...ROADTRIP_RESERVATION_TYPES] }, day: { $ne: null } })
.execute<RoadtripCarrierRow[]>('all', false);
}
/**
* RPL5 (`RoadtripPlanService.undatedRides`) — `SELECT id, type, title,
* day_id, end_day_id, reservation_time, reservation_end_time, metadata,
* day_plan_position FROM reservations WHERE trip_id = ? AND day_id IS
* NULL`. Which of them count as rides is `undatedRides` (shared) alone.
*/
async listRoadtripUndated(trip_id: number): Promise<RoadtripCarrierRow[]> {
return this.roadtripCarrierQuery()
.where({ trip: trip_id, day: null })
.execute<RoadtripCarrierRow[]>('all', false);
}
}
/**
* RPL4/RPL5's row: a booking the traveller rides, with what the road trip's
* seam needs of it (the days and clocks at both ends, where the day plan
* seats it). The terminals and day positions are joined on in the service.
*/
export interface RoadtripCarrierRow {
id: number;
type: string;
title: string;
day_id: number | null;
end_day_id: number | null;
reservation_time: string | null;
reservation_end_time: string | null;
metadata: string | null;
day_plan_position: number | null;
}