Files
TREK/server/tests/helpers/factories/tours.ts
T
Maurice 2e1dca9fe9 feat(tours): let a background check hint at changes of your own wanderer trails
Settings, Integrations, wanderer gains an optional background check of the
trails you imported, off until you switch it on. While on, TREK reads the
short record of each such trail at most once a day (never its GPX file) and
leaves a hint when the trail may have moved on: "may have changed" when only
wanderer's change time moved, "changed" when its name or description proves
it, and "not checkable" when the trail is gone, no longer yours or carries no
change time. Nothing is ever said to be unchanged. A new hint arrives as a
notification that opens the tour (a new ?place= link in the planner); the
tour's provenance shows it with the source check right beside it, and the
card lists all open hints in a dialog, each leading to its tour. The check
reads the source in full as before and spends the hint; taking a change in
stays the confirmed reimport. No tour is ever changed or deleted.

The check only asks through your own connection, only about trails your
account imported into trips you can still open, and stops when you switch it
off, remove the connection, wanderer refuses the token (until a test passes
or a new token is saved) or the admin switches wanderer off; hints and
snapshots stay. A run asks at most 25 connections for at most 10 records
each, one after another, and an instance that fails (an outage, a rate
limit) is left alone for an hour, doubling up to two days.

Server: migration 046120 adds the switch and the run bookkeeping to
wanderer_connections and the tour_source_hints table (one row per snapshot,
for its importer, gone with it). WandererWatchService and an hourly
wanderer-watch job on the CronRegistrar live in the tours domain and read
through WandererService; GET and PUT /api/integrations/wanderer/watch and GET
/api/trips/:tripId/tours/wanderer/sources/:placeId/hint (place_edit, like the
check) answer with @trek/shared contracts. A new user notification event,
wanderer_source_hint, can be muted per channel. Saving a new token now clears
what the last check said about the old one. There is no MCP tool, as for the
rest of the personal wanderer connection. All 27 locales carry the strings,
and the admin wiki page says what the check does.
2026-10-20 15:35:32 +02:00

127 lines
4.5 KiB
TypeScript

import { TourActivities } from '../../../src/db/entities/TourActivities.entity';
import { TourPackingLinks } from '../../../src/db/entities/TourPackingLinks.entity';
import { TourSources } from '../../../src/db/entities/TourSources.entity';
import { TourStationVisits, type TTourStationVisitsStatus } from '../../../src/db/entities/TourStationVisits.entity';
import { TourWaypoints, type TTourWaypointsImportance } from '../../../src/db/entities/TourWaypoints.entity';
import { Tours } from '../../../src/db/entities/Tours.entity';
import type { FactoryOrm } from './context';
import { createRow, findRow, insertRow, insertRows } from './rows';
import type { EntityData, EntityDTO } from '@mikro-orm/core';
export type TourRow = EntityDTO<Tours>;
/**
* Turns the place into a tour. A tour is keyed by its place, so it has no id
* of its own; its type defaults to 'hike', the one the tours migration seeds.
*/
export async function makeTour(orm: FactoryOrm, placeId: number, overrides: EntityData<Tours> = {}): Promise<TourRow> {
await insertRow(orm, Tours, { place: placeId, tourTypeRef: 'hike' }, overrides);
const tour = await findRow(orm, Tours, { place: placeId });
if (!tour) throw new Error(`makeTour: no tour for place ${placeId} after its insert`);
return tour;
}
type WaypointRole = 'start' | 'via' | 'end';
/** A city walk station's columns (CW-01 to CW-04), a shaping point's and a stage end's flags and the place a point stands for. */
interface StationColumns {
station_key?: string | null;
name?: string | null;
stay_minutes?: number | null;
importance?: TTourWaypointsImportance | null;
/** 1 for a protected station. */
protected?: number;
opening_hours?: string | null;
/** 1 for a shaping point. */
shaping?: number;
/** MDT-01: 1 for a stop that ends a stage of a multi-day tour. */
stage_end?: number;
/** The id of the place of the trip the point stands for. */
linkedPlace?: number | null;
/** The leg to the next point as a fixed line, as its JSON column stores it. */
fixed_leg?: string | null;
}
/**
* The tour's waypoints, in the order given and numbered from 0. Without a
* role the first is the start, the last the end and the rest are vias. A
* point may carry a station's columns; those it leaves out take their defaults.
*/
export async function addTourWaypoints(
orm: FactoryOrm,
placeId: number,
points: Array<{ lat: number; lng: number; role?: WaypointRole } & StationColumns>,
): Promise<void> {
const roleAt = (i: number): WaypointRole => (i === 0 ? 'start' : i === points.length - 1 ? 'end' : 'via');
await insertRows(
orm,
TourWaypoints,
points.map(({ lat, lng, role, ...station }, sequence) => ({
...station,
place: placeId,
lat,
lng,
role: role ?? roleAt(sequence),
sequence,
})),
);
}
/** CW-03: a station of a city walk visited or skipped on the day the assignment puts the walk on. */
export async function markStationVisit(
orm: FactoryOrm,
assignmentId: number,
stationKey: string,
status: TTourStationVisitsStatus,
): Promise<void> {
await insertRow(orm, TourStationVisits, { assignment: assignmentId, station_key: stationKey, status });
}
type TourActivityRow = EntityDTO<TourActivities>;
/** ACT-01: the recording `trackPlaceId` assigned to the tour, on no day unless `assignment` names one. */
export function makeTourActivity(
orm: FactoryOrm,
placeId: number,
trackPlaceId: number,
overrides: EntityData<TourActivities> = {},
): Promise<TourActivityRow> {
return createRow(orm, TourActivities, { place: placeId, trackPlace: trackPlaceId }, overrides);
}
/** P2D-XD-01: points the tour at its trip's packing list (category null) or at one category of it. */
export async function linkTourPacking(
orm: FactoryOrm,
placeId: number,
category: string | null,
linkedBy: number | null = null,
): Promise<void> {
await insertRow(orm, TourPackingLinks, { place: placeId, category, linkedByRef: linkedBy });
}
/**
* RS-05: the import record of a tour copied in from wanderer, as the import
* writes it; the content and local fingerprints default to placeholders.
*/
export async function makeTourSource(
orm: FactoryOrm,
placeId: number,
importedBy: number | null,
overrides: EntityData<TourSources> = {},
): Promise<void> {
await insertRow(
orm,
TourSources,
{
place: placeId,
provider: 'wanderer',
instance: 'https://wanderer.example.org',
external_id: 'trail1',
content_hash: 'n.d.r',
local_hash: 'n.r',
importedByRef: importedBy,
},
overrides,
);
}