06/01 Optimize account balances

This commit is contained in:
2026-06-01 17:42:25 -04:00
parent b19c202786
commit d8608c7058
2 changed files with 104 additions and 18 deletions
+46 -2
View File
@@ -279,14 +279,58 @@ def refresh_balance(teller_account_id):
try:
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
db.session.commit()
return jsonify({'balance': available, 'status': 'ok'})
log.info('[teller] balance refreshed for %s: available=%.2f ledger=%.2f',
ta.account_name, available, ledger)
return jsonify({
'status': 'ok',
'available': available,
'ledger': ledger,
'account': ta.account_name,
})
except Exception as e:
log.error('[teller] balance refresh failed for teller_account_id=%s: %s', teller_account_id, e, exc_info=True)
log.error('[teller] balance refresh failed for teller_account_id=%s: %s',
teller_account_id, e, exc_info=True)
return jsonify({'error': str(e)}), 502
@teller_bp.route('/balance/all', methods=['POST'])
@login_required
def refresh_all_balances():
"""Refresh live balances for every mapped Teller account across all enrollments."""
enrollments = TellerEnrollment.query.filter_by(is_active=True).all()
refreshed, failed = [], []
for enrollment in enrollments:
for ta in enrollment.accounts.filter_by(is_active=True).all():
if not ta.pfm_account_id:
continue
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
refreshed.append(ta.account_name)
log.info('[teller] balance refreshed: %s = %.2f', ta.account_name, available)
except Exception as exc:
failed.append(ta.account_name)
log.error('[teller] balance refresh failed for %s: %s',
ta.account_name, exc, exc_info=True)
if refreshed or failed:
db.session.commit()
if refreshed:
flash(f'Balances updated for: {", ".join(refreshed)}.', 'success')
if failed:
flash(f'Failed to refresh: {", ".join(failed)}. Check System Logs for details.', 'danger')
if not refreshed and not failed:
flash('No mapped accounts found to refresh.', 'warning')
return redirect(url_for('teller.index'))
# ── Disconnect ────────────────────────────────────────────────────────────────
@teller_bp.route('/disconnect/<int:enrollment_db_id>', methods=['POST'])