Skip to content

armgit / TREK

Public Imported from GitHub
0

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.

16.0 KB
Raw Blame
import { DayAccommodations } from '../../db/entities/DayAccommodations.entity';
import { DayAssignments } from '../../db/entities/DayAssignments.entity';
import { Days } from '../../db/entities/Days.entity';
import { ReservationDayPositions } from '../../db/entities/ReservationDayPositions.entity';
import { ReservationEndpoints } from '../../db/entities/ReservationEndpoints.entity';
import { Reservations } from '../../db/entities/Reservations.entity';
import type { DayAccommodationsRepository } from '../../db/repositories/DayAccommodations.repository';
import type { DayAssignmentsRepository } from '../../db/repositories/DayAssignments.repository';
import type { DaysRepository } from '../../db/repositories/Days.repository';
import type { ReservationDayPositionsRepository } from '../../db/repositories/ReservationDayPositions.repository';
import type { ReservationEndpointsRepository } from '../../db/repositories/ReservationEndpoints.repository';
import type { ReservationsRepository, RoadtripCarrierRow } from '../../db/repositories/Reservations.repository';
import { DomainError } from '../common/domain-error';
import { SettingsService } from '../settings/settings.service';
import { TripAccessService } from '../trip-membership/trip-access.service';
import { DayBoundariesService } from './day-boundaries.service';
import { RoadtripPreferencesService } from './roadtrip-preferences.service';
import { RoadtripRouterService } from './roadtrip-router.service';
import { RoadtripService } from './roadtrip.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { type RoadtripPreferences } from '@trek/shared';
import {
  assembleRoadtrip,
  carrierLegsFor,
  carrierSeam,
  foldRouteRun,
  hotelBookendsOn,
  isStationaryJoin,
  oneNightPerStay,
  splitIntoRuns,
  spillChains,
  dayWindow,
  effectiveRangeKm,
  parseAvoid,
  formatDistance,
  formatDurationShort,
  seatCarrierStops,
  seatNightBookends,
  undatedRides,
  viasOnLeg,
  type CarrierBooking,
  type CarrierSeam,
  type DistanceUnit,
  type RoadtripStop,
  type PlanDay,
  type RoutedLeg,
  type SnappedWaypoint,
  type RouteAvoidClass,
  standsAsDay,
} from '@trek/shared/roadtrip';

const stopKey = (s: RoadtripStop) =>
  `${s.lat.toFixed(5)},${s.lng.toFixed(5)},${s.legMode ?? ''},${s.incomingLegMode ?? ''}`;

@Injectable()
export class RoadtripPlanService {
  constructor(
    private readonly settings: SettingsService,
    private readonly preferences: RoadtripPreferencesService,
    private readonly router: RoadtripRouterService,
    private readonly roadtrip: RoadtripService,
    private readonly boundaries: DayBoundariesService,
    private readonly tripsRepo: TripAccessService,
    @InjectRepository(Days) private readonly daysRepo: DaysRepository,
    @InjectRepository(DayAssignments) private readonly dayAssignmentsRepo: DayAssignmentsRepository,
    @InjectRepository(DayAccommodations) private readonly dayAccommodationsRepo: DayAccommodationsRepository,
    @InjectRepository(Reservations) private readonly reservationsRepo: ReservationsRepository,
    @InjectRepository(ReservationEndpoints) private readonly endpointsRepo: ReservationEndpointsRepository,
    @InjectRepository(ReservationDayPositions) private readonly dayPositionsRepo: ReservationDayPositionsRepository,
  ) {}

