feat(tours): save_tour_change saves a confirmed change to a city walk whole or not at all

An assistant can now save the change prepare_tour_change prepared, once the
traveller said yes to it exactly as shown. Until now a proposal could only
be looked at; the traveller had to redo it by hand in the planner.

- The save names the proposal: the same changes, the revision it was
  prepared on, the proposal_id the preparation answered and confirmed true.
  Anything else, a change altered on the way or no yes, is refused and
  nothing is saved.
- Place editing on the trip and the tour are checked again before anything
  is written. A tour changed since the proposal was prepared is a conflict
  on the tour's one revision (no second one), telling the assistant to
  prepare the change anew instead of writing over newer edits. The changes
  are laid on the saved tour once more inside the write, so a station
  visited or skipped meanwhile, or a place of the trip gone, refuses the
  save with every reason.
- The change goes through the write the planner's save uses, in one
  transaction: line, stations, keys, measures and visits together or not at
  all, and the trip is told only after the commit. Stations kept keep their
  keys and visits. Where the router gives no way on foot the walk keeps its
  stations only and waits for the planner, as a created one does.
- Every save is kept as a step of the tour (tour_change_steps): the
  proposal, the revision it was prepared on and the one it raised the tour
  to, the changes and the tour as it was before them, so the step can be
  followed and taken back later. Nothing of the conversation is kept.
- A new walk has no revision to name and stays with create_city_walk; both
  tool descriptions and the wiki say which is which.
