06/03 Optimize code, fix account sync the current balances instead of credit lines
This commit is contained in:
+14
-5
@@ -311,14 +311,21 @@ def refresh_balance(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',
|
||||
|
||||
@@ -224,15 +224,23 @@ function refreshBalance(taId, btn) {
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="bi bi-arrow-clockwise"></i>';
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user