feat(tours): route choices for a hike, each checked against the router (MF-08)

A hike can ask the router for more than its difficulty limit: avoid steps,
avoid ferries, prefer footpaths, shorter over faster. They sit under the
limit in the planner's details as switches, each with what it really does,
and the info dot says they weigh the way and give way where there is no
other, and that none grades a path. Switching one plans the route again as
one step of the history, and the choice is kept in the draft.

Only preferences the public Valhalla was seen to honour are offered, with
the measured cases beside hikingCostingOptions: step_penalty took Würzburg
to the Festung from four flights to one, use_ferry 0 sent Kiel round the
Förde and Hamburg through the Elbe tunnel, walkway_factor moved Garmisch to
the Partnachklamm onto the paths, shortest cut the way to the Eckbauer.
use_hills and use_tracks showed no effect on foot and are left out, as are
marked, paved and rough ways, for which the router has no option.

tours.hiking_preferences (migration 045000) holds them as JSON, NULL for
none, so no tour saved before routes differently. Only a hike keeps them;
a paddle tour naming one is refused. A copy carries them, and list_tours
and get_tour_plan read them.

The trip copy's waypoint links now follow the copied places inside
TourWaypointsRepository.insertForPlace.
This commit is contained in:
Maurice
2026-10-19 11:31:31 +02:00
parent 66525c66b7
commit ad86713fdc
52 changed files with 759 additions and 33 deletions
+2
View File
@@ -35,6 +35,7 @@ export class Tours {
track_marks?: string | null;
paddle_lake_style?: TToursPaddleLakeStyle | null;
revision: number & Opt = 1;
hiking_preferences?: string | null;
tourTypeRef!: Ref<TourTypes>;
tour_obstacle_reviews_collection = new Collection<TourObstacleReviews>(this);
tour_sources: Ref<TourSources> | null = null;
@@ -110,6 +111,7 @@ export const ToursSchema = defineEntity({
track_marks: p.text().nullable(),
paddle_lake_style: p.enum(() => ToursPaddleLakeStyle).nullable(),
revision: p.integer().default(1),
hiking_preferences: p.text().nullable(),
tourTypeRef: () => p.manyToOne(TourTypes).ref().joinColumn('tour_type').hidden().index('idx_tours_tour_type'),
tour_obstacle_reviews_collection: () => p.oneToMany(TourObstacleReviews).mappedBy('place').hidden(),
tour_sources: () => p.oneToOne(TourSources).ref().mappedBy('place').hidden(),
+9
View File
@@ -2,6 +2,7 @@ import type { JsonColumn } from '../utils/json-column';
import {
FERRATAS_RESULT_CAP,
ferrataSchema,
hikingPreferencesSchema,
obstacleSnapshotSchema,
paddleGeometrySchema,
paddleLockSchema,
@@ -10,6 +11,7 @@ import {
tourFixedLegSchema,
trackMarksSchema,
type Ferrata,
type HikingPreferences,
type ObstacleSnapshot,
type PaddleGeometry,
type PaddleLock,
@@ -175,6 +177,13 @@ export const TOUR_TRACK_MARKS: JsonColumn<TrackMarks | null> = {
fallback: () => null,
};
/** MF-08: a hike's routing preferences. A broken row reads as none, so the hike routes as it would without. */
export const TOUR_HIKING_PREFERENCES: JsonColumn<HikingPreferences | null> = {
column: 'tours.hiking_preferences',
schema: hikingPreferencesSchema.nullable(),
fallback: () => null,
};
/** A paddle object's own geometry (PAD-05A). A broken row has none, and the object is left out rather than drawn anywhere. */
export const PADDLE_FEATURE_GEOMETRY: JsonColumn<PaddleGeometry | null> = {
column: 'paddle_features.geometry',
+1
View File
@@ -27,4 +27,5 @@ export interface ToursTable {
track_marks: InsertOptional<string | null>;
paddle_lake_style: InsertOptional<string | null>;
revision: InsertOptional<number>;
hiking_preferences: InsertOptional<string | null>;
}
@@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';
/**
* MF-08: what a hike asks the router for besides its difficulty limit (steps,
* ferries, footpaths, the shorter way), as JSON. NULL for none, which every
* tour saved before keeps, so no existing route changes by itself.
*/
export class Migration20200101045000_a_hike_keeps_its_routing_preferences extends Migration {
override name = 'Migration20200101045000_a_hike_keeps_its_routing_preferences';
override up(): void {
this.addSql('ALTER TABLE tours ADD COLUMN hiking_preferences TEXT');
}
}
@@ -103,10 +103,18 @@ export class TourWaypointsRepository extends TrekRepository<TourWaypoints> {
/**
* TW3 — `INSERT INTO tour_waypoints (place_id, lat, lng, role, sequence, fixed_leg, lock, shaping)
* VALUES (...)` for each point. The caller owns the transaction: a create
* writes the tour first, an edit clears the old points first.
* writes the tour first, an edit clears the old points first. A trip copy
* hands its map of copied places, which the links follow.
*/
async insertForPlace(place_id: number, waypoints: readonly TourWaypointRow[]): Promise<void> {
async insertForPlace(
place_id: number,
waypoints: readonly TourWaypointRow[],
copiedPlaceIds?: ReadonlyMap<number, number>,
): Promise<void> {
if (waypoints.length === 0) return;
// In a trip copy a point standing for a place of the trip stands for that place's copy (PR1-10).
const linkOf = (id: number | null | undefined) =>
id == null ? null : copiedPlaceIds ? (copiedPlaceIds.get(id) ?? null) : id;
await this.insertMany(
waypoints.map((w) => ({
place: place_id,
@@ -117,7 +125,7 @@ export class TourWaypointsRepository extends TrekRepository<TourWaypoints> {
fixed_leg: w.fixed_leg ? encodeJson(TOUR_WAYPOINT_FIXED_LEG, w.fixed_leg) : null,
lock: w.lock ? encodeJson(TOUR_WAYPOINT_LOCK, w.lock) : null,
shaping: w.shaping ? 1 : 0,
linkedPlace: w.linked_place_id ?? null,
linkedPlace: linkOf(w.linked_place_id),
})),
);
}
@@ -40,6 +40,8 @@ export interface TourListRow {
has_waypoints: number;
/** P2C-03: the tour's technical revision. */
revision: number;
/** MF-08: a hike's routing preferences, still JSON text. */
hiking_preferences: string | null;
}
/** Every `tours` column, as the trip copy reads and re-inserts it. */
@@ -68,6 +70,7 @@ export interface TourRow {
paddle_lake_style: string | null;
track_marks: string | null;
revision: number;
hiking_preferences: string | null;
}
const _tourRowKeys: AssertRowKeys<TourRow, Tours> = true;
@@ -89,6 +92,8 @@ export interface TourInsert {
paddle_speed_kmh?: number | null;
portage_speed_kmh?: number | null;
paddle_lake_style?: PaddleLakeStyle | null;
/** MF-08: a hike's routing preferences as JSON text; left out, none. */
hiking_preferences?: string | null;
}
/** What a route edit replaces on an existing tour: the type and everything that belongs to it. */
@@ -204,6 +209,7 @@ export class ToursRepository extends TrekRepository<Tours> {
't.paddle_lake_style as paddle_lake_style',
't.track_marks as track_marks',
't.revision as revision',
't.hiking_preferences as hiking_preferences',
eb('t.recorded_route_geometry', 'is not', null).as('has_recorded_track'),
eb
.exists(eb.selectFrom('day_assignments as da').select('da.id').whereRef('da.place_id', '=', 'p.id'))
@@ -461,6 +467,7 @@ export class ToursRepository extends TrekRepository<Tours> {
portage_speed_kmh: input.portage_speed_kmh ?? null,
paddle_lake_style: input.paddle_lake_style ?? null,
track_marks: input.track_marks ?? null,
hiking_preferences: input.hiking_preferences ?? null,
});
}
@@ -541,6 +548,7 @@ export class ToursRepository extends TrekRepository<Tours> {
paddle_speed_kmh: input.paddle_speed_kmh,
portage_speed_kmh: input.portage_speed_kmh,
paddle_lake_style: input.paddle_lake_style,
...(input.hiking_preferences !== undefined ? { hiking_preferences: input.hiking_preferences } : {}),
})
.where('place_id', '=', place_id)
.where('place_id', 'in', (eb) => eb.selectFrom('places').select('id').where('trip_id', '=', trip_id))
+1 -1
View File
@@ -48,7 +48,7 @@ export class ToursMcp {
@Tool({
name: 'list_tours',
description:
'List the saved tours of a trip (hikes and paddle tours planned or imported in the Tours addon): name, type (hike, or kayak for a paddle tour), where it came from (origin: trek, gpx or wanderer), distance in km, ascent and descent in metres, the stored routing limit of a hike (max_hiking_difficulty, SAC T1 to T6), the boat, water and chosen speed of a paddle tour (boat_type, water_context, paddle_speed_kmh in km/h, null while the suggested speed of the boat applies), the check bench of a paddle tour (paddle_check: how far the OpenStreetMap obstacle data covers its line and how many obstacles are open, to check again or decided by the travellers; a planning status, never a safety or legal clearance), the revision (raised by every saved change of the tour), and the days of the trip each tour is on.',
'List the saved tours of a trip (hikes and paddle tours planned or imported in the Tours addon): name, type (hike, or kayak for a paddle tour), where it came from (origin: trek, gpx or wanderer), distance in km, ascent and descent in metres, the stored routing limit of a hike (max_hiking_difficulty, SAC T1 to T6) and its route choices (hiking_preferences: avoid_steps, avoid_ferries, prefer_paths, shortest; weightings, never a grade of the path), the boat, water and chosen speed of a paddle tour (boat_type, water_context, paddle_speed_kmh in km/h, null while the suggested speed of the boat applies), the check bench of a paddle tour (paddle_check: how far the OpenStreetMap obstacle data covers its line and how many obstacles are open, to check again or decided by the travellers; a planning status, never a safety or legal clearance), the revision (raised by every saved change of the tour), and the days of the trip each tour is on.',
inputSchema: { tripId: idSchema },
annotations: TOOL_ANNOTATIONS_READONLY,
when: addonGate(ADDON_IDS.TOURS),
+14 -2
View File
@@ -2,7 +2,7 @@ import { Places } from '../../db/entities/Places.entity';
import { TourTypes } from '../../db/entities/TourTypes.entity';
import { TourWaypoints } from '../../db/entities/TourWaypoints.entity';
import { Tours } from '../../db/entities/Tours.entity';
import { TOUR_TRACK_MARKS } from '../../db/json-columns';
import { TOUR_HIKING_PREFERENCES, TOUR_TRACK_MARKS } from '../../db/json-columns';
import type { PlacesRepository } from '../../db/repositories/Places.repository';
import type { TourTypesRepository } from '../../db/repositories/TourTypes.repository';
import type { TourWaypointsRepository } from '../../db/repositories/TourWaypoints.repository';
@@ -13,7 +13,7 @@ import type {
ToursRepository,
TourUpdate,
} from '../../db/repositories/Tours.repository';
import { decodeJson } from '../../utils/json-column';
import { decodeJson, encodeJson } from '../../utils/json-column';
import { DomainError } from '../common/domain-error';
import { toRowId } from '../common/row-id';
import { UnitOfWork } from '../database/unit-of-work';
@@ -31,6 +31,7 @@ import {
import { InjectRepository } from '@mikro-orm/nestjs';
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import {
hikingPreferencesOf,
MAX_PLANNED_TOUR_DURATION_MINUTES,
plannedTourDurationMinutesSchema,
tourWebsiteSchema,
@@ -67,6 +68,12 @@ export interface ImportGpxAsTourResult {
/** A GPX file imported without a type is a hike. */
const HIKE_IMPORT: TourGpxImportFields = { tour_type: 'hike' };
/** MF-08: a save's hiking preferences as stored: a hike's switched-on ones as JSON, else NULL. */
function hikingFields(input: TourCreateRequest): string | null {
const preferences = input.tour_type === 'hike' ? hikingPreferencesOf(input.hiking_preferences) : null;
return preferences ? encodeJson(TOUR_HIKING_PREFERENCES, preferences) : null;
}
@Injectable()
export class ToursService {
private readonly logger = new Logger(ToursService.name);
@@ -112,6 +119,7 @@ export class ToursService {
caution: r.match_confidence !== null && r.match_confidence < LOW_CONFIDENCE_THRESHOLD,
has_waypoints: Boolean(r.has_waypoints),
revision: r.revision,
hiking_preferences: decodeJson(TOUR_HIKING_PREFERENCES, r.hiking_preferences, 'tour'),
};
}
@@ -140,6 +148,8 @@ export class ToursService {
paddle_speed_kmh: input.paddle_speed_kmh ?? null,
portage_speed_kmh: input.portage_speed_kmh ?? null,
paddle_lake_style: input.paddle_lake_style ?? null,
// MF-08: only a hike keeps preferences, and only those switched on; none is NULL.
hiking_preferences: hikingFields(input),
};
}
@@ -436,6 +446,7 @@ export class ToursService {
portage_speed_kmh: source.portage_speed_kmh,
paddle_lake_style: source.paddle_lake_style as PaddleLakeStyle | null,
track_marks: source.track_marks,
hiking_preferences: source.hiking_preferences,
wanderer_ref: source.wanderer_ref,
difficulty: source.difficulty,
derived_from_place_id: pid,
@@ -628,6 +639,7 @@ export class ToursService {
planned: 0,
has_waypoints: 0,
revision: 1,
hiking_preferences: null,
}),
);
}
+1 -5
View File
@@ -754,11 +754,7 @@ export class TripsService {
waypointsByPlace.set(place_id, list);
}
for (const [placeId, waypoints] of waypointsByPlace) {
// A point standing for a place of the trip stands for that place's copy (PR1-10).
const remapped = waypoints.map((point) =>
point.linked_place_id ? { ...point, linked_place_id: placeMap.get(point.linked_place_id) ?? null } : point,
);
await this.tourWaypointsRepo.insertForPlace(placeMap.get(placeId)!, remapped);
await this.tourWaypointsRepo.insertForPlace(placeMap.get(placeId)!, waypoints, placeMap);
}
const oldAssignments = await this.dayAssignmentsRepo.listAllForTrip(sourceTripId); // TP48