fix: address account valuation review findings
This commit is contained in:
@@ -4,6 +4,8 @@ from .shared import (
|
||||
Any,
|
||||
datetime,
|
||||
db,
|
||||
read_db,
|
||||
read_operation,
|
||||
fail,
|
||||
now_iso,
|
||||
ok,
|
||||
@@ -100,9 +102,10 @@ def seed_default_category_rules(overwrite: bool = False, db_path: str | None = N
|
||||
return ok(created_or_updated=created, skipped=skipped)
|
||||
|
||||
|
||||
@read_operation
|
||||
def list_category_rules(active_only: bool = True, db_path: str | None = None) -> dict[str, Any]:
|
||||
sql="SELECT * FROM category_rules" + (" WHERE active=1" if active_only else "") + " ORDER BY priority, id"
|
||||
with db(db_path) as conn:
|
||||
with read_db(db_path) as conn:
|
||||
rows=conn.execute(sql).fetchall()
|
||||
return ok(rules=[row_to_dict(r) for r in rows], count=len(rows))
|
||||
|
||||
@@ -115,7 +118,7 @@ def _load_active_rules(conn: Any) -> list[dict[str, Any]]:
|
||||
|
||||
|
||||
def _categorization_preview_data(limit: int = 1000, only_uncategorized: bool = True, db_path: str | None = None) -> dict[str, Any]:
|
||||
with db(db_path) as conn:
|
||||
with read_db(db_path) as conn:
|
||||
rules = _load_active_rules(conn)
|
||||
where = "WHERE status='active'" + (" AND category=''" if only_uncategorized else "")
|
||||
rows = [row_to_dict(r) for r in conn.execute(
|
||||
@@ -163,8 +166,11 @@ def _categorization_preview_data(limit: int = 1000, only_uncategorized: bool = T
|
||||
}
|
||||
|
||||
|
||||
@read_operation
|
||||
def preview_categorization(limit: int = 1000, only_uncategorized: bool = True, db_path: str | None = None) -> dict[str, Any]:
|
||||
data = _categorization_preview_data(limit, only_uncategorized, db_path)
|
||||
if data.get("success") is False:
|
||||
return data
|
||||
return ok(
|
||||
scanned=len(data["rows"]),
|
||||
matched_count=len(data["matches"]),
|
||||
@@ -180,7 +186,7 @@ def _category_rule_match_rows(rule_id: str, limit: int = 10000, db_path: str | N
|
||||
if not rule_id:
|
||||
return fail("validation_error", "rule_id is required")
|
||||
row_limit = max(1, min(int(limit), 50000))
|
||||
with db(db_path) as conn:
|
||||
with read_db(db_path) as conn:
|
||||
rule_row = conn.execute("SELECT * FROM category_rules WHERE id=?", (rule_id,)).fetchone()
|
||||
if not rule_row:
|
||||
return fail("not_found", f"category rule not found: {rule_id}")
|
||||
@@ -220,6 +226,7 @@ def _category_rule_match_rows(rule_id: str, limit: int = 10000, db_path: str | N
|
||||
return ok(rule=rule, scanned=len(rows), matched=matched, uncategorized=uncategorized)
|
||||
|
||||
|
||||
@read_operation
|
||||
def preview_category_rule_matches(rule_id: str, limit: int = 10000, sample_limit: int = 3, db_path: str | None = None) -> dict[str, Any]:
|
||||
data = _category_rule_match_rows(rule_id, limit, db_path)
|
||||
if not data.get("success"):
|
||||
@@ -235,6 +242,8 @@ def preview_category_rule_matches(rule_id: str, limit: int = 10000, sample_limit
|
||||
|
||||
|
||||
def apply_category_rule(rule_id: str, limit: int = 10000, db_path: str | None = None) -> dict[str, Any]:
|
||||
with db(db_path):
|
||||
pass
|
||||
data = _category_rule_match_rows(rule_id, limit, db_path)
|
||||
if not data.get("success"):
|
||||
return data
|
||||
@@ -266,7 +275,11 @@ def apply_category_rule(rule_id: str, limit: int = 10000, db_path: str | None =
|
||||
)
|
||||
|
||||
def apply_categorization_rules(limit: int = 1000, only_uncategorized: bool = True, db_path: str | None = None) -> dict[str, Any]:
|
||||
with db(db_path):
|
||||
pass
|
||||
data = _categorization_preview_data(limit, only_uncategorized, db_path)
|
||||
if not data.get("success", True):
|
||||
return data
|
||||
preview = ok(
|
||||
scanned=len(data["rows"]),
|
||||
matched_count=len(data["matches"]),
|
||||
@@ -380,12 +393,13 @@ def seed_default_ledger_categories(overwrite: bool = False, db_path: str | None
|
||||
return ok(created_or_updated=created)
|
||||
|
||||
|
||||
@read_operation
|
||||
def list_ledger_categories(active_only: bool = True, include_usage: bool = True, db_path: str | None = None) -> dict[str, Any]:
|
||||
clauses = []
|
||||
if active_only:
|
||||
clauses.append("active=1")
|
||||
where = " WHERE " + " AND ".join(clauses) if clauses else ""
|
||||
with db(db_path) as conn:
|
||||
with read_db(db_path) as conn:
|
||||
rows = conn.execute(f"SELECT * FROM ledger_categories{where} ORDER BY sort_order, category, subcategory").fetchall()
|
||||
items = [row_to_dict(r) for r in rows]
|
||||
if include_usage:
|
||||
|
||||
Reference in New Issue
Block a user