mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
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:
@@ -1,7 +1,11 @@
|
||||
import {
|
||||
journeyGpxImportRequestSchema,
|
||||
journeyRecordedDaySchema,
|
||||
journeyRecordingDateSchema,
|
||||
journeyRecordingDeleteQuerySchema,
|
||||
journeyRecordingPreviewRequestSchema,
|
||||
journeyRecordingSaveRequestSchema,
|
||||
oneRecordingPerDay,
|
||||
recordingStats,
|
||||
} from './journey-recordings.schema';
|
||||
|
||||
@@ -117,3 +121,62 @@ describe('recordingStats', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyRecordedDaySchema', () => {
|
||||
it('JRNREC-SCHEMA-006: reads a day kept from a GPX file, its pieces as tracks', () => {
|
||||
const day = {
|
||||
id: 1,
|
||||
date: '2026-05-14',
|
||||
source: 'gpx',
|
||||
sourceKind: 'tracks',
|
||||
utcOffsetMinutes: 120,
|
||||
window: { from: '2026-05-13T22:00:00.000Z', to: '2026-05-14T22:00:00.000Z' },
|
||||
segments: [],
|
||||
stats: recordingStats([]),
|
||||
truncated: false,
|
||||
provisional: false,
|
||||
fetchedAt: '2026-05-15T08:00:00.000Z',
|
||||
contentHash: HASH,
|
||||
importedBy: null,
|
||||
savedAt: '2026-05-15T08:00:00.000Z',
|
||||
};
|
||||
expect(journeyRecordedDaySchema.safeParse(day).success).toBe(true);
|
||||
expect(journeyRecordedDaySchema.safeParse({ ...day, source: 'strava' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oneRecordingPerDay', () => {
|
||||
it('JRNREC-SCHEMA-007: keeps one day per date, the GPX one where both sources kept it, earliest first', () => {
|
||||
const days = [
|
||||
{ date: '2026-05-15', source: 'dawarich' as const, id: 1 },
|
||||
{ date: '2026-05-14', source: 'gpx' as const, id: 2 },
|
||||
{ date: '2026-05-14', source: 'dawarich' as const, id: 3 },
|
||||
{ date: '2026-05-16', source: 'gpx' as const, id: 4 },
|
||||
{ date: '2026-05-16', source: 'dawarich' as const, id: 5 },
|
||||
];
|
||||
expect(oneRecordingPerDay(days).map((day) => day.id)).toEqual([2, 1, 4]);
|
||||
expect(oneRecordingPerDay([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyGpxImportRequestSchema', () => {
|
||||
it('JRNREC-SCHEMA-008: reads the multipart fields, the offset as whole minutes within the zones there are', () => {
|
||||
expect(journeyGpxImportRequestSchema.parse({})).toEqual({});
|
||||
expect(journeyGpxImportRequestSchema.parse({ date: '2026-05-14', utcOffsetMinutes: '-300' })).toEqual({
|
||||
date: '2026-05-14',
|
||||
utcOffsetMinutes: -300,
|
||||
});
|
||||
expect(journeyGpxImportRequestSchema.safeParse({ utcOffsetMinutes: '900' }).success).toBe(false);
|
||||
expect(journeyGpxImportRequestSchema.safeParse({ utcOffsetMinutes: '1.5' }).success).toBe(false);
|
||||
expect(journeyGpxImportRequestSchema.safeParse({ utcOffsetMinutes: 'abc' }).success).toBe(false);
|
||||
expect(journeyGpxImportRequestSchema.safeParse({ date: '2026-02-30' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyRecordingDeleteQuerySchema', () => {
|
||||
it('JRNREC-SCHEMA-009: names a known source or none', () => {
|
||||
expect(journeyRecordingDeleteQuerySchema.parse({})).toEqual({});
|
||||
expect(journeyRecordingDeleteQuerySchema.parse({ source: 'gpx' })).toEqual({ source: 'gpx' });
|
||||
expect(journeyRecordingDeleteQuerySchema.safeParse({ source: 'strava' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,8 +18,13 @@ import { z } from 'zod';
|
||||
* reveals one, else the one the traveller picks.
|
||||
*/
|
||||
|
||||
/** Where a recorded day came from. One source today; the column is there for a second. */
|
||||
export const JOURNEY_RECORDING_SOURCES = ['dawarich'] as const;
|
||||
/**
|
||||
* Where a recorded day came from: taken over from the caller's Dawarich, or
|
||||
* read from a GPX file someone uploaded (#3085). A journey keeps at most one
|
||||
* version of a day per source.
|
||||
*/
|
||||
export const JOURNEY_RECORDING_SOURCES = ['dawarich', 'gpx'] as const;
|
||||
export type JourneyRecordingSource = (typeof JOURNEY_RECORDING_SOURCES)[number];
|
||||
|
||||
/** `YYYY-MM-DD`, a calendar day that exists. */
|
||||
export const journeyRecordingDateSchema = z
|
||||
@@ -60,7 +65,11 @@ export const journeyRecordedDaySchema = z.object({
|
||||
id: z.number(),
|
||||
date: z.string(),
|
||||
source: z.enum(JOURNEY_RECORDING_SOURCES),
|
||||
/** Which Dawarich endpoint answered: pre-segmented tracks or thinned points. */
|
||||
/**
|
||||
* Which Dawarich endpoint answered: pre-segmented tracks or thinned points. A
|
||||
* day read from a GPX file is `tracks`, its pieces being the file's track
|
||||
* segments, so a reader that knows only these two still reads it.
|
||||
*/
|
||||
sourceKind: z.enum(['tracks', 'points']),
|
||||
utcOffsetMinutes: z.number(),
|
||||
window: recordingWindowSchema,
|
||||
@@ -191,3 +200,75 @@ export function recordingStats(segments: DawarichTrackSegment[]): JourneyRecordi
|
||||
lastAt: segments[segments.length - 1]?.endedAt ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The days a map draws, one per local day: where a journey keeps a day from
|
||||
* both sources, the GPX file wins, since someone uploaded it for that day on
|
||||
* purpose. Earliest day first.
|
||||
*/
|
||||
export function oneRecordingPerDay<T extends { date: string; source: JourneyRecordingSource }>(
|
||||
days: readonly T[],
|
||||
): T[] {
|
||||
const byDate = new Map<string, T>();
|
||||
for (const day of days) {
|
||||
const kept = byDate.get(day.date);
|
||||
if (!kept || (kept.source !== 'gpx' && day.source === 'gpx')) byDate.set(day.date, day);
|
||||
}
|
||||
return [...byDate.values()].sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0));
|
||||
}
|
||||
|
||||
// ── GPX as a source (#3085) ──────────────────────────────────────────────────
|
||||
|
||||
/** The largest GPX file a journey reads its recorded days from. */
|
||||
export const JOURNEY_GPX_MAX_BYTES = 15 * 1024 * 1024;
|
||||
|
||||
/** The most local days one GPX file may put into a journey at once. */
|
||||
export const JOURNEY_GPX_MAX_DAYS = 120;
|
||||
|
||||
/**
|
||||
* How a GPX file is cut into days. Without `utcOffsetMinutes` every point is
|
||||
* placed in the zone of where it was recorded; with it, every day is cut in
|
||||
* that one zone. `date` keeps only that one local day of the file.
|
||||
*/
|
||||
export const journeyGpxImportOptionsSchema = z.object({
|
||||
date: journeyRecordingDateSchema.optional(),
|
||||
utcOffsetMinutes: utcOffsetMinutesSchema.optional(),
|
||||
});
|
||||
export type JourneyGpxImportOptions = z.infer<typeof journeyGpxImportOptionsSchema>;
|
||||
|
||||
/** A zone offset as a multipart field carries it: the digits of a whole number of minutes. */
|
||||
const offsetFieldSchema = z
|
||||
.string()
|
||||
.regex(/^-?\d{1,3}$/)
|
||||
.transform(Number)
|
||||
.pipe(utcOffsetMinutesSchema);
|
||||
|
||||
/** The text fields beside the `file` of `POST /api/journeys/:id/recordings/import-gpx`. */
|
||||
export const journeyGpxImportRequestSchema = z.object({
|
||||
date: journeyRecordingDateSchema.optional(),
|
||||
utcOffsetMinutes: offsetFieldSchema.optional(),
|
||||
});
|
||||
export type JourneyGpxImportRequest = z.input<typeof journeyGpxImportRequestSchema>;
|
||||
|
||||
/** A GPX document handed over as text (MCP), under the same cap as an upload. */
|
||||
export const journeyGpxTextSchema = z.string().min(1).max(JOURNEY_GPX_MAX_BYTES);
|
||||
|
||||
export const journeyGpxImportResultSchema = z.object({
|
||||
/** One per local day the file covers, earliest first. */
|
||||
days: z.array(
|
||||
z.object({
|
||||
day: journeyRecordedDaySchema,
|
||||
/** `unchanged`: the journey kept this very day from a GPX file already. */
|
||||
outcome: z.enum(['created', 'replaced', 'unchanged']),
|
||||
}),
|
||||
),
|
||||
/** Track points left out because they carry no time with a zone, so no day can be named for them. */
|
||||
untimedPoints: z.number(),
|
||||
});
|
||||
export type JourneyGpxImportResult = z.infer<typeof journeyGpxImportResultSchema>;
|
||||
|
||||
/** Which source's day `DELETE /api/journeys/:id/recordings/:date` drops; Dawarich's when left out. */
|
||||
export const journeyRecordingDeleteQuerySchema = z.object({
|
||||
source: z.enum(JOURNEY_RECORDING_SOURCES).optional(),
|
||||
});
|
||||
export type JourneyRecordingDeleteQuery = z.infer<typeof journeyRecordingDeleteQuerySchema>;
|
||||
|
||||
Reference in New Issue
Block a user