This commit is contained in:
Maurice
2026-10-20 12:53:40 +02:00
parent 1993ed4d28
commit f4533fc69b
27 changed files with 1171 additions and 51 deletions
@@ -0,0 +1,40 @@
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
import { TourChangeStepsRepository } from '../repositories/TourChangeSteps.repository';
import { DbTimestampType } from '../types';
import { Tours } from './Tours.entity';
export class TourChangeSteps {
[EntityRepositoryType]?: TourChangeStepsRepository;
id!: number & Opt;
place!: Ref<Tours>;
place_id!: number;
proposal_id!: string;
base_revision!: number;
revision!: number;
changes!: string;
before_state!: string;
saved_by?: number | null;
saved_at!: string & Opt;
}
export const TourChangeStepsSchema = defineEntity({
class: TourChangeSteps,
repository: () => TourChangeStepsRepository,
uniques: [{ properties: ['place', 'revision'] }],
checks: [
{ name: 'tour_change_steps_base_revision_check', expression: 'base_revision >= 1' },
{ name: 'tour_change_steps_revision_check', expression: 'revision = base_revision + 1' },
],
properties: {
id: p.integer().primary(),
place: () => p.manyToOne(Tours).ref().name('place_id').deleteRule('cascade').hidden(),
place_id: p.integer().persist(false),
proposal_id: p.text(),
base_revision: p.integer(),
revision: p.integer(),
changes: p.text(),
before_state: p.text(),
saved_by: p.integer().nullable(),
saved_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
},
});
+3
View File
@@ -1,6 +1,7 @@
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 { TourChangeSteps } from './TourChangeSteps.entity';
import { TourObstacleReviews } from './TourObstacleReviews.entity';
import { TourSources } from './TourSources.entity';
import { TourTypes } from './TourTypes.entity';
@@ -45,6 +46,7 @@ export class Tours {
ski_discipline?: TToursSkiDiscipline | null;
ski_technique?: TToursSkiTechnique | null;
tourTypeRef!: Ref<TourTypes>;
tour_change_steps_collection = new Collection<TourChangeSteps>(this);
tour_obstacle_reviews_collection = new Collection<TourObstacleReviews>(this);
tour_sources: Ref<TourSources> | null = null;
tour_waypoints_collection = new Collection<TourWaypoints>(this);
@@ -175,6 +177,7 @@ export const ToursSchema = defineEntity({
ski_discipline: p.enum(() => ToursSkiDiscipline).nullable(),
ski_technique: p.enum(() => ToursSkiTechnique).nullable(),
tourTypeRef: () => p.manyToOne(TourTypes).ref().joinColumn('tour_type').hidden().index('idx_tours_tour_type'),
tour_change_steps_collection: () => p.oneToMany(TourChangeSteps).mappedBy('place').hidden(),
tour_obstacle_reviews_collection: () => p.oneToMany(TourObstacleReviews).mappedBy('place').hidden(),
tour_sources: () => p.oneToOne(TourSources).ref().mappedBy('place').hidden(),
tour_waypoints_collection: () => p.oneToMany(TourWaypoints).mappedBy('place').hidden(),
+2
View File
@@ -107,6 +107,7 @@ import { ShareTokensSchema } from './ShareTokens.entity';
import { TagsSchema } from './Tags.entity';
import { TodoCategoryAssigneesSchema } from './TodoCategoryAssignees.entity';
import { TodoItemsSchema } from './TodoItems.entity';
import { TourChangeStepsSchema } from './TourChangeSteps.entity';
import { TourObstacleReviewsSchema } from './TourObstacleReviews.entity';
import { TourSourcesSchema } from './TourSources.entity';
import { TourStationVisitsSchema } from './TourStationVisits.entity';
@@ -252,6 +253,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
TagsSchema,
TodoCategoryAssigneesSchema,
TodoItemsSchema,
TourChangeStepsSchema,
TourObstacleReviewsSchema,
TourSourcesSchema,
TourStationVisitsSchema,
+49
View File
@@ -8,16 +8,21 @@ import {
paddleLockSchema,
paddleRegionBoundarySchema,
paddleRegionGraphStatsSchema,
tourChangeSchema,
tourFixedLegSchema,
tourWaypointSchema,
trackMarksSchema,
type Ferrata,
type GeometryPoint,
type HikingPreferences,
type ObstacleSnapshot,
type PaddleGeometry,
type PaddleLock,
type PaddleRegionBoundary,
type PaddleRegionGraphStats,
type TourChange,
type TourFixedLeg,
type TourWaypoint,
type TrackMarks,
} from '@trek/shared';
@@ -218,3 +223,47 @@ export const TOUR_OBSTACLE_SNAPSHOT: JsonColumn<ObstacleSnapshot | null> = {
schema: obstacleSnapshotSchema.nullable(),
fallback: () => null,
};
/**
* MCP-T03: the changes a saved step confirmed, as they were prepared. A broken
* row reads as none, so a step never claims changes it cannot show.
*/
export const TOUR_CHANGE_STEP_CHANGES: JsonColumn<TourChange[]> = {
column: 'tour_change_steps.changes',
schema: z.array(tourChangeSchema),
fallback: () => [],
};
/** A point of a saved line: latitude, longitude and the height where it has one. */
const savedLinePoint = z.union([z.tuple([z.number(), z.number()]), z.tuple([z.number(), z.number(), z.number()])]);
/** A tour as it was before a saved step (MCP-T03), as its JSON column holds it. */
export interface TourChangeStepBefore {
route_geometry: GeometryPoint[] | null;
waypoints: TourWaypoint[];
distance: number | null;
duration: number | null;
elevation_gain: number | null;
elevation_loss: number | null;
}
/**
* MCP-T03: a tour as it was before a saved step changed it: its line (null
* while it had none), its points with their keys, and the measures the save
* replaced, null where they were not known. A broken row reads as no earlier
* state, so nothing is ever restored from a guess.
*/
export const TOUR_CHANGE_STEP_BEFORE: JsonColumn<TourChangeStepBefore | null> = {
column: 'tour_change_steps.before_state',
schema: z
.object({
route_geometry: z.array(savedLinePoint).nullable(),
waypoints: z.array(tourWaypointSchema),
distance: z.number().nullable(),
duration: z.number().nullable(),
elevation_gain: z.number().nullable(),
elevation_loss: z.number().nullable(),
})
.nullable(),
fallback: () => null,
};
@@ -0,0 +1,15 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
import type { InsertOptional } from './columns';
export interface TourChangeStepsTable {
id: InsertOptional<number>;
place_id: number;
proposal_id: string;
base_revision: number;
revision: number;
changes: string;
before_state: string;
saved_by: InsertOptional<number | null>;
saved_at: InsertOptional<string>;
}
+2
View File
@@ -113,6 +113,7 @@ import type { ShareTokensTable } from './ShareTokens.table';
import type { TagsTable } from './Tags.table';
import type { TodoCategoryAssigneesTable } from './TodoCategoryAssignees.table';
import type { TodoItemsTable } from './TodoItems.table';
import type { TourChangeStepsTable } from './TourChangeSteps.table';
import type { TourObstacleReviewsTable } from './TourObstacleReviews.table';
import type { TourSourcesTable } from './TourSources.table';
import type { TourStationVisitsTable } from './TourStationVisits.table';
@@ -264,6 +265,7 @@ export interface DB {
tags: TagsTable;
todo_category_assignees: TodoCategoryAssigneesTable;
todo_items: TodoItemsTable;
tour_change_steps: TourChangeStepsTable;
tour_obstacle_reviews: TourObstacleReviewsTable;
tour_sources: TourSourcesTable;
tour_station_visits: TourStationVisitsTable;
@@ -0,0 +1,34 @@
import { Migration } from '@mikro-orm/migrations';
/**
* MCP-T03: a change to a saved tour the traveller confirmed through an
* assistant is kept as one step of the tour, so it can be followed and taken
* back later (MCP-T04). A step names the proposal it saved (`proposal_id`),
* the revision the tour was at when it was prepared (`base_revision`) and the
* one the save raised it to (`revision`, always the next), the changes as
* confirmed (JSON) and the tour as it was before them (`before_state`, JSON:
* its line, its points and the measures the save replaced). Nothing of the
* conversation is kept, only the plan. `saved_by` names who confirmed it,
* kept as a plain id: a step outlives its author, who then reads as unknown.
* One step per revision of a tour, and the steps go with their tour.
*/
export class Migration20200101045700_a_tour_records_the_changes_saved_from_a_proposal extends Migration {
override name = 'Migration20200101045700_a_tour_records_the_changes_saved_from_a_proposal';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS tour_change_steps (
id INTEGER PRIMARY KEY,
place_id INTEGER NOT NULL REFERENCES tours(place_id) ON DELETE CASCADE,
proposal_id TEXT NOT NULL,
base_revision INTEGER NOT NULL CHECK (base_revision >= 1),
revision INTEGER NOT NULL CHECK (revision = base_revision + 1),
changes TEXT NOT NULL,
before_state TEXT NOT NULL,
saved_by INTEGER,
saved_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (place_id, revision)
)
`);
}
}
@@ -0,0 +1,38 @@
import type { TourChangeSteps } from '../entities/TourChangeSteps.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
type StepsDB = Pick<DB, 'tour_change_steps'>;
/** What a save of a confirmed change records; `changes` and `before_state` are their JSON text. */
export interface TourChangeStepWrite {
place_id: number;
proposal_id: string;
base_revision: number;
revision: number;
changes: string;
before_state: string;
saved_by: number | null;
}
/**
* `tour_change_steps` (MCP-T03): the changes to a tour saved from a proposal
* the traveller confirmed, one step per revision they raised the tour to,
* each with the tour as it was before, so a step can be followed and taken
* back (MCP-T04). A step goes with its tour.
*/
export class TourChangeStepsRepository extends TrekRepository<TourChangeSteps> {
private db() {
return this.kysely<StepsDB>();
}
/** Records one saved step and answers its id; the caller holds the transaction of the save. */
async record(step: TourChangeStepWrite): Promise<number> {
const inserted = await this.db()
.insertInto('tour_change_steps')
.values(step)
.returning('id')
.executeTakeFirstOrThrow();
return inserted.id;
}
}