Files
TREK/server/src/nest/tours/tour-library.controller.ts
T
Maurice f6fd2722d0 feat(tours): take a tour over from another trip, server and contract (LIB-02)
GET /api/tours/library lists the tours of every trip the user owns or is a
member of, with the trip each is in and how many tours each trip holds, to
choose one to take over into the trip being planned. It is narrowed by a
part of the name (matched ignoring case, a "%" or "_" just a character), a
type or one trip, and the asking trip can be left out; up to 100 tours, and
the answer says when there were more. GET /api/tours/library/:placeId
previews one: its facts and its line thinned to 400 points, its ends kept.
Neither is trip-scoped, so every answer is bounded by the user's own trips,
read anew each call: a tour of a trip out of reach is "not found", nothing
hints that it exists. Measures stay null where the tour has none.

POST /api/trips/:tripId/tours/take-over copies one into this trip, which the
guard checks with 'place_edit'; the source trip is checked against the
user's trips as they are at that moment, and the source is read again inside
the write. The copy is a tour of its own: its line, measures, settings and
control points, without days, without its stops' links to places of the
other trip and without the obstacle decisions taken there; it keeps the
import record of its line. A copy that fails leaves nothing behind.

Where it came from is kept as text (tours.taken_over_from_trip_title and
taken_over_from_tour_name, migration 045100), not as a pointer, and read
back in the provenance as takenOverFrom; a copy of such a copy names it too.
The in-trip copy (P2C-07) and the take-over share one copy routine.

MCP: list_tour_library and take_over_tour, over the same service and
checks. The repository tests follow the hiking preferences column of MF-08.
2026-10-19 14:53:08 +02:00

64 lines
2.6 KiB
TypeScript

import { ADDON_IDS } from '../../addons';
import type { User } from '../../types';
import { AddonGuard } from '../addons/addon.guard';
import { RequireAddon } from '../addons/require-addon.decorator';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { ResponseContract } from '../common/response-contract';
import { toRowId } from '../common/row-id';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { TourLibraryQueryDto, TourTakeOverDto } from './dto/tour-library.dto';
import { TourLibraryService } from './tour-library.service';
import { Body, Controller, Get, Headers, NotFoundException, Param, Post, Query, UseGuards } from '@nestjs/common';
import { tourCreateResponseSchema, tourLibraryDetailResponseSchema, tourLibraryResponseSchema } from '@trek/shared';
/**
* LIB-02 /api/tours/library: the tours of every trip the user can open, to
* pick one to take over. Not trip-scoped, so there is no TripAccessGuard; the
* service bounds every answer by the user's own trips.
*/
@Controller('api/tours/library')
@UseGuards(AddonGuard, JwtAuthGuard)
@RequireAddon(ADDON_IDS.TOURS, 'Tours')
export class TourLibraryController {
constructor(private readonly library: TourLibraryService) {}
@Get()
@ResponseContract(tourLibraryResponseSchema)
async list(@CurrentUser() user: User, @Query() query: TourLibraryQueryDto) {
return await this.library.list(user.id, query);
}
@Get(':placeId')
@ResponseContract(tourLibraryDetailResponseSchema)
async detail(@CurrentUser() user: User, @Param('placeId') placeId: string) {
const id = toRowId(placeId);
if (id === null) throw new NotFoundException('Tour not found');
return await this.library.detail(user.id, id);
}
}
/**
* LIB-02 POST /api/trips/:tripId/tours/take-over: a tour of another trip the
* user can open, copied into this one. The guard checks this trip and
* 'place_edit' on it; the service checks the source trip.
*/
@Controller('api/trips/:tripId/tours')
@UseGuards(AddonGuard, JwtAuthGuard, TripAccessGuard)
@RequireAddon(ADDON_IDS.TOURS, 'Tours')
export class TourTakeOverController {
constructor(private readonly library: TourLibraryService) {}
@RequirePermission('place_edit')
@Post('take-over')
@ResponseContract(tourCreateResponseSchema)
async takeOver(
@Param('tripId') tripId: string,
@CurrentUser() user: User,
@Body() body: TourTakeOverDto,
@Headers('x-socket-id') socketId?: string,
) {
return await this.library.takeOver(tripId, user.id, body, socketId);
}
}