  async context(tripId: number, userId: number) {
    if (!(await this.tripsRepo.findAccessible(tripId, userId))) throw new DomainError(404, 'Trip not found');
    // RPL1 — `DaysRepository.listPlanDays`.
    const days = await this.daysRepo.listPlanDays(tripId);
    // RPL2 — `DayAssignmentsRepository.listRoadtripVisits`.
    const visits = await this.dayAssignmentsRepo.listRoadtripVisits(tripId);
    // Every stay of the trip in id order, the order the rule reads them in, so a browser
    // holding them in another order still picks the same hotel where two overlap. The
    // booking is the earliest linked one, the one the planner opens from the hotel's row.
    const stays = await this.dayAccommodationsRepo.listRoadtripStays(tripId); // RPL3
    return {
      days,
      visits,
      stays,
      carriers: await this.carriers(tripId),
      settings: await this.preferences.read(tripId),
      profiles: await this.router.profiles(),
      vias: await this.roadtrip.listForTrip(tripId),
      tracks: await this.roadtrip.tracksForTrip(tripId),
      boundaries: await this.boundaries.list(tripId),
    };
  }

  /**
   * The bookings that seam the drive, a flight, train, ferry, cruise or bus on a day, and
   * the hire cars whose desks stand on it, with their terminals and the slots the day
   * plan gave them. The reservation service reads the same three tables for the booking
   * list; this is the road trip's own cut of them, so the module stays out of the
   * reservations graph.
   */
  private async carriers(tripId: number): Promise<CarrierBooking[]> {
    return this.withTerminals(tripId, await this.reservationsRepo.listRoadtripCarriers(tripId)); // RPL4
  }

  /**
   * The rides the drive leaves out because they are on no day: a flight, train, ferry,
   * cruise or bus with both terminals located and no day to leave on (#2461). The map
   * draws their arcs all the same, so the answer names them rather than leaving an
   * assistant to read a drive around a booked crossing as the plan. Which ones count is
   * `undatedRides` alone, the rule the planner's rail lists them by, so the statement
   * names no types of its own.
   */
  async undatedRides(tripId: number): Promise<{ id: number; type: string; title: string }[]> {
    const rows = await this.reservationsRepo.listRoadtripUndated(tripId); // RPL5
    return undatedRides(await this.withTerminals(tripId, rows)).map(({ id, type, title }) => ({ id, type, title }));
  }

  /** The rows with their terminals and the slots the day plan gave them joined on. */
  private async withTerminals(tripId: number, rows: RoadtripCarrierRow[]): Promise<CarrierBooking[]> {
    if (!rows.length) return [];
    const endpoints = await this.endpointsRepo.listRoadtripTerminals(tripId); // RPL6
    // RS19 — `SELECT p.reservation_id, p.day_id, p.position FROM reservation_day_positions p JOIN reservations r …`.
    const positions = await this.dayPositionsRepo.listForTrip(tripId);
    return rows.map((row) => ({
      ...row,
      endpoints: endpoints.filter((e) => e.reservation_id === row.id),
      day_positions: Object.fromEntries(
        positions.filter((p) => p.reservation_id === row.id).map((p) => [String(p.day_id), p.position]),
      ),
    }));
  }

