06/03 Optimize code, fix account sync the current balances instead of credit lines
This commit is contained in:
+15
-6
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user