feat(journey): read what a journey map shows beside its entries, and share its plan and recorded days on purpose

GET /api/journeys/:id/map-layers answers the planned places of the linked
trips (one per place, with every day the plan puts it on and the entry
written about it), the trips' GPX routes while the track switch is on, and
the located stays entries were taken over from. It goes through
journey_trips, so a trip nobody wrote about yet shows its plan too. Every
source is read on its own: one that fails says unavailable and the others
still draw. A position that is not one is left out, never moved.

GET /api/public/journey/:token/map-layers cuts the same on the server by the
link: nothing while the map is not shared, the plan and its routes only with
share_plan, the recorded days only with share_recordings and only beside a
published entry, stays only of published entries. Migration 046190 adds both
flags to journey_share_tokens, off by default, so a journey that is public
already shows what it showed before. The share link REST route and the MCP
share tool take them alike; get_journey_map_layers reads the layers over MCP
on the same access, a route by its size only.
This commit is contained in:
Maurice
2026-10-21 18:15:24 +02:00
parent bb10b94046
commit 9b71948497
35 changed files with 1465 additions and 41 deletions
+1
View File
@@ -103,6 +103,7 @@ export * from './journey/journey.schema';
export * from './journey/journey-recordings.schema';
export * from './journey/journey-day-import.schema';
export * from './journey/journey-recording-prep.schema';
export * from './journey/journey-map.schema';
export * from './journey/trip-journal-link.schema';
export * from './book/book.schema';
export * from './book/journey-stats.schema';
@@ -0,0 +1,78 @@
import { journeyMapLayersResponseSchema, journeyPublicMapLayersResponseSchema } from './journey-map.schema';
import { describe, expect, it } from 'vitest';
const place = {
place_id: 4,
trip_id: 2,
trip_title: 'Venice',
name: 'Piazza San Marco',
lat: 45.434,
lng: 12.3388,
days: ['2026-05-14'],
time: '10:00',
category_color: '#e11d48',
category_icon: 'landmark',
entry_id: null,
};
describe('journeyMapLayersResponseSchema', () => {
it('JRNMAP-SCHEMA-001: each source answers on its own, a failed one beside the others', () => {
const parsed = journeyMapLayersResponseSchema.safeParse({
places: { status: 'ok', items: [place] },
routes: { status: 'unavailable' },
stays: { status: 'ok', items: [] },
});
expect(parsed.success).toBe(true);
});
it('JRNMAP-SCHEMA-002: refuses a status it does not know and items on a source that has none', () => {
expect(
journeyMapLayersResponseSchema.safeParse({
places: { status: 'partial', items: [] },
routes: { status: 'ok', items: [] },
stays: { status: 'ok', items: [] },
}).success,
).toBe(false);
expect(
journeyMapLayersResponseSchema.safeParse({
places: { status: 'ok', items: [{ ...place, lat: 'north' }] },
routes: { status: 'ok', items: [] },
stays: { status: 'ok', items: [] },
}).success,
).toBe(false);
});
});
describe('journeyPublicMapLayersResponseSchema', () => {
it('JRNMAP-SCHEMA-003: the public answer carries the recorded days, a withheld source as such', () => {
const parsed = journeyPublicMapLayersResponseSchema.safeParse({
places: { status: 'withheld' },
routes: { status: 'withheld' },
stays: { status: 'ok', items: [] },
recordings: {
status: 'ok',
items: [
{
date: '2026-05-14',
pieces: [
[
[45.43, 12.33],
[45.44, 12.34],
],
],
distance_m: 1520,
},
],
},
});
expect(parsed.success).toBe(true);
expect(
journeyPublicMapLayersResponseSchema.safeParse({
places: { status: 'withheld' },
routes: { status: 'withheld' },
stays: { status: 'withheld' },
}).success,
).toBe(false);
});
});
+92
View File
@@ -0,0 +1,92 @@
import { journeyTrackSchema } from './journey.schema';
import { z } from 'zod';
/**
* JRN-04: what the journey map can show besides the entries and the photos
* the journey answers itself: the plan of its trips, the days of route it
* keeps and the stays its entries were taken over from.
*
* Every source answers on its own. One that failed says `unavailable` and the
* others still draw; one a share link does not show says `withheld`, so a
* reader is never offered a layer that is not theirs to switch on. Nothing
* here stands for a visit: a planned place is what the trip meant to do.
*/
/** A place of a linked trip on the day(s) the plan puts it on. */
export const journeyMapPlaceSchema = z.object({
place_id: z.number(),
trip_id: z.number(),
trip_title: z.string(),
name: z.string(),
lat: z.number(),
lng: z.number(),
/** The days of the plan it is on, `YYYY-MM-DD`, earliest first. */
days: z.array(z.string()),
/** The time the plan gives it on its first day, `HH:MM`, or null. */
time: z.string().nullable(),
category_color: z.string().nullable(),
category_icon: z.string().nullable(),
/** The journal entry written about it, when there is one; a planned place is no visit by itself. */
entry_id: z.number().nullable(),
});
export type JourneyMapPlace = z.infer<typeof journeyMapPlaceSchema>;
/** One stay a journal entry was taken over from (JRN-02), at the place it was recorded. */
export const journeyMapStaySchema = z.object({
id: z.number(),
entry_id: z.number(),
name: z.string(),
lat: z.number(),
lng: z.number(),
started_at: z.string(),
ended_at: z.string(),
});
export type JourneyMapStay = z.infer<typeof journeyMapStaySchema>;
/** A day of route the journey keeps (JRN-01), its pieces apart: a gap stays a gap. */
export const journeyMapRecordingSchema = z.object({
date: z.string(),
pieces: z.array(z.array(z.tuple([z.number(), z.number()]))),
distance_m: z.number().nullable(),
});
export type JourneyMapRecording = z.infer<typeof journeyMapRecordingSchema>;
/** How one source of the map answered. */
function layerResult<T extends z.ZodType>(item: T) {
return z.discriminatedUnion('status', [
z.object({ status: z.literal('ok'), items: z.array(item) }),
z.object({ status: z.literal('unavailable') }),
z.object({ status: z.literal('withheld') }),
]);
}
export const journeyMapPlacesLayerSchema = layerResult(journeyMapPlaceSchema);
export const journeyMapRoutesLayerSchema = layerResult(journeyTrackSchema);
export const journeyMapStaysLayerSchema = layerResult(journeyMapStaySchema);
export const journeyMapRecordingsLayerSchema = layerResult(journeyMapRecordingSchema);
export type JourneyMapLayerResult<T> =
{ status: 'ok'; items: T[] } | { status: 'unavailable' } | { status: 'withheld' };
/**
* GET /api/journeys/:id/map-layers. The trips' GPX tracks are the plan's
* routes, drawn while the journey's track switch is on. The recorded days are
* not here: the journey answers them with their details at `/recordings`.
*/
export const journeyMapLayersResponseSchema = z.object({
places: journeyMapPlacesLayerSchema,
routes: journeyMapRoutesLayerSchema,
stays: journeyMapStaysLayerSchema,
});
export type JourneyMapLayersResponse = z.infer<typeof journeyMapLayersResponseSchema>;
/**
* GET /api/public/journey/:token/map-layers: the same, cut to what the link
* shares. Nothing while the map is not shared; the plan and its routes only
* with `share_plan`; the recorded days only with `share_recordings` and only
* on a day with a published entry; stays only of published entries.
*/
export const journeyPublicMapLayersResponseSchema = journeyMapLayersResponseSchema.extend({
recordings: journeyMapRecordingsLayerSchema,
});
export type JourneyPublicMapLayersResponse = z.infer<typeof journeyPublicMapLayersResponseSchema>;
@@ -87,6 +87,12 @@ describe('journeyShareLinkRequestSchema', () => {
// The controller coerces with !!; a string flag has always worked.
expect(journeyShareLinkRequestSchema.safeParse({ share_map: 'true' }).success).toBe(true);
});
it('takes the plan and recordings flags as booleans only, being new (JRN-04)', () => {
expect(journeyShareLinkRequestSchema.safeParse({ share_plan: true, share_recordings: false }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ share_plan: 'true' }).success).toBe(false);
expect(journeyShareLinkRequestSchema.safeParse({ share_recordings: 1 }).success).toBe(false);
});
});
describe('the free-form bodies keep their unknown keys', () => {
+6
View File
@@ -173,6 +173,10 @@ export const journeyShareLinkRequestSchema = z.looseObject({
share_map: z.unknown().optional(),
/** Show the newest entry first, blog-style, rather than in trip order (#1614). */
newest_first: z.unknown().optional(),
/** Put the linked trips' plan on the shared map (JRN-04). Nothing without `share_map`. */
share_plan: z.boolean().optional(),
/** Put the recorded days on the shared map, on days with a published entry (JRN-04). */
share_recordings: z.boolean().optional(),
});
export type JourneyShareLinkRequest = z.infer<typeof journeyShareLinkRequestSchema>;
@@ -252,6 +256,8 @@ export const journeyShareLinkSchema = z.object({
share_gallery: z.boolean(),
share_map: z.boolean(),
newest_first: z.boolean(),
share_plan: z.boolean(),
share_recordings: z.boolean(),
});
export type JourneyShareLink = z.infer<typeof journeyShareLinkSchema>;