  async calculate(tripId: number, userId: number, overrides?: RoadtripPreferences) {
    const context = await this.context(tripId, userId);
    const preferences = { ...context.settings, ...overrides };
    const window = dayWindow(
      preferences.roadtrip_day_start,
      preferences.roadtrip_day_end,
      preferences.roadtrip_day_end_mode,
    );
    if (preferences.roadtrip_day_start && preferences.roadtrip_day_end && !window)
      throw new DomainError(400, 'Day end must be later than day start.');
    const seams = context.carriers
      .map((booking) => carrierSeam(booking))
      .filter((seam): seam is CarrierSeam => seam !== null);
    const dayNumberOf = (dayId: number): number => context.days.find((d) => d.id === dayId)?.day_number ?? 0;
    const stored: PlanDay[] = context.days.map((day) => {
      const visits = context.visits.filter((v) => v.day_id === day.id && v.lat !== null && v.lng !== null);
      // One night per hotel (oneNightPerStay), the way the browser reads the day.
      const stops = oneNightPerStay(
        visits.map((v, index): RoadtripStop => ({
          assignmentId: v.id,
          ownerDayId: day.id,
          ownerIndex: index,
          placeId: v.place_id,
          name: v.name,
          lat: v.lat!,
          lng: v.lng!,
          time: v.time,
          // The visit's end time is when the drive leaves it, the same reading the
          // planner makes in the browser (useRoadtripRoutes).
          leaveAt: v.end_time,
          checkInTime: v.check_in,
          night: v.stay_id !== null,
          bookedNightId: v.accommodation_id,
          dwellMinutes: v.duration_minutes,
          endDay: v.end_day === 1,
          legMode: v.leg_transport_mode,
          incomingLegMode: v.incoming_leg_transport_mode,
          stopType: v.stop_type,
          fillPercent: v.fill_percent,
        })),
      );
      return {
        dayId: day.id,
        dayNumber: day.day_number,
        date: day.date,
        title: day.title,
        // The terminals of the day's rides, seated where the day plan shows the booking.
        stops: seatCarrierStops(
          day.id,
          stops,
          visits.map((v) => v.order_index),
          seams,
        ),
      };
    });
    // A day after a booked night starts at the stay and a day before one ends there, by
    // the rule the planner runs in the browser, and only while the trip (or the preview's
    // own settings) has it switched on. The rides go in with or without their stations,
    // the way the browser hands over its bookings. Everything below reads the seated days.
    const plan = hotelBookendsOn(preferences)
      ? seatNightBookends(stored, context.days, context.stays, context.carriers)
      : stored;
    const allLegs: Record<string, RoutedLeg> = {};
    const snapByDay: Record<number, Record<string, SnappedWaypoint>> = {};
    const missedByDay: Record<number, RouteAvoidClass[]> = {};
    const failures: { fromAssignmentId: number; toAssignmentId: number; reason: string }[] = [];
    const avoid = parseAvoid(preferences.roadtrip_avoid);
    const connectDays = preferences.roadtrip_connect_days || window !== null;
    if (context.visits.length > 150) throw new DomainError(400, 'This trip exceeds the 150-visit calculation limit.');
    const asked = new Set<string>();
    const distanceUnit: DistanceUnit =
      (await this.settings.getUserSettings(userId)).distance_unit === 'imperial' ? 'imperial' : 'metric';
    const fetchRun = async (stops: RoadtripStop[], dayId: number, profile: string) => {
      const pairs = stops.slice(0, -1).map((from, index) => ({ from, to: stops[index + 1] }));
      pairs.forEach(({ from, to }) => asked.add(stopKey(from) + '>' + stopKey(to)));
      // A ride is no road: the leg between its terminals is the booking's minutes, and
      // the router is never asked for it.
      const rides = carrierLegsFor(
        stops,
        profile,
        (from, to) => dayNumberOf(to.ownerDayId) - dayNumberOf(from.ownerDayId),
        (from, to) => stopKey(from) + '>' + stopKey(to),
      );
      if (rides) {
        Object.assign(allLegs, rides);
        return;
      }
      const points: { lat: number; lng: number }[] = [];
      const stopAt: number[] = [];
      stops.forEach((stop, index) => {
        stopAt.push(points.length);
        points.push({ lat: stop.lat, lng: stop.lng });
        // None bend the drive from or to a booked night's hotel (`viasOnLeg`).
        if (index < stops.length - 1)
          points.push(...viasOnLeg(stop, stops[index + 1], context.vias).map((v) => ({ lat: v.lat, lng: v.lng })));
      });
      try {
        if (points.length > 100) throw new Error('Too many waypoints');
        const routed = await this.router.route(userId, tripId, dayId, points, profile, avoid);
        // Spreading the run's own seg first carried its whole-route text and
        // midpoint onto every leg of a multi-stop run: `part` only overrides the
        // metres and the seconds, so leg one of a 480 km day reported 100 km with
        // "480 km" printed beside it. The four texts and the midpoint are per leg
        // now, the way the browser has always computed them — and in the reader's
        // own unit, rather than the router's hard-coded kilometres.
        const legs = routed.parts.map((part, index) => {
          const from = [points[index].lat, points[index].lng] as [number, number];
          const to = [points[index + 1].lat, points[index + 1].lng] as [number, number];
          const durationText = formatDurationShort(part.duration);
          return {
            ...routed.leg.seg,
            ...part,
            from,
            to,
            mid: [(from[0] + to[0]) / 2, (from[1] + to[1]) / 2] as [number, number],
            distanceText: formatDistance(part.distance / 1000, distanceUnit),
            durationText,
            drivingText: durationText,
            walkingText: durationText,
          };
        });
        Object.assign(
          allLegs,
          foldRouteRun(
            stops,
            stopAt,
            { coordinates: routed.leg.line, legs, vias: routed.leg.vias },
            profile,
            distanceUnit,
          ),
        );
        missedByDay[dayId] = [...new Set([...(missedByDay[dayId] ?? []), ...routed.avoidMissed])];
        stops.forEach((stop, index) => {
          const snapped = routed.snapped?.[stopAt[index]];
          if (snapped) (snapByDay[stop.ownerDayId] ??= {})[stopKey(stop)] = snapped;
        });
      } catch {
        pairs.forEach(({ from, to }) =>
          failures.push({
            fromAssignmentId: from.assignmentId,
            toAssignmentId: to.assignmentId,
            reason:
              points.length > 100
                ? 'More than 100 waypoints in this run.'
                : 'Routing provider could not calculate this run. Check the profile, connection and provider limits.',
          }),
        );
      }
    };
    for (const day of plan.filter((d) => d.stops.length > 1)) {
      const fallback = context.days.find((d) => d.id === day.dayId)?.default_transport_mode || 'driving';
      for (const run of splitIntoRuns(day.stops, (from) => from.legMode ?? fallback))
        await fetchRun(run.stops, day.dayId, run.mode);
    }
    const chains = window
      ? plan.filter((d) => d.stops.length)
      : spillChains(
          plan.filter((d) => d.stops.length > 1),
          plan.filter((d) => d.stops.length < 2),
          (a, b) => allLegs[stopKey(a) + '>' + stopKey(b)],
        );
    let previous: RoadtripStop | undefined;
    for (const chain of chains) {
      const pairs = chain.stops.slice(0, -1).map((from, index) => ({ from, to: chain.stops[index + 1] }));
      if (connectDays && previous && chain.stops.length) pairs.push({ from: previous, to: chain.stops[0] });
      for (const { from, to } of pairs) {
        // A night spent at one hotel is no drive, and no router is asked about it.
        if (isStationaryJoin(from, to) || asked.has(stopKey(from) + '>' + stopKey(to))) continue;
        await fetchRun(
          [from, to],
          chain.dayId,
          from.legMode ?? context.days.find((d) => d.id === chain.dayId)?.default_transport_mode ?? 'driving',
        );
      }
      previous = chain.stops[chain.stops.length - 1] ?? previous;
    }
    const vehicleKind = preferences.roadtrip_vehicle || null;
    const rangeKm =
      effectiveRangeKm(
        vehicleKind,
        {
          tankLitres: preferences.roadtrip_tank_litres,
          litresPer100: preferences.roadtrip_litres_per_100,
          batteryKwh: preferences.roadtrip_battery_kwh,
          kwhPer100: preferences.roadtrip_kwh_per_100,
          degradationPercent: preferences.roadtrip_battery_degradation,
        },
        preferences.roadtrip_range_km,
      ) || null;
    const calculated = assembleRoadtrip({
      plan: plan.filter((d) => standsAsDay(d.stops)),
      quietDays: plan.filter((d) => !standsAsDay(d.stops)),
      window,
      allLegs,
      snapByDay,
      missedByDay,
      loading: false,
      limits: {
        legMinutes: preferences.roadtrip_leg_minutes || null,
        dayMinutes: preferences.roadtrip_day_minutes || null,
        rangeKm,
        fillPercent: preferences.roadtrip_fill_percent || null,
      },
      vehicleKind,
      connectDays,
      boundaries: context.boundaries,
      distanceUnit,
      labels: { start: 'Continue journey', end: 'End of day' },
    });
    return {
      context,
      preferences,
      calculated,
      failures,
      omittedVisits: context.visits.filter((v) => v.lat === null || v.lng === null).map((v) => v.id),
      undatedRides: await this.undatedRides(tripId),
    };
  }
}