diff --git a/server/src/nest/tours/tour-file-read.service.ts b/server/src/nest/tours/tour-file-read.service.ts index 7bc7266bc..5843d62fc 100644 --- a/server/src/nest/tours/tour-file-read.service.ts +++ b/server/src/nest/tours/tour-file-read.service.ts @@ -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 { 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) => diff --git a/server/tests/unit/nest/tour-file-read.service.test.ts b/server/tests/unit/nest/tour-file-read.service.test.ts index a459c52f5..85cbbecea 100644 --- a/server/tests/unit/nest/tour-file-read.service.test.ts +++ b/server/tests/unit/nest/tour-file-read.service.test.ts @@ -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 },