Files
TREK/server/src/db/entities/Tours.entity.ts
T
Maurice d72352911e feat(tours): tour revisions, conflicts and a live editor (P2C-03)
A saved tour now has a technical revision, raised inside the transaction of
every write that changes it: the route and its controls, the plan laid onto a
recorded paddle track, going back to that track. A write that names the
revision it was based on (expected_revision) is refused with a 409
{ code: 'tour_revision_conflict', revision } before anything of it is
written, so a stale draft never overwrites a newer tour unseen. A write that
names none writes as before, so older clients and queued offline writes keep
working; the queue never carries a base. A copy starts at 1, a source's
version stays provenance. Migration 044800 adds the column; list_tours and
get_tour_plan read it.

The editor follows the trip's tours live: a newer revision of the open tour
loads at once while the editor has no changes of its own, and shows a notice
while it has. A refused save keeps the draft and asks: save it as a new tour,
load the newer version, or overwrite on purpose.

useTourPlanner was at the size limit; its derived facts, undo history and
draft storage move into tourPlannerFacts, usePlannerHistory and
useTourDraftStorage, unchanged in behaviour.
2026-10-19 10:22:04 +02:00

119 lines
4.5 KiB
TypeScript

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 { TourObstacleReviews } from './TourObstacleReviews.entity';
import { TourSources } from './TourSources.entity';
import { TourTypes } from './TourTypes.entity';
import { TourWaypoints } from './TourWaypoints.entity';
export class Tours {
[EntityRepositoryType]?: ToursRepository;
[PrimaryKeyProp]?: 'place';
place?: Ref<Places> | null;
place_id?: number | null;
tour_type!: string;
distance?: number | null;
elevation_gain?: number | null;
elevation_loss?: number | null;
duration?: number | null;
difficulty?: string | null;
wanderer_ref?: string | null;
match_confidence?: number | null;
created_at?: string | null;
max_hiking_difficulty?: number | null = 2;
planned_duration_minutes?: number | null;
break_additional_minutes?: number | null;
derivedFromPlace?: Ref<Places> | null;
derived_from_place_id?: number | null;
recorded_route_geometry?: string | null;
origin: TToursOrigin & Opt = ToursOrigin.TREK;
origin_file_name?: string | null;
boat_type?: TToursBoatType | null;
water_context?: TToursWaterContext | null;
paddle_speed_kmh?: number | null;
portage_speed_kmh?: number | null;
track_marks?: string | null;
paddle_lake_style?: TToursPaddleLakeStyle | null;
revision: number & Opt = 1;
tourTypeRef!: Ref<TourTypes>;
tour_obstacle_reviews_collection = new Collection<TourObstacleReviews>(this);
tour_sources: Ref<TourSources> | null = null;
tour_waypoints_collection = new Collection<TourWaypoints>(this);
}
export const ToursOrigin = {
TREK: 'trek',
GPX: 'gpx',
WANDERER: 'wanderer',
} as const;
export type TToursOrigin = (typeof ToursOrigin)[keyof typeof ToursOrigin];
export const ToursBoatType = {
KAYAK: 'kayak',
CANOE: 'canoe',
SUP: 'sup',
OTHER: 'other',
} as const;
export type TToursBoatType = (typeof ToursBoatType)[keyof typeof ToursBoatType];
export const ToursWaterContext = {
RIVER: 'river',
LAKE: 'lake',
} as const;
export type TToursWaterContext = (typeof ToursWaterContext)[keyof typeof ToursWaterContext];
export const ToursPaddleLakeStyle = {
DIRECT: 'direct',
SHORE: 'shore',
} as const;
export type TToursPaddleLakeStyle = (typeof ToursPaddleLakeStyle)[keyof typeof ToursPaddleLakeStyle];
export const ToursSchema = defineEntity({
class: Tours,
repository: () => ToursRepository,
indexes: [
{
name: 'idx_tours_derived_from_place_id',
where: 'derived_from_place_id IS NOT NULL',
properties: ['derived_from_place_id'],
},
],
checks: [{ name: 'tours_revision_check', expression: 'revision >= 1' }],
properties: {
place: () => p.oneToOne(Places).primary().ref().deleteRule('cascade').nullable().hidden(),
place_id: p.integer().nullable().persist(false),
tour_type: p.text().persist(false).index('idx_tours_tour_type'),
distance: p.double().nullable(),
elevation_gain: p.double().nullable(),
elevation_loss: p.double().nullable(),
duration: p.double().nullable(),
difficulty: p.text().nullable(),
wanderer_ref: p.text().nullable(),
match_confidence: p.double().nullable(),
created_at: p.text().nullable().defaultRaw(`CURRENT_TIMESTAMP`),
max_hiking_difficulty: p.integer().nullable(),
planned_duration_minutes: p.integer().nullable(),
break_additional_minutes: p.integer().nullable(),
derivedFromPlace: () => p.manyToOne(Places).ref().nullable().hidden(),
derived_from_place_id: p.integer().nullable().persist(false),
recorded_route_geometry: p.text().nullable(),
origin: p.enum(() => ToursOrigin).default('trek'),
origin_file_name: p.text().nullable(),
boat_type: p.enum(() => ToursBoatType).nullable(),
water_context: p.enum(() => ToursWaterContext).nullable(),
paddle_speed_kmh: p.double().nullable(),
portage_speed_kmh: p.double().nullable(),
track_marks: p.text().nullable(),
paddle_lake_style: p.enum(() => ToursPaddleLakeStyle).nullable(),
revision: p.integer().default(1),
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(),
tour_waypoints_collection: () => p.oneToMany(TourWaypoints).mappedBy('place').hidden(),
},
});