Correcion de error al tomar asistencia con codigo qr
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<Asistencia>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -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<string>('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;
|
||||
|
||||
+2
-1
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user