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.
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""API schemas (Pydantic) for the tips engine.
|
|
|
|
The route used to return `list[dict]` — no shape guarantee at all. `Tip.data`
|
|
stays a free-form bag on purpose (§ tips_service.py docstring: it is
|
|
interpolation context for the frontend, and every rule shapes it
|
|
differently), but everything around it is now a real, typed contract.
|
|
"""
|
|
|
|
from datetime import date
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from app.services.tips_service import Tip
|
|
|
|
# Every value any rule currently puts in `Tip.data` is a JSON scalar (str,
|
|
# int, float, bool) or None — never a nested dict/list. Typing it this way
|
|
# (rather than `dict[str, Any]`) still catches a rule accidentally stuffing
|
|
# something un-renderable into `data`.
|
|
TipDataValue = str | int | float | bool | None
|
|
|
|
|
|
class TipResponse(BaseModel):
|
|
id: str
|
|
severity: str
|
|
message_key: str
|
|
data: dict[str, TipDataValue]
|
|
anchor_date: date | None
|
|
anchor_range: tuple[date, date] | None
|
|
|
|
@classmethod
|
|
def from_domain(cls, tip: Tip) -> "TipResponse":
|
|
return cls(
|
|
id=tip.id,
|
|
severity=tip.severity,
|
|
message_key=tip.message_key,
|
|
data=tip.data,
|
|
anchor_date=tip.anchor_date,
|
|
anchor_range=tip.anchor_range,
|
|
)
|
|
|
|
|
|
class TipDismissalCreate(BaseModel):
|
|
tip_id: str
|