fix(tours): fit the merged tracks together after integration

Types, test expectations and helpers the seven tracks each changed on their
own, brought in line with the integrated code. tours.service.ts and the tours
repository grew past the size limit with every track and hand their row and
write helpers to modules of their own.
This commit is contained in:
Maurice
2026-10-20 16:30:55 +02:00
parent 65ee472d66
commit f4750352e6
56 changed files with 582 additions and 505 deletions
+22 -20
View File
@@ -236,18 +236,29 @@ export const TOUR_CHANGE_STEP_CHANGES: JsonColumn<TourChange[]> = {
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 point of a saved line: latitude, longitude and the height where it has one.
* Checked by hand rather than as a union of tuples: the server compiles without
* strictNullChecks, where a tuple schema reads as one of optional elements and
* would not hand back the GeometryPoint the restore writes.
*/
const savedLinePoint = z.custom<GeometryPoint>(
(value) =>
Array.isArray(value) &&
(value.length === 2 || value.length === 3) &&
value.every((n: unknown) => typeof n === '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;
}
const tourChangeStepBeforeSchema = 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(),
});
export type TourChangeStepBefore = z.infer<typeof tourChangeStepBeforeSchema>;
/**
* MCP-T03: a tour as it was before a saved step changed it: its line (null
@@ -257,16 +268,7 @@ export interface TourChangeStepBefore {
*/
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(),
schema: tourChangeStepBeforeSchema.nullable(),
fallback: () => null,
};
+1 -53
View File
@@ -2,6 +2,7 @@ import { type Tours, type TToursOrigin, ToursOrigin } from '../entities/Tours.en
import type { DB } from '../kysely/db';
import type { AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
import { toLibraryRow, toListRow } from './tour-rows';
import type {
BikeProfile,
PaddleBoatType,
@@ -588,17 +589,6 @@ export class ToursRepository extends TrekRepository<Tours> {
.execute();
}
/**
* What a copied trip's tour keeps besides its facet and its points: its
* import record, its obstacle decisions and its packing link, onto its copy.
* Called inside the trip copy's transaction.
*/
async copyTourRelations(source_place_id: number, place_id: number): Promise<void> {
await this.copySource(source_place_id, place_id);
await this.copyObstacleReviews(source_place_id, place_id);
await this.copyPackingLink(source_place_id, place_id);
}
/** The days every tour of a trip sits on, each tour and day once, in day order, with the stage a day holds (MDT-02). */
async listTourDays(trip_id: number): Promise<TourDayRow[]> {
return await this.db()
@@ -973,45 +963,3 @@ export class ToursRepository extends TrekRepository<Tours> {
await this.nativeUpdate({ place: place_id }, { recorded_route_geometry: null });
}
}
function toLibraryRow<R extends Omit<TourLibraryRow, 'has_waypoints'> & { has_waypoints: unknown }>(
r: R,
): TourLibraryRow {
return {
place_id: r.place_id,
name: r.name,
description: r.description,
tour_type: r.tour_type,
distance: r.distance,
elevation_gain: r.elevation_gain,
elevation_loss: r.elevation_loss,
duration: r.duration,
planned_duration_minutes: r.planned_duration_minutes,
break_additional_minutes: r.break_additional_minutes,
max_hiking_difficulty: r.max_hiking_difficulty,
origin: r.origin,
trip_id: r.trip_id,
trip_title: r.trip_title,
has_waypoints: Number(r.has_waypoints),
};
}
function toListRow<
R extends Omit<TourListRow, 'planned' | 'has_waypoints' | 'has_recorded_track' | 'route_missing' | 'stage_ends'> & {
planned: unknown;
has_waypoints: unknown;
has_recorded_track: unknown;
route_missing: unknown;
stage_ends: unknown;
},
>(r: R): TourListRow {
return {
...r,
planned: Number(r.planned),
has_waypoints: Number(r.has_waypoints),
has_recorded_track: Number(r.has_recorded_track),
route_missing: Number(r.route_missing),
// Postgres counts in a bigint, which its driver hands back as text.
stage_ends: Number(r.stage_ends),
};
}
+48
View File
@@ -0,0 +1,48 @@
/**
* The rows the tours repository answers, as its queries read them: counts and
* flags come back from SQLite and Postgres in different spellings, and are
* made numbers here.
*/
import type { TourLibraryRow, TourListRow } from './Tours.repository';
export function toLibraryRow<R extends Omit<TourLibraryRow, 'has_waypoints'> & { has_waypoints: unknown }>(
r: R,
): TourLibraryRow {
return {
place_id: r.place_id,
name: r.name,
description: r.description,
tour_type: r.tour_type,
distance: r.distance,
elevation_gain: r.elevation_gain,
elevation_loss: r.elevation_loss,
duration: r.duration,
planned_duration_minutes: r.planned_duration_minutes,
break_additional_minutes: r.break_additional_minutes,
max_hiking_difficulty: r.max_hiking_difficulty,
origin: r.origin,
trip_id: r.trip_id,
trip_title: r.trip_title,
has_waypoints: Number(r.has_waypoints),
};
}
export function toListRow<
R extends Omit<TourListRow, 'planned' | 'has_waypoints' | 'has_recorded_track' | 'route_missing' | 'stage_ends'> & {
planned: unknown;
has_waypoints: unknown;
has_recorded_track: unknown;
route_missing: unknown;
stage_ends: unknown;
},
>(r: R): TourListRow {
return {
...r,
planned: Number(r.planned),
has_waypoints: Number(r.has_waypoints),
has_recorded_track: Number(r.has_recorded_track),
route_missing: Number(r.route_missing),
// Postgres counts in a bigint, which its driver hands back as text.
stage_ends: Number(r.stage_ends),
};
}