fix(assistant): propose a route file's track from before Tours instead of skipping it

The Tours import takes over a same-named track a trip kept from 4.x, but a
route file attached in the assistant chat was read with the plain duplicate
rule only, so that track was reported as already in the trip and never
proposed. The read now leaves such a track to the import, which takes it
over as a tour like the Tours dialog does.

Refs #2810
This commit is contained in:
Maurice
2026-10-27 15:02:24 +01:00
parent fed564e7c0
commit c383b11648
2 changed files with 30 additions and 3 deletions
@@ -2,6 +2,7 @@ import { PlaceImportService } from '../place-import/place-import.service';
import type { PreparedGpxPlace } from '../place-import/place-import.types';
import { PlacesService } from '../places/places.service';
import { readTourFile } from './tour-file-import.helpers';
import { tracksToAdopt } from './tour-track-adoption';
import { gpxOfLine, splitGpxTrackPieces } from './tour-file-split.helpers';
import { computeTourMetrics, parseRouteGeometry } from './tours.helpers';
import { Injectable } from '@nestjs/common';
@@ -15,7 +16,10 @@ export interface TourFileTrack {
point_count: number;
distance_km: number;
has_heights: boolean;
/** A track already in the trip under the same name and start, which the import would skip. */
/**
* A track already in the trip under the same name and start, which the import would skip. A track
* from before Tours under the same name is none: the import takes it over as a tour (#2810).
*/
duplicate: boolean;
}
@@ -55,7 +59,9 @@ export class TourFileReadService {
async read(tripId: number, buffer: Buffer, fileName: string): Promise<TourFileTracks> {
const { rows, leftOut } = await readTourFile(this.placeImport, buffer, fileName);
if (rows.length === 0) return { tracks: [], whole: false };
const duplicates = await this.places.gpxDuplicates(String(tripId), rows);
const found = await this.places.gpxDuplicates(String(tripId), rows);
const adopt = tracksToAdopt(rows, await this.places.list(String(tripId), {}));
const duplicates = found.map((duplicate, index) => duplicate && !adopt.has(index));
const extra = leftOut ? { left_out: leftOut } : {};
if (!isGpxName(fileName)) {
const tracks = rows.map((row, index) =>
@@ -6,10 +6,12 @@
* line is written as GPX.
*/
import { PlaceImportService } from '../../../src/nest/place-import/place-import.service';
import { PlacesService } from '../../../src/nest/places/places.service';
import { TourFileReadService } from '../../../src/nest/tours/tour-file-read.service';
import { gpxOfLine, splitGpxTracks } from '../../../src/nest/tours/tour-file-split.helpers';
import { TourFilesModule } from '../../../src/nest/tours/tour-files.module';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { makePlace } from '../../helpers/factories/places';
import { makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { createTestModule, type TestModule } from '../../helpers/test-module';
@@ -18,7 +20,7 @@ import { TOUR_GPX_TEXT_MAX } from '@trek/shared';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
// TOUR-FILE-READ-001 to TOUR-FILE-READ-008
// TOUR-FILE-READ-001 to TOUR-FILE-READ-009
const db = createSnapshotTestDb();
let t: TestModule;
@@ -130,6 +132,25 @@ describe('TourFileReadService', () => {
});
});
it('TOUR-FILE-READ-009: a track from before Tours under the same name is proposed, for the import to take over (#2810)', async () => {
const trip = await tripId();
// 4.x stored a GPX track as a plain place: a line, no tours row.
await makePlace(t.get(MikroORM), trip, {
name: 'Saturday',
lat: 47.5,
lng: 11.1,
route_geometry: '[[47.5,11.1,800],[47.51,11.1,1000]]',
});
const rows = t.get(PlaceImportService).readGpx(Buffer.from(TWO_TRACKS, 'utf8'), { importTracks: true });
expect((await t.get(PlacesService).gpxDuplicates(String(trip), rows)).some(Boolean)).toBe(true);
const read = await files.read(trip, Buffer.from(TWO_TRACKS, 'utf8'), 'weekend.gpx');
expect(read.tracks.map((track) => [track.name, track.duplicate])).toEqual([
['Saturday', false],
['Sunday', false],
]);
});
it('TOUR-FILE-READ-008: a track longer than one import takes is named but carries no text', async () => {
const points = Array.from(
{ length: 20_000 },