mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(tours): via ferratas on the tour as fixed legs of their own line, crossing warnings, violet ferrata look, tour list and editor polish
This commit is contained in:
@@ -11,6 +11,7 @@ export class TourWaypoints {
|
||||
lng!: number;
|
||||
role!: string;
|
||||
sequence!: number;
|
||||
fixed_leg?: string | null;
|
||||
}
|
||||
|
||||
export const TourWaypointsSchema = defineEntity({
|
||||
@@ -25,5 +26,6 @@ export const TourWaypointsSchema = defineEntity({
|
||||
lng: p.double(),
|
||||
role: p.text(),
|
||||
sequence: p.integer(),
|
||||
fixed_leg: p.text().nullable(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { JsonColumn } from '../utils/json-column';
|
||||
import { FERRATAS_RESULT_CAP, ferrataSchema, type Ferrata } from '@trek/shared';
|
||||
import { FERRATAS_RESULT_CAP, ferrataSchema, tourFixedLegSchema, type Ferrata, type TourFixedLeg } from '@trek/shared';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -124,3 +124,14 @@ export const FERRATA_TILE_PAYLOAD: JsonColumn<FerrataTilePayload | null> = {
|
||||
schema: z.object({ ferratas: z.array(ferrataSchema).max(FERRATAS_RESULT_CAP), truncated: z.boolean() }).nullable(),
|
||||
fallback: () => null,
|
||||
};
|
||||
|
||||
/**
|
||||
* A tour leg taken as a fixed line (a via ferrata's own line from OSM) rather
|
||||
* than routed. A row that does not decode reads as a routed leg, which the
|
||||
* editor then plans again.
|
||||
*/
|
||||
export const TOUR_WAYPOINT_FIXED_LEG: JsonColumn<TourFixedLeg | null> = {
|
||||
column: 'tour_waypoints.fixed_leg',
|
||||
schema: tourFixedLegSchema.nullable(),
|
||||
fallback: () => null,
|
||||
};
|
||||
|
||||
@@ -9,4 +9,5 @@ export interface TourWaypointsTable {
|
||||
lng: number;
|
||||
role: string;
|
||||
sequence: number;
|
||||
fixed_leg: InsertOptional<string | null>;
|
||||
}
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* `tour_waypoints.fixed_leg` holds the leg from a waypoint to the next one
|
||||
* when the tour takes it as a fixed line rather than as a routed one: a via
|
||||
* ferrata put onto the tour from the map, whose own line from OpenStreetMap
|
||||
* the tour follows, since a router walks around a ferrata wherever it can.
|
||||
*
|
||||
* The column is JSON text (kind, the OSM reference, the name and the line) and
|
||||
* NULL for every routed leg, so every existing waypoint keeps its meaning.
|
||||
*/
|
||||
export class Migration20200101043300_a_tour_leg_can_follow_a_via_ferrata extends Migration {
|
||||
override name = 'Migration20200101043300_a_tour_leg_can_follow_a_via_ferrata';
|
||||
|
||||
override up(): void {
|
||||
this.addSql('ALTER TABLE tour_waypoints ADD COLUMN fixed_leg TEXT');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
import { decodeJson, encodeJson } from '../../utils/json-column';
|
||||
import type { TourWaypoints } from '../entities/TourWaypoints.entity';
|
||||
import { TOUR_WAYPOINT_FIXED_LEG } from '../json-columns';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
import type { TourFixedLeg } from '@trek/shared';
|
||||
|
||||
/** One saved routing control point of a tour, in route order. */
|
||||
export interface TourWaypointRow {
|
||||
@@ -8,6 +11,23 @@ export interface TourWaypointRow {
|
||||
lng: number;
|
||||
role: 'start' | 'via' | 'end';
|
||||
sequence: number;
|
||||
/** The leg to the next waypoint as a fixed line; absent for a routed leg. */
|
||||
fixed_leg?: TourFixedLeg | null;
|
||||
}
|
||||
|
||||
/** The row as stored, the fixed leg still JSON text. */
|
||||
interface StoredWaypoint {
|
||||
lat: number;
|
||||
lng: number;
|
||||
role: TourWaypointRow['role'];
|
||||
sequence: number;
|
||||
fixed_leg: string | null;
|
||||
}
|
||||
|
||||
/** A stored row as callers read it: the fixed leg decoded, and left out entirely for a routed leg. */
|
||||
function readWaypoint({ fixed_leg, ...point }: StoredWaypoint): TourWaypointRow {
|
||||
const leg = decodeJson(TOUR_WAYPOINT_FIXED_LEG, fixed_leg, 'tour waypoint');
|
||||
return leg ? { ...point, fixed_leg: leg } : point;
|
||||
}
|
||||
|
||||
/** {@link TourWaypointRow} plus the tour it belongs to, as the trip copy reads it. */
|
||||
@@ -30,37 +50,46 @@ export class TourWaypointsRepository extends TrekRepository<TourWaypoints> {
|
||||
return this.kysely<TourWaypointsKyselyDB>();
|
||||
}
|
||||
|
||||
/** TW1 — `SELECT lat, lng, role, sequence FROM tour_waypoints WHERE place_id = ? ORDER BY sequence`. */
|
||||
/** TW1 — `SELECT lat, lng, role, sequence, fixed_leg FROM tour_waypoints WHERE place_id = ? ORDER BY sequence`. */
|
||||
async listForPlace(place_id: number): Promise<TourWaypointRow[]> {
|
||||
return await this.db()
|
||||
const rows = await this.db()
|
||||
.selectFrom('tour_waypoints')
|
||||
.select(['lat', 'lng', 'role', 'sequence'])
|
||||
.select(['lat', 'lng', 'role', 'sequence', 'fixed_leg'])
|
||||
.where('place_id', '=', place_id)
|
||||
.orderBy('sequence', 'asc')
|
||||
.execute();
|
||||
return rows.map(readWaypoint);
|
||||
}
|
||||
|
||||
/** TW2 — every waypoint of a trip's tours, for the trip copy to remap by place. */
|
||||
async listForTrip(trip_id: number): Promise<TourWaypointWithPlaceRow[]> {
|
||||
return await this.db()
|
||||
const rows = await this.db()
|
||||
.selectFrom('tour_waypoints as tw')
|
||||
.innerJoin('places as p', 'p.id', 'tw.place_id')
|
||||
.select(['tw.place_id', 'tw.lat', 'tw.lng', 'tw.role', 'tw.sequence'])
|
||||
.select(['tw.place_id', 'tw.lat', 'tw.lng', 'tw.role', 'tw.sequence', 'tw.fixed_leg'])
|
||||
.where('p.trip_id', '=', trip_id)
|
||||
.orderBy('tw.place_id', 'asc')
|
||||
.orderBy('tw.sequence', 'asc')
|
||||
.execute();
|
||||
return rows.map(({ place_id, ...point }) => ({ place_id, ...readWaypoint(point) }));
|
||||
}
|
||||
|
||||
/**
|
||||
* TW3 — `INSERT INTO tour_waypoints (place_id, lat, lng, role, sequence)
|
||||
* TW3 — `INSERT INTO tour_waypoints (place_id, lat, lng, role, sequence, fixed_leg)
|
||||
* VALUES (...)` for each point. The caller owns the transaction: a create
|
||||
* writes the tour first, an edit clears the old points first.
|
||||
*/
|
||||
async insertForPlace(place_id: number, waypoints: readonly TourWaypointRow[]): Promise<void> {
|
||||
if (waypoints.length === 0) return;
|
||||
await this.insertMany(
|
||||
waypoints.map((w) => ({ place: place_id, lat: w.lat, lng: w.lng, role: w.role, sequence: w.sequence })),
|
||||
waypoints.map((w) => ({
|
||||
place: place_id,
|
||||
lat: w.lat,
|
||||
lng: w.lng,
|
||||
role: w.role,
|
||||
sequence: w.sequence,
|
||||
fixed_leg: w.fixed_leg ? encodeJson(TOUR_WAYPOINT_FIXED_LEG, w.fixed_leg) : null,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user