diff --git a/api/src/auth/otp/otp.controller.ts b/api/src/auth/otp/otp.controller.ts index 97d8f6e..4b27e89 100644 --- a/api/src/auth/otp/otp.controller.ts +++ b/api/src/auth/otp/otp.controller.ts @@ -1,7 +1,8 @@ -import { Body, Controller, Logger, Post } from '@nestjs/common'; +import { Body, Controller, Inject, Logger, Post } from '@nestjs/common'; import { ApiCreatedResponse, ApiProperty, ApiResponse, ApiResponseProperty, ApiTags } from '@nestjs/swagger'; import { IsMobilePhone, IsNumberString, IsUUID, Length } from 'class-validator'; import { v4 as uuid4 } from 'uuid'; +import { UsersService } from '../../database/users/users.service'; class OtpSendBody { @ApiProperty({required: true, example: '+918800233266'}) @@ -47,6 +48,9 @@ class OtpVerifyResponse { @Controller('otp') export class OtpController { + @Inject() private readonly users!: UsersService; + + @ApiCreatedResponse({type: OtpSendResponse}) @Post('/') async requestOtp(@Body() reqOtp: OtpSendBody): Promise { @@ -60,10 +64,11 @@ export class OtpController { @ApiResponse({type: OtpVerifyResponse}) @Post('/verify') async verifyOtp(@Body() otpVerify: OtpVerifyBody): Promise { - Logger.debug(JSON.stringify(otpVerify), 'OTP_VERIFY') + Logger.debug(JSON.stringify(otpVerify), 'OTP_VERIFY'); + const [ user, isNewUser ] = await this.users.findOrCreate(otpVerify.phno); return new OtpVerifyResponse( uuid4().toString(), - true + isNewUser ); } } diff --git a/api/src/database/users/users.service.ts b/api/src/database/users/users.service.ts index ce432a3..cb2632e 100644 --- a/api/src/database/users/users.service.ts +++ b/api/src/database/users/users.service.ts @@ -16,7 +16,7 @@ export class UsersService { private authRepo!: AuthtokenRepository; async findByAuthToken(token: string): Promise { - return await this.authRepo.validateToken(token) + return await this.authRepo.validateToken(token); } async findById(id: number): Promise { @@ -27,4 +27,14 @@ export class UsersService { return await this.repo.findAll(); } + async findOrCreate(number: string): Promise<[ User, boolean ]> { + const user = await this.repo.findByPhNo(number); + if (user != null) + if (user.email == null || user.player == null) + return [ user, true ]; + else + return [ user, false ]; + else return [ await this.repo.createUser({emailid: '', phno: number}), true ]; + } + }