mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 13:49:40 +00:00
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
|
import { JuniorTokenRepository } from '../repositories';
|
|
import { QrcodeService } from './qrcode.service';
|
|
|
|
@Injectable()
|
|
export class JuniorTokenService {
|
|
private readonly logger = new Logger(JuniorTokenService.name);
|
|
constructor(
|
|
private readonly juniorTokenRepository: JuniorTokenRepository,
|
|
private readonly qrCodeService: QrcodeService,
|
|
) {}
|
|
async generateToken(juniorId: string): Promise<string> {
|
|
this.logger.log(`Generating token for junior ${juniorId}`);
|
|
const tokenEntity = await this.juniorTokenRepository.generateToken(juniorId);
|
|
this.logger.log(`Token generated successfully for junior ${juniorId}`);
|
|
return this.qrCodeService.generateQrCode(tokenEntity.token);
|
|
}
|
|
|
|
async validateToken(token: string) {
|
|
this.logger.log(`Validating token ${token}`);
|
|
const tokenEntity = await this.juniorTokenRepository.findByToken(token);
|
|
|
|
if (!tokenEntity) {
|
|
this.logger.error(`Token ${token} not found`);
|
|
throw new BadRequestException('TOKEN.INVALID');
|
|
}
|
|
|
|
if (tokenEntity.expiryDate < new Date()) {
|
|
this.logger.error(`Token ${token} expired`);
|
|
throw new BadRequestException('TOKEN.EXPIRED');
|
|
}
|
|
|
|
this.logger.log(`Token validated successfully`);
|
|
return tokenEntity.juniorId;
|
|
}
|
|
|
|
invalidateToken(token: string) {
|
|
this.logger.log(`Invalidating token ${token}`);
|
|
return this.juniorTokenRepository.invalidateToken(token);
|
|
}
|
|
}
|