import logging import uuid from typing import TYPE_CHECKING import domain.purchase from src import domain, dto if TYPE_CHECKING: from .. import Usecase log = logging.getLogger(__name__) async def create_placement( self: 'Usecase', input: dto.CreatePlacementInput, user_id: uuid.UUID, workspace_id: uuid.UUID ) -> dto.PlacementOutput: await self.ensure_workspace_permission( workspace_id, user_id, domain.PermissionKey.PLACEMENTS_WRITE, for_project_id=input.project_id, ) project = await self.database.get_project(workspace_id, project_id=input.project_id) if not project: raise domain.ProjectNotFound(input.project_id) placement_channel = await self.database.get_channel(channel_id=input.placement_channel_id) if not placement_channel: raise domain.ChannelNotFound(input.placement_channel_id) creative = await self.database.get_creative(workspace_id, input.creative_id) if not creative: raise domain.CreativeNotFound(input.creative_id) if creative.project_id != project.id: raise domain.CreativeNotFound(input.creative_id) # Берем тип ссылки из настроек проекта requires_approval = project.purchase_invite_type_default == domain.purchase.InviteLinkType.APPROVAL if project.channel.telegram_id is None: raise domain.ChannelNotFound(project.channel.id) invite_link = await self.telegram_bot.create_chat_invite_link(project.channel.telegram_id, requires_approval) log.info( 'Created invite link for project=%s channel_telegram_id=%s requires_approval=%s invite_link=%s', project.id, project.channel.telegram_id, requires_approval, invite_link, ) placement = domain.Placement( project_id=input.project_id, placement_channel_id=input.placement_channel_id, creative_id=input.creative_id, workspace_id=workspace_id, placement_date=input.placement_date, cost=input.cost, comment=input.comment, invite_link=invite_link, status=domain.PlacementStatus.ACTIVE, ) await self.database.create_placement(placement) return dto.PlacementOutput( id=placement.id, project_id=placement.project_id, project_channel_title=project.channel.title, placement_channel_id=placement.placement_channel_id, placement_channel_title=placement_channel.title, creative_id=placement.creative_id, creative_name=creative.name, placement_date=placement.wanted_placement_date, cost=placement.cost, comment=placement.comment, ad_post_url=None, invite_link_type=project.purchase_invite_type_default, invite_link=placement.invite_link, status=placement.status, subscriptions_count=0, created_at=placement.created_at, )