Adds per-user wallet configuration

Allows users to configure their own wallet ID, enabling
the system to track expenses and receivables on a per-user basis.

Introduces new database table, models, API endpoints, and UI elements
to manage user-specific wallet settings.
This commit is contained in:
padreug 2025-10-22 14:54:25 +02:00
parent 31344607c6
commit bb1dbcccd8
7 changed files with 236 additions and 6 deletions

View file

@ -36,8 +36,9 @@ from .models import (
ReceivableEntry,
RevenueEntry,
UserBalance,
UserWalletSettings,
)
from .services import get_settings, update_settings
from .services import get_settings, get_user_wallet, update_settings, update_user_wallet
castle_api_router = APIRouter()
@ -56,6 +57,17 @@ async def check_castle_wallet_configured() -> str:
return settings.castle_wallet_id
async def check_user_wallet_configured(user_id: str) -> str:
"""Ensure user has configured their wallet, return wallet_id"""
settings = await get_user_wallet(user_id)
if not settings or not settings.user_wallet_id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="You must configure your wallet in settings before using this feature.",
)
return settings.user_wallet_id
# ===== UTILITY ENDPOINTS =====
@ -170,6 +182,11 @@ async def api_create_expense_entry(
If currency is provided, amount is converted from fiat to satoshis.
"""
# Check that castle wallet is configured
await check_castle_wallet_configured()
# Check that user has configured their wallet
await check_user_wallet_configured(wallet.wallet.user)
# Handle currency conversion
amount_sats = int(data.amount)
metadata = {}
@ -502,3 +519,32 @@ async def api_update_settings(
)
user_id = "admin"
return await update_settings(user_id, data)
# ===== USER WALLET ENDPOINTS =====
@castle_api_router.get("/api/v1/user/wallet")
async def api_get_user_wallet(
user: User = Depends(check_user_exists),
) -> UserWalletSettings:
"""Get current user's wallet settings"""
settings = await get_user_wallet(user.id)
# Return empty settings if not configured (so UI can show setup screen)
if not settings:
return UserWalletSettings()
return settings
@castle_api_router.put("/api/v1/user/wallet")
async def api_update_user_wallet(
data: UserWalletSettings,
user: User = Depends(check_user_exists),
) -> UserWalletSettings:
"""Update current user's wallet settings"""
if not data.user_wallet_id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="User wallet ID is required",
)
return await update_user_wallet(user.id, data)