Files
TREK/shared/src/tours/paddle-plan.schema.ts
T
Maurice d72352911e feat(tours): tour revisions, conflicts and a live editor (P2C-03)
A saved tour now has a technical revision, raised inside the transaction of
every write that changes it: the route and its controls, the plan laid onto a
recorded paddle track, going back to that track. A write that names the
revision it was based on (expected_revision) is refused with a 409
{ code: 'tour_revision_conflict', revision } before anything of it is
written, so a stale draft never overwrites a newer tour unseen. A write that
names none writes as before, so older clients and queued offline writes keep
working; the queue never carries a base. A copy starts at 1, a source's
version stays provenance. Migration 044800 adds the column; list_tours and
get_tour_plan read it.

The editor follows the trip's tours live: a newer revision of the open tour
loads at once while the editor has no changes of its own, and shows a notice
while it has. A refused save keeps the draft and asks: save it as a new tour,
load the newer version, or overwrite on purpose.

useTourPlanner was at the size limit; its derived facts, undo history and
draft storage move into tourPlannerFacts, usePlannerHistory and
useTourDraftStorage, unchanged in behaviour.
2026-10-19 10:22:04 +02:00

84 lines
3.5 KiB
TypeScript

import { z } from 'zod';
/**
* PAD-03 and PAD-07: what a paddle tour carries beyond its line. A stretch the
* boat is carried (a portage), a lock with the times the paddler expects at it,
* and the planning speeds the estimate is reckoned with. These are the
* paddler's own plan, never facts from a data source.
*/
/** TREK's planning starting values, to adjust: not norms, not measurements. */
export const DEFAULT_PORTAGE_SPEED_KMH = 2;
export const DEFAULT_PORTAGE_EXTRA_MINUTES = 10;
/** The highest carrying speed a paddler can enter, in km/h. */
export const MAX_PORTAGE_SPEED_KMH = 10;
export const portageSpeedKmhSchema = z.number().finite().min(0.5).max(MAX_PORTAGE_SPEED_KMH);
/** Minutes one portage or one lock may take in a plan; a day tour stays within a day. */
export const MAX_PADDLE_STOP_MINUTES = 12 * 60;
export const paddleStopMinutesSchema = z.number().int().min(0).max(MAX_PADDLE_STOP_MINUTES);
/**
* A lock on the tour, with the time the paddler expects to wait for it and to
* pass it. Either may be unknown: an unknown time is missing from the
* estimate, never zero.
*/
export const paddleLockSchema = z.object({
wait_minutes: paddleStopMinutesSchema.nullable(),
transit_minutes: paddleStopMinutesSchema.nullable(),
});
export type PaddleLock = z.infer<typeof paddleLockSchema>;
/** The most portages or locks one recorded track may be marked with. */
export const MAX_TRACK_MARKS = 50;
/**
* Marks laid onto a recorded track (a GPX import), which is never redrawn: a
* portage is the stretch between two distances along it, a lock one distance.
* Distances are metres from the start of the track as recorded.
*/
export const trackPortageMarkSchema = z
.object({
from_m: z.number().finite().min(0),
to_m: z.number().finite().min(0),
/** Getting out, unloading and putting back in; null takes the starting value. */
extra_minutes: paddleStopMinutesSchema.nullable(),
})
.refine((mark) => mark.to_m > mark.from_m, { message: 'a portage ends after it begins', path: ['to_m'] });
export type TrackPortageMark = z.infer<typeof trackPortageMarkSchema>;
export const trackLockMarkSchema = paddleLockSchema.extend({
at_m: z.number().finite().min(0),
});
export type TrackLockMark = z.infer<typeof trackLockMarkSchema>;
export const trackMarksSchema = z
.object({
portages: z.array(trackPortageMarkSchema).max(MAX_TRACK_MARKS),
locks: z.array(trackLockMarkSchema).max(MAX_TRACK_MARKS),
})
.superRefine((marks, ctx) => {
const sorted = [...marks.portages].sort((a, b) => a.from_m - b.from_m);
sorted.forEach((mark, i) => {
const before = sorted[i - 1];
if (before && mark.from_m < before.to_m)
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ['portages'], message: 'portages must not overlap' });
});
});
export type TrackMarks = z.infer<typeof trackMarksSchema>;
/**
* PUT /api/trips/:tripId/tours/:placeId/paddle-plan: the paddle plan of a tour
* that has no control points of its own, a recorded track. A planned tour
* saves the same values with its route.
*/
export const tourPaddlePlanRequestSchema = z.object({
paddle_speed_kmh: z.number().finite().min(0.5).max(20).nullable(),
portage_speed_kmh: portageSpeedKmhSchema.nullable(),
track_marks: trackMarksSchema,
/** P2C-03: the revision the plan was laid onto; refused with a 409 once the tour moved on. */
expected_revision: z.number().int().min(1).nullable().optional(),
});
export type TourPaddlePlanRequest = z.infer<typeof tourPaddlePlanRequestSchema>;