From 9c7b81bf118cadf8b9b240f609cb63fa459bb8c7 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Sat, 24 Jan 2026 13:20:35 +0300 Subject: [PATCH] feat: add send otp method --- eslint.config.mjs | 5 +++++ src/modules/auth/auth.controller.ts | 7 ++----- src/modules/auth/auth.module.ts | 3 ++- src/modules/auth/auth.repository.ts | 26 ++++++++++++++++++++++++ src/modules/auth/auth.service.ts | 31 ++++++++++++++++++++++++++++- tsconfig.json | 7 ++++++- 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 src/modules/auth/auth.repository.ts diff --git a/eslint.config.mjs b/eslint.config.mjs index 4e9f827..7e44d3a 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -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", }, }, ); diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 7f16761..3bea983 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -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 { + return this.authService.sendOtp(data) } } diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index 32dcd68..ce68699 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -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 {} diff --git a/src/modules/auth/auth.repository.ts b/src/modules/auth/auth.repository.ts new file mode 100644 index 0000000..0777b8d --- /dev/null +++ b/src/modules/auth/auth.repository.ts @@ -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) { + return this.prismaService.account.findUnique({ + where: { phone } + }) + } + + public findByEmail(email: NonNullable) { + return this.prismaService.account.findUnique({ + where: { email } + }) + } + + public createAccount(data: AccountCreateInput) { + return this.prismaService.account.create({ data }) + } +} diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 7f4cbbf..33bebb7 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -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 { + 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 } + } +} diff --git a/tsconfig.json b/tsconfig.json index aba29b0..28a05ca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,11 @@ "forceConsistentCasingInFileNames": true, "noImplicitAny": false, "strictBindCallApply": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "paths": { + "@/*": ["src/*"], + "@prisma/generated": ["./prisma/generated"], + "@prisma/generated/*": ["./prisma/generated/*"] + } } }