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.
This commit is contained in:
padreug 2025-11-11 01:57:42 +01:00
parent cb62cbb0a2
commit 657e3d54da
2 changed files with 37 additions and 13 deletions

View file

@ -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}

46
crud.py
View file

@ -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: