feat: add set phone

This commit is contained in:
2026-05-14 21:45:43 +03:00
parent 6465807394
commit 75362b07ae
10 changed files with 105 additions and 29 deletions

View File

@@ -1,10 +1,9 @@
from __future__ import annotations
from fastapi import status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import SQLAlchemyError
from src.application.contracts import ILogger
from src.application.domain.exceptions import ApplicationException
from src.application.domain.exceptions import ApplicationException, BadRequestException, InternalException, NotFoundException
from src.application.abstractions.repositories import IUserRepository
from src.application.domain.entities import UserEntity
from src.infrastructure.database.models import UserModel
@@ -27,7 +26,7 @@ class UserRepository(IUserRepository):
user: UserModel | None = result.scalar_one_or_none()
if user is None:
self._logger.warning(f'User not found with user_id {user_id}')
raise ApplicationException(status_code=status.HTTP_404_NOT_FOUND, message='User not found')
raise NotFoundException(message='User not found')
return user
@staticmethod
@@ -60,7 +59,7 @@ class UserRepository(IUserRepository):
raise
except SQLAlchemyError as exception:
self._logger.exception(str(exception))
raise ApplicationException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, message=f'Database error: {str(exception)}')
raise InternalException(message=f'Database error: {str(exception)}')
async def _update_field(self, user_id: str, **fields: object) -> UserEntity:
try:
@@ -74,7 +73,7 @@ class UserRepository(IUserRepository):
raise
except SQLAlchemyError as exception:
self._logger.exception(str(exception))
raise ApplicationException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, message=f'Database error: {str(exception)}')
raise InternalException(message=f'Database error: {str(exception)}')
async def set_phone(self, user_id: str, phone: str) -> UserEntity:
return await self._update_field(user_id, phone=phone)
@@ -83,10 +82,7 @@ class UserRepository(IUserRepository):
allowed = {'passport_data', 'inn', 'erc20'}
payload = {k: v for k, v in fields.items() if k in allowed and v is not None}
if not payload:
raise ApplicationException(
status_code=status.HTTP_400_BAD_REQUEST,
message='No identity fields to update',
)
raise BadRequestException(message='No identity fields to update')
return await self._update_field(user_id, **payload)
async def set_encrypted_mnemonic(self, user_id: str, encrypted_mnemonic: str) -> UserEntity:
@@ -100,7 +96,7 @@ class UserRepository(IUserRepository):
raise
except SQLAlchemyError as exception:
self._logger.exception(str(exception))
raise ApplicationException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, message=f'Database error: {str(exception)}')
raise InternalException(message=f'Database error: {str(exception)}')
async def set_password(self, user_id: str, password_hash: str) -> UserEntity:
return await self._update_field(user_id, password_hash=password_hash)
@@ -121,4 +117,4 @@ class UserRepository(IUserRepository):
return result.scalar_one_or_none() is not None
except SQLAlchemyError as exception:
self._logger.exception(str(exception))
raise ApplicationException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, message=f'Database error: {str(exception)}')
raise InternalException(message=f'Database error: {str(exception)}')