06/05 Optimize app
This commit is contained in:
@@ -231,13 +231,6 @@ def parse_transaction(teller_txn, pfm_account_id, category_id_map, is_credit_car
|
||||
}
|
||||
|
||||
|
||||
def build_category_map():
|
||||
"""Build {pfm_category_name: category_id} from DB."""
|
||||
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 sync_preview(teller_account, days_back=90):
|
||||
"""
|
||||
Fetch transactions for a TellerAccount and return a preview list.
|
||||
@@ -272,6 +265,7 @@ def sync_preview(teller_account, days_back=90):
|
||||
)
|
||||
raise
|
||||
|
||||
from app.services.bank_import_service import build_category_map
|
||||
cat_map = build_category_map()
|
||||
is_cc = (
|
||||
teller_account.account_type == 'credit' or
|
||||
@@ -302,37 +296,38 @@ def import_transactions(parsed_txns, teller_account):
|
||||
skipped = 0
|
||||
affected_accounts = set()
|
||||
|
||||
# Batch duplicate check — one IN query instead of one LIKE per transaction
|
||||
candidate_notes = {f'Teller:{p["teller_id"]}' for p in parsed_txns}
|
||||
existing_notes = {
|
||||
r[0] for r in
|
||||
db.session.query(Transaction.notes)
|
||||
.filter(Transaction.notes.in_(candidate_notes))
|
||||
.all()
|
||||
}
|
||||
|
||||
for p in parsed_txns:
|
||||
# Duplicate check: match on teller_id in notes
|
||||
teller_id = p['teller_id']
|
||||
existing = Transaction.query.filter(
|
||||
Transaction.notes.like(f'%{teller_id}%')
|
||||
).first()
|
||||
if existing:
|
||||
note_str = f'Teller:{p["teller_id"]}'
|
||||
if note_str in existing_notes:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
txn = Transaction(
|
||||
account_id=p['account_id'],
|
||||
category_id=p.get('category_id'),
|
||||
transaction_type=p['transaction_type'],
|
||||
amount=p['amount'],
|
||||
description=p['description'],
|
||||
date=p['date'],
|
||||
notes=f"Teller:{teller_id}",
|
||||
)
|
||||
db.session.add(txn)
|
||||
db.session.add(Transaction(
|
||||
account_id = p['account_id'],
|
||||
category_id = p.get('category_id'),
|
||||
transaction_type = p['transaction_type'],
|
||||
amount = p['amount'],
|
||||
description = p['description'],
|
||||
date = p['date'],
|
||||
notes = note_str,
|
||||
))
|
||||
if p['account_id']:
|
||||
affected_accounts.add(p['account_id'])
|
||||
imported += 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Update sync metadata on both the account and its parent enrollment
|
||||
# Single commit — transactions + sync metadata together
|
||||
from datetime import datetime
|
||||
now = datetime.utcnow()
|
||||
teller_account.last_sync_date = date.today()
|
||||
teller_account.enrollment.last_synced_at = now
|
||||
teller_account.enrollment.last_synced_at = datetime.utcnow()
|
||||
if parsed_txns:
|
||||
teller_account.last_teller_txn_id = parsed_txns[0]['teller_id']
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user