From 2735559ff50700f51271ce1f2c5deb2a803bd963 Mon Sep 17 00:00:00 2001 From: NguyenND Date: Wed, 3 Jun 2026 13:45:16 -0400 Subject: [PATCH] 06/03 Optimize codes, fix Schwab Account --- app/routes/accounts.py | 22 ++++++++++++++------ app/routes/schwab.py | 3 +++ app/templates/accounts/index.html | 30 +++++++++++++++++++++++++++- app/templates/investments/index.html | 9 ++++++++- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/routes/accounts.py b/app/routes/accounts.py index d7a4ee2..021cc7f 100644 --- a/app/routes/accounts.py +++ b/app/routes/accounts.py @@ -60,19 +60,28 @@ def index(): tab = 'bank' from app.models.teller_enrollment import TellerAccount + from app.models.schwab_connection import SchwabAccount all_accounts = Account.query.filter_by(is_active=True).order_by(Account.name).all() + pfm_ids = [a.id for a in all_accounts] - # Build Teller map first so we can skip calc_balance for Teller-linked accounts. - # Their balance comes from Teller (live refresh or transaction sync) and must not - # be overwritten by the transaction-computed sum on every page load. + # Build sync-provider maps before the calc_balance loop. + # Accounts linked to Teller or Schwab get their balance from the provider, + # not from transaction summation, so we skip calc_balance for them. teller_accounts = TellerAccount.query.filter( - TellerAccount.pfm_account_id.in_([a.id for a in all_accounts]), + TellerAccount.pfm_account_id.in_(pfm_ids), TellerAccount.is_active == True, ).all() teller_map = {ta.pfm_account_id: ta for ta in teller_accounts} + schwab_accounts = SchwabAccount.query.filter( + SchwabAccount.pfm_account_id.in_(pfm_ids), + SchwabAccount.is_active == True, + ).all() + schwab_map = {sa.pfm_account_id: sa for sa in schwab_accounts} + + provider_ids = set(teller_map) | set(schwab_map) for a in all_accounts: - if a.id not in teller_map: + if a.id not in provider_ids: calc_balance(a.id) all_accounts = Account.query.filter_by(is_active=True).order_by(Account.name).all() @@ -101,7 +110,8 @@ def index(): bank_count=len(bank_accounts), credit_count=len(credit_accounts), monthly_charges=monthly_charges, - teller_map=teller_map) + teller_map=teller_map, + schwab_map=schwab_map) @accounts_bp.route('/new', methods=['GET', 'POST']) diff --git a/app/routes/schwab.py b/app/routes/schwab.py index 8e3faa7..c6c7e19 100644 --- a/app/routes/schwab.py +++ b/app/routes/schwab.py @@ -292,6 +292,9 @@ def sync_snapshot(schwab_account_id): except Exception as e: log.error('[schwab] sync_snapshot failed for id=%s: %s', schwab_account_id, e, exc_info=True) flash(f'Snapshot sync failed: {e}', 'danger') + next_url = request.form.get('next', '') + if next_url and next_url.startswith('/'): + return redirect(next_url) return redirect(url_for('schwab.index')) diff --git a/app/templates/accounts/index.html b/app/templates/accounts/index.html index 8579892..ce71e14 100644 --- a/app/templates/accounts/index.html +++ b/app/templates/accounts/index.html @@ -26,6 +26,7 @@
{% for acct in accounts %} {% set ta = teller_map.get(acct.id) %} + {% set sa = schwab_map.get(acct.id) %}
@@ -41,6 +42,9 @@ {% if ta %} Teller {% endif %} + {% if sa %} + Schwab + {% endif %}
{{ acct.account_type | replace('_',' ') | title }}
@@ -119,7 +123,7 @@
{% endif %} - + {% if ta %}
+ + + Transactions + +
+ {% if sa.last_sync_date %} +
+ Last synced {{ sa.last_sync_date.strftime('%b %d') }} +
+ {% endif %} + {% endif %} + {% endfor %} diff --git a/app/templates/investments/index.html b/app/templates/investments/index.html index 96ddd5e..498943b 100644 --- a/app/templates/investments/index.html +++ b/app/templates/investments/index.html @@ -57,7 +57,14 @@
No investments yet

Track stocks, ETFs, crypto, real estate, and more.

- Add First Holding +
+ + Add Holding + + + Sync from Schwab + +
{% else %}