Files
formularios_api/src/qr/qr.controller.ts
T

123 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-04-02 13:54:50 -06:00
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Patch,
Post,
Query,
} from '@nestjs/common';
2025-04-01 15:07:11 -06:00
import { QrService } from './qr.service';
import { QrTokenService } from './qr-token.service';
2025-04-01 15:07:11 -06:00
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';
2025-04-01 15:07:11 -06:00
@ApiTags('QR')
2025-04-01 15:07:11 -06:00
@Controller('qr')
export class QrController {
constructor(
private qrService: QrService,
private qrTokenService: QrTokenService,
) {}
2025-04-01 15:07:11 -06:00
2025-04-02 13:54:50 -06:00
@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<string> {
return this.qrService.generateQRCode(text);
}
2025-04-01 15:07:11 -06:00
@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<string> {
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,
},
};
}
2025-04-02 13:54:50 -06:00
@Get()
getQrs(): Promise<Qr[]> {
return this.qrService.getQrs();
}
2025-04-01 15:07:11 -06:00
2025-04-02 13:54:50 -06:00
@Get(':id')
getQr(@Param('id', ParseIntPipe) id: number) {
return this.qrService.getQr(id);
}
2025-04-01 15:07:11 -06:00
2025-04-02 13:54:50 -06:00
@Post() //en el body ValidationPipe
createQr(@Body() newQr: CreateQrDto) {
return this.qrService.createQr(newQr);
}
2025-04-01 15:07:11 -06:00
2025-04-02 13:54:50 -06:00
@Delete(':id')
deleteQr(@Param('id', ParseIntPipe) id: number) {
return this.qrService.deleteQr(id);
}
2025-04-01 15:07:11 -06:00
2025-04-02 13:54:50 -06:00
@Patch(':id')
updateQr(@Param('id', ParseIntPipe) id: number, @Body() qr: UpdateQrDto) {
return this.qrService.updateQr(id, qr);
}
2025-04-01 15:07:11 -06:00
}