mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(places): make the kind of stop editable, and enforce it on the way in
Three holes, one seam. Leaving the corridor popup by "more details" dropped the kind and the dwell it had just worked out, so a fuel stop silently became a numbered destination that counted in every total, in the badge and in the PDF. Once set, the kind could never be changed back through the web UI at all. And the REST route did not validate the field: both request schemas are open objects, so stop_type: 'banana' was written straight to the column, where isServiceStopType read it as false and the stop counted as a destination again. The form now carries the field, so the popup's answer survives the trip through it, an ordinary place can become a service stop, and a wrong kind can be taken off. Editing keeps stop_type and deliberately leaves duration_minutes out: how long a stay takes belongs to the rail's own dialog, and sending it from here would overwrite what was set there. The schemas gain the one field rather than closing: roughly 190 call sites post whatever their form holds, and a closed object would reject all of them at once. The doc comment claiming the list was already enforced is corrected — it was describing an intention. The two driving findings move into the stop's badge row in warning colours, beside the stay and the walk from the road. All three say what this stop costs, and one row reads as one answer rather than as notes stacked underneath.
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
placeBulkDeleteRequestSchema,
|
||||
placeImportListRequestSchema,
|
||||
placeSchema,
|
||||
placeUpdateRequestSchema,
|
||||
} from './place.schema';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
@@ -52,3 +53,27 @@ describe('placeImportListRequestSchema', () => {
|
||||
expect(placeImportListRequestSchema.safeParse({ url: '' }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stop_type on the write routes', () => {
|
||||
it('SHARED-PLACE-020: a known kind goes through, on create and on update', () => {
|
||||
expect(placeCreateRequestSchema.safeParse({ name: 'A', stop_type: 'fuel' }).success).toBe(true);
|
||||
expect(placeUpdateRequestSchema.safeParse({ stop_type: 'charging' }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('SHARED-PLACE-021: an invented kind is refused rather than written to the column', () => {
|
||||
// It used to go straight through: both schemas are open objects, so the value landed
|
||||
// in the column and isServiceStopType then read it as false, which quietly turned a
|
||||
// pause into a numbered destination.
|
||||
expect(placeCreateRequestSchema.safeParse({ name: 'A', stop_type: 'banana' }).success).toBe(false);
|
||||
expect(placeUpdateRequestSchema.safeParse({ stop_type: 'banana' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('SHARED-PLACE-022: null is how a stop stops being a fuel stop', () => {
|
||||
expect(placeUpdateRequestSchema.safeParse({ stop_type: null }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('SHARED-PLACE-023: every other key stays open, because roughly 190 callers rely on it', () => {
|
||||
const body = { name: 'A', whatever: 1, notes: 'x', lat: 53.5, tags: [{ id: 1 }] };
|
||||
expect(placeCreateRequestSchema.safeParse(body).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -79,10 +79,17 @@ export const placeRatingVoteSchema = z.object({
|
||||
export type PlaceRatingVote = z.infer<typeof placeRatingVoteSchema>;
|
||||
|
||||
/**
|
||||
* The kinds of stop a drive has, matching the categories the corridor search can look for
|
||||
* (`client/src/components/Map/poiCategories.ts` and the OSM tag map in
|
||||
* `server/src/nest/maps/maps.helpers.ts`). Kept as a closed list here so a typo cannot
|
||||
* reach the column, and extended alongside those two when the search learns a new kind.
|
||||
* The kinds of stop a drive has.
|
||||
*
|
||||
* The client reads the same six out of one table (`client/src/components/Roadtrip/
|
||||
* stopKinds.ts`), which carries their icon, colour and usual length; that table is pinned
|
||||
* against this enum by its own test, because it cannot import from here without dragging
|
||||
* React into the shared package.
|
||||
*
|
||||
* A closed list, and enforced as one on both write routes below. It was not: the request
|
||||
* schemas are open objects, so a POST with an invented kind used to be written straight
|
||||
* to the column, where `isServiceStopType` then read it as false and the stop quietly
|
||||
* counted as a destination.
|
||||
*/
|
||||
export const roadtripStopTypeSchema = z.enum(['fuel', 'charging', 'rest_area', 'campsite', 'restaurant', 'sights']);
|
||||
export type RoadtripStopType = z.infer<typeof roadtripStopTypeSchema>;
|
||||
@@ -173,10 +180,23 @@ export const assignmentPlaceSchema = z.object({
|
||||
});
|
||||
export type AssignmentPlace = z.infer<typeof assignmentPlaceSchema>;
|
||||
|
||||
export const placeCreateRequestSchema = open.and(z.object({ name: z.string().min(1) }));
|
||||
/**
|
||||
* Additive on one field, deliberately not a closed object.
|
||||
*
|
||||
* Both request schemas stay open because roughly 190 call sites still post whatever the
|
||||
* form happens to hold, and closing them would reject all of them at once. Naming
|
||||
* `stop_type` on top of the open object validates that one key and leaves every other
|
||||
* untouched, which is the only part of the body with a fixed vocabulary.
|
||||
*
|
||||
* Nullable: an explicit null is how a fuel stop becomes an ordinary place again, and the
|
||||
* service already reads it that way rather than as "leave alone".
|
||||
*/
|
||||
const stopTypeField = z.object({ stop_type: roadtripStopTypeSchema.nullable().optional() });
|
||||
|
||||
export const placeCreateRequestSchema = open.and(z.object({ name: z.string().min(1) })).and(stopTypeField);
|
||||
export type PlaceCreateRequest = z.infer<typeof placeCreateRequestSchema>;
|
||||
|
||||
export const placeUpdateRequestSchema = open;
|
||||
export const placeUpdateRequestSchema = open.and(stopTypeField);
|
||||
export type PlaceUpdateRequest = z.infer<typeof placeUpdateRequestSchema>;
|
||||
|
||||
// Collaborative ratings (#1435): one 1-5 star vote per user and place.
|
||||
|
||||
Reference in New Issue
Block a user