68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
import logging
|
|
import uuid
|
|
from typing import TYPE_CHECKING
|
|
|
|
from src import domain, dto
|
|
|
|
if TYPE_CHECKING:
|
|
from .. import Usecase
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
async def attach_channel_to_purchase_plan(
|
|
self: 'Usecase',
|
|
project_id: uuid.UUID,
|
|
workspace_id: uuid.UUID,
|
|
user_id: uuid.UUID,
|
|
input: dto.AttachChannelToPurchasePlanInput,
|
|
) -> dto.PurchasePlanChannelOutput:
|
|
await self.ensure_workspace_access(workspace_id, user_id)
|
|
|
|
project = await self.database.get_project(workspace_id, project_id=project_id)
|
|
if not project:
|
|
raise domain.ProjectNotFound(project_id)
|
|
|
|
plan = await self.ensure_active_purchase_plan(project)
|
|
|
|
# Поиск канала по username
|
|
channel = await self.database.get_channel(workspace_id, username=input.username)
|
|
|
|
if not channel:
|
|
# Создать неподтвержденный канал
|
|
channel = domain.Channel(
|
|
workspace_id=workspace_id,
|
|
username=input.username,
|
|
telegram_id=None,
|
|
title=None,
|
|
verification_status=domain.ChannelVerificationStatus.UNVERIFIED,
|
|
)
|
|
await self.database.create_channel(channel)
|
|
log.info('Created unverified channel with username %s', input.username)
|
|
else:
|
|
log.info('Found existing channel %s for username %s', channel.id, input.username)
|
|
|
|
plan_channel = await self.database.add_channel_to_purchase_plan(
|
|
plan.id,
|
|
channel.id,
|
|
status=input.status,
|
|
planned_cost=input.planned_cost,
|
|
comment=input.comment,
|
|
)
|
|
|
|
log.info('Channel %s attached to purchase plan %s (project %s)', channel.id, plan.id, project.id)
|
|
|
|
return dto.PurchasePlanChannelOutput(
|
|
id=plan_channel.id,
|
|
status=plan_channel.status,
|
|
planned_cost=plan_channel.planned_cost,
|
|
comment=plan_channel.comment,
|
|
channel=dto.ChannelOutput(
|
|
id=channel.id,
|
|
telegram_id=channel.telegram_id,
|
|
title=channel.title,
|
|
username=channel.username,
|
|
verification_status=channel.verification_status,
|
|
),
|
|
)
|