06/05 Optimize app
This commit is contained in:
@@ -336,12 +336,6 @@ def get_transactions(connection, account_hash, start_date, end_date):
|
||||
return data
|
||||
|
||||
|
||||
def build_category_map():
|
||||
from app.models.category import Category
|
||||
cats = Category.query.filter_by(is_active=True).all()
|
||||
return {c.name: c.id for c in cats}
|
||||
|
||||
|
||||
def parse_transaction(schwab_txn, pfm_account_id, cat_id_map):
|
||||
"""
|
||||
Convert a Schwab transaction dict to a PFM-ready dict.
|
||||
@@ -413,6 +407,7 @@ def sync_preview(schwab_account, days_back=90):
|
||||
end_date=today,
|
||||
)
|
||||
|
||||
from app.services.bank_import_service import build_category_map
|
||||
cat_map = build_category_map()
|
||||
return [
|
||||
parse_transaction(t, schwab_account.pfm_account_id, cat_map)
|
||||
@@ -428,14 +423,20 @@ def import_transactions(parsed_txns, schwab_account):
|
||||
"""
|
||||
from app.extensions import db
|
||||
from app.models.transaction import Transaction
|
||||
from app.services.account_service import calc_balance
|
||||
|
||||
imported = skipped = 0
|
||||
affected = set()
|
||||
|
||||
# Batch duplicate check — one IN query instead of one LIKE per transaction
|
||||
candidate_notes = {p['notes'] for p in parsed_txns} # 'Schwab:{id}'
|
||||
existing_notes = {
|
||||
r[0] for r in
|
||||
db.session.query(Transaction.notes)
|
||||
.filter(Transaction.notes.in_(candidate_notes))
|
||||
.all()
|
||||
} if candidate_notes else set()
|
||||
|
||||
for p in parsed_txns:
|
||||
sid = p['schwab_id']
|
||||
if Transaction.query.filter(Transaction.notes.like(f'%Schwab:{sid}%')).first():
|
||||
if p['notes'] in existing_notes:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
@@ -448,19 +449,15 @@ def import_transactions(parsed_txns, schwab_account):
|
||||
date = p['date'],
|
||||
notes = p['notes'],
|
||||
))
|
||||
if p['account_id']:
|
||||
affected.add(p['account_id'])
|
||||
imported += 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Single commit — transactions + metadata together
|
||||
schwab_account.last_sync_date = date.today()
|
||||
schwab_account.connection.last_synced_at = datetime.utcnow()
|
||||
if parsed_txns:
|
||||
schwab_account.last_schwab_txn_id = parsed_txns[0]['schwab_id']
|
||||
db.session.commit()
|
||||
|
||||
for acct_id in affected:
|
||||
calc_balance(acct_id)
|
||||
# Balance for Schwab accounts is set by sync_account_snapshot (liquidationValue),
|
||||
# not by summing transactions — skip calc_balance here.
|
||||
|
||||
return imported, skipped
|
||||
|
||||
Reference in New Issue
Block a user