refactor, удалены интерфейс БД из-за слабой необходимости

This commit is contained in:
Artem Tsyrulnikov
2026-01-18 13:08:04 +03:00
parent 049024ebe8
commit 4705e88663
8 changed files with 193 additions and 427 deletions

View File

@@ -1,4 +1,3 @@
import datetime
import typing
from dataclasses import dataclass
from uuid import UUID
@@ -6,6 +5,8 @@ from uuid import UUID
if typing.TYPE_CHECKING:
from aiogram.types import InlineKeyboardButton
from src.adapter.postgres import Postgres
from src import domain
from .analytics.get_channel_analytics import get_channel_analytics
@@ -56,237 +57,9 @@ from .workspace.update_workspace import update_workspace
from .workspace.update_workspace_member_permissions import update_workspace_member_permissions
class Database(typing.Protocol):
def transaction(self) -> typing.AsyncContextManager[None]: ...
async def get_user(
self,
user_id: UUID | None = None,
telegram_id: int | None = None,
) -> domain.User | None: ...
async def get_user_by_username(self, username: str) -> domain.User | None: ...
async def create_user(self, user: domain.User) -> None: ...
async def get_telegram_user(
self,
telegram_user_id: UUID | None = None,
telegram_id: int | None = None,
) -> domain.TelegramUser | None: ...
async def create_telegram_user(self, telegram_user: domain.TelegramUser) -> None: ...
async def update_telegram_user(self, telegram_user: domain.TelegramUser) -> None: ...
async def create_workspace(self, workspace: domain.Workspace) -> None: ...
async def update_workspace(self, workspace: domain.Workspace) -> None: ...
async def delete_workspace(self, workspace_id: UUID) -> None: ...
async def add_user_to_workspace(self, workspace_id: UUID, user_id: UUID) -> domain.WorkspaceUser: ...
async def set_workspace_user_permissions(
self,
workspace_user_id: UUID,
global_permissions: set[domain.PermissionKey],
scoped_permissions: list[tuple[domain.PermissionKey, domain.PermissionScopeType, UUID]],
) -> None: ...
async def update_workspace_user(self, workspace_user: domain.WorkspaceUser) -> None: ...
async def get_user_workspaces(self, user_id: UUID) -> list[domain.WorkspaceUser]: ...
async def get_workspace(self, workspace_id: UUID) -> domain.Workspace | None: ...
async def get_workspace_for_user(self, workspace_id: UUID, user_id: UUID) -> domain.Workspace | None: ...
async def get_default_workspace_for_user(self, user_id: UUID) -> domain.Workspace | None: ...
async def get_workspace_membership(self, workspace_id: UUID, user_id: UUID) -> domain.WorkspaceUser | None: ...
async def get_workspace_members(self, workspace_id: UUID) -> list[domain.WorkspaceUser]: ...
async def get_workspace_member(self, workspace_user_id: UUID) -> domain.WorkspaceUser | None: ...
async def create_workspace_invite(self, invite: domain.WorkspaceInvite) -> None: ...
async def update_workspace_invite(self, invite: domain.WorkspaceInvite) -> None: ...
async def get_workspace_invite(self, invite_id: UUID) -> domain.WorkspaceInvite | None: ...
async def get_workspace_invite_by_user(
self, workspace_id: UUID, user_id: UUID
) -> domain.WorkspaceInvite | None: ...
async def get_workspace_invites(self, workspace_id: UUID) -> list[domain.WorkspaceInvite]: ...
async def create_login_token(self, login_token: domain.LoginToken) -> None: ...
async def create_creative(self, creative: domain.Creative) -> None: ...
async def get_login_token(self, token: str) -> domain.LoginToken | None: ...
async def mark_token_as_used(self, token: str) -> None: ...
async def update_user(self, user: domain.User) -> None: ...
async def get_channel(
self,
channel_id: UUID | None = None,
telegram_id: int | None = None,
username: str | None = None,
) -> domain.Channel | None: ...
async def create_channel(self, channel: domain.Channel) -> None: ...
async def update_channel(self, channel: domain.Channel) -> None: ...
async def delete_channel(self, channel_id: UUID) -> None: ...
async def search_channels(self, username_query: str | None = None) -> list[domain.Channel]: ...
async def get_project(
self, workspace_id: UUID, project_id: UUID | None = None, channel_id: UUID | None = None
) -> domain.Project | None: ...
async def get_project_for_user_by_telegram(
self, user_id: UUID, channel_telegram_id: int
) -> domain.Project | None: ...
async def get_project_by_channel_telegram(self, channel_telegram_id: int) -> domain.Project | None: ...
async def create_project(self, project: domain.Project) -> None: ...
async def update_project(self, project: domain.Project) -> None: ...
async def get_workspace_projects(
self, workspace_id: UUID, allowed_project_ids: set[UUID] | None = None, include_archived: bool = False
) -> list[domain.Project]: ...
async def archive_project(self, workspace_id: UUID, project_id: UUID) -> None: ...
async def delete_project(self, workspace_id: UUID, project_id: UUID) -> None: ...
async def check_channel_exists_in_workspace(self, channel_id: UUID, workspace_id: UUID) -> bool: ...
# Placement methods (user-managed, бывший PurchaseChannel)
async def create_placement(self, placement: domain.Placement) -> None: ...
async def create_placements(self, placements: list[domain.Placement]) -> None: ...
async def get_placement(self, workspace_id: UUID, placement_id: UUID) -> domain.Placement | None: ...
async def get_project_placements(
self, workspace_id: UUID, project_id: UUID, include_archived: bool = False
) -> list[domain.Placement]: ...
async def update_placement_user(self, placement: domain.Placement) -> None: ...
async def get_creative(self, workspace_id: UUID, creative_id: UUID) -> domain.Creative | None: ...
async def update_creative(self, creative: domain.Creative) -> None: ...
async def get_workspace_creatives(
self,
workspace_id: UUID,
project_id: UUID | None = None,
include_archived: bool = False,
allowed_project_ids: set[UUID] | None = None,
) -> list[domain.Creative]: ...
async def delete_creative(self, creative_id: UUID) -> None: ...
# Publication methods (system-managed, бывший Placement)
async def get_publication(self, workspace_id: UUID, publication_id: UUID) -> domain.Publication | None: ...
async def get_workspace_publications(
self,
workspace_id: UUID,
project_id: UUID | None = None,
placement_channel_id: UUID | None = None,
creative_id: UUID | None = None,
include_archived: bool = False,
allowed_project_ids: set[UUID] | None = None,
date_from: datetime.datetime | None = None,
date_to: datetime.datetime | None = None,
) -> list[domain.Publication]: ...
async def create_publication(self, publication: domain.Publication) -> None: ...
async def update_publication(self, publication: domain.Publication) -> None: ...
async def delete_publication(self, publication_id: UUID) -> None: ...
async def get_publication_by_invite_link(self, invite_link: str) -> domain.Publication | None: ...
# Subscription methods
async def create_subscription(self, subscription: domain.Subscription) -> None: ...
async def get_subscription_by_subscriber_and_publication(
self, telegram_user_id: UUID, publication_id: UUID
) -> domain.Subscription | None: ...
async def update_subscription(self, subscription: domain.Subscription) -> None: ...
async def get_subscriptions_for_publications(
self,
publication_ids: list[UUID],
*,
date_from: datetime.datetime | None = None,
date_to: datetime.datetime | None = None,
) -> list[domain.Subscription]: ...
async def get_active_subscriptions_by_subscriber_and_project(
self, telegram_user_id: UUID, project_id: UUID
) -> list[domain.Subscription]: ...
async def get_active_subscription_by_subscriber_and_project(
self, telegram_user_id: UUID, project_id: UUID
) -> domain.Subscription | None: ...
# Count methods
async def count_publications_by_creative(self, creative_id: UUID) -> int: ...
async def count_subscriptions_by_publication(self, publication_id: UUID) -> int: ...
async def count_publications_by_creative_batch(self, creative_ids: list[UUID]) -> dict[UUID, int]: ...
async def count_subscriptions_by_publication_batch(
self, publication_ids: list[UUID]
) -> dict[UUID, int]: ...
async def has_publications_for_creative(self, creative_id: UUID) -> bool: ...
# Post methods
async def create_post(self, post: domain.Post) -> None: ...
async def get_post(self, post_id: UUID) -> domain.Post | None: ...
async def get_post_by_channel_and_message(self, channel_id: UUID, message_id: int) -> domain.Post | None: ...
# Post views history
async def create_post_views_history(self, history: domain.PostViewsHistory) -> None: ...
async def get_views_history(
self,
post_id: UUID,
*,
from_date: datetime.datetime | None = None,
to_date: datetime.datetime | None = None,
) -> list[domain.PostViewsHistory]: ...
async def get_latest_views_data(self, post_id: UUID) -> tuple[int, datetime.datetime] | None: ...
async def get_latest_views_data_batch(self, post_ids: list[UUID]) -> dict[UUID, tuple[int, datetime.datetime]]: ...
class TelegramBotWriter(typing.Protocol):
async def send_message(self, text: str, chat_id: int) -> None: ...
async def send_message_with_button(self, text: str, chat_id: int, button_text: str, button_url: str) -> None: ...
async def send_message_with_inline_keyboard(
self, text: str, chat_id: int, buttons: list[list['InlineKeyboardButton']]
) -> None: ...
@@ -311,10 +84,6 @@ class Parser(typing.Protocol):
class S3Storage(typing.Protocol):
async def connect(self) -> None: ...
async def close(self) -> None: ...
async def upload(self, key: str, data: bytes, content_type: str) -> None: ...
async def get(self, key: str) -> bytes: ...
@@ -324,26 +93,14 @@ class S3Storage(typing.Protocol):
@dataclass
class Usecase:
database: Database
database: 'Postgres'
telegram_bot: TelegramBotWriter
jwt_encoder: JWTEncoder
parser: Parser
s3: S3Storage
async def ensure_workspace_access(self, workspace_id: UUID, user_id: UUID) -> domain.Workspace:
workspace = await self.database.get_workspace_for_user(workspace_id, user_id)
if not workspace:
raise domain.WorkspaceNotFound(workspace_id)
return workspace
async def ensure_workspace_permission(
self,
workspace_id: UUID,
user_id: UUID,
permission: domain.PermissionKey,
*,
for_project_id: UUID | None = None,
self, workspace_id: UUID, user_id: UUID, permission: domain.PermissionKey, *, for_project_id: UUID | None = None
) -> domain.WorkspacePermissionContext:
membership = await self.database.get_workspace_membership(workspace_id, user_id)
if not membership or not membership.workspace:

View File

@@ -233,7 +233,6 @@ async def get_projects_analytics(
total_metrics.total_subscriptions += subs_count
total_metrics.clicks_count += subs_count
views_count = 0
if publication.post and publication.post.id in views_map:
views_count = views_map[publication.post.id][0]
pd.total_views += views_count
@@ -245,7 +244,9 @@ async def get_projects_analytics(
placement = publication.placement
if placement and placement.cost_before_bargain and placement.cost_before_bargain > cost:
discount = placement.cost_before_bargain - cost
discount_percent = (discount / placement.cost_before_bargain) * 100 if placement.cost_before_bargain > 0 else 0.0
discount_percent = (
(discount / placement.cost_before_bargain) * 100 if placement.cost_before_bargain > 0 else 0.0
)
pd.total_discounts += discount
pd.discount_count += 1
@@ -296,4 +297,3 @@ async def get_projects_analytics(
totals = _calculate_metrics(total_metrics, input.metrics)
return dto.GetProjectsAnalyticsOutput(periods=periods, totals=totals)