mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
feat: add loggers to all services
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { NotificationsService } from '../../notification/services/notifications.service';
|
||||
import { DEFAULT_OTP_DIGIT, DEFAULT_OTP_LENGTH } from '../constants';
|
||||
@ -9,27 +9,37 @@ import { generateRandomOtp } from '../utils';
|
||||
|
||||
@Injectable()
|
||||
export class OtpService {
|
||||
private readonly logger = new Logger(OtpService.name);
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly otpRepository: OtpRepository,
|
||||
private readonly notificationService: NotificationsService,
|
||||
) {}
|
||||
private useMock = this.configService.get<boolean>('USE_MOCK', false);
|
||||
async generateAndSendOtp(sendotpRequest: ISendOtp): Promise<string> {
|
||||
async generateAndSendOtp(sendOtpRequest: ISendOtp): Promise<string> {
|
||||
this.logger.log(`Generating OTP for ${sendOtpRequest.recipient}`);
|
||||
const otp = this.useMock ? DEFAULT_OTP_DIGIT.repeat(DEFAULT_OTP_LENGTH) : generateRandomOtp(DEFAULT_OTP_LENGTH);
|
||||
|
||||
await this.otpRepository.createOtp({ ...sendotpRequest, value: otp });
|
||||
await this.otpRepository.createOtp({ ...sendOtpRequest, value: otp });
|
||||
|
||||
this.sendOtp(sendotpRequest, otp);
|
||||
|
||||
return sendotpRequest.otpType == OtpType.EMAIL
|
||||
? sendotpRequest.recipient
|
||||
: sendotpRequest.recipient?.replace(/.(?=.{4})/g, '*');
|
||||
await this.sendOtp(sendOtpRequest, otp);
|
||||
this.logger.log(`OTP generated and sent successfully to ${sendOtpRequest.recipient}`);
|
||||
return sendOtpRequest.otpType == OtpType.EMAIL
|
||||
? sendOtpRequest.recipient
|
||||
: sendOtpRequest.recipient?.replace(/.(?=.{4})/g, '*');
|
||||
}
|
||||
|
||||
async verifyOtp(verifyOtpRequest: IVerifyOtp) {
|
||||
this.logger.log(`Verifying OTP for ${verifyOtpRequest.userId}`);
|
||||
const otp = await this.otpRepository.findOtp(verifyOtpRequest);
|
||||
|
||||
if (!otp) {
|
||||
this.logger.error(
|
||||
`OTP value ${verifyOtpRequest.value} not found for ${verifyOtpRequest.userId} and ${verifyOtpRequest.otpType}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!otp;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user