From 657e3d54da55a9056f118a0b6006bc371075e0f9 Mon Sep 17 00:00:00 2001 From: padreug Date: Tue, 11 Nov 2025 01:57:42 +0100 Subject: [PATCH] Filter inactive accounts from default queries - Updated get_all_accounts() to add include_inactive parameter (default False) - Updated get_accounts_by_type() to add include_inactive parameter (default False) - Modified account_sync to use include_inactive=True (needs to see all accounts) - Default behavior now hides inactive accounts from user-facing API endpoints This ensures inactive accounts are automatically hidden from users while still allowing internal operations (like sync) to access all accounts. --- account_sync.py | 4 ++-- crud.py | 46 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/account_sync.py b/account_sync.py index 990a099..9591929 100644 --- a/account_sync.py +++ b/account_sync.py @@ -135,8 +135,8 @@ async def sync_accounts_from_beancount(force_full_sync: bool = False) -> dict: "errors": [str(e)], } - # Get all accounts from Castle DB - castle_accounts = await get_all_accounts() + # Get all accounts from Castle DB (including inactive ones for sync) + castle_accounts = await get_all_accounts(include_inactive=True) # Build lookup maps beancount_account_names = {acc["account"] for acc in beancount_accounts} diff --git a/crud.py b/crud.py index 11cfeb8..a403924 100644 --- a/crud.py +++ b/crud.py @@ -120,19 +120,43 @@ async def get_account_by_name(name: str) -> Optional[Account]: return account -async def get_all_accounts() -> list[Account]: - return await db.fetchall( - "SELECT * FROM accounts ORDER BY account_type, name", - model=Account, - ) +async def get_all_accounts(include_inactive: bool = False) -> list[Account]: + """ + Get all accounts, optionally including inactive ones. + + Args: + include_inactive: If True, include inactive accounts. Default False. + + Returns: + List of Account objects + """ + if include_inactive: + query = "SELECT * FROM accounts ORDER BY account_type, name" + else: + query = "SELECT * FROM accounts WHERE is_active = TRUE ORDER BY account_type, name" + + return await db.fetchall(query, model=Account) -async def get_accounts_by_type(account_type: AccountType) -> list[Account]: - return await db.fetchall( - "SELECT * FROM accounts WHERE account_type = :type ORDER BY name", - {"type": account_type.value}, - Account, - ) +async def get_accounts_by_type( + account_type: AccountType, include_inactive: bool = False +) -> list[Account]: + """ + Get accounts by type, optionally including inactive ones. + + Args: + account_type: The account type to filter by + include_inactive: If True, include inactive accounts. Default False. + + Returns: + List of Account objects + """ + if include_inactive: + query = "SELECT * FROM accounts WHERE account_type = :type ORDER BY name" + else: + query = "SELECT * FROM accounts WHERE account_type = :type AND is_active = TRUE ORDER BY name" + + return await db.fetchall(query, {"type": account_type.value}, Account) async def update_account_is_active(account_id: str, is_active: bool) -> None: