feat: add send otp method
This commit is contained in:
parent
f9dc39f29b
commit
9c7b81bf11
@ -30,6 +30,11 @@ export default tseslint.config(
|
||||
'@typescript-eslint/no-floating-promises': 'warn',
|
||||
'@typescript-eslint/no-unsafe-argument': 'warn',
|
||||
"prettier/prettier": ["error", { endOfLine: "auto" }],
|
||||
"@typescript-eslint/no-unsafe-call": "off",
|
||||
"@typescript-eslint/no-unsafe-return": "off",
|
||||
"@typescript-eslint/no-unsafe-member-access": "off",
|
||||
"@typescript-eslint/no-unsafe-assignment": "off",
|
||||
"@typescript-eslint/no-redundant-type-constituents": "off",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@ -12,10 +12,7 @@ export class AuthController {
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@GrpcMethod('AuthService', 'SendOtp')
|
||||
public sendOtp(data: SendOtpRequest): SendOtpResponse {
|
||||
console.log('Incoming OTP request', data)
|
||||
return {
|
||||
ok: true
|
||||
}
|
||||
public sendOtp(data: SendOtpRequest): Promise<SendOtpResponse> {
|
||||
return this.authService.sendOtp(data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { AuthController } from './auth.controller'
|
||||
import { AuthRepository } from './auth.repository'
|
||||
import { AuthService } from './auth.service'
|
||||
|
||||
@Module({
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService]
|
||||
providers: [AuthService, AuthRepository]
|
||||
})
|
||||
export class AuthModule {}
|
||||
|
||||
26
src/modules/auth/auth.repository.ts
Normal file
26
src/modules/auth/auth.repository.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { Account } from '@prisma/generated/client'
|
||||
import { AccountCreateInput } from '@prisma/generated/models/Account'
|
||||
|
||||
import { PrismaService } from '@/infra/prisma/prisma.service'
|
||||
|
||||
@Injectable()
|
||||
export class AuthRepository {
|
||||
public constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
public findByPhone(phone: NonNullable<Account['phone']>) {
|
||||
return this.prismaService.account.findUnique({
|
||||
where: { phone }
|
||||
})
|
||||
}
|
||||
|
||||
public findByEmail(email: NonNullable<Account['email']>) {
|
||||
return this.prismaService.account.findUnique({
|
||||
where: { email }
|
||||
})
|
||||
}
|
||||
|
||||
public createAccount(data: AccountCreateInput) {
|
||||
return this.prismaService.account.create({ data })
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,33 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { Account } from '@prisma/generated/client'
|
||||
import { SendOtpRequest, SendOtpResponse } from '@teacinema/contracts/gen/auth'
|
||||
|
||||
import { AuthRepository } from './auth.repository'
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {}
|
||||
export class AuthService {
|
||||
public constructor(private readonly authRepository: AuthRepository) {}
|
||||
|
||||
public async sendOtp(data: SendOtpRequest): Promise<SendOtpResponse> {
|
||||
const { identifier, type } = data
|
||||
|
||||
let account: Account | null = null
|
||||
const isPhoneType = type === 'phone'
|
||||
const isEmailType = type === 'email'
|
||||
|
||||
if (isPhoneType) {
|
||||
account = await this.authRepository.findByPhone(identifier)
|
||||
} else {
|
||||
account = await this.authRepository.findByEmail(identifier)
|
||||
}
|
||||
|
||||
if (!account) {
|
||||
account = await this.authRepository.createAccount({
|
||||
email: isEmailType ? identifier : undefined,
|
||||
phone: isPhoneType ? identifier : undefined
|
||||
})
|
||||
}
|
||||
|
||||
return { ok: true }
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,6 +20,11 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
"noFallthroughCasesInSwitch": false,
|
||||
"paths": {
|
||||
"@/*": ["src/*"],
|
||||
"@prisma/generated": ["./prisma/generated"],
|
||||
"@prisma/generated/*": ["./prisma/generated/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user