mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-10-25 10:17:56 +00:00
feat: add waiting number and handle resent otp
This commit is contained in:
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { MoreThan, Repository } from 'typeorm';
|
||||
import { Otp } from '../entities';
|
||||
import { IVerifyOtp } from '../interfaces';
|
||||
import { ISendOtp, IVerifyOtp } from '../interfaces';
|
||||
const FIVE = 5;
|
||||
const SIXTY = 60;
|
||||
const ONE_THOUSAND = 1000;
|
||||
@@ -42,4 +42,18 @@ export class OtpRepository {
|
||||
updateOtp(id: string, data: Partial<Otp>) {
|
||||
return this.otpRepository.update(id, data);
|
||||
}
|
||||
|
||||
invalidateOtp(otp: ISendOtp) {
|
||||
return this.otpRepository.update(
|
||||
{
|
||||
userId: otp.userId,
|
||||
scope: otp.scope,
|
||||
otpType: otp.otpType,
|
||||
isUsed: false,
|
||||
},
|
||||
{
|
||||
isUsed: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,11 @@ export class OtpService {
|
||||
private readonly otpRepository: OtpRepository,
|
||||
private readonly notificationService: NotificationsService,
|
||||
) {}
|
||||
private useMock = this.configService.get<boolean>('USE_MOCK', false);
|
||||
private useMock = [true, 'true'].includes(this.configService.get<boolean>('USE_MOCK', false));
|
||||
async generateAndSendOtp(sendOtpRequest: ISendOtp): Promise<string> {
|
||||
this.logger.log(`invalidate OTP for ${sendOtpRequest.recipient} and ${sendOtpRequest.otpType}`);
|
||||
await this.otpRepository.invalidateOtp(sendOtpRequest);
|
||||
|
||||
this.logger.log(`Generating OTP for ${sendOtpRequest.recipient}`);
|
||||
const otp = this.useMock ? DEFAULT_OTP_DIGIT.repeat(DEFAULT_OTP_LENGTH) : generateRandomOtp(DEFAULT_OTP_LENGTH);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user