02db5430eb
Security - Every route now requires a login and refuses ids outside the caller's workspace with 404: plan, leave-wishes, day-overrides and vacation-days were fully anonymous; every other router trusted client-supplied workspace/child/parent ids. Scraper sessions are bound to their owner. - Invitations can no longer be created for a foreign workspace, and a redeemer who is already a member (or a full workspace) is refused. - Production refuses the default JWT secret; API docs are hidden in production; a failed migration aborts startup instead of serving. - Login and register no longer reveal whether an email exists; inactive accounts are rejected; /auth/refresh is rate limited. - The FK scraper writes debug artefacts only when explicitly enabled, logs no scraped values or page text, clears the BankID QR as soon as it is scanned and purges finished sessions after five minutes. - Real FK page captures are removed from the tree (history rewrite follows); the parser tests use a synthetic fixture. Performance and correctness - Plan apply/sync run inside one locked transaction with batched writes instead of one commit per row, so a failure mid-batch leaves nothing half-applied and concurrent syncs no longer corrupt each other. - SQLite runs in WAL mode with a busy timeout; 14 indexes cover the filters that were full scans; argon2 hashing runs off the event loop; responses are gzipped. - Frontend: a 401 now refreshes the token once and retries, so sessions no longer die silently after 15 minutes; every data hook discards stale responses; logout clears state even when the server call fails. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CsXCCcDfmV4Sps4ZQSvNEY
92 lines
3.3 KiB
Python
92 lines
3.3 KiB
Python
"""HTTP routes for analytics / projections."""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
|
|
from app.dependencies import (
|
|
ChildRepositoryDep,
|
|
ParentalLeaveRepositoryDep,
|
|
PersonalDataRepositoryDep,
|
|
PlannedLeaveRepositoryDep,
|
|
WorkspaceIdDep,
|
|
WorkspaceRepositoryDep,
|
|
WorkspaceScopeDep,
|
|
)
|
|
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: WorkspaceIdDep,
|
|
child_id: UUID,
|
|
leave_repo: ParentalLeaveRepositoryDep,
|
|
planned_repo: PlannedLeaveRepositoryDep,
|
|
child_repo: ChildRepositoryDep,
|
|
pd_repo: PersonalDataRepositoryDep,
|
|
workspace_repo: WorkspaceRepositoryDep,
|
|
) -> 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: WorkspaceIdDep,
|
|
child_id: UUID,
|
|
leave_repo: ParentalLeaveRepositoryDep,
|
|
planned_repo: PlannedLeaveRepositoryDep,
|
|
child_repo: ChildRepositoryDep,
|
|
pd_repo: PersonalDataRepositoryDep,
|
|
workspace_repo: WorkspaceRepositoryDep,
|
|
) -> 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,
|
|
scope: WorkspaceScopeDep,
|
|
) -> Age4ProjectionResponse:
|
|
await scope.check_member(personal_data_uuid)
|
|
await scope.check_child(child_id)
|
|
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, workspace_id=scope.workspace_id
|
|
)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
return Age4ProjectionResponse.from_entity(proj)
|