feat(journey): keep recorded days from a GPX file beside Dawarich

A GPX file is a second source of a journey's recorded days (#3085). The
new route POST /api/journeys/:id/recordings/import-gpx (multipart, 15 MB,
edit access, no Dawarich needed) and the MCP tool import_journey_gpx_days
read the file's track segments through the place import's GPX reader,
place every timed point on its local day in the zone of where it was
recorded (or in one zone the caller fixes), cut the pieces at gaps, thin
them like a Dawarich day and keep one day per date with source gpx in one
transaction. Reading the file again replaces only its GPX days and leaves
an unchanged day and every Dawarich day alone; a day that changed pings
journey:recording:updated.

A GPX day is stored with source_kind tracks, so the existing contract
still reads it. DELETE :date and delete_journey_recorded_day take an
optional source, Dawarich's when left out. The shared map shows a GPX day
also without a published entry and prefers it over a Dawarich day of the
same date; the list of kept days carries both, ordered by date and source.
No migration: source and source_kind are text columns already.
This commit is contained in:
Maurice
2026-11-30 14:32:11 +01:00
parent cd20a0371f
commit 9f3722f459
26 changed files with 1458 additions and 75 deletions
@@ -11,7 +11,8 @@
* The walk: preview, keep, read the day back once the connection is gone,
* take the same day over again without a second copy, replace it on purpose,
* keep the version when Dawarich fails, and drop it. Then the preparation of
* the journey's days (JRN-03) as each caller sees it.
* the journey's days (JRN-03) as each caller sees it, and the days read from a
* GPX file (#3085) beside the ones Dawarich gave, each dropped by its source.
*/
import { DawarichConnections } from '../../src/db/entities/DawarichConnections.entity';
import { JourneyRecordedDays } from '../../src/db/entities/JourneyRecordedDays.entity';
@@ -329,4 +330,97 @@ describe('Journey recordings e2e (real guards + temp SQLite)', () => {
expect((await request(server).get(`${base()}/prep`).set(as(VIEWER))).status).toBe(403);
expect((await request(server).get(`${base()}/prep`).set(as(OUTSIDER))).status).toBe(404);
});
describe('days from a GPX file (#3085)', () => {
const TRACK = Buffer.from(
'<?xml version="1.0"?><gpx version="1.1"><trk><trkseg>' +
`<trkpt lat="45.43" lon="12.33"><time>${DAY}T08:00:00Z</time></trkpt>` +
`<trkpt lat="45.44" lon="12.34"><time>${DAY}T08:05:00Z</time></trkpt>` +
'<trkpt lat="45.5" lon="11.9"><time>2026-05-15T07:00:00Z</time></trkpt>' +
'<trkpt lat="45.51" lon="11.91"><time>2026-05-15T07:05:00Z</time></trkpt>' +
'</trkseg></trk></gpx>',
);
const upload = (userId: number, file: Buffer = TRACK) =>
request(server).post(`${base()}/import-gpx`).set(as(userId)).attach('file', file, 'drive.gpx');
it('keeps the days of the file beside the Dawarich day, with Dawarich off, and drops each by its source', async () => {
const shown = await preview();
await request(server)
.put(`${base()}/${DAY}`)
.set(as(OWNER))
.send({ utcOffsetMinutes: 120, expectedHash: shown.body.contentHash });
await setAddonEnabled(orm, 'dawarich', false);
const imported = await upload(EDITOR).set('X-Socket-Id', '12');
expect(imported.status).toBe(200);
expect(imported.body.untimedPoints).toBe(0);
type Kept = { day: { date: string }; outcome: string };
const outcomes = imported.body.days.map((d: Kept) => [d.day.date, d.outcome]);
expect(outcomes).toEqual([
[DAY, 'created'],
['2026-05-15', 'created'],
]);
expect(realtime.broadcastToUserMock).toHaveBeenCalledWith(
VIEWER,
{ type: 'journey:recording:updated', journeyId, date: '2026-05-15' },
'12',
);
const listed = await request(server).get(base()).set(as(VIEWER));
expect(listed.body.days.map((d: { date: string; source: string }) => [d.date, d.source])).toEqual([
[DAY, 'dawarich'],
[DAY, 'gpx'],
['2026-05-15', 'gpx'],
]);
const again = await upload(EDITOR);
expect(again.body.days.map((d: { outcome: string }) => d.outcome)).toEqual(['unchanged', 'unchanged']);
expect(await countRows(orm, JourneyRecordedDays)).toBe(3);
const dropped = await request(server).delete(`${base()}/${DAY}?source=gpx`).set(as(EDITOR));
expect(dropped.status).toBe(200);
const left = await request(server).get(base()).set(as(OWNER));
expect(left.body.days.map((d: { date: string; source: string }) => [d.date, d.source])).toEqual([
[DAY, 'dawarich'],
['2026-05-15', 'gpx'],
]);
expect((await request(server).delete(`${base()}/${DAY}?source=strava`).set(as(EDITOR))).status).toBe(400);
});
it('cuts one day in the zone asked for, and refuses what it cannot read before writing anything', async () => {
const one = await request(server)
.post(`${base()}/import-gpx`)
.set(as(OWNER))
.field('date', DAY)
.field('utcOffsetMinutes', '120')
.attach('file', TRACK, 'drive.gpx');
expect(one.status).toBe(200);
expect(one.body.days).toHaveLength(1);
expect(one.body.days[0].day).toMatchObject({
date: DAY,
source: 'gpx',
sourceKind: 'tracks',
utcOffsetMinutes: 120,
});
await deleteRows(orm, JourneyRecordedDays);
const notGpx = await upload(OWNER, Buffer.from('<kml/>'));
expect(notGpx.status).toBe(422);
expect(notGpx.body).toEqual({ error: 'The file is not a GPX file', code: 'gpx_unreadable' });
const badZone = await request(server)
.post(`${base()}/import-gpx`)
.set(as(OWNER))
.field('utcOffsetMinutes', '2000')
.attach('file', TRACK, 'drive.gpx');
expect(badZone.status).toBe(400);
const noFile = await request(server).post(`${base()}/import-gpx`).set(as(OWNER)).send({});
expect(noFile.status).toBe(400);
expect(noFile.body).toEqual({ error: 'No file uploaded', code: 'no-file' });
expect((await upload(VIEWER)).status).toBe(403);
expect((await upload(OUTSIDER)).status).toBe(404);
expect((await request(server).post(`${base()}/import-gpx`).attach('file', TRACK, 'drive.gpx')).status).toBe(401);
expect(await countRows(orm, JourneyRecordedDays)).toBe(0);
});
});
});