Parses amount string for SATS and fiat

Improves handling of the amount field in user entries by parsing string formats that include both SATS and fiat currency information.

This change allows extracting the SATS amount and fiat amount/currency directly from the string, accommodating different display formats.
This commit is contained in:
padreug 2025-11-10 01:00:43 +01:00
parent 63d851ce94
commit 0c7356e228

View file

@ -387,14 +387,21 @@ async def api_get_user_entries(
if postings:
first_posting = postings[0]
if isinstance(first_posting, dict):
amount_field = first_posting.get("amount")
if isinstance(amount_field, dict):
amount_sats = abs(int(float(amount_field.get("number", 0))))
amount_str = first_posting.get("amount", "")
cost = first_posting.get("cost")
if isinstance(cost, dict):
fiat_amount = float(cost.get("number", 0))
fiat_currency = cost.get("currency")
# Parse amount string format: "36791 SATS {33.33 EUR, 2025-11-09}" or "36791 SATS"
if isinstance(amount_str, str) and amount_str:
import re
# Extract SATS amount (before " SATS")
sats_match = re.match(r'^(-?\d+)\s+SATS', amount_str)
if sats_match:
amount_sats = abs(int(sats_match.group(1)))
# Extract fiat from cost syntax: {33.33 EUR, ...}
cost_match = re.search(r'\{([\d.]+)\s+([A-Z]+)', amount_str)
if cost_match:
fiat_amount = float(cost_match.group(1))
fiat_currency = cost_match.group(2)
# Extract reference from links (first non-castle link)
reference = None