Give tips a date, a scope per child, and a dismissal that sticks
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.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""SQLite implementation of TipDismissalRepository.
|
||||
|
||||
Follows the DayOverrideRepository reference shape: upsert-by-natural-key
|
||||
((workspace_id, tip_id) is UNIQUE — see the `tip_dismissal` table), map rows
|
||||
<-> entities explicitly in `_row_to_entity`, always parameterize queries,
|
||||
raise NotFoundError from update/delete when the row is missing.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.tip_dismissal import TipDismissal
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.tip_dismissal_repository import TipDismissalRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
|
||||
class SQLiteTipDismissalRepository(TipDismissalRepository):
|
||||
def __init__(self, connection: aiosqlite.Connection) -> None:
|
||||
self._conn = connection
|
||||
|
||||
@staticmethod
|
||||
def _row_to_entity(row: aiosqlite.Row) -> TipDismissal:
|
||||
return TipDismissal(
|
||||
id=UUID(row["id"]),
|
||||
workspace_id=UUID(row["workspace_id"]),
|
||||
tip_id=row["tip_id"],
|
||||
data_hash=row["data_hash"],
|
||||
dismissed_at=datetime.fromisoformat(row["dismissed_at"]),
|
||||
)
|
||||
|
||||
async def create(self, item: TipDismissal) -> TipDismissal:
|
||||
item.dismissed_at = item.dismissed_at or datetime.now()
|
||||
await self._conn.execute(
|
||||
"""
|
||||
INSERT INTO tip_dismissal (id, workspace_id, tip_id, data_hash, dismissed_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
str(item.id),
|
||||
str(item.workspace_id),
|
||||
item.tip_id,
|
||||
item.data_hash,
|
||||
item.dismissed_at.isoformat(),
|
||||
),
|
||||
)
|
||||
await self._conn.commit()
|
||||
return item
|
||||
|
||||
async def get_by_id(self, id: UUID) -> TipDismissal | None:
|
||||
cursor = await self._conn.execute("SELECT * FROM tip_dismissal WHERE id = ?", (str(id),))
|
||||
row = await cursor.fetchone()
|
||||
return self._row_to_entity(row) if row else None
|
||||
|
||||
async def get_by_workspace_tip(self, workspace_id: UUID, tip_id: str) -> TipDismissal | None:
|
||||
cursor = await self._conn.execute(
|
||||
"SELECT * FROM tip_dismissal WHERE workspace_id = ? AND tip_id = ?",
|
||||
(str(workspace_id), tip_id),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return self._row_to_entity(row) if row else None
|
||||
|
||||
async def update(self, id: UUID, item: TipDismissal) -> TipDismissal:
|
||||
item.dismissed_at = datetime.now()
|
||||
cursor = await self._conn.execute(
|
||||
"UPDATE tip_dismissal SET data_hash = ?, dismissed_at = ? WHERE id = ?",
|
||||
(item.data_hash, item.dismissed_at.isoformat(), str(id)),
|
||||
)
|
||||
await self._conn.commit()
|
||||
if cursor.rowcount == 0:
|
||||
raise NotFoundError(f"TipDismissal {id} not found")
|
||||
refreshed = await self.get_by_id(id)
|
||||
assert refreshed is not None # just updated
|
||||
return refreshed
|
||||
|
||||
async def delete(self, id: UUID) -> None:
|
||||
cursor = await self._conn.execute("DELETE FROM tip_dismissal WHERE id = ?", (str(id),))
|
||||
await self._conn.commit()
|
||||
if cursor.rowcount == 0:
|
||||
raise NotFoundError(f"TipDismissal {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[TipDismissal]:
|
||||
validate_filters(filters, {"workspace_id"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("workspace_id",):
|
||||
if column in filters and filters[column] is not None:
|
||||
value = filters[column]
|
||||
clauses.append(f"{column} = ?")
|
||||
params.append(str(value) if isinstance(value, UUID) else value)
|
||||
where = f" WHERE {' AND '.join(clauses)}" if clauses else ""
|
||||
cursor = await self._conn.execute(
|
||||
f"SELECT * FROM tip_dismissal{where} ORDER BY dismissed_at ASC", params
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return [self._row_to_entity(row) for row in rows]
|
||||
|
||||
async def list_by_workspace(self, workspace_id: UUID) -> list[TipDismissal]:
|
||||
return await self.list(workspace_id=workspace_id)
|
||||
Reference in New Issue
Block a user