Files
TREK/server/src/nest/tours/tours.controller.ts
T
Maurice 3f38621024 feat(server): let a trip owner switch trip addons for one trip
A trip can now turn a trip addon (lists, costs, documents, collab, road
trip, tours) on or off for itself, whatever the instance switch says. No
row in the new trip_addon_overrides table means the trip follows the
instance; a row is the owner's choice. Going back to the instance setting
deletes the row, and no addon data is touched either way.

- GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the
  owner writes) and the MCP tools get_trip_addons and set_trip_addon, on
  the shared tripAddons contract; tripAddons:changed tells the members.
- Every trip-scoped addon surface asks the trip: REST through
  TripAddonGuard, MCP through a per-call gate on the named or pinned trip,
  plugin RPC and events, the share link, the offline bundle, reminders,
  doc sync, the assistant (tools, keep_attached_file), booking costs, the
  trip summary prompt, and the cross-trip tour reads (library, Atlas
  stats, collection save).
- Routes without a trip open while the addon is on in any of the user's
  trips. Switching an addon on that the instance has off ends the members'
  MCP sessions so they reconnect with its tools.
- A copied trip takes its owner's choices along.

Behaviour changes: an anonymous call to an addon route that is off now
gets 401 instead of 404, and with an instance switch off the packing,
todo and files plugin RPC methods, the files MCP tools, todo reminder
mails, core plugin events, plugin view contributions, the share link's
lists, costs and chat sections and the offline bundle's lists follow it.
2026-11-21 13:23:09 +01:00

144 lines
5.5 KiB
TypeScript

import { ADDON_IDS } from '../../addons';
import type { User } from '../../types';
import { RequireAddon } from '../addons/require-addon.decorator';
import { TripAddonGuard } from '../addons/trip-addon.guard';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { contentDisposition } from '../common/content-disposition';
import { ResponseContract } from '../common/response-contract';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { TourCopyDto, TourCreateDto } from './dto/tour-create.dto';
import { TourObstacleReviewsDto } from './dto/tour-obstacle-reviews.dto';
import { TourPaddlePlanDto } from './dto/tour-paddle-plan.dto';
import { TourGpxExportService } from './tour-gpx-export.service';
import { TourObstacleReviewsService } from './tour-obstacle-reviews.service';
import { TourPaddlePlanService } from './tour-paddle-plan.service';
import { ToursService } from './tours.service';
import { Body, Controller, Get, Headers, HttpCode, Param, Post, Put, Res, UseGuards } from '@nestjs/common';
import { tourCreateResponseSchema, tourDetailResponseSchema } from '@trek/shared';
import type { Response } from 'express';
/**
* /api/trips/:tripId/tours: the Tours facet, read and route edits.
* TripAccessGuard resolves
* :tripId (404 for no access); writes require 'place_edit'. Assignments stay
* on the existing day_edit endpoints. The multipart GPX import lives in
* ToursImportController, which checks the same rules in its handler.
*/
@Controller('api/trips/:tripId/tours')
@UseGuards(JwtAuthGuard, TripAccessGuard, TripAddonGuard)
@RequireAddon(ADDON_IDS.TOURS, 'Tours')
export class ToursController {
constructor(
private readonly tours: ToursService,
private readonly paddlePlans: TourPaddlePlanService,
private readonly obstacleReviews: TourObstacleReviewsService,
private readonly gpxExport: TourGpxExportService,
) {}
@Get()
async list(@Param('tripId') tripId: string) {
return { tours: await this.tours.listTours(tripId) };
}
/**
* PR1-06A: the saved tour as a GPX file, its whole line with the heights it
* has. A read, so seeing the trip is enough; 422 when the tour has no line.
* response-contract-exempt: a file download written through @Res(), not a JSON body.
*/
@Get(':placeId/export.gpx')
async exportGpx(@Param('tripId') tripId: string, @Param('placeId') placeId: string, @Res() res: Response) {
const { gpx, filename } = await this.gpxExport.export(tripId, placeId);
res.setHeader('Content-Type', 'application/gpx+xml; charset=utf-8');
res.setHeader('Content-Disposition', contentDisposition(filename, 'attachment'));
res.send(gpx);
}
@Get(':placeId')
async detail(@Param('tripId') tripId: string, @Param('placeId') placeId: string) {
return await this.tours.getTour(tripId, placeId);
}
@RequirePermission('place_edit')
@Post()
async create(
@Param('tripId') tripId: string,
@Body() body: TourCreateDto,
@Headers('x-socket-id') socketId?: string,
) {
return await this.tours.createTour(tripId, body, socketId);
}
@RequirePermission('place_edit')
@Put(':placeId')
async update(
@Param('tripId') tripId: string,
@Param('placeId') placeId: string,
@Body() body: TourCreateDto,
@Headers('x-socket-id') socketId?: string,
) {
return await this.tours.updateTour(tripId, placeId, body, socketId);
}
/** P2C-07: the tour saved again as a new, independent tour of the trip, without its days. */
@RequirePermission('place_edit')
@Post(':placeId/copy')
@ResponseContract(tourCreateResponseSchema)
async copy(
@Param('tripId') tripId: string,
@Param('placeId') placeId: string,
@Body() body: TourCopyDto,
@Headers('x-socket-id') socketId?: string,
) {
return await this.tours.copyTour(tripId, placeId, body.name, socketId);
}
/**
* PAD-03/PAD-07: the portages, locks and speeds of a recorded paddle track,
* laid onto it without redrawing it. A planned tour saves these with its route.
*/
@RequirePermission('place_edit')
@Put(':placeId/paddle-plan')
@ResponseContract(tourDetailResponseSchema)
async updatePaddlePlan(
@Param('tripId') tripId: string,
@Param('placeId') placeId: string,
@Body() body: TourPaddlePlanDto,
@Headers('x-socket-id') socketId?: string,
) {
return await this.paddlePlans.update(tripId, placeId, body, socketId);
}
/**
* PAD-06: what the paddlers decided about the obstacles of a paddle tour,
* each bound to the route and the data the user saw. Answers the detail
* with its checklist; 503 while the obstacle data cannot be read.
*/
@RequirePermission('place_edit')
@Put(':placeId/obstacle-reviews')
@ResponseContract(tourDetailResponseSchema)
async saveObstacleReviews(
@Param('tripId') tripId: string,
@Param('placeId') placeId: string,
@Body() body: TourObstacleReviewsDto,
@CurrentUser() user: User,
@Headers('x-socket-id') socketId?: string,
) {
return await this.obstacleReviews.save(tripId, placeId, body, user.id, socketId);
}
/** Takes an import planned over in place back to its recorded track; the planning is dropped. */
@RequirePermission('place_edit')
@Post(':placeId/restore-recorded')
@HttpCode(200)
@ResponseContract(tourDetailResponseSchema)
async restoreRecorded(
@Param('tripId') tripId: string,
@Param('placeId') placeId: string,
@Headers('x-socket-id') socketId?: string,
) {
return await this.tours.restoreRecordedTrack(tripId, placeId, socketId);
}
}