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
93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
"""Business logic for child management."""
|
|
|
|
from datetime import datetime
|
|
from uuid import UUID, uuid4
|
|
|
|
from app.entities.child import Child
|
|
from app.repositories.child_repository import ChildRepository
|
|
from app.utils.errors import NotFoundError, ValidationError
|
|
|
|
|
|
class ChildService:
|
|
def __init__(self, repo: ChildRepository) -> None:
|
|
self._repo = repo
|
|
|
|
async def create(
|
|
self,
|
|
workspace_id: UUID,
|
|
name: str,
|
|
birth_date,
|
|
preschool_start_date=None,
|
|
preschool_planning_days_per_year: int = 6,
|
|
) -> Child:
|
|
"""Create a new child."""
|
|
if preschool_start_date is not None and preschool_start_date <= birth_date:
|
|
raise ValidationError("preschool_start_date must be after birth_date")
|
|
_validate_preschool_planning_days(preschool_planning_days_per_year)
|
|
|
|
now = datetime.now()
|
|
entity = Child(
|
|
id=uuid4(),
|
|
workspace_id=workspace_id,
|
|
name=name,
|
|
birth_date=birth_date,
|
|
preschool_start_date=preschool_start_date,
|
|
preschool_planning_days_per_year=preschool_planning_days_per_year,
|
|
created_at=now,
|
|
)
|
|
return await self._repo.create(entity)
|
|
|
|
async def get(self, child_id: UUID, workspace_id: UUID | None = None) -> Child:
|
|
"""Get a child by ID. `workspace_id`, when given, must match the
|
|
child's own — a foreign workspace's child 404s exactly like a
|
|
nonexistent one."""
|
|
entity = await self._repo.get_by_id(child_id)
|
|
if entity is None or (workspace_id is not None and entity.workspace_id != workspace_id):
|
|
raise NotFoundError(f"Child {child_id} not found")
|
|
return entity
|
|
|
|
async def list_by_workspace(self, workspace_id: UUID) -> list[Child]:
|
|
"""Get all children in a workspace."""
|
|
return await self._repo.list_by_workspace(workspace_id)
|
|
|
|
async def update(
|
|
self,
|
|
child_id: UUID,
|
|
name: str | None = None,
|
|
birth_date=None,
|
|
preschool_start_date=None,
|
|
preschool_planning_days_per_year: int | None = None,
|
|
workspace_id: UUID | None = None,
|
|
) -> Child:
|
|
"""Update a child (partial update)."""
|
|
entity = await self.get(child_id, workspace_id)
|
|
|
|
if name is not None:
|
|
entity.name = name
|
|
if birth_date is not None:
|
|
entity.birth_date = birth_date
|
|
if preschool_start_date is not None:
|
|
entity.preschool_start_date = preschool_start_date
|
|
if preschool_planning_days_per_year is not None:
|
|
_validate_preschool_planning_days(preschool_planning_days_per_year)
|
|
entity.preschool_planning_days_per_year = preschool_planning_days_per_year
|
|
|
|
# Validate after all updates
|
|
if (
|
|
entity.preschool_start_date is not None
|
|
and entity.preschool_start_date <= entity.birth_date
|
|
):
|
|
raise ValidationError("preschool_start_date must be after birth_date")
|
|
|
|
return await self._repo.update(child_id, entity)
|
|
|
|
async def delete(self, child_id: UUID, workspace_id: UUID | None = None) -> None:
|
|
"""Delete a child."""
|
|
await self.get(child_id, workspace_id) # raises NotFoundError if missing/foreign
|
|
await self._repo.delete(child_id)
|
|
|
|
|
|
def _validate_preschool_planning_days(value: int) -> None:
|
|
if not 0 <= value <= 30:
|
|
raise ValidationError("preschool_planning_days_per_year must be between 0 and 30")
|