fdbfb7d580
Three faults, all invisible while the tests passed. Advice was built for the first child only, so a second child got none. The planned days feeding every count were fetched once per parent and appended to one list, so a couple's days were counted twice and every intensity and projection derived from them was wrong. And dismissing a tip forgot the moment the page reloaded. Tips now carry the date they concern, lifted from figures the rules already computed, so a view of a month can show only what applies to it. They are built per child and identified per child, so the same rule firing for two children is two separate pieces of advice. A dismissal is stored against a fingerprint of the facts that were dismissed. When those facts change the advice returns with its new numbers, rather than one click silencing a warning about a forfeiture that keeps growing.
169 lines
5.5 KiB
Python
169 lines
5.5 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 (
|
|
ChildServiceDep,
|
|
CurrentUserDep,
|
|
PersonalDataServiceDep,
|
|
TipsServiceDep,
|
|
)
|
|
from app.models.child import ChildCreate, ChildResponse, ChildUpdate
|
|
from app.models.personal_data import PersonalDataResponse, PersonalDataUpdate
|
|
from app.models.tips import TipDismissalCreate, TipResponse
|
|
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[TipResponse])
|
|
async def get_tips(
|
|
workspace_id: UUID,
|
|
service: TipsServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> list[TipResponse]:
|
|
tips = await service.get_tips(workspace_id)
|
|
return [TipResponse.from_domain(tip) for tip in tips]
|
|
|
|
|
|
@tips_router.post("/{workspace_id}/dismissals", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def dismiss_tip(
|
|
workspace_id: UUID,
|
|
payload: TipDismissalCreate,
|
|
service: TipsServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> None:
|
|
try:
|
|
await service.dismiss(workspace_id, payload.tip_id)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 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
|