Files
TREK/server/src/nest/wallet-pass/wallet-pass.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

64 lines
2.6 KiB
TypeScript

import type { User } from '../../types';
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 { TripAccessGuard } from '../permissions/trip-access.guard';
import { WalletPassService } from './wallet-pass.service';
import { walletLanguage } from './wallet-pass.words';
import { Controller, Get, Param, Query, Res, UseGuards } from '@nestjs/common';
import {
googleWalletAvailabilityResponseSchema,
googleWalletSaveResponseSchema,
type GoogleWalletAvailabilityResponse,
type GoogleWalletSaveResponse,
} from '@trek/shared';
import type { Response } from 'express';
/**
* GET /api/trips/:tripId/reservations/:id/wallet-pass: the booking's Apple
* Wallet pass, which the iOS app hands to Wallet's own sheet (native/wallet.ts).
* GET .../wallet-pass/google: the same booking as a Google Wallet save link,
* which the Android app opens. GET .../google-wallet: whether that works here
* at all, so the app offers the button only where it does.
*/
@Controller('api/trips/:tripId/reservations')
// TripAccessGuard resolves :tripId and 404s a trip the user cannot reach.
@UseGuards(JwtAuthGuard, TripAccessGuard)
export class WalletPassController {
constructor(private readonly passes: WalletPassService) {}
// response-contract-exempt: answers the signed .pkpass bytes through @Res(), not JSON.
@Get(':id/wallet-pass')
async pass(
@CurrentUser() user: User,
@Param('tripId', ParseRowIdPipe) tripId: number,
@Param('id', ParseRowIdPipe) id: number,
@Query('lang') lang: string | undefined,
@Res() res: Response,
): Promise<void> {
const pass = await this.passes.passFor(user.id, tripId, id, walletLanguage(lang));
res.set('Cache-Control', 'no-store');
res.set('Content-Disposition', `attachment; filename="${pass.filename}"`);
res.status(200).type('application/vnd.apple.pkpass').send(pass.bytes);
}
@Get('google-wallet')
@ResponseContract(googleWalletAvailabilityResponseSchema)
async googleAvailability(): Promise<GoogleWalletAvailabilityResponse> {
return { available: await this.passes.googleAvailable() };
}
@Get(':id/wallet-pass/google')
@ResponseContract(googleWalletSaveResponseSchema)
async google(
@CurrentUser() user: User,
@Param('tripId', ParseRowIdPipe) tripId: number,
@Param('id', ParseRowIdPipe) id: number,
@Query('lang') lang: string | undefined,
): Promise<GoogleWalletSaveResponse> {
return { saveUrl: await this.passes.googleSaveUrlFor(user.id, tripId, id, walletLanguage(lang)) };
}
}