Files
tgex-backend/src/usecase/purchase/update_purchase.py
Artem Tsyrulnikov 552a3ddef2 feat: полный patch External Channel архивирование creative в patch и
purchase, удалить у них доп ручки/usecase/dto
2025-11-11 23:16:38 +03:00

57 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 update_purchase(
self: 'Usecase', purchase_id: uuid.UUID, input: dto.UpdatePurchaseInput, user_id: uuid.UUID
) -> dto.PurchaseOutput:
async with self.database.transaction():
purchase = await self.database.get_purchase(user_id, purchase_id)
if not purchase:
log.warning('Purchase %s not found for user %s', purchase_id, user_id)
raise domain.PurchaseNotFound(purchase_id)
# Обновляем только те поля, которые были переданы
if input.placement_date is not None:
purchase.placement_date = input.placement_date
if input.cost is not None:
purchase.cost = input.cost
if input.comment is not None:
purchase.comment = input.comment
if input.ad_post_url is not None:
purchase.ad_post_url = input.ad_post_url
if input.status is not None:
purchase.status = input.status
updated = await self.database.update_purchase(purchase)
return dto.PurchaseOutput(
id=updated.id,
target_channel_id=updated.target_channel_id,
target_channel_title=updated.target_channel.title,
external_channel_id=updated.external_channel_id,
external_channel_title=updated.external_channel.title,
creative_id=updated.creative_id,
creative_name=updated.creative.name,
placement_date=updated.placement_date,
cost=updated.cost,
comment=updated.comment,
ad_post_url=updated.ad_post_url,
invite_link_type=updated.invite_link_type,
invite_link=updated.invite_link,
status=updated.status,
subscriptions_count=updated.subscriptions_count,
views_count=updated.views_count,
views_availability=updated.views_availability,
last_views_fetch_at=updated.last_views_fetch_at,
created_at=updated.created_at,
)