feat(tours): complete planner follow-up scope

This commit completes the reviewed Tours follow-up scope after the Tours MVP was merged into dev.

Included feature blocks:

- RC-01: identify Tours in PLAN day rows and show compact Tour facts;
- RC-06: search for a city or address in TOUR-PLANNER without hidden writes;
- RC-07: reorder Tour waypoints in the left sidebar;
- RC-08: drag Tour waypoints on Leaflet, MapLibre and Mapbox with stable viewport, history and routing behavior;
- RS-01: persist Tour descriptions and HTTPS information links with local provider classification;
- RS-02: persist breaks/additional time and calculated or manually adjusted planned Tour duration without changing scheduling semantics.
The implementation preserves:

- ordinary Place and Legacy Track behavior;
- Tours-off fallback behavior;
- existing GPX/import behavior;
- Tour assignment and deletion behavior;
- calculated routing duration as a separate value;
- existing Tour provenance fields;
- keyboard/list alternatives for waypoint editing.
Not included:

- PR1-09 trip-wide Tour summary;
- Wanderer integration;
- GPX import changes;
- new Tour types;
- Tour-to-Day drag-and-drop;
- further roadmap or Post-Release features.
Validation:

- focused RC-01/RC-06/RC-07/RC-08/RS-01/RS-02 tests passed;
- affected shared/server/client typechecks passed;
- migration and generated DB type checks passed;
- scoped ESLint passed;
- staged diff checks passed;
- owner manual tests passed for all six feature blocks.
This commit is contained in:
elmocito
2026-10-09 13:52:11 +02:00
committed by Maurice
parent af9f013c5e
commit 57ea219a43
56 changed files with 4315 additions and 462 deletions
+5 -1
View File
@@ -1,8 +1,8 @@
import { Collection, EntityRepositoryType, type Opt, PrimaryKeyProp, type Ref, defineEntity, p } from '@mikro-orm/core';
import { ToursRepository } from '../repositories/Tours.repository';
import { Places } from './Places.entity';
import { TourTypes } from './TourTypes.entity';
import { TourWaypoints } from './TourWaypoints.entity';
import { Collection, EntityRepositoryType, type Opt, PrimaryKeyProp, type Ref, defineEntity, p } from '@mikro-orm/core';
export class Tours {
[EntityRepositoryType]?: ToursRepository;
@@ -14,6 +14,8 @@ export class Tours {
elevation_gain?: number | null;
elevation_loss?: number | null;
duration?: number | null;
planned_duration_minutes?: number | null;
break_additional_minutes?: number | null;
difficulty?: string | null;
wanderer_ref?: string | null;
match_confidence?: number | null;
@@ -34,6 +36,8 @@ export const ToursSchema = defineEntity({
elevation_gain: p.double().nullable(),
elevation_loss: p.double().nullable(),
duration: p.double().nullable(),
planned_duration_minutes: p.integer().nullable(),
break_additional_minutes: p.integer().nullable(),
difficulty: p.text().nullable(),
wanderer_ref: p.text().nullable(),
match_confidence: p.double().nullable(),
+2
View File
@@ -14,4 +14,6 @@ export interface ToursTable {
match_confidence: InsertOptional<number | null>;
created_at: InsertOptional<string | null>;
max_hiking_difficulty: InsertOptional<number>;
planned_duration_minutes: InsertOptional<number | null>;
break_additional_minutes: InsertOptional<number | null>;
}
@@ -0,0 +1,16 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/** A Tour-owned manual total, separate from the route engine's duration. */
export class Migration20200101042900_tour_planned_total_duration extends Migration {
override name = 'Migration20200101042900_tour_planned_total_duration';
override async up(): Promise<void> {
await addColumnIfMissing(
this,
'tours',
'planned_duration_minutes',
'planned_duration_minutes INTEGER CHECK(planned_duration_minutes BETWEEN 0 AND 1440)',
);
}
}
@@ -0,0 +1,16 @@
import { addColumnIfMissing } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/** Breaks and additional planned time stay separate from walking ETA and total override. */
export class Migration20200101043000_tour_break_additional_duration extends Migration {
override name = 'Migration20200101043000_tour_break_additional_duration';
override async up(): Promise<void> {
await addColumnIfMissing(
this,
'tours',
'break_additional_minutes',
'break_additional_minutes INTEGER CHECK(break_additional_minutes BETWEEN 0 AND 1440)',
);
}
}
+288 -137
View File
@@ -1,13 +1,13 @@
import type { Places } from '../entities/Places.entity';
import { absDifference, coalesceParam, columnRef, currentTimestamp, lowerTrim } from '../dialect/sql-functions';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
import type { Places } from '../entities/Places.entity';
import type { DB } from '../kysely/db';
// Task 9 fix wave (B-L2): `TagRow` is Tags.repository.ts's own row type,
// byte-identical to what used to be hand-mirrored here — a second
// declaration of the same shape is exactly the duplication Sonar flags and
// the program's "single source of truth" rule (root CLAUDE.md) forbids.
import type { TagRow } from './Tags.repository';
import type { DB } from '../kysely/db';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** A `places` row as the API emits it — every scalar column of the entity. */
export interface PlaceRow {
@@ -75,9 +75,29 @@ type PlacesTripFileKyselyDB = Pick<DB, 'trip_files'>;
*/
export type SharePublicPlaceRow = Pick<
PlaceWithCategoryRow,
| 'id' | 'trip_id' | 'name' | 'description' | 'lat' | 'lng' | 'address' | 'category_id' | 'price' | 'currency'
| 'place_time' | 'end_time' | 'duration_minutes' | 'notes' | 'image_url' | 'website' | 'phone' | 'transport_mode'
| 'created_at' | 'updated_at' | 'category_name' | 'category_color' | 'category_icon'
| 'id'
| 'trip_id'
| 'name'
| 'description'
| 'lat'
| 'lng'
| 'address'
| 'category_id'
| 'price'
| 'currency'
| 'place_time'
| 'end_time'
| 'duration_minutes'
| 'notes'
| 'image_url'
| 'website'
| 'phone'
| 'transport_mode'
| 'created_at'
| 'updated_at'
| 'category_name'
| 'category_color'
| 'category_icon'
>;
type SharePublicPlaceKyselyDB = Pick<DB, 'places' | 'categories'>;
@@ -142,7 +162,10 @@ export class PlacesRepository extends TrekRepository<Places> {
.leftJoin('p.category', 'c')
.leftJoin('p.tours', 't')
.select([
'p.*', 'c.name as category_name', 'c.color as category_color', 'c.icon as category_icon',
'p.*',
'c.name as category_name',
'c.color as category_color',
'c.icon as category_icon',
columnRef(platform, 't.place_id').as('tour_place_id'),
])
.where('p.id = ?', [place_id])
@@ -281,10 +304,7 @@ export class PlacesRepository extends TrekRepository<Places> {
* the 3g plan's task split — every other method here belongs to Plan 3c.
*/
async findRaw(id: number): Promise<PlaceRow | undefined> {
return this.qb('p')
.select(['p.*'])
.where({ 'p.id': id })
.execute<PlaceRow | undefined>('get', false);
return this.qb('p').select(['p.*']).where({ 'p.id': id }).execute<PlaceRow | undefined>('get', false);
}
/**
@@ -294,7 +314,10 @@ export class PlacesRepository extends TrekRepository<Places> {
* callers immediately follow a `true` result with `deleteById(id)` on the
* SAME numeric id.
*/
async reclaimInputs(id: number, trip_id: number): Promise<{ google_place_id: string | null; image_url: string | null } | undefined> {
async reclaimInputs(
id: number,
trip_id: number,
): Promise<{ google_place_id: string | null; image_url: string | null } | undefined> {
return this.qb('p')
.select(['p.google_place_id', 'p.image_url'])
.where({ 'p.id': id, 'p.trip_id': trip_id })
@@ -428,67 +451,73 @@ export class PlacesRepository extends TrekRepository<Places> {
* `findInTrip` first) — this method writes exactly what it is handed,
* plus the timestamp stamp every write gets.
*/
async updatePlace(id: number, write: {
// Task 9 fix wave (B-L10): `places.name` is `NOT NULL` (`PlaceRow.name:
// string`); the caller's `(name || null) ?? existingPlace.name` fold
// always resolves to a real string before this is called — `string |
// null` here was a widened lie the type checker could never catch a
// real NULL write through.
name: string;
description: string | null;
lat: number | null;
lng: number | null;
address: string | null;
category_id: number | null;
price: number | null;
currency: string | null;
place_time: string | null;
end_time: string | null;
duration_minutes: number | null;
notes: string | null;
image_url: string | null;
google_place_id: string | null;
google_ftid: string | null;
osm_id: string | null;
amap_poi_id: string | null;
website: string | null;
phone: string | null;
transport_mode: string | null;
route_color: string | null;
stop_type: string | null;
fill_percent: number | null;
email: string | null;
opening_hours: string | null;
}): Promise<void> {
async updatePlace(
id: number,
write: {
// Task 9 fix wave (B-L10): `places.name` is `NOT NULL` (`PlaceRow.name:
// string`); the caller's `(name || null) ?? existingPlace.name` fold
// always resolves to a real string before this is called — `string |
// null` here was a widened lie the type checker could never catch a
// real NULL write through.
name: string;
description: string | null;
lat: number | null;
lng: number | null;
address: string | null;
category_id: number | null;
price: number | null;
currency: string | null;
place_time: string | null;
end_time: string | null;
duration_minutes: number | null;
notes: string | null;
image_url: string | null;
google_place_id: string | null;
google_ftid: string | null;
osm_id: string | null;
amap_poi_id: string | null;
website: string | null;
phone: string | null;
transport_mode: string | null;
route_color: string | null;
stop_type: string | null;
fill_percent: number | null;
email: string | null;
opening_hours: string | null;
},
): Promise<void> {
const platform = this.getEntityManager().getPlatform();
await this.nativeUpdate({ id }, {
name: write.name,
description: write.description,
lat: write.lat,
lng: write.lng,
address: write.address,
category: write.category_id,
price: write.price,
currency: write.currency,
place_time: write.place_time,
end_time: write.end_time,
duration_minutes: write.duration_minutes,
notes: write.notes,
image_url: write.image_url,
google_place_id: write.google_place_id,
google_ftid: write.google_ftid,
osm_id: write.osm_id,
amap_poi_id: write.amap_poi_id,
website: write.website,
phone: write.phone,
transport_mode: write.transport_mode,
route_color: write.route_color,
stop_type: write.stop_type,
fill_percent: write.fill_percent,
email: write.email,
opening_hours: write.opening_hours,
updated_at: currentTimestamp(platform),
});
await this.nativeUpdate(
{ id },
{
name: write.name,
description: write.description,
lat: write.lat,
lng: write.lng,
address: write.address,
category: write.category_id,
price: write.price,
currency: write.currency,
place_time: write.place_time,
end_time: write.end_time,
duration_minutes: write.duration_minutes,
notes: write.notes,
image_url: write.image_url,
google_place_id: write.google_place_id,
google_ftid: write.google_ftid,
osm_id: write.osm_id,
amap_poi_id: write.amap_poi_id,
website: write.website,
phone: write.phone,
transport_mode: write.transport_mode,
route_color: write.route_color,
stop_type: write.stop_type,
fill_percent: write.fill_percent,
email: write.email,
opening_hours: write.opening_hours,
updated_at: currentTimestamp(platform),
},
);
}
/**
@@ -498,7 +527,10 @@ export class PlacesRepository extends TrekRepository<Places> {
* 1` — the same candidate id bound in all four positions, exactly as the
* legacy statement does.
*/
async findDuplicateByExternalId(trip_id: string, external_id: string): Promise<{ id: number; google_ftid: string | null } | undefined> {
async findDuplicateByExternalId(
trip_id: string,
external_id: string,
): Promise<{ id: number; google_ftid: string | null } | undefined> {
return this.qb('p')
.select(['p.id', 'p.google_ftid'])
.where('p.trip_id = ?', [trip_id])
@@ -530,7 +562,10 @@ export class PlacesRepository extends TrekRepository<Places> {
* :609-619`) already accepts and this repository reproduces literally,
* not fixes).
*/
async findDuplicateByName(trip_id: string, lowered_trimmed_name: string): Promise<{ id: number; google_ftid: string | null } | undefined> {
async findDuplicateByName(
trip_id: string,
lowered_trimmed_name: string,
): Promise<{ id: number; google_ftid: string | null } | undefined> {
const platform = this.getEntityManager().getPlatform();
return this.qb('p')
.select(['p.id', 'p.google_ftid'])
@@ -552,7 +587,12 @@ export class PlacesRepository extends TrekRepository<Places> {
* `ABS(lat - ?) <= ?` then `ABS(lng - ?) <= ?`, the SAME `tolerance` value
* on both.
*/
async findDuplicateByCoords(trip_id: string, lat: number, lng: number, tolerance: number): Promise<{ id: number; google_ftid: string | null } | undefined> {
async findDuplicateByCoords(
trip_id: string,
lat: number,
lng: number,
tolerance: number,
): Promise<{ id: number; google_ftid: string | null } | undefined> {
const platform = this.getEntityManager().getPlatform();
return this.qb('p')
.select(['p.id', 'p.google_ftid'])
@@ -576,17 +616,33 @@ export class PlacesRepository extends TrekRepository<Places> {
* is `DayAssignmentsRepository.listItineraryForGpx` — a different table
* root, kept there per the task-4 brief's own pointer.
*/
async listForGpx(trip_id: string): Promise<{
name: string; description: string | null; address: string | null;
lat: number | null; lng: number | null; route_geometry: string | null;
category: string | null;
}[]> {
async listForGpx(trip_id: string): Promise<
{
name: string;
description: string | null;
address: string | null;
lat: number | null;
lng: number | null;
route_geometry: string | null;
category: string | null;
}[]
> {
return this.qb('p')
.leftJoin('p.category', 'c')
.select(['p.name', 'p.description', 'p.address', 'p.lat', 'p.lng', 'p.route_geometry', 'c.name as category'])
.where('p.trip_id = ?', [trip_id])
.orderBy({ 'p.id': 'asc' })
.execute<{ name: string; description: string | null; address: string | null; lat: number | null; lng: number | null; route_geometry: string | null; category: string | null }[]>('all', false);
.execute<
{
name: string;
description: string | null;
address: string | null;
lat: number | null;
lng: number | null;
route_geometry: string | null;
category: string | null;
}[]
>('all', false);
}
/**
@@ -603,17 +659,31 @@ export class PlacesRepository extends TrekRepository<Places> {
* folding logic, which stays in `PlacesService.buildDedupSet` exactly as
* it was.
*/
async listDedupInputs(trip_id: string): Promise<{
name: string | null; lat: number | null; lng: number | null;
google_place_id: string | null; google_ftid: string | null; osm_id: string | null; amap_poi_id: string | null;
}[]> {
async listDedupInputs(trip_id: string): Promise<
{
name: string | null;
lat: number | null;
lng: number | null;
google_place_id: string | null;
google_ftid: string | null;
osm_id: string | null;
amap_poi_id: string | null;
}[]
> {
return this.qb('p')
.select(['p.name', 'p.lat', 'p.lng', 'p.google_place_id', 'p.google_ftid', 'p.osm_id', 'p.amap_poi_id'])
.where('p.trip_id = ?', [trip_id])
.execute<{
name: string | null; lat: number | null; lng: number | null;
google_place_id: string | null; google_ftid: string | null; osm_id: string | null; amap_poi_id: string | null;
}[]>('all', false);
.execute<
{
name: string | null;
lat: number | null;
lng: number | null;
google_place_id: string | null;
google_ftid: string | null;
osm_id: string | null;
amap_poi_id: string | null;
}[]
>('all', false);
}
/**
@@ -658,12 +728,15 @@ export class PlacesRepository extends TrekRepository<Places> {
* with the legacy statement's own `SELECT DISTINCT`, not a functional
* requirement.
*/
async listForTrip(trip_id: string, filters: {
searchPattern?: string;
category?: string;
tag?: string;
assignment?: 'all' | 'unassigned' | 'assigned';
}): Promise<PlaceListRow[]> {
async listForTrip(
trip_id: string,
filters: {
searchPattern?: string;
category?: string;
tag?: string;
assignment?: 'all' | 'unassigned' | 'assigned';
},
): Promise<PlaceListRow[]> {
const platform = this.getEntityManager().getPlatform();
const qb = this.qb('p')
.leftJoin('p.category', 'c')
@@ -673,18 +746,26 @@ export class PlacesRepository extends TrekRepository<Places> {
// places. `columnRef`, because the bare `t.place_id` twin is
// persist(false) and selects nothing.
.leftJoin('p.tours', 't')
.select([
'p.*', columnRef(platform, 't.place_id').as('tour_place_id'),
'c.name as category_name', 'c.color as category_color', 'c.icon as category_icon',
'pr.country_code as country_code', 'pr.region_name as region_name',
], true)
.select(
[
'p.*',
columnRef(platform, 't.place_id').as('tour_place_id'),
'c.name as category_name',
'c.color as category_color',
'c.icon as category_icon',
'pr.country_code as country_code',
'pr.region_name as region_name',
],
true,
)
.where('p.trip_id = ?', [trip_id]);
if (filters.searchPattern) {
qb.andWhere(
"(p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\' OR p.description LIKE ? ESCAPE '\\')",
[filters.searchPattern, filters.searchPattern, filters.searchPattern],
);
qb.andWhere("(p.name LIKE ? ESCAPE '\\' OR p.address LIKE ? ESCAPE '\\' OR p.description LIKE ? ESCAPE '\\')", [
filters.searchPattern,
filters.searchPattern,
filters.searchPattern,
]);
}
if (filters.category) {
qb.andWhere('p.category_id = ?', [filters.category]);
@@ -715,7 +796,12 @@ export class PlacesRepository extends TrekRepository<Places> {
* trip_id = ? AND deleted_at IS NULL`: the attachment a place takes its
* picture from, trash excluded.
*/
async findActiveTripFile(id: number, trip_id: number): Promise<{ filename: string; original_name: string; mime_type: string | null; file_size: number | null } | undefined> {
async findActiveTripFile(
id: number,
trip_id: number,
): Promise<
{ filename: string; original_name: string; mime_type: string | null; file_size: number | null } | undefined
> {
return await this.kysely<PlacesTripFileKyselyDB>()
.selectFrom('trip_files')
.select(['filename', 'original_name', 'mime_type', 'file_size'])
@@ -798,14 +884,18 @@ export class PlacesRepository extends TrekRepository<Places> {
* - PL44 (`enrichOne`'s photo update) — `image_url` alone.
* - PL46 (`backfillMissingAddresses`) — `address` alone.
*/
async fillIfEmpty(id: number, trip_id: number, fields: {
google_place_id?: string | null;
google_ftid?: string | null;
address?: string | null;
website?: string | null;
phone?: string | null;
image_url?: string | null;
}): Promise<void> {
async fillIfEmpty(
id: number,
trip_id: number,
fields: {
google_place_id?: string | null;
google_ftid?: string | null;
address?: string | null;
website?: string | null;
phone?: string | null;
image_url?: string | null;
},
): Promise<void> {
const platform = this.getEntityManager().getPlatform();
const data: {
google_place_id?: ReturnType<typeof coalesceParam>;
@@ -816,7 +906,8 @@ export class PlacesRepository extends TrekRepository<Places> {
image_url?: ReturnType<typeof coalesceParam>;
updated_at: ReturnType<typeof currentTimestamp>;
} = { updated_at: currentTimestamp(platform) };
if (fields.google_place_id !== undefined) data.google_place_id = coalesceParam(platform, 'google_place_id', fields.google_place_id);
if (fields.google_place_id !== undefined)
data.google_place_id = coalesceParam(platform, 'google_place_id', fields.google_place_id);
if (fields.google_ftid !== undefined) data.google_ftid = coalesceParam(platform, 'google_ftid', fields.google_ftid);
if (fields.address !== undefined) data.address = coalesceParam(platform, 'address', fields.address);
if (fields.website !== undefined) data.website = coalesceParam(platform, 'website', fields.website);
@@ -869,10 +960,29 @@ export class PlacesRepository extends TrekRepository<Places> {
.selectFrom('places as p')
.leftJoin('categories as c', 'c.id', 'p.category_id')
.select([
'p.id', 'p.trip_id', 'p.name', 'p.description', 'p.lat', 'p.lng', 'p.address', 'p.category_id',
'p.price', 'p.currency', 'p.place_time', 'p.end_time', 'p.duration_minutes', 'p.notes',
'p.image_url', 'p.website', 'p.phone', 'p.transport_mode', 'p.created_at', 'p.updated_at',
'c.name as category_name', 'c.color as category_color', 'c.icon as category_icon',
'p.id',
'p.trip_id',
'p.name',
'p.description',
'p.lat',
'p.lng',
'p.address',
'p.category_id',
'p.price',
'p.currency',
'p.place_time',
'p.end_time',
'p.duration_minutes',
'p.notes',
'p.image_url',
'p.website',
'p.phone',
'p.transport_mode',
'p.created_at',
'p.updated_at',
'c.name as category_name',
'c.color as category_color',
'c.icon as category_icon',
])
.where('p.trip_id', '=', trip_id as number)
.orderBy('p.created_at', 'desc')
@@ -912,10 +1022,7 @@ export class PlacesRepository extends TrekRepository<Places> {
* relatively ordered) the same way the legacy loop did.
*/
async listAllForTrip(trip_id: number | string): Promise<PlaceRow[]> {
return await this.qb('p')
.select(['p.*'])
.where('p.trip_id = ?', [trip_id])
.execute<PlaceRow[]>('all', false);
return await this.qb('p').select(['p.*']).where('p.trip_id = ?', [trip_id]).execute<PlaceRow[]>('all', false);
}
/**
@@ -1015,11 +1122,17 @@ export class PlacesRepository extends TrekRepository<Places> {
}
/** CH1 (`ChargingService.read`) — `SELECT name, lat, lng, stop_type FROM places WHERE id = ? AND trip_id = ?`. */
async findChargingProbe(id: number, trip_id: number): Promise<{ name: string; lat: number | null; lng: number | null; stop_type: string | null } | undefined> {
async findChargingProbe(
id: number,
trip_id: number,
): Promise<{ name: string; lat: number | null; lng: number | null; stop_type: string | null } | undefined> {
const row = await this.qb('p')
.select(['p.name', 'p.lat', 'p.lng', 'p.stop_type'])
.where({ id, trip: trip_id })
.execute<{ name: string; lat: number | null; lng: number | null; stop_type: string | null } | undefined>('get', false);
.execute<{ name: string; lat: number | null; lng: number | null; stop_type: string | null } | undefined>(
'get',
false,
);
return row ?? undefined;
}
@@ -1044,10 +1157,7 @@ export class PlacesRepository extends TrekRepository<Places> {
/** AC31 (`AccommodationsService.createAccommodation`) — `SELECT name FROM places WHERE id = ?`, the auto-created partner booking's title source. */
async getName(id: number): Promise<{ name: string } | undefined> {
return await this.qb('p')
.select(['p.name'])
.where({ id })
.execute<{ name: string } | undefined>('get', false);
return await this.qb('p').select(['p.name']).where({ id }).execute<{ name: string } | undefined>('get', false);
}
// ---------------------------------------------------------------------------
@@ -1119,7 +1229,9 @@ export class PlacesRepository extends TrekRepository<Places> {
* city apart from the region sitting right above it in the same address
* (#1115) — unchanged from the legacy projection.
*/
async listAddressesForUser(user_id: number): Promise<{ address: string | null; lat: number | null; lng: number | null; region_name: string | null }[]> {
async listAddressesForUser(
user_id: number,
): Promise<{ address: string | null; lat: number | null; lng: number | null; region_name: string | null }[]> {
return await this.kysely<PlacesAddressesForUserKyselyDB>()
.selectFrom('places as p')
.innerJoin('trips as t', 't.id', 'p.trip_id')
@@ -1178,8 +1290,16 @@ export class PlacesRepository extends TrekRepository<Places> {
return await this.kysely<ImportablePlacesKyselyDB>()
.selectFrom('places as p')
.select((eb) => [
'p.id as place_id', 'p.name', 'p.address', 'p.lat', 'p.lng', 'p.category_id', 'p.image_url',
'p.google_place_id', 'p.google_ftid', 'p.osm_id',
'p.id as place_id',
'p.name',
'p.address',
'p.lat',
'p.lng',
'p.category_id',
'p.image_url',
'p.google_place_id',
'p.google_ftid',
'p.osm_id',
eb
.selectFrom('day_assignments as da')
.innerJoin('days as d', 'd.id', 'da.day_id')
@@ -1265,7 +1385,11 @@ export class PlacesRepository extends TrekRepository<Places> {
phone: string | null;
osm_id: string | null;
}): Promise<number> {
const inserted = await this.kysely<PlacesNarrowInsertKyselyDB>().insertInto('places').values(row).returning('id').executeTakeFirstOrThrow();
const inserted = await this.kysely<PlacesNarrowInsertKyselyDB>()
.insertInto('places')
.values(row)
.returning('id')
.executeTakeFirstOrThrow();
return inserted.id;
}
@@ -1316,8 +1440,16 @@ export class PlacesRepository extends TrekRepository<Places> {
.leftJoin('categories as c', 'c.id', 'p.category_id')
.select([
'da.day_id',
'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',
'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('da.accommodation_id', 'is', null)
@@ -1333,7 +1465,15 @@ export class PlacesRepository extends TrekRepository<Places> {
* ?)`. The place a drawn tour lives on starts at the route's first point;
* every other column keeps its default. Returns the generated id.
*/
async insertTourPlace(input: { trip_id: number; name: string; lat: number; lng: number; route_geometry: string }): Promise<number> {
async insertTourPlace(input: {
trip_id: number;
name: string;
lat: number;
lng: number;
route_geometry: string;
description?: string | null;
website?: string | null;
}): Promise<number> {
return await this.insert({
trip: input.trip_id,
name: input.name,
@@ -1341,6 +1481,8 @@ export class PlacesRepository extends TrekRepository<Places> {
lng: input.lng,
transport_mode: 'walking',
route_geometry: input.route_geometry,
description: input.description ?? null,
website: input.website ?? null,
});
}
@@ -1354,7 +1496,14 @@ export class PlacesRepository extends TrekRepository<Places> {
async updateTourRoute(
id: number,
trip_id: number,
input: { name: string; lat: number; lng: number; route_geometry: string },
input: {
name: string;
lat: number;
lng: number;
route_geometry: string;
description?: string | null;
website?: string | null;
},
): Promise<boolean> {
const platform = this.getEntityManager().getPlatform();
const updated = await this.nativeUpdate(
@@ -1365,6 +1514,8 @@ export class PlacesRepository extends TrekRepository<Places> {
lng: input.lng,
transport_mode: 'walking',
route_geometry: input.route_geometry,
...(input.description !== undefined ? { description: input.description } : {}),
...(input.website !== undefined ? { website: input.website } : {}),
updated_at: currentTimestamp(platform),
},
);
+24 -1
View File
@@ -12,11 +12,15 @@ import { TrekRepository } from './_shared/trek-repository';
export interface TourListRow {
place_id: number;
name: string;
description?: string | null;
website?: string | null;
tour_type: string;
distance: number | null;
elevation_gain: number | null;
elevation_loss: number | null;
duration: number | null;
planned_duration_minutes: number | null;
break_additional_minutes: number | null;
difficulty: string | null;
wanderer_ref: string | null;
match_confidence: number | null;
@@ -33,6 +37,8 @@ export interface TourRow {
elevation_gain: number | null;
elevation_loss: number | null;
duration: number | null;
planned_duration_minutes: number | null;
break_additional_minutes: number | null;
difficulty: string | null;
wanderer_ref: string | null;
match_confidence: number | null;
@@ -49,12 +55,17 @@ export interface TourInsert {
elevation_gain: number | null;
elevation_loss: number | null;
duration: number | null;
planned_duration_minutes: number | null;
break_additional_minutes?: number | null;
match_confidence: number | null;
max_hiking_difficulty: number;
}
/** What a route edit replaces on an existing tour. */
export type TourUpdate = Omit<TourInsert, 'place_id'>;
export type TourUpdate = Omit<TourInsert, 'place_id' | 'planned_duration_minutes' | 'break_additional_minutes'> & {
planned_duration_minutes?: number | null;
break_additional_minutes?: number | null;
};
type ToursKyselyDB = Pick<DB, 'tours' | 'places' | 'day_assignments' | 'tour_waypoints'>;
@@ -81,11 +92,15 @@ export class ToursRepository extends TrekRepository<Tours> {
.select((eb) => [
'p.id as place_id',
'p.name as name',
'p.description as description',
'p.website as website',
't.tour_type as tour_type',
't.distance as distance',
't.elevation_gain as elevation_gain',
't.elevation_loss as elevation_loss',
't.duration as duration',
't.planned_duration_minutes as planned_duration_minutes',
't.break_additional_minutes as break_additional_minutes',
't.difficulty as difficulty',
't.wanderer_ref as wanderer_ref',
't.match_confidence as match_confidence',
@@ -177,6 +192,8 @@ export class ToursRepository extends TrekRepository<Tours> {
elevation_gain: input.elevation_gain,
elevation_loss: input.elevation_loss,
duration: input.duration,
planned_duration_minutes: input.planned_duration_minutes,
break_additional_minutes: input.break_additional_minutes ?? null,
match_confidence: input.match_confidence,
max_hiking_difficulty: input.max_hiking_difficulty,
});
@@ -207,6 +224,12 @@ export class ToursRepository extends TrekRepository<Tours> {
elevation_gain: input.elevation_gain,
elevation_loss: input.elevation_loss,
duration: input.duration,
...(input.planned_duration_minutes !== undefined
? { planned_duration_minutes: input.planned_duration_minutes }
: {}),
...(input.break_additional_minutes !== undefined
? { break_additional_minutes: input.break_additional_minutes }
: {}),
match_confidence: input.match_confidence,
max_hiking_difficulty: input.max_hiking_difficulty,
})
+66 -7
View File
@@ -13,12 +13,15 @@ import { PlacesService } from '../places/places.service';
import { computeTourMetrics, parseRouteGeometry, LOW_CONFIDENCE_THRESHOLD, type GeometryPoint } from './tours.helpers';
import { InjectRepository } from '@mikro-orm/nestjs';
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import type {
TourCreateRequest,
TourCreateResponse,
TourDetailResponse,
TourListItem,
TourWaypoint,
import {
MAX_PLANNED_TOUR_DURATION_MINUTES,
plannedTourDurationMinutesSchema,
tourWebsiteSchema,
type TourCreateRequest,
type TourCreateResponse,
type TourDetailResponse,
type TourListItem,
type TourWaypoint,
} from '@trek/shared';
export interface ImportGpxAsTourResult {
@@ -52,11 +55,15 @@ export class ToursService {
return {
place_id: r.place_id,
name: r.name,
description: r.description,
website: r.website,
tour_type: r.tour_type as TourListItem['tour_type'],
distance: r.distance,
elevation_gain: r.elevation_gain,
elevation_loss: r.elevation_loss,
duration: r.duration,
planned_duration_minutes: r.planned_duration_minutes,
break_additional_minutes: r.break_additional_minutes,
difficulty: r.difficulty,
wanderer_ref: r.wanderer_ref,
match_confidence: r.match_confidence,
@@ -76,6 +83,12 @@ export class ToursService {
elevation_gain: metrics.elevationGainM,
elevation_loss: metrics.elevationLossM,
duration: input.duration_seconds == null ? null : Math.round(input.duration_seconds / 60),
...(input.planned_duration_minutes !== undefined
? { planned_duration_minutes: input.planned_duration_minutes }
: {}),
...(input.break_additional_minutes !== undefined
? { break_additional_minutes: input.break_additional_minutes }
: {}),
match_confidence: 1,
max_hiking_difficulty: input.max_hiking_difficulty,
};
@@ -90,6 +103,33 @@ export class ToursService {
if (!(await this.tourTypesRepo.isEnabled(key))) throw new BadRequestException('Tour type is not available');
}
private validateInformationalMetadata(input: TourCreateRequest): void {
if (input.description != null && input.description.length > 2000) {
throw new BadRequestException('description must be 2000 characters or less');
}
if (input.website != null && !tourWebsiteSchema.safeParse(input.website).success) {
throw new BadRequestException('website must be a valid HTTPS URL without credentials');
}
}
private validatePlannedTimes(input: TourCreateRequest): void {
for (const value of [input.planned_duration_minutes, input.break_additional_minutes]) {
if (value !== undefined && !plannedTourDurationMinutesSchema.safeParse(value).success) {
throw new BadRequestException('planned Tour times must be whole minutes from 0 to 1440');
}
}
if (
input.planned_duration_minutes == null &&
input.duration_seconds != null &&
Math.round(input.duration_seconds / 60) + (input.break_additional_minutes ?? 0) >
MAX_PLANNED_TOUR_DURATION_MINUTES
) {
throw new BadRequestException(
'walking time plus breaks must be 1440 minutes or less unless a total is overridden',
);
}
}
/** All tours (the facet + owning place) for a trip, newest first. */
async listTours(tripId: string): Promise<TourListItem[]> {
const tid = toRowId(tripId);
@@ -131,6 +171,8 @@ export class ToursService {
// The controller's TripAccessGuard already resolved this trip id.
const tid = toRowId(tripId)!;
await this.assertTourTypeEnabled(input.tour_type);
this.validateInformationalMetadata(input);
this.validatePlannedTimes(input);
const start = input.route_geometry[0];
const fields = this.routeFields(input);
@@ -141,8 +183,15 @@ export class ToursService {
lat: start[0],
lng: start[1],
route_geometry: JSON.stringify(input.route_geometry),
description: input.description ?? null,
website: input.website ?? null,
});
await this.toursRepo.insertTour({
place_id: id,
...fields,
planned_duration_minutes: fields.planned_duration_minutes ?? null,
break_additional_minutes: fields.break_additional_minutes ?? null,
});
await this.toursRepo.insertTour({ place_id: id, ...fields });
await this.waypointsRepo.insertForPlace(id, input.waypoints);
return id;
});
@@ -174,6 +223,8 @@ export class ToursService {
const pid = toRowId(placeId);
if (tid === null || pid === null) throw new NotFoundException('Tour not found');
await this.assertTourTypeEnabled(input.tour_type);
this.validateInformationalMetadata(input);
this.validatePlannedTimes(input);
const start = input.route_geometry[0];
const fields = this.routeFields(input);
@@ -188,6 +239,8 @@ export class ToursService {
lat: start[0],
lng: start[1],
route_geometry: JSON.stringify(input.route_geometry),
...(input.description !== undefined ? { description: input.description } : {}),
...(input.website !== undefined ? { website: input.website } : {}),
});
if (!placeUpdated) throw new NotFoundException('Tour not found');
if (!(await this.toursRepo.updateInTrip(tid, pid, fields))) throw new NotFoundException('Tour not found');
@@ -253,6 +306,8 @@ export class ToursService {
elevation_gain: metrics.elevationGainM,
elevation_loss: metrics.elevationLossM,
duration: null,
planned_duration_minutes: null,
break_additional_minutes: null,
match_confidence: matchConfidence,
max_hiking_difficulty: 2,
});
@@ -260,11 +315,15 @@ export class ToursService {
this.toItem({
place_id: place.id,
name: place.name,
description: place.description ?? null,
website: place.website ?? null,
tour_type: 'hike',
distance: metrics.distanceKm,
elevation_gain: metrics.elevationGainM,
elevation_loss: metrics.elevationLossM,
duration: null,
planned_duration_minutes: null,
break_additional_minutes: null,
difficulty: null,
wanderer_ref: null,
match_confidence: matchConfidence,