Files
F-r-ldraledigplannerare/backend/app/routers/analytics.py
T
jacobh2 cc3ced905e Link planned days to their wish, fix the age-4 cap, drop the planning goal
Three changes that together unblock treating a calendar period as one
editable object.

Migration 008 adds `planned_leave_day.wish_id` (nullable, ON DELETE SET NULL)
so a persisted day knows which `leave_wish` produced it, `is_pinned` to mark a
day the user hand-edited, and `leave_wish.target_days` so a period drawn on a
calendar need not be a whole number of weeks. Apply-time identity still keys on
day_type/is_dubbeldag only, so a wish_id-only difference is a metadata update
rather than a collision.

The age-4 savable ceiling was applying the per-child figure of 96 to each
parent's pool, modelling a two-parent family as able to save 192 days past the
child's fourth birthday. `resolve_age4_savable_cap` is now the single place
that decides 48 (couple with two parents) versus 96 (everyone else), and both
AnalyticsService and the allocator read it.

Migration 009 drops `workspace.planning_goal` and `leave_wish.goal_override`.
The allocator's tier-3 policy collapses to one rule; `salary_type` still
branches, since that reflects what a vacation day actually costs a parent
rather than a user preference.
2026-09-15 20:28:06 +02:00

89 lines
3.2 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,
WorkspaceRepositoryDep,
)
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,
workspace_repo: WorkspaceRepositoryDep,
) -> AnalyticsService:
return AnalyticsService(leave_repo, planned_repo, child_repo, pd_repo, workspace_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,
workspace_repo: WorkspaceRepositoryDep,
_current_user: CurrentUserDep,
) -> WorkspaceOverviewResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo, workspace_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,
workspace_repo: WorkspaceRepositoryDep,
_current_user: CurrentUserDep,
) -> WorkspaceOverviewResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo, workspace_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,
workspace_repo: WorkspaceRepositoryDep,
_current_user: CurrentUserDep,
) -> Age4ProjectionResponse:
service = _make_service(leave_repo, planned_repo, child_repo, pd_repo, workspace_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)