From daeea836d02a9fa37d6b7ff3f47004f23b8aa979 Mon Sep 17 00:00:00 2001 From: NguyenND Date: Wed, 3 Jun 2026 10:09:06 -0400 Subject: [PATCH] 06/03 Optimize code, fix account sync the current balances instead of credit lines --- app/routes/teller.py | 21 +++++++++++++++------ app/templates/teller/index.html | 16 ++++++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/routes/teller.py b/app/routes/teller.py index bb1d742..7c62b3e 100644 --- a/app/routes/teller.py +++ b/app/routes/teller.py @@ -308,17 +308,24 @@ def refresh_balance(teller_account_id): return jsonify({'error': 'Account not mapped'}), 400 try: - bal_data = get_balance(ta.enrollment.access_token, ta.teller_account_id) + bal_data = get_balance(ta.enrollment.access_token, ta.teller_account_id) available = float(bal_data.get('available') or bal_data.get('ledger') or 0) ledger = float(bal_data.get('ledger') or bal_data.get('available') or 0) - ta.pfm_account.balance = available + + # Credit cards: 'available' is the remaining credit line, not what's owed. + # Use 'ledger' (amount owed, negative in Teller's convention) for credit cards. + is_credit = ta.pfm_account.account_type == 'credit_card' + balance_to_store = ledger if is_credit else available + + ta.pfm_account.balance = balance_to_store db.session.commit() - log.info('[teller] balance refreshed for %s: available=%.2f ledger=%.2f', - ta.account_name, available, ledger) + log.info('[teller] balance refreshed for %s: available=%.2f ledger=%.2f stored=%.2f', + ta.account_name, available, ledger, balance_to_store) return jsonify({ 'status': 'ok', 'available': available, 'ledger': ledger, + 'balance': balance_to_store, 'account': ta.account_name, }) except Exception as e: @@ -341,9 +348,11 @@ def refresh_all_balances(): try: bal_data = get_balance(enrollment.access_token, ta.teller_account_id) available = float(bal_data.get('available') or bal_data.get('ledger') or 0) - ta.pfm_account.balance = available + ledger = float(bal_data.get('ledger') or bal_data.get('available') or 0) + is_credit = ta.pfm_account.account_type == 'credit_card' + ta.pfm_account.balance = ledger if is_credit else available refreshed.append(ta.account_name) - log.info('[teller] balance refreshed: %s = %.2f', ta.account_name, available) + log.info('[teller] balance refreshed: %s = %.2f', ta.account_name, ta.pfm_account.balance) except Exception as exc: failed.append(ta.account_name) log.error('[teller] balance refresh failed for %s: %s', diff --git a/app/templates/teller/index.html b/app/templates/teller/index.html index f05c74b..2f5cdbf 100644 --- a/app/templates/teller/index.html +++ b/app/templates/teller/index.html @@ -224,15 +224,23 @@ function refreshBalance(taId, btn) { btn.disabled = false; btn.innerHTML = ''; - if (data.available != null) { + if (data.balance != null || data.available != null) { const sym = '{{ current_user.currency_symbol }}'; - const fmt = v => sym + parseFloat(v).toLocaleString(undefined, {minimumFractionDigits:2, maximumFractionDigits:2}); + const fmt = v => { + const n = parseFloat(v); + const abs = Math.abs(n).toLocaleString(undefined, {minimumFractionDigits:2, maximumFractionDigits:2}); + return (n < 0 ? '-' : '') + sym + abs; + }; + + // 'balance' is the correct value for the account type (ledger for credit cards, + // available for bank accounts). Fall back to 'available' for older responses. + const displayVal = data.balance != null ? data.balance : data.available; // Update inline balance display const balEl = document.getElementById('bal-' + taId); if (balEl) { - balEl.textContent = fmt(data.available); - balEl.style.color = '#10b981'; + balEl.textContent = fmt(displayVal); + balEl.style.color = displayVal < 0 ? '#ef4444' : '#10b981'; setTimeout(() => balEl.style.color = '', 2500); }