Files
TREK/server/src/nest/tours/tour-drafts.controller.ts
T
Maurice 3eac508baf fix(api): parse path ids with a rowId pipe that answers like an unknown id
RowIdPipe (src/nest/common/row-id.pipe.ts) parses a path param with toRowId. It answers a refused spelling with the route's own not-found answer, so '0x5', '5e0' and 'abc' get what an id that names no row gets. ParseRowIdPipe replaces Nest's ParseIntPipe with the same 400 and message: ParseIntPipe let '-1' and numbers past 2^53 through, and Postgres refuses the latter as out of range. Journey, journey recordings, day import, book design, map, public photo, collections, doc-sync, vacay, atlas and the plugin user-data export declare their ids through it. The upload routes parse in the handler with withRowId, so a refused id takes the existing cleanup path, or carry SpoolCleanupInterceptor (collections).
2026-11-18 13:20:05 +01:00

50 lines
2.0 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 { ParseRowIdPipe } from '../common/row-id.pipe';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { TourDraftPutDto } from './dto/tour-draft.dto';
import { TourDraftsService } from './tour-drafts.service';
import { Body, Controller, Delete, Get, Param, Put, UseGuards } from '@nestjs/common';
import { successResponseSchema, tourDraftResponseSchema } from '@trek/shared';
/**
* /api/trips/:tripId/tour-draft: the caller's own unsaved draft of the Tours
* planner in this trip. Reading it and letting it go need the trip; keeping
* one needs 'place_edit', as saving the tour it becomes does. Every route
* names the signed-in user, so a traveller only ever reaches their own draft.
*/
@Controller('api/trips/:tripId/tour-draft')
@UseGuards(AddonGuard, JwtAuthGuard, TripAccessGuard)
@RequireAddon(ADDON_IDS.TOURS, 'Tours')
export class TourDraftsController {
constructor(private readonly drafts: TourDraftsService) {}
@Get()
@ResponseContract(tourDraftResponseSchema)
async read(@CurrentUser() user: User, @Param('tripId', ParseRowIdPipe) tripId: number) {
return await this.drafts.read(tripId, user.id);
}
@RequirePermission('place_edit')
@Put()
@ResponseContract(tourDraftResponseSchema)
async keep(
@CurrentUser() user: User,
@Param('tripId', ParseRowIdPipe) tripId: number,
@Body() body: TourDraftPutDto,
) {
return await this.drafts.keep(tripId, user.id, body);
}
@Delete()
@ResponseContract(successResponseSchema)
async forget(@CurrentUser() user: User, @Param('tripId', ParseRowIdPipe) tripId: number) {
return await this.drafts.forget(tripId, user.id);
}
}