fix(tours): one tour type catalogue, and no column nothing writes

tour_types now holds every key the shared contract accepts, all but hike
disabled, and create and edit turn a disabled type away with a 400 instead
of failing on the foreign key. tours.tour_group_id is dropped: nothing ever
wrote or read it. difficulty and wanderer_ref stay for the Wanderer import.
This commit is contained in:
Maurice
2026-10-07 20:54:10 +02:00
parent f64f9e58de
commit f7edd849ab
29 changed files with 182 additions and 50 deletions
-2
View File
@@ -17,7 +17,6 @@ export class Tours {
difficulty?: string | null;
wanderer_ref?: string | null;
match_confidence?: number | null;
tour_group_id?: number | null;
created_at?: string | null;
max_hiking_difficulty: number & Opt = 2;
tourTypeRef!: Ref<TourTypes>;
@@ -38,7 +37,6 @@ export const ToursSchema = defineEntity({
difficulty: p.text().nullable(),
wanderer_ref: p.text().nullable(),
match_confidence: p.double().nullable(),
tour_group_id: p.integer().nullable(),
created_at: p.text().nullable().defaultRaw(`CURRENT_TIMESTAMP`),
max_hiking_difficulty: p.integer().default(2),
tourTypeRef: () => p.manyToOne(TourTypes).ref().joinColumn('tour_type').hidden().index('idx_tours_tour_type'),
@@ -0,0 +1,33 @@
import { Migration } from '@mikro-orm/migrations';
import { columnNames } from '../migration-utils';
/**
* `tour_types` holds every key the shared `tourTypeKeySchema` accepts. Only
* `hike` was seeded, so a tour of any other type the contract allows failed
* on the foreign key with a 500 instead of a clean refusal. The six others
* are written disabled: the planner only offers `hike` today, and the
* service turns a disabled type away with a 400.
*
* `tours.tour_group_id` goes. Nothing ever wrote or read it, and a column
* nothing writes lets every copy and export carry a meaning nobody defined.
* `difficulty` and `wanderer_ref` stay: the Wanderer import fills them.
*/
export class Migration20200101042300_every_tour_type_the_contract_names extends Migration {
override name = 'Migration20200101042300_every_tour_type_the_contract_names';
override async up(): Promise<void> {
this.addSql(`
INSERT OR IGNORE INTO tour_types (key, label_key, icon, color, routing_profile, is_sport, enabled, sort_order) VALUES
('bike', 'tourTypes.bike', 'Bike', '#2563eb', 'bicycle', 1, 0, 1),
('run', 'tourTypes.run', 'Footprints', '#dc2626', 'pedestrian', 1, 0, 2),
('ski', 'tourTypes.ski', 'MountainSnow', '#0891b2', NULL, 1, 0, 3),
('kayak', 'tourTypes.kayak', 'Sailboat', '#0d9488', NULL, 1, 0, 4),
('walk', 'tourTypes.walk', 'Landmark', '#9333ea', 'pedestrian', 0, 0, 5),
('food', 'tourTypes.food', 'UtensilsCrossed', '#ea580c', 'pedestrian', 0, 0, 6)
`);
if ((await columnNames(this, 'tours')).has('tour_group_id')) {
this.addSql('ALTER TABLE tours DROP COLUMN tour_group_id');
}
}
}
@@ -2,8 +2,14 @@ import type { TourTypes } from '../entities/TourTypes.entity';
import { TrekRepository } from './_shared/trek-repository';
/**
* `tour_types` — the controlled vocabulary `tours.tour_type` points at. Only
* `hike` exists, written by the migration that creates the table; nothing
* reads the catalogue yet, so this repository has no methods of its own.
* `tour_types` — the controlled vocabulary `tours.tour_type` points at, one
* row per key of the shared `tourTypeKeySchema`. The migrations write it;
* `enabled` says which types a tour may be created with.
*/
export class TourTypesRepository extends TrekRepository<TourTypes> {}
export class TourTypesRepository extends TrekRepository<TourTypes> {
/** Whether `key` is a known tour type a tour may be created with. */
async isEnabled(key: string): Promise<boolean> {
const row = await this.findOne({ key }, { fields: ['enabled'] });
return row?.enabled === 1;
}
}
@@ -19,7 +19,6 @@ export interface TourListRow {
difficulty: string | null;
wanderer_ref: string | null;
match_confidence: number | null;
tour_group_id: number | null;
max_hiking_difficulty: number;
planned: number;
has_waypoints: number;
@@ -36,7 +35,6 @@ export interface TourRow {
difficulty: string | null;
wanderer_ref: string | null;
match_confidence: number | null;
tour_group_id: number | null;
created_at: string | null;
max_hiking_difficulty: number;
}
@@ -95,7 +93,6 @@ export class ToursRepository extends TrekRepository<Tours> {
't.difficulty as difficulty',
't.wanderer_ref as wanderer_ref',
't.match_confidence as match_confidence',
't.tour_group_id as tour_group_id',
't.max_hiking_difficulty as max_hiking_difficulty',
eb.exists(eb.selectFrom('day_assignments as da').select('da.id').whereRef('da.place_id', '=', 'p.id')).as('planned'),
eb.exists(eb.selectFrom('tour_waypoints as tw').select('tw.id').whereRef('tw.place_id', '=', 'p.id')).as('has_waypoints'),
+2 -1
View File
@@ -2,6 +2,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common';
import { Places } from '../../db/entities/Places.entity';
import { Tours } from '../../db/entities/Tours.entity';
import { TourTypes } from '../../db/entities/TourTypes.entity';
import { TourWaypoints } from '../../db/entities/TourWaypoints.entity';
import { ToursController } from './tours.controller';
import { ToursImportController } from './tours-import.controller';
@@ -24,7 +25,7 @@ import { AddonsModule } from '../addons/addons.module';
PermissionsModule,
AuthModule,
AddonsModule,
MikroOrmModule.forFeature([Tours, TourWaypoints, Places]),
MikroOrmModule.forFeature([Tours, TourTypes, TourWaypoints, Places]),
],
controllers: [ToursController, ToursImportController],
providers: [ToursService],
+15 -3
View File
@@ -1,11 +1,13 @@
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import type { TourCreateRequest, TourCreateResponse, TourDetailResponse, TourListItem, TourWaypoint } from '@trek/shared';
import { Places } from '../../db/entities/Places.entity';
import { Tours } from '../../db/entities/Tours.entity';
import { TourTypes } from '../../db/entities/TourTypes.entity';
import { TourWaypoints } from '../../db/entities/TourWaypoints.entity';
import type { PlacesRepository } from '../../db/repositories/Places.repository';
import type { TourListRow, ToursRepository, TourUpdate } from '../../db/repositories/Tours.repository';
import type { TourTypesRepository } from '../../db/repositories/TourTypes.repository';
import type { TourWaypointsRepository } from '../../db/repositories/TourWaypoints.repository';
import { toRowId } from '../common/row-id';
import { UnitOfWork } from '../database/unit-of-work';
@@ -33,6 +35,7 @@ export class ToursService {
private readonly uow: UnitOfWork,
private readonly places: PlacesService,
@InjectRepository(Tours) private readonly toursRepo: ToursRepository,
@InjectRepository(TourTypes) private readonly tourTypesRepo: TourTypesRepository,
@InjectRepository(TourWaypoints) private readonly waypointsRepo: TourWaypointsRepository,
@InjectRepository(Places) private readonly placesRepo: PlacesRepository,
) {}
@@ -49,7 +52,6 @@ export class ToursService {
difficulty: r.difficulty,
wanderer_ref: r.wanderer_ref,
match_confidence: r.match_confidence,
tour_group_id: r.tour_group_id,
max_hiking_difficulty: r.max_hiking_difficulty ?? 2,
planned: Boolean(r.planned),
caution: r.match_confidence !== null && r.match_confidence < LOW_CONFIDENCE_THRESHOLD,
@@ -71,6 +73,15 @@ export class ToursService {
};
}
/**
* `tour_types` knows every key the contract accepts, most of them disabled.
* Checked before the write, so a disabled type is a 400 and not a tour the
* planner cannot show.
*/
private async assertTourTypeEnabled(key: string): Promise<void> {
if (!(await this.tourTypesRepo.isEnabled(key))) throw new BadRequestException('Tour type is not available');
}
/** All tours (the facet + owning place) for a trip, newest first. */
async listTours(tripId: string): Promise<TourListItem[]> {
const tid = toRowId(tripId);
@@ -111,6 +122,7 @@ export class ToursService {
async createTour(tripId: string, input: TourCreateRequest, socketId?: string): Promise<TourCreateResponse> {
// The controller's TripAccessGuard already resolved this trip id.
const tid = toRowId(tripId)!;
await this.assertTourTypeEnabled(input.tour_type);
const start = input.route_geometry[0];
const fields = this.routeFields(input);
@@ -144,6 +156,7 @@ export class ToursService {
const tid = toRowId(tripId);
const pid = toRowId(placeId);
if (tid === null || pid === null) throw new NotFoundException('Tour not found');
await this.assertTourTypeEnabled(input.tour_type);
const start = input.route_geometry[0];
const fields = this.routeFields(input);
@@ -226,7 +239,6 @@ export class ToursService {
difficulty: null,
wanderer_ref: null,
match_confidence: matchConfidence,
tour_group_id: null,
max_hiking_difficulty: 2,
planned: 0,
has_waypoints: 0,