refactor(reservations): grouping endpoints and day slots moves into reservations helpers

This commit is contained in:
Maurice
2026-10-12 21:13:57 +00:00
parent 141ad0acb6
commit 5fcfe8b912
4 changed files with 174 additions and 63 deletions
@@ -20,34 +20,7 @@ import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared';
* folds `accommodation_id` (`Math.trunc(Number(x))`), `day_positions`,
* `endpoints` and `travelers` on afterward (unchanged from the legacy split).
*/
export interface ReservationJoinRow {
id: number;
trip_id: number;
day_id: number | null;
end_day_id: number | null;
place_id: number | null;
assignment_id: number | null;
title: string;
accommodation_id: string | null;
reservation_time: string | null;
reservation_end_time: string | null;
location: string | null;
confirmation_number: string | null;
notes: string | null;
status: string | null;
type: string | null;
created_at: string | null;
metadata: string | null;
day_plan_position: number | null;
needs_review: number;
external_source: string | null;
external_id: string | null;
external_owner_user_id: number | null;
external_synced_at: string | null;
sync_enabled: number | null;
external_hash: string | null;
url: string | null;
ingest_state: string;
export interface ReservationJoinRow extends ReservationAllColumnsRow {
day_number: number | null;
place_name: string | null;
accommodation_place_id: number | null;
@@ -0,0 +1,60 @@
import type { ReservationEndpointRow } from '../../db/repositories/ReservationEndpoints.repository';
import type { EndpointInput, ReservationEndpoint } from './reservations.service';
/**
* Pure helpers of the reservations domain: how a trip's endpoint and per-day
* position rows are grouped by booking for the list, and which endpoint rows a
* save inserts. Nothing here touches the DB, so they stay plain exports rather
* than methods on ReservationsService (the places.helpers.ts split).
*/
/** A trip's endpoint rows, grouped by the reservation they belong to, in the order read. */
export function groupEndpointsByReservation(rows: ReservationEndpointRow[]): Map<number, ReservationEndpoint[]> {
const map = new Map<number, ReservationEndpoint[]>();
for (const r of rows) {
const list = map.get(r.reservation_id) ?? [];
// `role` is TEXT at the DB (unconstrained); `ReservationEndpoint.role`
// narrows it to the three values every writer of this column sends:
// the same widen-on-read the legacy `this.db.all<ReservationEndpoint>`
// generic parameter asserted without any runtime check.
list.push(r as ReservationEndpoint);
map.set(r.reservation_id, list);
}
return map;
}
/** Each reservation's slot per day, `{ [day_id]: position }`, keyed by the reservation's id. */
export function groupDayPositions(
dayPositions: { reservation_id: number; day_id: number; position: number }[],
): Map<number, Record<number, number>> {
const posMap = new Map<number, Record<number, number>>();
for (const dp of dayPositions) {
if (!posMap.has(dp.reservation_id)) posMap.set(dp.reservation_id, {});
posMap.get(dp.reservation_id)![dp.day_id] = dp.position;
}
return posMap;
}
/**
* The rows a save of a reservation's endpoints inserts, one per kept endpoint
* in the order given. A missing `sequence` is the index after the filter.
*/
export function endpointRows(reservationId: number, endpoints: EndpointInput[]) {
// lat/lng are NOT NULL: an imported transport whose pick-up/return (or station/
// stop) couldn't be geocoded reaches here with null coords. Skip those rows rather
// than let the INSERT throw and fail the entire booking save; the dates still live
// on reservation_time/reservation_end_time, so the booking lands on its day either way.
const filtered = endpoints.filter((e) => e.lat != null && e.lng != null);
return filtered.map((e, i) => ({
reservation_id: reservationId,
role: e.role,
sequence: e.sequence ?? i,
name: e.name,
code: e.code ?? null,
lat: e.lat!,
lng: e.lng!,
timezone: e.timezone ?? null,
local_time: e.local_time ?? null,
local_date: e.local_date ?? null,
}));
}
@@ -7,6 +7,7 @@ import { RealtimeService } from '../realtime/realtime.service';
import { PermissionsService } from '../permissions/permissions.service';
import { ReservationsReadService, toTraveler } from './reservations-read.service';
import { keepMirroredPrice } from './reservation-metadata';
import { endpointRows, groupDayPositions, groupEndpointsByReservation } from './reservations.helpers';
import type { Reservation, User } from '../../types';
import { BudgetService } from '../budget/budget.service';
import { typeToCostCategory } from '@trek/shared';
@@ -256,17 +257,7 @@ export class ReservationsService {
async loadEndpointsByTrip(tripId: string | number): Promise<Map<number, ReservationEndpoint[]>> {
const rows: ReservationEndpointRow[] = await this.endpointsRepo.listForTrip(this.rowIdNum(tripId));
const map = new Map<number, ReservationEndpoint[]>();
for (const r of rows) {
const list = map.get(r.reservation_id) ?? [];
// `role` is TEXT at the DB (unconstrained); `ReservationEndpoint.role`
// narrows it to the three values every writer of this column sends —
// the same widen-on-read the legacy `this.db.all<ReservationEndpoint>`
// generic parameter asserted without any runtime check.
list.push(r as ReservationEndpoint);
map.set(r.reservation_id, list);
}
return map;
return groupEndpointsByReservation(rows);
}
/**
@@ -376,24 +367,8 @@ export class ReservationsService {
// database connection is not open").
await this.uow.transactional(async () => {
await this.endpointsRepo.deleteForReservation(reservationId);
// lat/lng are NOT NULL: an imported transport whose pick-up/return (or station/
// stop) couldn't be geocoded reaches here with null coords. Skip those rows rather
// than let the INSERT throw and fail the entire booking save — the dates still live
// on reservation_time/reservation_end_time, so the booking lands on its day either way.
const filtered = endpoints.filter((e) => e.lat != null && e.lng != null);
for (const [i, e] of filtered.entries()) {
await this.endpointsRepo.insertEndpoint({
reservation_id: reservationId,
role: e.role,
sequence: e.sequence ?? i,
name: e.name,
code: e.code ?? null,
lat: e.lat!,
lng: e.lng!,
timezone: e.timezone ?? null,
local_time: e.local_time ?? null,
local_date: e.local_date ?? null,
});
for (const row of endpointRows(reservationId, endpoints)) {
await this.endpointsRepo.insertEndpoint(row);
}
});
}
@@ -412,12 +387,7 @@ export class ReservationsService {
const reservations: ReservationRow[] = (await this.reservationsRepo.listForTrip(tripId)).map((r) => ({ ...r }));
const dayPositions = await this.dayPositionsRepo.listForTrip(this.rowIdNum(tripId));
const posMap = new Map<number, Record<number, number>>();
for (const dp of dayPositions) {
if (!posMap.has(dp.reservation_id)) posMap.set(dp.reservation_id, {});
posMap.get(dp.reservation_id)![dp.day_id] = dp.position;
}
const posMap = groupDayPositions(dayPositions);
const endpointsMap = await this.loadEndpointsByTrip(tripId);
const travelersMap = await this.loadTravelersByTrip(tripId);