Fixes user account creation in Fava/Beancount
This commit fixes two critical bugs in the user account creation flow:
1. **Always check/create in Fava regardless of Castle DB status**
- Previously, if an account existed in Castle DB, the function would
return early without checking if the Open directive existed in Fava
- This caused accounts to exist in Castle DB but not in Beancount
- Now we always check Fava and create Open directives if needed
2. **Fix Open directive insertion to preserve metadata**
- The insertion logic now skips over metadata lines when finding
the insertion point
- Prevents new Open directives from being inserted between existing
directives and their metadata, which was causing orphaned metadata
3. **Add comprehensive logging**
- Added detailed logging with [ACCOUNT CHECK], [FAVA CHECK],
[FAVA CREATE], [CASTLE DB], and [WALLET UPDATE] prefixes
- Makes it easier to trace account creation flow and debug issues
4. **Fix Fava filename handling**
- Now queries /api/options to get the Beancount file path dynamically
- Fixes "Parameter 'filename' is missing" errors with /api/source
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
a3c3e44e5f
commit
538751f21a
3 changed files with 45 additions and 13 deletions
27
crud.py
27
crud.py
|
|
@ -125,7 +125,12 @@ async def get_or_create_user_account(
|
|||
Account,
|
||||
)
|
||||
|
||||
if not account:
|
||||
logger.info(f"[ACCOUNT CHECK] User {user_id[:8]}, Account: {account_name}, In Castle DB: {account is not None}")
|
||||
|
||||
# Always check/create in Fava, even if account exists in Castle DB
|
||||
# This ensures Beancount has the Open directive
|
||||
fava_account_exists = False
|
||||
if True: # Always check Fava
|
||||
# Check if account exists in Fava/Beancount
|
||||
fava = get_fava_client()
|
||||
try:
|
||||
|
|
@ -140,11 +145,12 @@ async def get_or_create_user_account(
|
|||
result = response.json()
|
||||
|
||||
# Check if account exists in Fava
|
||||
fava_has_account = len(result.get("data", {}).get("rows", [])) > 0
|
||||
fava_account_exists = len(result.get("data", {}).get("rows", [])) > 0
|
||||
logger.info(f"[FAVA CHECK] Account {account_name} exists in Fava: {fava_account_exists}")
|
||||
|
||||
if not fava_has_account:
|
||||
if not fava_account_exists:
|
||||
# Create account in Fava/Beancount via Open directive
|
||||
logger.info(f"Creating account in Fava: {account_name}")
|
||||
logger.info(f"[FAVA CREATE] Creating account in Fava: {account_name}")
|
||||
await fava.add_account(
|
||||
account_name=account_name,
|
||||
currencies=["EUR", "SATS", "USD"], # Support common currencies
|
||||
|
|
@ -153,15 +159,15 @@ async def get_or_create_user_account(
|
|||
"description": f"User-specific {account_type.value} account"
|
||||
}
|
||||
)
|
||||
logger.info(f"Created account in Fava: {account_name}")
|
||||
else:
|
||||
logger.info(f"Account already exists in Fava: {account_name}")
|
||||
logger.info(f"[FAVA CREATE] Successfully created account in Fava: {account_name}")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not check/create account in Fava: {e}")
|
||||
logger.error(f"[FAVA ERROR] Could not check/create account in Fava: {e}", exc_info=True)
|
||||
# Continue anyway - account creation in Castle DB is still useful for metadata
|
||||
|
||||
# Create account in Castle DB for metadata tracking
|
||||
# Create account in Castle DB for metadata tracking (only if it doesn't exist)
|
||||
if not account:
|
||||
logger.info(f"[CASTLE DB] Creating account in Castle DB: {account_name}")
|
||||
account = await create_account(
|
||||
CreateAccount(
|
||||
name=account_name,
|
||||
|
|
@ -170,6 +176,9 @@ async def get_or_create_user_account(
|
|||
user_id=user_id,
|
||||
)
|
||||
)
|
||||
logger.info(f"[CASTLE DB] Created account in Castle DB: {account_name}")
|
||||
else:
|
||||
logger.info(f"[CASTLE DB] Account already exists in Castle DB: {account_name}")
|
||||
|
||||
return account
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue