mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
Tours: provenance contract, source check, one assignment path and the map overview
- P2C-02: tours record their origin (planned, GPX with file name, wanderer) instead of reading it off their shape; the detail answers a provenance contract and a trip copy keeps a tour's import record - MF-03: a wanderer snapshot's source can be checked on purpose, read only, with a confirmed reimport as a new snapshot - PR1-18: every way of putting a tour on a day shares one set of rules - PR1-07/08: each person can hide tours from the trip map; the list shows each tour's line colour and the picked tour stands out on both maps
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Collection, EntityRepositoryType, PrimaryKeyProp, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
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 { TourSources } from './TourSources.entity';
|
||||
@@ -25,11 +25,21 @@ export class Tours {
|
||||
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;
|
||||
tourTypeRef!: Ref<TourTypes>;
|
||||
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 ToursSchema = defineEntity({
|
||||
class: Tours,
|
||||
repository: () => ToursRepository,
|
||||
@@ -58,6 +68,8 @@ export const ToursSchema = defineEntity({
|
||||
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(),
|
||||
tourTypeRef: () => p.manyToOne(TourTypes).ref().joinColumn('tour_type').hidden().index('idx_tours_tour_type'),
|
||||
tour_sources: () => p.oneToOne(TourSources).ref().mappedBy('place').hidden(),
|
||||
tour_waypoints_collection: () => p.oneToMany(TourWaypoints).mappedBy('place').hidden(),
|
||||
|
||||
@@ -18,4 +18,6 @@ export interface ToursTable {
|
||||
break_additional_minutes: InsertOptional<number | null>;
|
||||
derived_from_place_id: InsertOptional<number | null>;
|
||||
recorded_route_geometry: InsertOptional<string | null>;
|
||||
origin: InsertOptional<string>;
|
||||
origin_file_name: InsertOptional<string | null>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* Where a tour came from, recorded instead of read off its shape: planned in
|
||||
* TREK, imported from a GPX file (with the file's name) or copied in from
|
||||
* wanderer. Until now a list told a GPX import from a planned tour by whether it
|
||||
* had waypoints, which an import planned over in place no longer holds.
|
||||
*
|
||||
* The backfill is exact for every tour that can exist before this step: a
|
||||
* wanderer reference only comes from the wanderer import, and a tour without
|
||||
* stored waypoints, or one that keeps a recorded track, only from an import.
|
||||
* A GPX import's file name was never stored, so it stays unknown (NULL).
|
||||
*/
|
||||
export class Migration20200101043800_a_tour_records_where_it_came_from extends Migration {
|
||||
override name = 'Migration20200101043800_a_tour_records_where_it_came_from';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(
|
||||
"ALTER TABLE tours ADD COLUMN origin TEXT NOT NULL DEFAULT 'trek' CHECK (origin IN ('trek', 'gpx', 'wanderer'))",
|
||||
);
|
||||
this.addSql('ALTER TABLE tours ADD COLUMN origin_file_name TEXT');
|
||||
this.addSql("UPDATE tours SET origin = 'wanderer' WHERE wanderer_ref IS NOT NULL");
|
||||
this.addSql(
|
||||
"UPDATE tours SET origin = 'gpx' WHERE origin = 'trek' AND (recorded_route_geometry IS NOT NULL OR NOT EXISTS (SELECT 1 FROM tour_waypoints w WHERE w.place_id = tours.place_id))",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Tours } from '../entities/Tours.entity';
|
||||
import { type Tours, type TToursOrigin, ToursOrigin } from '../entities/Tours.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { AssertRowKeys } from './_shared/rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
@@ -26,6 +26,7 @@ export interface TourListRow {
|
||||
match_confidence: number | null;
|
||||
max_hiking_difficulty: number | null;
|
||||
derived_from_place_id: number | null;
|
||||
origin: string;
|
||||
has_recorded_track: number;
|
||||
planned: number;
|
||||
has_waypoints: number;
|
||||
@@ -48,6 +49,8 @@ export interface TourRow {
|
||||
max_hiking_difficulty: number | null;
|
||||
derived_from_place_id: number | null;
|
||||
recorded_route_geometry: string | null;
|
||||
origin: string;
|
||||
origin_file_name: string | null;
|
||||
}
|
||||
const _tourRowKeys: AssertRowKeys<TourRow, Tours> = true;
|
||||
|
||||
@@ -71,7 +74,32 @@ export type TourUpdate = Omit<TourInsert, 'place_id' | 'planned_duration_minutes
|
||||
break_additional_minutes?: number | null;
|
||||
};
|
||||
|
||||
type ToursKyselyDB = Pick<DB, 'tours' | 'places' | 'day_assignments' | 'tour_waypoints'>;
|
||||
type ToursKyselyDB = Pick<DB, 'tours' | 'places' | 'day_assignments' | 'tour_waypoints' | 'tour_sources'>;
|
||||
|
||||
/**
|
||||
* Where one tour came from, as the provenance contract reads it: the facet's
|
||||
* own record (origin, the GPX file name, when it was saved, the wanderer
|
||||
* reference), the import's snapshot row when there is one, and the name of the
|
||||
* import a planned tour came from while that import is still in the trip.
|
||||
*/
|
||||
export interface TourProvenanceRow {
|
||||
origin: string;
|
||||
origin_file_name: string | null;
|
||||
created_at: string | null;
|
||||
wanderer_ref: string | null;
|
||||
has_recorded_track: number;
|
||||
derived_from_place_id: number | null;
|
||||
derived_from_name: string | null;
|
||||
source_instance: string | null;
|
||||
source_external_id: string | null;
|
||||
source_external_url: string | null;
|
||||
source_external_updated: string | null;
|
||||
source_imported_at: string | null;
|
||||
source_content_hash: string | null;
|
||||
source_local_hash: string | null;
|
||||
route_geometry: string | null;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* `tours` — the facet that makes a place a tour (#2586). Keyed on the owning
|
||||
@@ -110,6 +138,7 @@ export class ToursRepository extends TrekRepository<Tours> {
|
||||
't.match_confidence as match_confidence',
|
||||
't.max_hiking_difficulty as max_hiking_difficulty',
|
||||
't.derived_from_place_id as derived_from_place_id',
|
||||
't.origin as origin',
|
||||
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'))
|
||||
@@ -141,6 +170,79 @@ export class ToursRepository extends TrekRepository<Tours> {
|
||||
return row ? toListRow(row) : undefined;
|
||||
}
|
||||
|
||||
/** Where a tour of this trip came from; `undefined` unless the place is a tour of that trip. */
|
||||
async findProvenance(trip_id: number, place_id: number): Promise<TourProvenanceRow | undefined> {
|
||||
return await this.db()
|
||||
.selectFrom('tours as t')
|
||||
.innerJoin('places as p', 'p.id', 't.place_id')
|
||||
.leftJoin('tour_sources as s', 's.place_id', 't.place_id')
|
||||
.leftJoin('places as d', (join) =>
|
||||
join.onRef('d.id', '=', 't.derived_from_place_id').on('d.trip_id', '=', trip_id),
|
||||
)
|
||||
.select((eb) => [
|
||||
't.origin as origin',
|
||||
't.origin_file_name as origin_file_name',
|
||||
't.created_at as created_at',
|
||||
't.wanderer_ref as wanderer_ref',
|
||||
eb('t.recorded_route_geometry', 'is not', null).as('has_recorded_track'),
|
||||
't.derived_from_place_id as derived_from_place_id',
|
||||
'd.name as derived_from_name',
|
||||
's.instance as source_instance',
|
||||
's.external_id as source_external_id',
|
||||
's.external_url as source_external_url',
|
||||
's.external_updated as source_external_updated',
|
||||
's.imported_at as source_imported_at',
|
||||
's.content_hash as source_content_hash',
|
||||
's.local_hash as source_local_hash',
|
||||
'p.route_geometry as route_geometry',
|
||||
'p.name as name',
|
||||
])
|
||||
.where('p.trip_id', '=', trip_id)
|
||||
.where('p.id', '=', place_id)
|
||||
.executeTakeFirst()
|
||||
.then((row) => (row ? { ...row, has_recorded_track: Number(row.has_recorded_track) } : undefined));
|
||||
}
|
||||
|
||||
/**
|
||||
* A copied tour keeps the import record of the tour it was copied from, so a
|
||||
* trip copy still says where its imports came from and when. Nothing happens
|
||||
* for a tour without one.
|
||||
*/
|
||||
async copySource(source_place_id: number, place_id: number): Promise<void> {
|
||||
await this.db()
|
||||
.insertInto('tour_sources')
|
||||
.columns([
|
||||
'place_id',
|
||||
'provider',
|
||||
'instance',
|
||||
'external_id',
|
||||
'external_url',
|
||||
'external_updated',
|
||||
'content_hash',
|
||||
'local_hash',
|
||||
'imported_by',
|
||||
'imported_at',
|
||||
])
|
||||
.expression((eb) =>
|
||||
eb
|
||||
.selectFrom('tour_sources')
|
||||
.select((inner) => [
|
||||
inner.val(place_id).as('place_id'),
|
||||
'provider',
|
||||
'instance',
|
||||
'external_id',
|
||||
'external_url',
|
||||
'external_updated',
|
||||
'content_hash',
|
||||
'local_hash',
|
||||
'imported_by',
|
||||
'imported_at',
|
||||
])
|
||||
.where('place_id', '=', source_place_id),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** TO3 — `SELECT 1 FROM tours WHERE place_id = ?`: whether the place is a tour. */
|
||||
async existsForPlace(place_id: number): Promise<boolean> {
|
||||
const row = await this.db()
|
||||
@@ -193,15 +295,21 @@ export class ToursRepository extends TrekRepository<Tours> {
|
||||
/**
|
||||
* `wanderer_ref` and `difficulty` are set only by an import from a source that
|
||||
* carries them, `derived_from_place_id` only by a tour planned from an import.
|
||||
* `origin` says which way the tour came in (planned here unless an import
|
||||
* says otherwise), `origin_file_name` the GPX file it was read from.
|
||||
*/
|
||||
async insertTour(
|
||||
input: TourInsert & {
|
||||
wanderer_ref?: string | null;
|
||||
difficulty?: string | null;
|
||||
derived_from_place_id?: number | null;
|
||||
origin?: TToursOrigin;
|
||||
origin_file_name?: string | null;
|
||||
},
|
||||
): Promise<void> {
|
||||
await this.insert({
|
||||
origin: input.origin ?? ToursOrigin.TREK,
|
||||
origin_file_name: input.origin_file_name ?? null,
|
||||
derivedFromPlace: input.derived_from_place_id ?? null,
|
||||
wanderer_ref: input.wanderer_ref ?? null,
|
||||
difficulty: input.difficulty ?? null,
|
||||
|
||||
Reference in New Issue
Block a user