diff --git a/src/participante_evento/participante_evento.service.ts b/src/participante_evento/participante_evento.service.ts index 548e2e6..99a1dcb 100644 --- a/src/participante_evento/participante_evento.service.ts +++ b/src/participante_evento/participante_evento.service.ts @@ -188,7 +188,7 @@ export class ParticipanteEventoService { */ async registrarAsistenciaConToken(qrToken: string) { // Validar el token QR - const tokenData = this.qrTokenService.validateQrToken(qrToken); + const tokenData = await this.qrTokenService.validateQrToken(qrToken); if (!tokenData) { throw new HttpException( @@ -196,7 +196,7 @@ export class ParticipanteEventoService { HttpStatus.BAD_REQUEST, ); } - + const { id_participante, id_evento, id_cuestionario } = tokenData; // Verificar que el participante existe diff --git a/src/qr/qr-token.service.ts b/src/qr/qr-token.service.ts index e5243c4..1317541 100644 --- a/src/qr/qr-token.service.ts +++ b/src/qr/qr-token.service.ts @@ -1,12 +1,17 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, UnauthorizedException } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Asistencia } from 'src/asistencia/entities/asistencia.entity'; @Injectable() export class QrTokenService { constructor( private jwtService: JwtService, private configService: ConfigService, + @InjectRepository(Asistencia) + private readonly asistenciaRepository: Repository, ) {} /** @@ -45,13 +50,14 @@ export class QrTokenService { * @param token Token JWT a validar * @returns Datos decodificados del token o null si es inválido */ - validateQrToken(token: string): { - id_participante: number; + async validateQrToken(token: string): Promise<{ + id_participante + : number; id_evento: number; id_cuestionario: number; type: string; iat: number; - } | null { + } | null >{ try { const secret = this.configService.get('QR_JWT_SECRET') || @@ -61,6 +67,23 @@ export class QrTokenService { const decoded = this.jwtService.verify(token, { secret }); + let asistenciaEvento = await this.asistenciaRepository.findOne({ + where: { + id_participante: decoded.id_participante, + id_evento: decoded.id_evento, + } + }) + if(!asistenciaEvento){ + let newAsistencia=this.asistenciaRepository.create({estado:false,id_evento:decoded.id_evento, id_participante:decoded.id_participante}) + asistenciaEvento= await this.asistenciaRepository.save(newAsistencia) + } + + if (!asistenciaEvento?.estado) { + asistenciaEvento.estado = true + }else{ + throw new UnauthorizedException("El usuario ya se registró"); + } + // Verificar que sea un token de tipo QR de asistencia if (decoded.type !== 'qr_asistencia') { return null; diff --git a/src/qr/qr.module.ts b/src/qr/qr.module.ts index e7471b9..1bfdb76 100644 --- a/src/qr/qr.module.ts +++ b/src/qr/qr.module.ts @@ -6,9 +6,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Qr } from './qr.entity'; import { AuthModule } from '../auth/auth.module'; import { ConfigModule } from '@nestjs/config'; +import { ParticipanteEvento } from 'src/participante_evento/entities/participante_evento.entity'; @Module({ - imports: [TypeOrmModule.forFeature([Qr]), AuthModule, ConfigModule], + imports: [TypeOrmModule.forFeature([Qr]), AuthModule, ConfigModule, ParticipanteEvento], controllers: [QrController], providers: [QrService, QrTokenService], exports: [QrTokenService],