import { Body, Controller, Get, HttpCode, Param, Post, Query, UseGuards } from '@nestjs/common'; import { Throttle } from '@nestjs/throttler'; import { IsIn, IsInt, IsOptional, IsString, Max, Min } from 'class-validator'; import { CurrentUser, JwtAuthGuard, StaffGuard } from '../common/guards'; import { JwtPayload } from '../common/auth.types'; import { CounterService, StaffContext } from './counter.service'; class CreditDto { /** Absent en mode tampons. */ @IsOptional() @IsInt() @Min(1) @Max(1_000_000) amountCents?: number; } class RedeemDto { @IsString() rewardId!: string; } class HistoryQuery { @IsOptional() @IsIn(['today', 'week']) period?: 'today' | 'week'; @IsOptional() @IsString() cursor?: string; } function ctx(user: JwtPayload): StaffContext { if (user.kind !== 'staff') throw new Error('StaffGuard manquant'); return { staffId: user.sub, storeId: user.storeId, role: user.role }; } /** API de l'app Pro (employés au comptoir). */ @Controller('staff') @UseGuards(JwtAuthGuard, StaffGuard) export class CounterController { constructor(private counter: CounterService) {} @Get('me') me(@CurrentUser() user: JwtPayload) { return this.counter.me(ctx(user)); } @Get('summary') summary(@CurrentUser() user: JwtPayload) { return this.counter.summary(ctx(user)); } @Get('cards/:serial') @Throttle({ default: { limit: 60, ttl: 60_000 } }) lookup(@CurrentUser() user: JwtPayload, @Param('serial') serial: string) { return this.counter.lookup(ctx(user), serial); } @Post('cards/:serial/credit') @HttpCode(200) credit(@CurrentUser() user: JwtPayload, @Param('serial') serial: string, @Body() dto: CreditDto) { return this.counter.credit(ctx(user), serial, dto.amountCents); } @Post('cards/:serial/redeem') @HttpCode(200) redeem(@CurrentUser() user: JwtPayload, @Param('serial') serial: string, @Body() dto: RedeemDto) { return this.counter.redeem(ctx(user), serial, dto.rewardId); } @Post('cards/:serial/gifts/:giftId/use') @HttpCode(200) useGift(@CurrentUser() user: JwtPayload, @Param('serial') serial: string, @Param('giftId') giftId: string) { return this.counter.useGift(ctx(user), serial, giftId); } @Get('transactions') history(@CurrentUser() user: JwtPayload, @Query() q: HistoryQuery) { return this.counter.history(ctx(user), q.period ?? 'today', q.cursor); } }