feat: purchases added
This commit is contained in:
@@ -16,6 +16,12 @@ from .external_channel.delete_external_channel import delete_external_channel
|
||||
from .external_channel.get_external_channels import get_external_channels
|
||||
from .external_channel.import_external_channels_from_excel import import_external_channels_from_excel
|
||||
from .external_channel.update_external_channel_links import update_external_channel_links
|
||||
from .purchase.archive_purchase import archive_purchase
|
||||
from .purchase.create_purchase import create_purchase
|
||||
from .purchase.delete_purchase import delete_purchase
|
||||
from .purchase.get_purchase import get_purchase
|
||||
from .purchase.get_purchases import get_purchases
|
||||
from .purchase.update_purchase import update_purchase
|
||||
from .target_channel.connect_target_chan import connect_target_chan
|
||||
from .target_channel.disconnect_target_chan import disconnect_target_chan
|
||||
from .target_channel.disconnect_target_chan_by_tg_id import disconnect_target_chan_by_tg_id
|
||||
@@ -84,6 +90,26 @@ class Database(typing.Protocol):
|
||||
|
||||
async def delete_creative(self, creative_id: UUID) -> None: ...
|
||||
|
||||
async def get_purchase(self, user_id: UUID, purchase_id: UUID) -> domain.Purchase | None: ...
|
||||
|
||||
async def create_purchase(self, purchase: domain.Purchase) -> domain.Purchase: ...
|
||||
|
||||
async def update_purchase(self, purchase: domain.Purchase) -> domain.Purchase: ...
|
||||
|
||||
async def get_user_purchases(
|
||||
self,
|
||||
user_id: UUID,
|
||||
*,
|
||||
target_channel_id: UUID | None = None,
|
||||
external_channel_id: UUID | None = None,
|
||||
creative_id: UUID | None = None,
|
||||
include_archived: bool = False,
|
||||
) -> list[domain.Purchase]: ...
|
||||
|
||||
async def delete_purchase(self, purchase_id: UUID) -> None: ...
|
||||
|
||||
async def check_creative_in_use(self, creative_id: UUID) -> bool: ...
|
||||
|
||||
|
||||
class TelegramWriter(typing.Protocol):
|
||||
async def send_message(self, text: str, chat_id: int) -> None: ...
|
||||
@@ -92,6 +118,8 @@ class TelegramWriter(typing.Protocol):
|
||||
|
||||
async def get_chat_member(self, chat_id: int, user_id: int) -> dict[str, typing.Any]: ...
|
||||
|
||||
async def create_chat_invite_link(self, chat_id: int, requires_approval: bool = False) -> str: ...
|
||||
|
||||
|
||||
class JWTEncoder(typing.Protocol):
|
||||
def encode_access_token(self, user_id: UUID, telegram_id: int, username: str | None = None) -> str: ...
|
||||
@@ -121,3 +149,9 @@ class Usecase:
|
||||
update_creative = update_creative
|
||||
archive_creative = archive_creative
|
||||
delete_creative = delete_creative
|
||||
get_purchases = get_purchases
|
||||
get_purchase = get_purchase
|
||||
create_purchase = create_purchase
|
||||
update_purchase = update_purchase
|
||||
archive_purchase = archive_purchase
|
||||
delete_purchase = delete_purchase
|
||||
|
||||
41
src/usecase/purchase/archive_purchase.py
Normal file
41
src/usecase/purchase/archive_purchase.py
Normal file
@@ -0,0 +1,41 @@
|
||||
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 archive_purchase(self: 'Usecase', purchase_id: uuid.UUID, 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)
|
||||
|
||||
purchase.status = domain.PurchaseStatus.ARCHIVED
|
||||
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,
|
||||
created_at=updated.created_at,
|
||||
)
|
||||
83
src/usecase/purchase/create_purchase.py
Normal file
83
src/usecase/purchase/create_purchase.py
Normal file
@@ -0,0 +1,83 @@
|
||||
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 create_purchase(self: 'Usecase', input: dto.CreatePurchaseInput, user_id: uuid.UUID) -> dto.PurchaseOutput:
|
||||
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 purchase 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 purchase 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 purchase 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_writer.create_chat_invite_link(
|
||||
chat_id=target_channel.telegram_id,
|
||||
requires_approval=requires_approval,
|
||||
)
|
||||
|
||||
# Создаём закуп
|
||||
purchase = domain.Purchase(
|
||||
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,
|
||||
ad_post_url=input.ad_post_url,
|
||||
invite_link_type=input.invite_link_type,
|
||||
invite_link=invite_link,
|
||||
status=domain.PurchaseStatus.ACTIVE,
|
||||
)
|
||||
|
||||
created = await self.database.create_purchase(purchase)
|
||||
|
||||
# Увеличиваем счётчик закупов у креатива
|
||||
creative.purchases_count += 1
|
||||
await self.database.update_creative(creative)
|
||||
|
||||
return dto.PurchaseOutput(
|
||||
id=created.id,
|
||||
target_channel_id=created.target_channel_id,
|
||||
target_channel_title=created.target_channel.title,
|
||||
external_channel_id=created.external_channel_id,
|
||||
external_channel_title=created.external_channel.title,
|
||||
creative_id=created.creative_id,
|
||||
creative_name=created.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,
|
||||
created_at=created.created_at,
|
||||
)
|
||||
26
src/usecase/purchase/delete_purchase.py
Normal file
26
src/usecase/purchase/delete_purchase.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def delete_purchase(self: 'Usecase', input: dto.DeletePurchaseInput) -> None:
|
||||
async with self.database.transaction():
|
||||
purchase = await self.database.get_purchase(input.user_id, input.purchase_id)
|
||||
if not purchase:
|
||||
log.warning('Purchase %s not found for user %s', input.purchase_id, input.user_id)
|
||||
raise domain.PurchaseNotFound(input.purchase_id)
|
||||
|
||||
# Уменьшаем счётчик закупов у креатива, если закуп был активным
|
||||
if purchase.status == domain.PurchaseStatus.ACTIVE:
|
||||
creative = await self.database.get_creative(input.user_id, purchase.creative_id)
|
||||
if creative and creative.purchases_count > 0:
|
||||
creative.purchases_count -= 1
|
||||
await self.database.update_creative(creative)
|
||||
|
||||
await self.database.delete_purchase(input.purchase_id)
|
||||
37
src/usecase/purchase/get_purchase.py
Normal file
37
src/usecase/purchase/get_purchase.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_purchase(self: 'Usecase', input: dto.GetPurchaseInput) -> dto.PurchaseOutput:
|
||||
async with self.database.transaction():
|
||||
purchase = await self.database.get_purchase(input.user_id, input.purchase_id)
|
||||
if not purchase:
|
||||
log.warning('Purchase %s not found for user %s', input.purchase_id, input.user_id)
|
||||
raise domain.PurchaseNotFound(input.purchase_id)
|
||||
|
||||
return dto.PurchaseOutput(
|
||||
id=purchase.id,
|
||||
target_channel_id=purchase.target_channel_id,
|
||||
target_channel_title=purchase.target_channel.title,
|
||||
external_channel_id=purchase.external_channel_id,
|
||||
external_channel_title=purchase.external_channel.title,
|
||||
creative_id=purchase.creative_id,
|
||||
creative_name=purchase.creative.name,
|
||||
placement_date=purchase.placement_date,
|
||||
cost=purchase.cost,
|
||||
comment=purchase.comment,
|
||||
ad_post_url=purchase.ad_post_url,
|
||||
invite_link_type=purchase.invite_link_type,
|
||||
invite_link=purchase.invite_link,
|
||||
status=purchase.status,
|
||||
subscriptions_count=purchase.subscriptions_count,
|
||||
views_count=purchase.views_count,
|
||||
created_at=purchase.created_at,
|
||||
)
|
||||
42
src/usecase/purchase/get_purchases.py
Normal file
42
src/usecase/purchase/get_purchases.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
|
||||
async def get_purchases(self: 'Usecase', input: dto.GetPurchasesInput) -> dto.GetPurchasesOutput:
|
||||
async with self.database.transaction():
|
||||
purchases = await self.database.get_user_purchases(
|
||||
input.user_id,
|
||||
target_channel_id=input.target_channel_id,
|
||||
external_channel_id=input.external_channel_id,
|
||||
creative_id=input.creative_id,
|
||||
include_archived=input.include_archived,
|
||||
)
|
||||
|
||||
return dto.GetPurchasesOutput(
|
||||
purchases=[
|
||||
dto.PurchaseOutput(
|
||||
id=purchase.id,
|
||||
target_channel_id=purchase.target_channel_id,
|
||||
target_channel_title=purchase.target_channel.title,
|
||||
external_channel_id=purchase.external_channel_id,
|
||||
external_channel_title=purchase.external_channel.title,
|
||||
creative_id=purchase.creative_id,
|
||||
creative_name=purchase.creative.name,
|
||||
placement_date=purchase.placement_date,
|
||||
cost=purchase.cost,
|
||||
comment=purchase.comment,
|
||||
ad_post_url=purchase.ad_post_url,
|
||||
invite_link_type=purchase.invite_link_type,
|
||||
invite_link=purchase.invite_link,
|
||||
status=purchase.status,
|
||||
subscriptions_count=purchase.subscriptions_count,
|
||||
views_count=purchase.views_count,
|
||||
created_at=purchase.created_at,
|
||||
)
|
||||
for purchase in purchases
|
||||
]
|
||||
)
|
||||
52
src/usecase/purchase/update_purchase.py
Normal file
52
src/usecase/purchase/update_purchase.py
Normal file
@@ -0,0 +1,52 @@
|
||||
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
|
||||
|
||||
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,
|
||||
created_at=updated.created_at,
|
||||
)
|
||||
Reference in New Issue
Block a user