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: