Files
F-r-ldraledigplannerare/backend/app/routers/analytics.py
T
2026-06-01 16:48:21 +02:00

84 lines
2.9 KiB
Python

"""HTTP routes for analytics / projections."""
from uuid import UUID
from fastapi import APIRouter, HTTPException, status
from app.dependencies import (
ChildRepositoryDep,
CurrentUserDep,
ParentalLeaveRepositoryDep,
PersonalDataRepositoryDep,
PlannedLeaveRepositoryDep,
)
from app.models.analytics import Age4ProjectionResponse, WorkspaceOverviewResponse
from app.services.analytics_service import AnalyticsService
from app.utils.errors import NotFoundError
router = APIRouter(prefix="/api/analytics", tags=["analytics"])
def _make_service(
leave_repo: ParentalLeaveRepositoryDep,
planned_repo: PlannedLeaveRepositoryDep,
child_repo: ChildRepositoryDep,
pd_repo: PersonalDataRepositoryDep,
) -> AnalyticsService:
return AnalyticsService(leave_repo, planned_repo, child_repo, pd_repo)
@router.get("/{workspace_id}/overview", response_model=WorkspaceOverviewResponse)
async def get_overview(
workspace_id: UUID,
child_id: UUID,
leave_repo: ParentalLeaveRepositoryDep,
planned_repo: PlannedLeaveRepositoryDep,
child_repo: ChildRepositoryDep,
pd_repo: PersonalDataRepositoryDep,
_current_user: CurrentUserDep,
) -> WorkspaceOverviewResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo)
try:
overview = await service.get_overview(workspace_id, child_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
return WorkspaceOverviewResponse.from_entity(overview)
@router.get("/{workspace_id}/projection", response_model=WorkspaceOverviewResponse)
async def get_projection(
workspace_id: UUID,
child_id: UUID,
leave_repo: ParentalLeaveRepositoryDep,
planned_repo: PlannedLeaveRepositoryDep,
child_repo: ChildRepositoryDep,
pd_repo: PersonalDataRepositoryDep,
_current_user: CurrentUserDep,
) -> WorkspaceOverviewResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo)
try:
overview = await service.get_overview(workspace_id, child_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
return WorkspaceOverviewResponse.from_entity(overview)
@router.get(
"/parent/{personal_data_uuid}/child/{child_id}/age4", response_model=Age4ProjectionResponse
)
async def get_age4_projection(
personal_data_uuid: UUID,
child_id: UUID,
leave_repo: ParentalLeaveRepositoryDep,
planned_repo: PlannedLeaveRepositoryDep,
child_repo: ChildRepositoryDep,
pd_repo: PersonalDataRepositoryDep,
_current_user: CurrentUserDep,
) -> Age4ProjectionResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo)
try:
proj = await service.get_days_left_at_age_4(personal_data_uuid, child_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
return Age4ProjectionResponse.from_entity(proj)