diff --git a/src/__init__.py b/src/__init__.py index ca96a66..e69de29 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +0,0 @@ -"""tgex backend package.""" diff --git a/src/adapter/postgres.py b/src/adapter/postgres.py index 617a3bc..83bd644 100644 --- a/src/adapter/postgres.py +++ b/src/adapter/postgres.py @@ -135,7 +135,14 @@ class Postgres(DatabaseBase): async def add_external_channel_to_targets( self, external_channel_id: uuid.UUID, target_channel_ids: list[uuid.UUID] ) -> None: - external_channel = await self.session.get(domain.ExternalChannel, external_channel_id) + stmt = ( + select(domain.ExternalChannel) + .where(domain.ExternalChannel.id == external_channel_id) + .options(selectinload(domain.ExternalChannel.target_channels)) + ) + result = await self.session.execute(stmt) + external_channel = result.scalar_one_or_none() + if not external_channel: raise ValueError(f'ExternalChannel {external_channel_id} not found') @@ -149,7 +156,14 @@ class Postgres(DatabaseBase): async def remove_external_channel_from_target( self, external_channel_id: uuid.UUID, target_channel_id: uuid.UUID ) -> None: - external_channel = await self.session.get(domain.ExternalChannel, external_channel_id) + stmt = ( + select(domain.ExternalChannel) + .where(domain.ExternalChannel.id == external_channel_id) + .options(selectinload(domain.ExternalChannel.target_channels)) + ) + result = await self.session.execute(stmt) + external_channel = result.scalar_one_or_none() + if not external_channel: raise ValueError(f'ExternalChannel {external_channel_id} not found') diff --git a/src/usecase/target_channel/disconnect_target_chan_by_tg_id.py b/src/usecase/target_channel/disconnect_target_chan_by_tg_id.py index 3eea08a..6b2a28e 100644 --- a/src/usecase/target_channel/disconnect_target_chan_by_tg_id.py +++ b/src/usecase/target_channel/disconnect_target_chan_by_tg_id.py @@ -10,13 +10,12 @@ log = logging.getLogger(__name__) async def disconnect_target_chan_by_tg_id(self: 'Usecase', input: dto.DisconnectTargetChanByTgIdInput) -> None: - # Получаем user_id по telegram_id - user = await self.database.get_user(telegram_id=input.user_telegram_id) - if not user: - log.warning(f'User with telegram_id {input.user_telegram_id} not found when disconnecting channel') - return - async with self.database.transaction(): + user = await self.database.get_user(telegram_id=input.user_telegram_id) + if not user: + log.warning(f'User with telegram_id {input.user_telegram_id} not found when disconnecting channel') + return + channel = await self.database.get_target_channel(user.id, telegram_id=input.telegram_id) if not channel: log.warning(f'Target channel {input.telegram_id} not found')