From 966762556230e66e56c0729f930c564459bd04c5 Mon Sep 17 00:00:00 2001 From: yousef-alkhrissat Date: Fri, 9 Aug 2024 18:54:53 +0300 Subject: [PATCH] otp check if user exists in region --- src/auth/dtos/user-otp.dto.ts | 5 +++++ src/auth/services/user-auth.service.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/auth/dtos/user-otp.dto.ts b/src/auth/dtos/user-otp.dto.ts index bab47c8..b588cb2 100644 --- a/src/auth/dtos/user-otp.dto.ts +++ b/src/auth/dtos/user-otp.dto.ts @@ -12,6 +12,11 @@ export class UserOtpDto { @IsEnum(OtpType) @IsNotEmpty() type: OtpType; + + @ApiProperty() + @IsNotEmpty() + @IsString() + regionName: string; } export class VerifyOtpDto extends UserOtpDto { diff --git a/src/auth/services/user-auth.service.ts b/src/auth/services/user-auth.service.ts index 69541cc..5bc8a6b 100644 --- a/src/auth/services/user-auth.service.ts +++ b/src/auth/services/user-auth.service.ts @@ -116,7 +116,7 @@ export class UserAuthService { async deleteUser(uuid: string) { const user = await this.findOneById(uuid); if (!user) { - throw new BadRequestException('User does not found'); + throw new BadRequestException('User not found'); } return await this.userRepository.update({ uuid }, { isActive: false }); } @@ -128,6 +128,17 @@ export class UserAuthService { async generateOTP(data: UserOtpDto): Promise { const threeDaysAgo = new Date(); threeDaysAgo.setDate(threeDaysAgo.getDate() - 3); + const userExists = await this.userRepository.exists({ + where: { + region: { + regionName: data.regionName, + }, + email: data.email, + }, + }); + if (!userExists) { + throw new BadRequestException('User not found'); + } await this.otpRepository.softDelete({ email: data.email, type: data.type }); await this.otpRepository.delete({ email: data.email, @@ -153,7 +164,7 @@ export class UserAuthService { const timeSinceLastOtp = differenceInSeconds(now, lastOtp.createdAt); if (timeSinceLastOtp < cooldown) { - throw new Error( + throw new BadRequestException( `Please wait ${cooldown - timeSinceLastOtp} more seconds before requesting a new OTP.`, ); }