mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 01:12:16 +00:00
feat(tours): a paddle tour's obstacle checklist and check bench on the server
PAD-06 and PAD-09. The tours list carries the check bench of every paddle tour and the detail its whole checklist, worked out of the obstacle data along the tour's line and the decisions stored for it. The obstacle data is read along the line run by run and kept in memory under the line, the boat and the data version, so a list of a trip reads it once; a hike costs nothing. While the data cannot be read the checklist says so and keeps every decision visible. PUT /api/trips/:tripId/tours/:placeId/obstacle-reviews stores what the paddlers decided, each against the basis the user saw, and answers the detail. It needs place_edit, refuses a tour that is no paddle tour with a 400, waits with a 503 while the data cannot be read, and skips an obstacle no longer on the checklist instead of failing. A copied tour and a copied trip take their decisions along. The MCP read tools describe the new fields; there is no write tool, since a decision is a statement of the paddlers. The obstacle data keeps the dates of checks on site, covers a line by sampling it, and tells its version by its rows and regions.
This commit is contained in:
@@ -2,12 +2,14 @@ import type { JsonColumn } from '../utils/json-column';
|
||||
import {
|
||||
FERRATAS_RESULT_CAP,
|
||||
ferrataSchema,
|
||||
obstacleSnapshotSchema,
|
||||
paddleGeometrySchema,
|
||||
paddleLockSchema,
|
||||
paddleRegionBoundarySchema,
|
||||
tourFixedLegSchema,
|
||||
trackMarksSchema,
|
||||
type Ferrata,
|
||||
type ObstacleSnapshot,
|
||||
type PaddleGeometry,
|
||||
type PaddleLock,
|
||||
type PaddleRegionBoundary,
|
||||
@@ -191,3 +193,10 @@ export const PADDLE_REGION_BOUNDARY: JsonColumn<PaddleRegionBoundary | null> = {
|
||||
schema: paddleRegionBoundarySchema.nullable(),
|
||||
fallback: () => null,
|
||||
};
|
||||
|
||||
/** What a paddle tour's obstacle decision keeps of its obstacle (PAD-06). A broken row has none, and the decision is left out. */
|
||||
export const TOUR_OBSTACLE_SNAPSHOT: JsonColumn<ObstacleSnapshot | null> = {
|
||||
column: 'tour_obstacle_reviews.obstacle_snapshot',
|
||||
schema: obstacleSnapshotSchema.nullable(),
|
||||
fallback: () => null,
|
||||
};
|
||||
|
||||
@@ -66,6 +66,18 @@ export class PaddleFeaturesRepository extends TrekRepository<PaddleFeatures> {
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** How many objects TREK holds and the newest row's id: together they change whenever a read lands. */
|
||||
async stats(): Promise<{ count: number; max_id: number | null }> {
|
||||
const row = await this.db()
|
||||
.selectFrom('paddle_features')
|
||||
.select((eb) => [eb.fn.countAll<number>().as('count'), eb.fn.max('id').as('max_id')])
|
||||
.executeTakeFirst();
|
||||
return {
|
||||
count: Number(row?.count ?? 0),
|
||||
max_id: row?.max_id === null || row?.max_id === undefined ? null : Number(row.max_id),
|
||||
};
|
||||
}
|
||||
|
||||
/** Drops a region's objects and writes the new read in their place; the caller holds the transaction. */
|
||||
async replaceRegion(regionId: string, rows: readonly PaddleFeatureRow[]): Promise<void> {
|
||||
await this.db().deleteFrom('paddle_features').where('region_id', '=', regionId).execute();
|
||||
|
||||
@@ -1,4 +1,98 @@
|
||||
import type { TourObstacleReviews } from '../entities/TourObstacleReviews.entity';
|
||||
import type { TourObstacleReviews, TTourObstacleReviewsDecision } from '../entities/TourObstacleReviews.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
export class TourObstacleReviewsRepository extends TrekRepository<TourObstacleReviews> {}
|
||||
/** `decision` narrowed to the four values the column's CHECK constraint allows. */
|
||||
type ReviewsDB = {
|
||||
tour_obstacle_reviews: Omit<DB['tour_obstacle_reviews'], 'decision'> & { decision: TTourObstacleReviewsDecision };
|
||||
places: DB['places'];
|
||||
};
|
||||
|
||||
/** One stored decision, its snapshot still JSON text and its time the stored text. */
|
||||
export interface TourObstacleReviewRow {
|
||||
place_id: number;
|
||||
obstacle_key: string;
|
||||
decision: TTourObstacleReviewsDecision;
|
||||
note: string | null;
|
||||
route_fingerprint: string;
|
||||
source_fingerprint: string;
|
||||
obstacle_snapshot: string;
|
||||
decided_at: string;
|
||||
decided_by: number | null;
|
||||
}
|
||||
|
||||
export type TourObstacleReviewWrite = Omit<TourObstacleReviewRow, 'place_id'>;
|
||||
|
||||
const COLUMNS = [
|
||||
'r.place_id',
|
||||
'r.obstacle_key',
|
||||
'r.decision',
|
||||
'r.note',
|
||||
'r.route_fingerprint',
|
||||
'r.source_fingerprint',
|
||||
'r.obstacle_snapshot',
|
||||
'r.decided_at',
|
||||
'r.decided_by',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* `tour_obstacle_reviews`: what the paddlers decided about the obstacles on a
|
||||
* tour (PAD-06), each bound to the route and the data it was decided on. A
|
||||
* decision goes with its tour; nothing here removes one the data no longer
|
||||
* holds.
|
||||
*/
|
||||
export class TourObstacleReviewsRepository extends TrekRepository<TourObstacleReviews> {
|
||||
private db() {
|
||||
return this.kysely<ReviewsDB>();
|
||||
}
|
||||
|
||||
async listForPlace(place_id: number): Promise<TourObstacleReviewRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('tour_obstacle_reviews as r')
|
||||
.select(COLUMNS)
|
||||
.where('r.place_id', '=', place_id)
|
||||
.orderBy('r.obstacle_key', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Every decision on the tours of one trip, for its tours list. */
|
||||
async listForTrip(trip_id: number): Promise<TourObstacleReviewRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('tour_obstacle_reviews as r')
|
||||
.innerJoin('places as p', 'p.id', 'r.place_id')
|
||||
.select(COLUMNS)
|
||||
.where('p.trip_id', '=', trip_id)
|
||||
.orderBy('r.place_id', 'asc')
|
||||
.orderBy('r.obstacle_key', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Writes decisions, each replacing the one stored for its obstacle; the caller holds the transaction. */
|
||||
async writeMany(place_id: number, rows: readonly TourObstacleReviewWrite[]): Promise<void> {
|
||||
await this.db()
|
||||
.insertInto('tour_obstacle_reviews')
|
||||
.values(rows.map((row) => ({ ...row, place_id })))
|
||||
.onConflict((oc) =>
|
||||
oc.columns(['place_id', 'obstacle_key']).doUpdateSet((eb) => ({
|
||||
decision: eb.ref('excluded.decision'),
|
||||
note: eb.ref('excluded.note'),
|
||||
route_fingerprint: eb.ref('excluded.route_fingerprint'),
|
||||
source_fingerprint: eb.ref('excluded.source_fingerprint'),
|
||||
obstacle_snapshot: eb.ref('excluded.obstacle_snapshot'),
|
||||
decided_at: eb.ref('excluded.decided_at'),
|
||||
decided_by: eb.ref('excluded.decided_by'),
|
||||
})),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Removes the decisions about the given obstacles; returns how many went. */
|
||||
async deleteKeys(place_id: number, keys: readonly string[]): Promise<number> {
|
||||
const result = await this.db()
|
||||
.deleteFrom('tour_obstacle_reviews')
|
||||
.where('place_id', '=', place_id)
|
||||
.where('obstacle_key', 'in', [...keys])
|
||||
.executeTakeFirst();
|
||||
return Number(result.numDeletedRows ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,10 @@ export interface TourPaddlePlanUpdate {
|
||||
duration: number | null;
|
||||
}
|
||||
|
||||
type ToursKyselyDB = Pick<DB, 'tours' | 'places' | 'day_assignments' | 'tour_waypoints' | 'tour_sources' | 'days'>;
|
||||
type ToursKyselyDB = Pick<
|
||||
DB,
|
||||
'tours' | 'places' | 'day_assignments' | 'tour_waypoints' | 'tour_sources' | 'days' | 'tour_obstacle_reviews'
|
||||
>;
|
||||
|
||||
/** One day a tour of the trip sits on, in the order of the trip. */
|
||||
export interface TourDayRow {
|
||||
@@ -260,6 +263,52 @@ export class ToursRepository extends TrekRepository<Tours> {
|
||||
* trip copy still says where its imports came from and when. Nothing happens
|
||||
* for a tour without one.
|
||||
*/
|
||||
/** The line of every paddle tour of a trip, for its check bench (PAD-09). */
|
||||
async listPaddleLinesForTrip(trip_id: number): Promise<Array<{ place_id: number; route_geometry: string | null }>> {
|
||||
return await this.db()
|
||||
.selectFrom('tours as t')
|
||||
.innerJoin('places as p', 'p.id', 't.place_id')
|
||||
.select(['t.place_id', 'p.route_geometry'])
|
||||
.where('p.trip_id', '=', trip_id)
|
||||
.where('t.tour_type', '=', 'kayak')
|
||||
.orderBy('t.place_id', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** A copied tour takes the decisions about its obstacles along, bound to what they were decided on. */
|
||||
async copyObstacleReviews(source_place_id: number, place_id: number): Promise<void> {
|
||||
await this.db()
|
||||
.insertInto('tour_obstacle_reviews')
|
||||
.columns([
|
||||
'place_id',
|
||||
'obstacle_key',
|
||||
'decision',
|
||||
'note',
|
||||
'route_fingerprint',
|
||||
'source_fingerprint',
|
||||
'obstacle_snapshot',
|
||||
'decided_at',
|
||||
'decided_by',
|
||||
])
|
||||
.expression((eb) =>
|
||||
eb
|
||||
.selectFrom('tour_obstacle_reviews')
|
||||
.select((inner) => [
|
||||
inner.val(place_id).as('place_id'),
|
||||
'obstacle_key',
|
||||
'decision',
|
||||
'note',
|
||||
'route_fingerprint',
|
||||
'source_fingerprint',
|
||||
'obstacle_snapshot',
|
||||
'decided_at',
|
||||
'decided_by',
|
||||
])
|
||||
.where('place_id', '=', source_place_id),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async copySource(source_place_id: number, place_id: number): Promise<void> {
|
||||
await this.db()
|
||||
.insertInto('tour_sources')
|
||||
|
||||
Reference in New Issue
Block a user