mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
feat(tours): plan a multi-day tour as one route split into stages
A hut-to-hut hike or a paddle trip over several days is now planned in the Tours planner as one tour: the whole route is planned as before, and stops along it are marked as stage ends (MDT-01). The stages are read off that one route in its order, never kept as copies, so a change inside a stage changes the whole tour and every figure on it. - A Day tour / Multi-day tour switch beside the tour type; a stop's menu ends a stage there or lets it go on. Back to a day tour removes every stage end after a confirmation, as one step undo brings back. - The list of points is headed per stage with its distance and moving time; a stop that ends a stage carries a moon at its number, on the map too. - The overview lists the stages from stop to stop with distance, climb, descent and time (hiking rule per stage, paddle estimate per stage, the router's time shared by distance otherwise), marks where each ends across the elevation profile, and a stage of more than a day on the move warns. - A stage end a change cannot keep (its stop removed, moved to an end or made a shaping point) is dropped and said so, with undo, never moved to another point. - A multi-day tour may take a day a stage, so its time limit grows with its stages; the tour keeps its one type, portages included. - The list and the MCP tools show the stage count and where stages end. Stage ends are a flag on the waypoint (migration 046000); a tour without one is a day tour, as every tour saved before.
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { MAX_TOUR_STAGES, maxTourMinutesFor, TOUR_DAY_MINUTES, tourStageCountOf, tourStageIssues } from './tour-stages';
|
||||
import {
|
||||
MAX_PLANNED_TOUR_DURATION_MINUTES,
|
||||
plannedTimeLimitMessage,
|
||||
tourCreateRequestSchema,
|
||||
tourListItemSchema,
|
||||
tourWaypointSchema,
|
||||
} from './tours.schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
type Role = 'start' | 'via' | 'end';
|
||||
|
||||
/** A straight run of points along a meridian, each a stop unless told otherwise. */
|
||||
function points(count: number, extra: (index: number) => Record<string, unknown> = () => ({})) {
|
||||
return Array.from({ length: count }, (_, sequence) => ({
|
||||
lat: 47 + sequence * 0.01,
|
||||
lng: 11,
|
||||
role: (sequence === 0 ? 'start' : sequence === count - 1 ? 'end' : 'via') as Role,
|
||||
sequence,
|
||||
...extra(sequence),
|
||||
}));
|
||||
}
|
||||
|
||||
function request(waypoints: ReturnType<typeof points>, fields: Record<string, unknown> = {}) {
|
||||
return {
|
||||
name: 'Hut to hut',
|
||||
route_geometry: waypoints.map((point) => [point.lat, point.lng, 1000]),
|
||||
waypoints,
|
||||
...fields,
|
||||
};
|
||||
}
|
||||
|
||||
describe('MDT-01 tour stages', () => {
|
||||
it('MDT-SH-001: counts one stage for a day tour and one more for every stage end', () => {
|
||||
expect(tourStageCountOf([])).toBe(1);
|
||||
expect(tourStageCountOf(points(3))).toBe(1);
|
||||
expect(tourStageCountOf(points(5, (i) => (i === 1 || i === 3 ? { stage_end: true } : {})))).toBe(3);
|
||||
expect(tourStageCountOf([{ stage_end: false }])).toBe(1);
|
||||
});
|
||||
|
||||
it('MDT-SH-002: plans a day a stage, and never less than one day', () => {
|
||||
expect(TOUR_DAY_MINUTES).toBe(MAX_PLANNED_TOUR_DURATION_MINUTES);
|
||||
expect(maxTourMinutesFor(1)).toBe(1440);
|
||||
expect(maxTourMinutesFor(3)).toBe(3 * 1440);
|
||||
expect(maxTourMinutesFor(0)).toBe(1440);
|
||||
});
|
||||
|
||||
it('MDT-SH-003: takes a stage end at a stop between start and end only', () => {
|
||||
expect(tourStageIssues(points(4, (i) => (i === 1 ? { stage_end: true } : {})))).toEqual([]);
|
||||
expect(tourStageIssues(points(3, (i) => (i === 0 ? { stage_end: true } : {})))).toEqual([
|
||||
{ path: ['waypoints', 0, 'stage_end'], message: 'a stage ends at a stop between start and end' },
|
||||
]);
|
||||
expect(tourStageIssues(points(3, (i) => (i === 2 ? { stage_end: true } : {})))[0]?.path).toEqual([
|
||||
'waypoints',
|
||||
2,
|
||||
'stage_end',
|
||||
]);
|
||||
expect(tourStageIssues(points(3, (i) => (i === 1 ? { stage_end: true, shaping: true } : {})))).toEqual([
|
||||
{ path: ['waypoints', 1, 'stage_end'], message: 'a shaping point ends no stage' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('MDT-SH-004: refuses a tour of more stages than the limit', () => {
|
||||
const many = points(MAX_TOUR_STAGES + 1, () => ({ stage_end: true })).map((point, index, all) =>
|
||||
index === 0 || index === all.length - 1 ? { ...point, stage_end: undefined } : point,
|
||||
);
|
||||
expect(tourStageCountOf(many)).toBe(MAX_TOUR_STAGES);
|
||||
expect(tourStageIssues(many)).toEqual([]);
|
||||
const tooMany = points(MAX_TOUR_STAGES + 2, (i) => (i > 0 ? { stage_end: true } : {})).map((point, index, all) =>
|
||||
index === all.length - 1 ? { ...point, stage_end: undefined } : point,
|
||||
);
|
||||
expect(tourStageIssues(tooMany)).toContainEqual({
|
||||
path: ['waypoints'],
|
||||
message: `a tour has at most ${MAX_TOUR_STAGES} stages`,
|
||||
});
|
||||
});
|
||||
|
||||
it('MDT-SH-005: a waypoint carries its stage end, and one saved before reads as none', () => {
|
||||
expect(tourWaypointSchema.parse({ lat: 47, lng: 11, role: 'via', sequence: 1, stage_end: true })).toMatchObject({
|
||||
stage_end: true,
|
||||
});
|
||||
expect(tourWaypointSchema.parse({ lat: 47, lng: 11, role: 'via', sequence: 1 })).not.toHaveProperty('stage_end');
|
||||
expect(tourWaypointSchema.safeParse({ lat: 47, lng: 11, role: 'via', sequence: 1, stage_end: 'yes' }).success).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it('MDT-SH-006: saves a multi-day tour of any one type, and refuses a stage end out of place', () => {
|
||||
const hutToHut = points(4, (i) => (i === 1 ? { stage_end: true } : {}));
|
||||
expect(tourCreateRequestSchema.safeParse(request(hutToHut)).success).toBe(true);
|
||||
const paddle = request(hutToHut, { tour_type: 'kayak', boat_type: 'canoe', water_context: 'river' });
|
||||
expect(tourCreateRequestSchema.safeParse(paddle).success).toBe(true);
|
||||
const atEnd = tourCreateRequestSchema.safeParse(request(points(3, (i) => (i === 2 ? { stage_end: true } : {}))));
|
||||
expect(atEnd.success).toBe(false);
|
||||
expect(atEnd.error?.issues.map((issue) => issue.path.join('.'))).toContain('waypoints.2.stage_end');
|
||||
});
|
||||
|
||||
it('MDT-SH-007: holds a multi-day tour to a day a stage, a day tour to one day', () => {
|
||||
const twoDays = points(3, (i) => (i === 1 ? { stage_end: true } : {}));
|
||||
const long = { duration_seconds: 30 * 3600, break_additional_minutes: 60 };
|
||||
expect(tourCreateRequestSchema.safeParse(request(twoDays, long)).success).toBe(true);
|
||||
const tooLong = tourCreateRequestSchema.safeParse(request(twoDays, { ...long, duration_seconds: 48 * 3600 }));
|
||||
expect(tooLong.success).toBe(false);
|
||||
expect(tooLong.error?.issues[0]?.message).toBe(plannedTimeLimitMessage(2));
|
||||
const dayTour = tourCreateRequestSchema.safeParse(request(points(3), long));
|
||||
expect(dayTour.success).toBe(false);
|
||||
expect(dayTour.error?.issues[0]?.message).toBe(
|
||||
'walking time plus breaks must be 1440 minutes or less unless a total is overridden',
|
||||
);
|
||||
expect(plannedTimeLimitMessage(2)).toContain('2880 minutes or less, a day a stage');
|
||||
});
|
||||
|
||||
it('MDT-SH-008: lists the stage count of a multi-day tour, and reads a list cached before it', () => {
|
||||
const item = {
|
||||
place_id: 1,
|
||||
tour_type: 'hike',
|
||||
distance: 12,
|
||||
elevation_gain: null,
|
||||
elevation_loss: null,
|
||||
duration: null,
|
||||
difficulty: null,
|
||||
wanderer_ref: null,
|
||||
match_confidence: 1,
|
||||
max_hiking_difficulty: 2,
|
||||
name: 'Hut to hut',
|
||||
planned: false,
|
||||
caution: false,
|
||||
};
|
||||
expect(tourListItemSchema.parse({ ...item, stage_count: 3 }).stage_count).toBe(3);
|
||||
expect(tourListItemSchema.parse({ ...item, stage_count: null }).stage_count).toBeNull();
|
||||
expect(tourListItemSchema.parse(item).stage_count).toBeUndefined();
|
||||
expect(tourListItemSchema.safeParse({ ...item, stage_count: 1 }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* MDT-01: a multi-day tour is one tour, planned whole, whose stops mark where
|
||||
* each day's stage ends. The stages are read off the route in its order: the
|
||||
* first runs from the start to the first stage end, the next from there on,
|
||||
* the last to the end of the tour. Nothing about a stage is stored besides the
|
||||
* mark on its last stop, so the whole route and its stages cannot drift apart.
|
||||
* A tour without a mark is a day tour, as every tour saved before was.
|
||||
*/
|
||||
|
||||
/** A day in minutes: the most one stage, and a day tour, is planned for. */
|
||||
export const TOUR_DAY_MINUTES = 24 * 60;
|
||||
|
||||
/** The most stages a tour is split into, two months of days. */
|
||||
export const MAX_TOUR_STAGES = 60;
|
||||
|
||||
/** A point as a stage end is read from: where it sits on the tour, and whether it steers only. */
|
||||
interface StagePoint {
|
||||
role: 'start' | 'via' | 'end';
|
||||
shaping?: boolean;
|
||||
stage_end?: boolean;
|
||||
}
|
||||
|
||||
/** Where a request's stage ends do not hold, and why. */
|
||||
interface StageIssue {
|
||||
path: Array<string | number>;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/** How many stages the points make: one more than their stage ends, one for a day tour. */
|
||||
export function tourStageCountOf(points: ReadonlyArray<{ stage_end?: boolean }>): number {
|
||||
return 1 + points.filter((point) => point.stage_end === true).length;
|
||||
}
|
||||
|
||||
/** The longest moving time plus breaks a tour of so many stages is planned for: a day each. */
|
||||
export function maxTourMinutesFor(stageCount: number): number {
|
||||
return Math.max(1, stageCount) * TOUR_DAY_MINUTES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a stage end may not sit: at the start or the end of the tour, which
|
||||
* begin and finish the first and the last stage anyway, and on a shaping
|
||||
* point, which is no stop to spend a night at. Beyond {@link MAX_TOUR_STAGES}
|
||||
* the tour is refused as a whole.
|
||||
*/
|
||||
export function tourStageIssues(points: readonly StagePoint[]): StageIssue[] {
|
||||
const issues: StageIssue[] = [];
|
||||
points.forEach((point, index) => {
|
||||
if (point.stage_end !== true) return;
|
||||
if (point.role !== 'via')
|
||||
issues.push({ path: ['waypoints', index, 'stage_end'], message: 'a stage ends at a stop between start and end' });
|
||||
else if (point.shaping === true)
|
||||
issues.push({ path: ['waypoints', index, 'stage_end'], message: 'a shaping point ends no stage' });
|
||||
});
|
||||
if (tourStageCountOf(points) > MAX_TOUR_STAGES)
|
||||
issues.push({ path: ['waypoints'], message: `a tour has at most ${MAX_TOUR_STAGES} stages` });
|
||||
return issues;
|
||||
}
|
||||
@@ -11,19 +11,21 @@ import { skiDisciplineSchema, skiTechniqueSchema } from './ski.schema';
|
||||
import { tourActivityRefSchema, tourActivitySchema } from './tour-activity.schema';
|
||||
import { tourPoiCategorySchema } from './tour-poi-category';
|
||||
import { tourOriginSchema, tourProvenanceSchema } from './tour-provenance.schema';
|
||||
import { maxTourMinutesFor, TOUR_DAY_MINUTES, tourStageCountOf, tourStageIssues } from './tour-stages';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Tours contracts — the generalisation of "hike" into a domain of self-contained,
|
||||
* single-day, non-motorized activities that follow a track (hike, bike, run, ski,
|
||||
* kayak, city walk, food tour, ...). Tour types use a controlled key list.
|
||||
* non-motorized activities that follow a track (hike, bike, run, ski, kayak,
|
||||
* city walk, food tour, ...), a day each or (MDT-01) one route split into
|
||||
* stages over several days. Tour types use a controlled key list.
|
||||
*
|
||||
* Isolated bounded context:
|
||||
* no columns on `places`/`days`/`users`. A tour is a Place (route_geometry reused
|
||||
* as-is) plus a day_assignments row (reused as-is, `assignPlaceToDay`) plus this
|
||||
* facet table keyed by place_id. Boundary vs. Road-Trip: Tours = non-motorized +
|
||||
* single-day, Road-Trip = motorized + multi-day.
|
||||
* facet table keyed by place_id. Boundary vs. Road-Trip: Tours = non-motorized,
|
||||
* Road-Trip = motorized.
|
||||
*/
|
||||
|
||||
export const tourTypeKeySchema = z.enum(['hike', 'bike', 'run', 'ski', 'kayak', 'walk', 'food', 'horse']);
|
||||
@@ -136,8 +138,8 @@ export function hikingPreferencesOf(value: HikingPreferences | null | undefined)
|
||||
return kept.length === 0 ? null : Object.fromEntries(kept.map((key) => [key, true]));
|
||||
}
|
||||
|
||||
/** Tours are single-day activities, so a planned total is bounded to 24 hours. */
|
||||
export const MAX_PLANNED_TOUR_DURATION_MINUTES = 24 * 60;
|
||||
/** A planned total is bounded to a day; a multi-day tour's moving time to a day a stage (MDT-01). */
|
||||
export const MAX_PLANNED_TOUR_DURATION_MINUTES = TOUR_DAY_MINUTES;
|
||||
export const plannedTourDurationMinutesSchema = z
|
||||
.number()
|
||||
.int()
|
||||
@@ -305,6 +307,11 @@ export const tourListItemSchema = tourSchema.extend({
|
||||
activity_tracks: z.array(tourActivityRefSchema).optional(),
|
||||
/** ACT-01: the tour this recording documents, when it is assigned to one; null otherwise. */
|
||||
activity_of: z.number().int().positive().nullable().optional(),
|
||||
/**
|
||||
* MDT-01: the stages a multi-day tour's stops split it into; null for a day
|
||||
* tour. Absent in a list cached before it existed.
|
||||
*/
|
||||
stage_count: z.number().int().min(2).nullable().optional(),
|
||||
});
|
||||
export type TourListItem = z.infer<typeof tourListItemSchema>;
|
||||
|
||||
@@ -448,6 +455,11 @@ export const tourWaypointSchema = z.object({
|
||||
* map drew the place. Null on a point set by hand.
|
||||
*/
|
||||
poi_category: tourPoiCategorySchema.nullable().optional(),
|
||||
/**
|
||||
* MDT-01: a stop where a stage of a multi-day tour ends and the next one
|
||||
* begins. Absent means the route goes on, as on every point saved before.
|
||||
*/
|
||||
stage_end: z.boolean().optional(),
|
||||
});
|
||||
export type TourWaypoint = z.infer<typeof tourWaypointSchema>;
|
||||
|
||||
@@ -511,15 +523,17 @@ export const tourCreateRequestSchema = z
|
||||
value.planned_duration_minutes == null &&
|
||||
value.duration_seconds != null &&
|
||||
value.break_additional_minutes != null &&
|
||||
Math.round(value.duration_seconds / 60) + value.break_additional_minutes > MAX_PLANNED_TOUR_DURATION_MINUTES
|
||||
Math.round(value.duration_seconds / 60) + value.break_additional_minutes >
|
||||
maxTourMinutesFor(tourStageCountOf(value.waypoints))
|
||||
) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ['break_additional_minutes'],
|
||||
message: 'walking time plus breaks must be 1440 minutes or less unless a total is overridden',
|
||||
message: plannedTimeLimitMessage(tourStageCountOf(value.waypoints)),
|
||||
});
|
||||
}
|
||||
tourTypeIssues(value).forEach((issue) => ctx.addIssue({ code: z.ZodIssueCode.custom, ...issue }));
|
||||
tourStageIssues(value.waypoints).forEach((issue) => ctx.addIssue({ code: z.ZodIssueCode.custom, ...issue }));
|
||||
value.waypoints.forEach((point, index) => {
|
||||
const leg = point.fixed_leg;
|
||||
const next = value.waypoints[index + 1];
|
||||
@@ -572,6 +586,16 @@ const TYPE_NAMED: Record<PlannableTourType, string> = {
|
||||
horse: 'a horse tour',
|
||||
};
|
||||
|
||||
/**
|
||||
* What a save over its time limit is told: a day for a day tour, as it always
|
||||
* was, and a day a stage for a multi-day tour (MDT-01).
|
||||
*/
|
||||
export function plannedTimeLimitMessage(stageCount: number): string {
|
||||
return stageCount > 1
|
||||
? `walking time plus breaks must be ${maxTourMinutesFor(stageCount)} minutes or less, a day a stage, unless a total is overridden`
|
||||
: 'walking time plus breaks must be 1440 minutes or less unless a total is overridden';
|
||||
}
|
||||
|
||||
const PADDLE_FIELDS = [
|
||||
'boat_type',
|
||||
'water_context',
|
||||
|
||||
Reference in New Issue
Block a user