Files
F-r-ldraledigplannerare/backend/app/routers/settings.py
T
jacobh2 76b1e712b9 Complete NextSteps roadmap: planning engine UI, calendar cluster, flexible wishes
Works through every numbered item in NextSteps260913.md section 3, plus the
three known verification issues in section 4.1.

Surface what the backend already computed:
- 3.1 render surplus_advice in PlanResults, mirroring the shortfall remedies
- 3.2 surface VacationBalance.is_assumed so assumed statutory years are visible
- 3.3 age-4 expiry given the proposed plan, via a new Age4Projection reusing
  the existing _age4_projections arithmetic (Dashboard stat verified, not rebuilt)

Calendar cluster (one pass over the holiday data):
- new GET /api/calendar/days exposing roda dagar, klamdagar and day overrides,
  paginated four years at a time
- 3.4 shade roda dagar/klamdagar, with R/K badges so colour is not the only signal
- 3.6 click-to-override behind an explicit mode toggle, leaving drag multi-select intact
- 3.7 click month/year to advance
- 3.8 per-year red-days list in Settings, statutory days plus overrides

i18n:
- 3.5 five backend-generated Swedish surfaces converted to message_key + data,
  following the tips_service pattern; old string fields replaced, not kept

Flexible wishes (3.9):
- PLANNING_ENGINE_SPEC section 10: window_mode, candidate search, cost function,
  deterministic tie-break, two-pass ordering, test matrix rows 15-24
- backend: entity, migration 006, _FULL_SCHEMA sync, repository, allocator
- frontend: season-preset toggle in WishEditor, unplaced outcome rendering

Fixes (4.1):
- pin typecheck to the local tsc rather than npx resolving a newer TypeScript
- ignore generated coverage/ and dist/ in the ESLint flat config
- fix the flaky usePlanApply test properly, with waitFor rather than a retry

Tests: backend 361 -> 404, frontend 540 -> 704. Coverage 89.47% -> 96.52% lines.

Not yet verified in a browser.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
2026-09-14 02:33:03 +02:00

174 lines
5.7 KiB
Python

"""HTTP routes for settings and tips.
Covers:
- Children (CRUD) — uses models/child.py
- Tips endpoint
Leave wishes (formerly "break periods") moved to their own router,
app/routers/leave_wishes.py, at /api/leave-wishes — see PLANNING_ENGINE_SPEC.md §4.
"""
from uuid import UUID
from fastapi import APIRouter, HTTPException, status
from app.dependencies import (
ChildRepositoryDep,
ChildServiceDep,
CurrentUserDep,
LeaveWishRepositoryDep,
ParentalLeaveRepositoryDep,
PersonalDataRepositoryDep,
PersonalDataServiceDep,
PlannedLeaveRepositoryDep,
VacationDaysRepositoryDep,
WorkspaceRepositoryDep,
)
from app.models.child import ChildCreate, ChildResponse, ChildUpdate
from app.models.personal_data import PersonalDataResponse, PersonalDataUpdate
from app.services.tips_service import TipsService
from app.utils.errors import NotFoundError, ValidationError
router = APIRouter(tags=["settings"])
# ---------------------------------------------------------------------------
# Tips
# ---------------------------------------------------------------------------
tips_router = APIRouter(prefix="/api/tips", tags=["tips"])
@tips_router.get("/{workspace_id}", response_model=list[dict])
async def get_tips(
workspace_id: UUID,
leave_repo: ParentalLeaveRepositoryDep,
planned_repo: PlannedLeaveRepositoryDep,
child_repo: ChildRepositoryDep,
pd_repo: PersonalDataRepositoryDep,
vacation_repo: VacationDaysRepositoryDep,
leave_wish_repo: LeaveWishRepositoryDep,
ws_repo: WorkspaceRepositoryDep,
_current_user: CurrentUserDep,
) -> list[dict]:
service = TipsService(
leave_repo, planned_repo, child_repo, pd_repo, vacation_repo, leave_wish_repo, ws_repo
)
tips = await service.get_tips(workspace_id)
return [
{"id": t.id, "severity": t.severity, "message_key": t.message_key, "data": t.data}
for t in tips
]
# ---------------------------------------------------------------------------
# Children
# ---------------------------------------------------------------------------
child_router = APIRouter(prefix="/api/children", tags=["children"])
def _child_to_response(child) -> ChildResponse:
return ChildResponse.from_entity(child)
@child_router.post("", response_model=ChildResponse, status_code=status.HTTP_201_CREATED)
async def create_child(
payload: ChildCreate,
service: ChildServiceDep,
_current_user: CurrentUserDep,
) -> ChildResponse:
try:
child = await service.create(
workspace_id=payload.workspace_id,
name=payload.name,
birth_date=payload.birth_date,
preschool_start_date=payload.preschool_start_date,
preschool_planning_days_per_year=payload.preschool_planning_days_per_year,
)
return _child_to_response(child)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
@child_router.get("/{child_id}", response_model=ChildResponse)
async def get_child(
child_id: UUID,
service: ChildServiceDep,
_current_user: CurrentUserDep,
) -> ChildResponse:
try:
child = await service.get(child_id)
return _child_to_response(child)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@child_router.get("/workspace/{workspace_id}", response_model=list[ChildResponse])
async def list_children(
workspace_id: UUID,
service: ChildServiceDep,
_current_user: CurrentUserDep,
) -> list[ChildResponse]:
children = await service.list_by_workspace(workspace_id)
return [_child_to_response(c) for c in children]
# Settings endpoint for updating child
settings_child_router = APIRouter(prefix="/api/settings", tags=["settings"])
@settings_child_router.put("/child/{child_id}", response_model=ChildResponse)
async def update_child(
child_id: UUID,
payload: ChildUpdate,
service: ChildServiceDep,
_current_user: CurrentUserDep,
) -> ChildResponse:
try:
child = await service.update(
child_id=child_id,
name=payload.name,
birth_date=payload.birth_date,
preschool_start_date=payload.preschool_start_date,
preschool_planning_days_per_year=payload.preschool_planning_days_per_year,
)
return _child_to_response(child)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@settings_child_router.delete("/{child_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_child(
child_id: UUID,
service: ChildServiceDep,
_current_user: CurrentUserDep,
) -> None:
try:
await service.delete(child_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@settings_child_router.put("/parent/{personal_data_uuid}", response_model=PersonalDataResponse)
async def update_parent_settings(
personal_data_uuid: UUID,
payload: PersonalDataUpdate,
service: PersonalDataServiceDep,
_current_user: CurrentUserDep,
) -> PersonalDataResponse:
try:
updated = await service.update(
personal_data_uuid,
salary_type=payload.salary_type,
employment_status=payload.employment_status,
min_vacation_days_per_year=payload.min_vacation_days_per_year,
)
return PersonalDataResponse.from_entity(updated)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc