fix: password change bugs

This commit is contained in:
2026-06-10 20:09:40 +03:00
parent de870b78d2
commit 99714ac476
4 changed files with 24 additions and 14 deletions

View File

@@ -34,10 +34,6 @@ class IUserRepository(ABC):
@abstractmethod
async def count_individuals(self, *, query: str) -> int:
raise NotImplementedError
@abstractmethod
async def get_password_hash(self, user_id: str) -> str:
raise NotImplementedError
@abstractmethod
async def set_password(self, user_id: str, password_hash: str) -> UserEntity:

View File

@@ -18,14 +18,12 @@ class SetPasswordCommand:
@transactional
async def __call__(self, email: str, password: str) -> bool:
try:
user = await self._unit_of_work.user_repository.get_user_by_email(email)
password_hash = self._hash_service.hash_password(password)
password_hash = await self._hash_service.hash(password)
await self._unit_of_work.user_repository.set_password(
user_id=user.id,
user_email=email,
password_hash=password_hash,
)
self._logger.info(f'Set password for user {user.id}')
self._logger.info(f'Set password for user {email}')
return True
except ApplicationException:
raise

View File

@@ -74,10 +74,25 @@ class UserRepository(IUserRepository):
except SQLAlchemyError as exc:
self._logger.exception(str(exc))
raise ApplicationException(status_code=500, message='Database error')
async def _get_user(self, user_id: str) -> UserModel:
stmt = (
select(UserModel)
.where(
UserModel.id == user_id,
UserModel.is_deleted.is_(False),
)
)
result = await self._session.execute(stmt)
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=404, message='User not found')
return user
async def _update_field(self, user_id: str, **fields: object) -> UserEntity:
try:
user = await self._get_active_user(user_id)
user = await self._get_user(user_id)
for key, value in fields.items():
setattr(user, key, value)
await self._session.flush()
@@ -89,8 +104,9 @@ class UserRepository(IUserRepository):
self._logger.exception(str(exception))
raise InternalServerException(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)
async def set_password(self, user_email: str, password_hash: str) -> UserEntity:
user = await self.get_user_by_email(user_email)
return await self._update_field(user.id, password_hash=password_hash)
async def get_user_by_email(self, email: str) -> UserEntity:
try:

View File

@@ -3,7 +3,7 @@ from fastapi.responses import ORJSONResponse
from starlette import status
from src.presentation.dependencies.commands import get_set_password_command
from src.application.commands.set_password import SetPasswordCommand
from presentation.schemas.password import SetPasswordRequest
from src.presentation.schemas.password import SetPasswordRequest
users_router = APIRouter(prefix='/users', tags=['users'])