From 6590e48ec8303ed959f9e93db66ce2f70a3abba0 Mon Sep 17 00:00:00 2001 From: AmruthVamshi Date: Thu, 12 Dec 2024 11:16:19 +0530 Subject: [PATCH 1/2] Updated sendOTP logic --- src/api/api.controller.ts | 28 ++++++++++++++++------------ src/api/api.service.ts | 9 +++++++-- src/api/dto/login.dto.ts | 6 +++++- src/api/dto/send-otp.dto.ts | 4 ++++ src/user/dto/login.dto.ts | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/api/api.controller.ts b/src/api/api.controller.ts index ea515e2..e5663ef 100644 --- a/src/api/api.controller.ts +++ b/src/api/api.controller.ts @@ -102,11 +102,15 @@ export class ApiController { } } - let status: any, isWhatsApp = false; + let status: any, isWhatsApp = false, countryCode, number; // Check if phone number contains country code (e.g. 91-1234567890) - if (params.phone.includes('-')) { + if (params.deliveryType=='WA') { isWhatsApp = true; - const [countryCode, number] = params.phone.split('-'); + if (params.phone.includes('-')) { + [countryCode, number] = params.phone.split('-'); + } else { + number = params.phone; + } params.phone = number; status = await this.gupshupWhatsappService.sendWhatsappOTP({ phone: number, @@ -136,6 +140,15 @@ export class ApiController { return { status }; } + @Post('login/otp') + @UsePipes(new ValidationPipe({ transform: true })) + async loginWithOtp( + @Body() user: LoginDto, + @Headers('authorization') authHeader, + ): Promise { + return await this.apiService.loginWithOtp(user, authHeader); + } + @Get('verifyOTP') @UsePipes(new ValidationPipe({ transform: true })) async verifyOTP(@Query() params: VerifyOtpDto): Promise { @@ -400,15 +413,6 @@ export class ApiController { ); } - @Post('login/otp') - @UsePipes(new ValidationPipe({ transform: true })) - async loginWithOtp( - @Body() user: LoginDto, - @Headers('authorization') authHeader, - ): Promise { - return await this.apiService.loginWithOtp(user, authHeader); - } - @Post('login-with-unique-id') @UsePipes(new ValidationPipe({ transform: true })) async loginWithUniqueId( diff --git a/src/api/api.service.ts b/src/api/api.service.ts index 87525ba..e35e004 100644 --- a/src/api/api.service.ts +++ b/src/api/api.service.ts @@ -564,6 +564,7 @@ export class ApiService { */ let otp = loginDto.password; let phone = loginDto.loginId; + let countryCode, number; const salt = this.configResolverService.getSalt(loginDto.applicationId); let verifyOTPResult; if( @@ -575,8 +576,12 @@ export class ApiService { verifyOTPResult = {status: SMSResponseStatus.success} else verifyOTPResult = {status: SMSResponseStatus.failure} - } else if (phone.includes('-')) { - const [countryCode, number] = phone.split('-'); + } else if (loginDto.deliveryType=='WA') { + if(phone.includes('-')){ + [countryCode, number] = phone.split('-'); + } else { + number = phone + } loginDto.loginId = number; const status: any = await this.gupshupWhatsappService.verifyWhatsappOTP(loginDto.loginId, loginDto.password); if(status.status == 'success') { diff --git a/src/api/dto/login.dto.ts b/src/api/dto/login.dto.ts index 348b9b9..fbe6eee 100644 --- a/src/api/dto/login.dto.ts +++ b/src/api/dto/login.dto.ts @@ -1,5 +1,5 @@ import { - IsNotEmpty, IsString, IsUUID, MaxLength, + IsNotEmpty, IsOptional, IsString, IsUUID, MaxLength, } from 'class-validator'; export class LoginDto { @@ -16,6 +16,10 @@ export class LoginDto { @IsUUID() @IsNotEmpty() applicationId: string; + + @IsString() + @IsOptional() + deliveryType?: string; } diff --git a/src/api/dto/send-otp.dto.ts b/src/api/dto/send-otp.dto.ts index e3dc1a0..791448a 100644 --- a/src/api/dto/send-otp.dto.ts +++ b/src/api/dto/send-otp.dto.ts @@ -19,4 +19,8 @@ export class SendOtpDto { @IsString() @IsOptional() orgId?: string; + + @IsString() + @IsOptional() + deliveryType?: string; } diff --git a/src/user/dto/login.dto.ts b/src/user/dto/login.dto.ts index 503d67c..6c3a3d8 100644 --- a/src/user/dto/login.dto.ts +++ b/src/user/dto/login.dto.ts @@ -7,4 +7,5 @@ export class LoginDto { roles?: Array; fingerprint?: string; timestamp?: string; + deliveryType?: string; } From f2412c8a1cd0efcfff6648509f0422a93f0ba1ee Mon Sep 17 00:00:00 2001 From: AmruthVamshi Date: Thu, 12 Dec 2024 11:34:27 +0530 Subject: [PATCH 2/2] Updated sendOTP logic --- src/api/api.controller.ts | 17 ++++++++++------- src/api/api.service.ts | 11 +++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/api/api.controller.ts b/src/api/api.controller.ts index e5663ef..2d02fb1 100644 --- a/src/api/api.controller.ts +++ b/src/api/api.controller.ts @@ -80,6 +80,15 @@ export class ApiController { @Headers('x-application-id') applicationId?, ): Promise { let startTime = Date.now(); + + let status: any, isWhatsApp = false, countryCode, number; + + if (params.phone.includes('-')) { + [countryCode, number] = params.phone.split('-'); + params.phone = number; + } else { + number = params.phone; + } if (applicationId) { const { total }: { total: number; users: Array } = await this.fusionAuthService.getUsersByString( @@ -102,16 +111,10 @@ export class ApiController { } } - let status: any, isWhatsApp = false, countryCode, number; // Check if phone number contains country code (e.g. 91-1234567890) if (params.deliveryType=='WA') { isWhatsApp = true; - if (params.phone.includes('-')) { - [countryCode, number] = params.phone.split('-'); - } else { - number = params.phone; - } - params.phone = number; + status = await this.gupshupWhatsappService.sendWhatsappOTP({ phone: number, template: null, diff --git a/src/api/api.service.ts b/src/api/api.service.ts index e35e004..67bc263 100644 --- a/src/api/api.service.ts +++ b/src/api/api.service.ts @@ -565,6 +565,10 @@ export class ApiService { let otp = loginDto.password; let phone = loginDto.loginId; let countryCode, number; + if (phone.includes('-')) { + [countryCode, number] = phone.split('-'); + phone = number; + } const salt = this.configResolverService.getSalt(loginDto.applicationId); let verifyOTPResult; if( @@ -577,12 +581,7 @@ export class ApiService { else verifyOTPResult = {status: SMSResponseStatus.failure} } else if (loginDto.deliveryType=='WA') { - if(phone.includes('-')){ - [countryCode, number] = phone.split('-'); - } else { - number = phone - } - loginDto.loginId = number; + loginDto.loginId = phone; const status: any = await this.gupshupWhatsappService.verifyWhatsappOTP(loginDto.loginId, loginDto.password); if(status.status == 'success') { verifyOTPResult = {status: SMSResponseStatus.success}