From 3ce779ccfde8de62a7475dad387af8fde5a2d618 Mon Sep 17 00:00:00 2001 From: NguyenND Date: Wed, 3 Jun 2026 10:44:36 -0400 Subject: [PATCH] 06/03 Optimize codes, fix transactions import functions --- app/routes/transactions.py | 12 +++++ app/services/teller_service.py | 12 +++-- app/templates/transactions/index.html | 65 ++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/app/routes/transactions.py b/app/routes/transactions.py index 9ed7a49..4ac1f5f 100644 --- a/app/routes/transactions.py +++ b/app/routes/transactions.py @@ -221,6 +221,18 @@ def edit(id): title='Edit Transaction') +@transactions_bp.route('//set-category', methods=['POST']) +@login_required +def set_category(id): + """AJAX endpoint: update only the category of a transaction.""" + txn = db.get_or_404(Transaction, id) + data = request.get_json(silent=True) or {} + raw = data.get('category_id') + txn.category_id = int(raw) if raw else None + db.session.commit() + return jsonify({'ok': True, 'category_id': txn.category_id}) + + @transactions_bp.route('//delete', methods=['POST']) @login_required def delete(id): diff --git a/app/services/teller_service.py b/app/services/teller_service.py index 1a0782a..4565740 100644 --- a/app/services/teller_service.py +++ b/app/services/teller_service.py @@ -200,10 +200,14 @@ def parse_transaction(teller_txn, pfm_account_id, category_id_map): if counterparty and counterparty.upper() != description.upper(): description = counterparty - # Map Teller category to PFM category - teller_cat = (details.get('category') or '').lower() - pfm_cat_name = CATEGORY_MAP.get(teller_cat, 'Other') - category_id = category_id_map.get(pfm_cat_name) + # Auto-categorize: keyword match on description first (most accurate), + # then fall back to Teller's own category field. + from app.services.bank_import_service import auto_categorize + pfm_cat_name = auto_categorize(description) + if not pfm_cat_name: + teller_cat = (details.get('category') or '').lower() + pfm_cat_name = CATEGORY_MAP.get(teller_cat, '') + category_id = category_id_map.get(pfm_cat_name) if pfm_cat_name else None return { 'teller_id': teller_txn['id'], diff --git a/app/templates/transactions/index.html b/app/templates/transactions/index.html index 8257c59..9e18c1a 100644 --- a/app/templates/transactions/index.html +++ b/app/templates/transactions/index.html @@ -82,12 +82,24 @@
{{ txn.description }}
{% if txn.notes %}
{{ txn.notes | truncate(60) }}
{% endif %} - - {% if txn.category %} - - {{ txn.category.name }} - - {% else %}{% endif %} + +
+ + +
{{ txn.account.name if txn.account else '—' }} @@ -141,3 +153,44 @@ {% endif %} {% endblock %} + +{% block extra_js %} + + +{% endblock %}