Files
tgex-backend/src/usecase/views/get_views_history.py
2025-12-14 13:51:59 +03:00

42 lines
1.3 KiB
Python

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_views_history(self: 'Usecase', input: dto.GetViewsHistoryInput) -> dto.GetViewsHistoryOutput:
context = await self.ensure_workspace_permission(
input.workspace_id, input.user_id, domain.PermissionKey.PLACEMENTS_READ
)
placement = await self.database.get_placement(input.workspace_id, input.placement_id)
if not placement:
log.warning('Placement %s not found for user %s', input.placement_id, input.user_id)
raise domain.PlacementNotFound(input.placement_id)
context.ensure_project_permission(domain.PermissionKey.PLACEMENTS_READ, placement.project_id)
histories = await self.database.get_views_history(
input.placement_id,
from_date=input.from_date,
to_date=input.to_date,
)
return dto.GetViewsHistoryOutput(
histories=[
dto.PlacementViewsHistoryOutput(
id=history.id,
placement_id=history.placement_id,
views_count=history.views_count,
fetched_at=history.fetched_at,
created_at=history.created_at,
)
for history in histories
]
)