import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query, } from '@nestjs/common'; import { QrService } from './qr.service'; import { QrTokenService } from './qr-token.service'; import { Qr } from './qr.entity'; import { CreateQrDto } from './dto/create-qr.dto'; import { UpdateQrDto } from './dto/update.qr.dto'; import { ValidateQrTokenDto } from './dto/validate-qr-token.dto'; import { ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; @ApiTags('QR') @Controller('qr') export class QrController { constructor( private qrService: QrService, private qrTokenService: QrTokenService, ) {} @Get('generate') @ApiOperation({ summary: 'Genera un código QR a partir de un texto' }) @ApiQuery({ name: 'text', required: true, description: 'Texto a codificar en el QR', }) async generateQRCode(@Query('text') text: string): Promise { return this.qrService.generateQRCode(text); } @Get('asistencia/:idParticipante/:idEvento') @ApiOperation({ summary: 'Genera un código QR para asistencia con IDs de participante y evento', }) @ApiParam({ name: 'idParticipante', description: 'ID del participante' }) @ApiParam({ name: 'idEvento', description: 'ID del evento' }) async generateAsistenciaQR( @Param('idParticipante', ParseIntPipe) idParticipante: number, @Param('idEvento', ParseIntPipe) idEvento: number, ): Promise { return this.qrService.generateAsistenciaQR(idParticipante, idEvento); } @Post('validate-token') @ApiOperation({ summary: 'Valida un token de QR para asistencia' }) async validateQrToken(@Body() validateDto: ValidateQrTokenDto) { const tokenData = this.qrTokenService.validateQrToken(validateDto.token); if (!tokenData) { return { valid: false, message: 'Token inválido o expirado', }; } return { valid: true, data: tokenData, message: 'Token válido', }; } @Get('generate-token/:idParticipante/:idEvento/:idCuestionario') @ApiOperation({ summary: 'Genera un token JWT para QR de asistencia' }) @ApiParam({ name: 'idParticipante', description: 'ID del participante' }) @ApiParam({ name: 'idEvento', description: 'ID del evento' }) @ApiParam({ name: 'idCuestionario', description: 'ID del cuestionario' }) async generateQrToken( @Param('idParticipante', ParseIntPipe) idParticipante: number, @Param('idEvento', ParseIntPipe) idEvento: number, @Param('idCuestionario', ParseIntPipe) idCuestionario: number, ) { const token = this.qrTokenService.generateQrToken( idParticipante, idEvento, idCuestionario, ); return { token, qr_data: { id_participante: idParticipante, id_evento: idEvento, id_cuestionario: idCuestionario, }, }; } @Get() getQrs(): Promise { return this.qrService.getQrs(); } @Get(':id') getQr(@Param('id', ParseIntPipe) id: number) { return this.qrService.getQr(id); } @Post() //en el body ValidationPipe createQr(@Body() newQr: CreateQrDto) { return this.qrService.createQr(newQr); } @Delete(':id') deleteQr(@Param('id', ParseIntPipe) id: number) { return this.qrService.deleteQr(id); } @Patch(':id') updateQr(@Param('id', ParseIntPipe) id: number, @Body() qr: UpdateQrDto) { return this.qrService.updateQr(id, qr); } }