ref: рефакторинг usecases
This commit is contained in:
@@ -11,74 +11,63 @@ 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
|
||||
invite_link = await self.telegram_bot.create_chat_invite_link(target_channel.telegram_id, requires_approval)
|
||||
|
||||
placement = domain.Placement(
|
||||
target_channel_id=input.target_channel_id,
|
||||
external_channel_id=input.external_channel_id,
|
||||
creative_id=input.creative_id,
|
||||
user_id=user_id,
|
||||
placement_date=input.placement_date,
|
||||
cost=input.cost,
|
||||
comment=input.comment,
|
||||
invite_link_type=input.invite_link_type,
|
||||
invite_link=invite_link,
|
||||
status=domain.PlacementStatus.PENDING,
|
||||
)
|
||||
|
||||
async with self.database.transaction():
|
||||
# Проверяем существование целевого канала
|
||||
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)
|
||||
await self.database.create_placement(placement)
|
||||
|
||||
# Проверяем существование внешнего канала
|
||||
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)
|
||||
|
||||
# Создаём уникальную пригласительную ссылку через Telegram Bot API
|
||||
requires_approval = input.invite_link_type == domain.InviteLinkType.APPROVAL
|
||||
invite_link = await self.telegram_bot.create_chat_invite_link(
|
||||
chat_id=target_channel.telegram_id,
|
||||
requires_approval=requires_approval,
|
||||
)
|
||||
|
||||
# Создаём закуп со статусом PENDING (ожидаем публикацию поста)
|
||||
placement = domain.Placement(
|
||||
target_channel_id=input.target_channel_id,
|
||||
external_channel_id=input.external_channel_id,
|
||||
creative_id=input.creative_id,
|
||||
user_id=user_id,
|
||||
placement_date=input.placement_date,
|
||||
cost=input.cost,
|
||||
comment=input.comment,
|
||||
invite_link_type=input.invite_link_type,
|
||||
invite_link=invite_link,
|
||||
status=domain.PlacementStatus.PENDING,
|
||||
)
|
||||
|
||||
created = await self.database.create_placement(placement)
|
||||
|
||||
# Увеличиваем счётчик закупов у креатива
|
||||
creative.placements_count += 1
|
||||
await self.database.update_creative(creative)
|
||||
|
||||
return dto.PlacementOutput(
|
||||
id=created.id,
|
||||
target_channel_id=created.target_channel_id,
|
||||
target_channel_title=target_channel.title,
|
||||
external_channel_id=created.external_channel_id,
|
||||
external_channel_title=external_channel.title,
|
||||
creative_id=created.creative_id,
|
||||
creative_name=creative.name,
|
||||
placement_date=created.placement_date,
|
||||
cost=created.cost,
|
||||
comment=created.comment,
|
||||
ad_post_url=created.ad_post_url,
|
||||
invite_link_type=created.invite_link_type,
|
||||
invite_link=created.invite_link,
|
||||
status=created.status,
|
||||
subscriptions_count=created.subscriptions_count,
|
||||
views_count=created.views_count,
|
||||
views_availability=created.views_availability,
|
||||
last_views_fetch_at=created.last_views_fetch_at,
|
||||
created_at=created.created_at,
|
||||
)
|
||||
return dto.PlacementOutput(
|
||||
id=placement.id,
|
||||
target_channel_id=placement.target_channel_id,
|
||||
target_channel_title=target_channel.title,
|
||||
external_channel_id=placement.external_channel_id,
|
||||
external_channel_title=external_channel.title,
|
||||
creative_id=placement.creative_id,
|
||||
creative_name=creative.name,
|
||||
placement_date=placement.placement_date,
|
||||
cost=placement.cost,
|
||||
comment=placement.comment,
|
||||
ad_post_url=placement.ad_post_url,
|
||||
invite_link_type=placement.invite_link_type,
|
||||
invite_link=placement.invite_link,
|
||||
status=placement.status,
|
||||
subscriptions_count=placement.subscriptions_count,
|
||||
views_count=placement.views_count,
|
||||
views_availability=placement.views_availability,
|
||||
last_views_fetch_at=placement.last_views_fetch_at,
|
||||
created_at=placement.created_at,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user