fix(tours): pair the tracks to take over in the tours domain, off the places list

This commit is contained in:
Maurice
2026-10-26 12:47:09 +01:00
parent 0313597b10
commit fd818050bd
7 changed files with 98 additions and 81 deletions
@@ -318,21 +318,6 @@ export class PlacesRepository extends TrekRepository<Places> {
return !!row;
}
/**
* The trip's tracks that are not tours: places with `route_geometry` and no
* `tours` row, oldest first. A GPX file imported before Tours existed (4.x)
* left its tracks like this, and the tour import takes one over by name
* instead of refusing it as a duplicate (#2810).
*/
async listTracksWithoutTour(trip_id: number): Promise<{ id: number; name: string; route_geometry: string }[]> {
return this.qb('p')
.leftJoin('p.tours', 't')
.select(['p.id', 'p.name', 'p.route_geometry'])
.where({ 'p.trip': trip_id, 'p.route_geometry': { $ne: null }, 't.place': null })
.orderBy({ 'p.id': 'asc' })
.execute<{ id: number; name: string; route_geometry: string }[]>('all', false);
}
/** PL19/PL21 — `DELETE FROM places WHERE id = ?`, unscoped: every caller proved trip access first. */
async deleteById(id: number): Promise<void> {
await this.nativeDelete({ id });
+1 -25
View File
@@ -3,7 +3,7 @@ import path from 'node:path';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { resolveCountryCodeSync } from '../atlas/atlas-geo';
import { TRACK_COLORS, normalizePlaceName, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
import { TRACK_COLORS, placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import { RealtimeService } from '../realtime/realtime.service';
import type { TripAccess } from '../../db/repositories/Trips.repository';
@@ -765,30 +765,6 @@ export class PlacesService {
});
}
/**
* The rows of a tour import that take over a track already in the trip,
* by row index: a place with a track and no tour yet, under the same name,
* one place per row. A GPX file imported before Tours existed (4.x) left
* its tracks as plain places, and the name alone made every tour import of
* the same file a duplicate with no tour to show for it (#2810).
*/
async tracksToAdopt(tripId: string, rows: PreparedGpxPlace[]): Promise<Map<number, { id: number; route_geometry: string }>> {
const adopt = new Map<number, { id: number; route_geometry: string }>();
const tid = toRowId(tripId);
if (tid === null) return adopt;
const byName = new Map<string, { id: number; route_geometry: string }[]>();
for (const track of await this.placesRepo.listTracksWithoutTour(tid)) {
const key = normalizePlaceName(track.name);
if (key) byName.set(key, [...(byName.get(key) ?? []), track]);
}
rows.forEach((row, index) => {
const key = normalizePlaceName(row.name);
const track = key ? byName.get(key)?.shift() : undefined;
if (track) adopt.set(index, track);
});
return adopt;
}
/**
* The id of the place on this trip that `candidate` already is, or null.
*
@@ -0,0 +1,42 @@
import { normalizePlaceName } from '@trek/shared';
/** A place of the trip as the places list reads it: enough to tell a track that is no tour yet. */
export interface TripPlaceForAdoption {
id: number;
name: string;
route_geometry?: string | null;
tour_place_id?: number | null;
}
/** A track of the trip a file row takes over: its place and the line it already holds. */
export interface AdoptedTrack {
id: number;
route_geometry: string;
}
/**
* The rows of a tour import that take over a track already in the trip, by
* row index: a place with a line and no tour yet, under the same name
* (trimmed, any case, the duplicate rule's own reading), one place per row
* and oldest first. A GPX file imported before Tours existed (4.x) left its
* tracks as plain places, and the name alone made every tour import of the
* same file a duplicate, with no tour to show for it (#2810).
*/
export function tracksToAdopt(
rows: ReadonlyArray<{ name: string }>,
places: ReadonlyArray<TripPlaceForAdoption>,
): Map<number, AdoptedTrack> {
const byName = new Map<string, AdoptedTrack[]>();
for (const place of [...places].sort((a, b) => a.id - b.id)) {
const key = normalizePlaceName(place.name);
if (!key || !place.route_geometry || place.tour_place_id != null) continue;
byName.set(key, [...(byName.get(key) ?? []), { id: place.id, route_geometry: place.route_geometry }]);
}
const adopt = new Map<number, AdoptedTrack>();
rows.forEach((row, index) => {
const key = normalizePlaceName(row.name);
const track = key ? byName.get(key)?.shift() : undefined;
if (track) adopt.set(index, track);
});
return adopt;
}
+8 -2
View File
@@ -29,6 +29,7 @@ import { readTourFile } from './tour-file-import.helpers';
import { provenanceSource, tourItemOf } from './tour-item.helpers';
import { TourPaddleCheckService } from './tour-paddle-check.service';
import { advanceTourRevision } from './tour-revision';
import { tracksToAdopt } from './tour-track-adoption';
import { toVisit } from './tour-visits.service';
import { checkTourCommand, firstPointOf, importFileName, keyedWaypoints, tourRouteFields } from './tour-write.helpers';
import { computeTourMetrics, localHashOf, parseRouteGeometry, type GeometryPoint } from './tours.helpers';
@@ -809,7 +810,7 @@ export class ToursService {
await this.assertTourTypeEnabled(fields.tour_type);
const typeFields = importedTypeFields(fields);
const adopt = await this.places.tracksToAdopt(tripId, rows);
const adopt = await this.adoptableTracks(tripId, rows);
const fresh = rows.filter((_, index) => !adopt.has(index));
const tours: TourListItem[] = [];
const adopted: number[] = [];
@@ -867,6 +868,11 @@ export class ToursService {
return { tours, caution: tours.some((t) => t.caution), skipped: result.skipped };
}
/** Which rows of a file take over a track of the trip from before Tours (#2810), read off the places list. */
private async adoptableTracks(tripId: string, rows: ReadonlyArray<{ name: string }>) {
return tracksToAdopt(rows, await this.places.list(tripId, {}));
}
/**
* The `tours` row for one place of a GPX import, with its metrics read off
* `geometry` and the recorded times, and the list item the import answers
@@ -944,7 +950,7 @@ export class ToursService {
const { rows, leftOut } = await readTourFile(this.placeImport, fileBuffer, fileName);
if (rows.length === 0) return null;
const duplicates = await this.places.gpxDuplicates(tripId, rows);
const adopt = await this.places.tracksToAdopt(tripId, rows);
const adopt = await this.adoptableTracks(tripId, rows);
return {
tracks: rows.map((row, index) => {
const geometry = parseRouteGeometry(row.routeGeometry);