fix: фиксы после рефакторинга

This commit is contained in:
Artem Tsyrulnikov
2025-12-11 22:10:46 +03:00
parent eb1005c995
commit 2fdd56c3e0
8 changed files with 92 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
import datetime
import logging
import typing
import uuid
@@ -8,6 +9,8 @@ from tortoise.transactions import in_transaction
from shared.datebase_base import DatabaseBase
from src import domain
log = logging.getLogger(__name__)
class Postgres(DatabaseBase):
def transaction(self) -> typing.AsyncContextManager[None]:
@@ -89,26 +92,16 @@ class Postgres(DatabaseBase):
return await query.first()
async def get_external_channels_for_target(self, target_channel_id: uuid.UUID) -> list[domain.ExternalChannel]:
target = await domain.TargetChannel.filter(id=target_channel_id).prefetch_related('external_channels').first()
if not target:
return []
return await target.external_channels.all()
return await domain.ExternalChannel.filter(target_channels__id=target_channel_id).all()
async def get_user_external_channels(self, user_id: uuid.UUID) -> list[domain.ExternalChannel]:
return await domain.ExternalChannel.filter(user_id=user_id).all()
async def add_external_channel_to_targets(
self, external_channel_id: uuid.UUID, target_channel_ids: list[uuid.UUID]
self, external_channel: domain.ExternalChannel, target_channel_ids: list[uuid.UUID]
) -> None:
external_channel = await domain.ExternalChannel.get_or_none(id=external_channel_id)
if not external_channel:
raise ValueError(f'ExternalChannel {external_channel_id} not found')
for target_id in target_channel_ids:
target_channel = await domain.TargetChannel.get_or_none(id=target_id)
if target_channel:
await external_channel.target_channels.add(target_channel)
target_channels = await domain.TargetChannel.filter(id__in=target_channel_ids).all()
await external_channel.target_channels.add(*target_channels)
async def remove_external_channel_from_target(
self, external_channel_id: uuid.UUID, target_channel_id: uuid.UUID

View File

@@ -12,7 +12,7 @@ if TYPE_CHECKING:
class ExternalChannel(TimestampedModel):
id = fields.UUIDField(pk=True)
telegram_id = fields.BigIntField(unique=True, index=True)
telegram_id = fields.BigIntField(index=True)
title = fields.CharField(max_length=255)
username = fields.CharField(max_length=255, null=True, index=True)
description = fields.TextField(null=True)
@@ -28,3 +28,4 @@ class ExternalChannel(TimestampedModel):
class Meta:
table = 'external_channel'
unique_together = (('user_id', 'telegram_id'),)

View File

@@ -2,7 +2,6 @@ __all__ = (
'UpdateTargetChanPermissionsInput',
'GetUserTargetChansInput',
'GetUserTargetChansOutput',
'DisconnectTargetChanInput',
'DisconnectTargetChanByTgIdInput',
'ConnectTargetChanInput',
'ConnectTargetChanOutput',
@@ -110,7 +109,6 @@ from .target_channel import (
ConnectTargetChanInput,
ConnectTargetChanOutput,
DisconnectTargetChanByTgIdInput,
DisconnectTargetChanInput,
GetUserTargetChansInput,
GetUserTargetChansOutput,
UpdateTargetChanPermissionsInput,

View File

@@ -80,7 +80,7 @@ class Database(typing.Protocol):
async def get_user_external_channels(self, user_id: UUID) -> list[domain.ExternalChannel]: ...
async def add_external_channel_to_targets(
self, external_channel_id: UUID, target_channel_ids: list[UUID]
self, external_channel: domain.ExternalChannel, target_channel_ids: list[UUID]
) -> None: ...
async def remove_external_channel_from_target(self, external_channel_id: UUID, target_channel_id: UUID) -> None: ...

View File

@@ -13,6 +13,10 @@ log = logging.getLogger(__name__)
async def create_external_channel(
self: 'Usecase', input: dto.CreateExternalChannelInput, user_id: uuid.UUID
) -> dto.ExternalChannelOutput:
existing_channel = await self.database.get_external_channel(user_id, telegram_id=input.telegram_id)
if existing_channel:
raise domain.ExternalChannelAlreadyExists(input.telegram_id)
for target_id in input.target_channel_ids:
target_channel = await self.database.get_target_channel(user_id, channel_id=target_id)
@@ -32,7 +36,7 @@ async def create_external_channel(
await self.database.create_external_channel(channel)
if input.target_channel_ids:
await self.database.add_external_channel_to_targets(channel.id, input.target_channel_ids)
await self.database.add_external_channel_to_targets(channel, input.target_channel_ids)
log.info(
'External channel %s created and linked to %s target channels', input.telegram_id, len(input.target_channel_ids)

View File

@@ -13,19 +13,14 @@ log = logging.getLogger(__name__)
async def create_placement(self: 'Usecase', input: dto.CreatePlacementInput, user_id: uuid.UUID) -> dto.PlacementOutput:
target_channel = await self.database.get_target_channel(user_id, channel_id=input.target_channel_id)
if not target_channel:
log.warning('User %s attempted to create placement for unavailable target %s', user_id, input.target_channel_id)
raise domain.TargetChannelNotFound(input.target_channel_id)
external_channel = await self.database.get_external_channel(user_id, channel_id=input.external_channel_id)
if not external_channel:
log.warning(
'User %s attempted to create placement for unavailable external %s', user_id, input.external_channel_id
)
raise domain.ExternalChannelNotFound(input.external_channel_id)
creative = await self.database.get_creative(user_id, input.creative_id)
if not creative:
log.warning('User %s attempted to create placement for unavailable creative %s', user_id, input.creative_id)
raise domain.CreativeNotFound(input.creative_id)
requires_approval = input.invite_link_type == domain.InviteLinkType.APPROVAL