06/03 Optimize codes, fix Schwab Account
This commit is contained in:
@@ -93,6 +93,46 @@ def _recalc_holding(investment):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@investments_bp.route('/sync-schwab', methods=['POST'])
|
||||
@login_required
|
||||
def sync_schwab():
|
||||
"""Sync balance + positions for every mapped Schwab account, then return here."""
|
||||
from app.models.schwab_connection import SchwabConnection, SchwabAccount
|
||||
from app.services.schwab_service import sync_account_snapshot
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
connection = SchwabConnection.query.filter_by(is_active=True).first()
|
||||
if not connection:
|
||||
flash('No active Schwab connection. Connect at the Schwab page first.', 'warning')
|
||||
return redirect(url_for('investments.index'))
|
||||
|
||||
accounts = SchwabAccount.query.filter(
|
||||
SchwabAccount.connection_id == connection.id,
|
||||
SchwabAccount.pfm_account_id != None,
|
||||
SchwabAccount.is_active == True,
|
||||
).all()
|
||||
|
||||
if not accounts:
|
||||
flash('No Schwab accounts mapped yet. Map them on the Schwab page first.', 'warning')
|
||||
return redirect(url_for('investments.index'))
|
||||
|
||||
total_pos = 0
|
||||
for sa in accounts:
|
||||
try:
|
||||
_, pos = sync_account_snapshot(sa)
|
||||
total_pos += pos
|
||||
except Exception as e:
|
||||
log.error('[schwab] sync_schwab investments failed for %s: %s', sa.account_name, e, exc_info=True)
|
||||
flash(f'Sync failed for {sa.account_name}: {e}', 'danger')
|
||||
|
||||
if total_pos:
|
||||
flash(f'Synced {total_pos} holding(s) from Schwab.', 'success')
|
||||
else:
|
||||
flash('Sync complete — no positions found. Check System Logs for details.', 'info')
|
||||
return redirect(url_for('investments.index'))
|
||||
|
||||
|
||||
@investments_bp.route('/')
|
||||
@login_required
|
||||
def index():
|
||||
|
||||
Reference in New Issue
Block a user