Add receivable/payable filtering with database-level query optimization

Add account type filtering to Recent Transactions table and fix pagination issue where filters were applied after fetching results, causing incomplete data display.

Database layer (crud.py):
  - Add get_journal_entries_by_user_and_account_type() to filter entries by
    both user_id and account_type at SQL query level
  - Add count_journal_entries_by_user_and_account_type() for accurate counts
  - Filters apply before pagination, ensuring all matching records are fetched

API layer (views_api.py):
  - Add filter_account_type parameter ('asset' for receivable, 'liability' for payable)
  - Refactor filtering logic to use new database-level filter functions
  - Support filter combinations: user only, account_type only, user+account_type, or all
  - Enrich entries with account_type metadata for UI display

Frontend (index.js):
  - Add account_type to transactionFilter state
  - Add accountTypeOptions computed property with receivable/payable choices
  - Reorder table columns to show User before Date
  - Update loadTransactions to send account_type filter parameter
  - Update clearTransactionFilter to clear both user and account_type filters

UI (index.html):
  - Add second filter dropdown for account type (Receivable/Payable)
  - Show clear button when either filter is active
  - Update button label from "Clear Filter" to "Clear Filters"

This fixes the critical bug where filtering for receivables would only show a subset of results (e.g., 2 out of 20 entries fetched) instead of all matching receivables. Now filters are applied at the database level before pagination, ensuring users see all relevant transactions.
This commit is contained in:
padreug 2025-11-09 00:28:54 +01:00
parent f3d0d8652b
commit 3af93c3479
4 changed files with 163 additions and 22 deletions

View file

@ -268,30 +268,51 @@ async def api_get_user_entries(
limit: int = 20,
offset: int = 0,
filter_user_id: str = None,
filter_account_type: str = None, # 'asset' for receivable, 'liability' for payable
) -> dict:
"""Get journal entries that affect the current user's accounts"""
from lnbits.settings import settings as lnbits_settings
from lnbits.core.crud.users import get_user
from .crud import count_all_journal_entries, count_journal_entries_by_user, get_account
from .crud import (
count_all_journal_entries,
count_journal_entries_by_user,
count_journal_entries_by_user_and_account_type,
get_account,
get_journal_entries_by_user_and_account_type,
)
# If super user, show all journal entries
# Determine which entries to fetch based on filters
if wallet.wallet.user == lnbits_settings.super_user:
entries = await get_all_journal_entries(limit, offset)
total = await count_all_journal_entries()
# Super user with user_id filter
if filter_user_id:
# Filter by both user_id and account_type
if filter_account_type:
entries = await get_journal_entries_by_user_and_account_type(
filter_user_id, filter_account_type, limit, offset
)
total = await count_journal_entries_by_user_and_account_type(
filter_user_id, filter_account_type
)
else:
# Filter by user_id only
entries = await get_journal_entries_by_user(filter_user_id, limit, offset)
total = await count_journal_entries_by_user(filter_user_id)
else:
# No user filter, show all entries (account_type filter not supported for all entries)
entries = await get_all_journal_entries(limit, offset)
total = await count_all_journal_entries()
else:
entries = await get_journal_entries_by_user(wallet.wallet.user, limit, offset)
total = await count_journal_entries_by_user(wallet.wallet.user)
# Filter by user_id if specified (super user only)
if filter_user_id and wallet.wallet.user == lnbits_settings.super_user:
entries = [e for e in entries if any(
line.account_id in [acc["id"] for acc in await db.fetchall(
"SELECT id FROM accounts WHERE user_id = :user_id",
{"user_id": filter_user_id}
)]
for line in e.lines
)]
total = len(entries)
# Regular user
if filter_account_type:
entries = await get_journal_entries_by_user_and_account_type(
wallet.wallet.user, filter_account_type, limit, offset
)
total = await count_journal_entries_by_user_and_account_type(
wallet.wallet.user, filter_account_type
)
else:
entries = await get_journal_entries_by_user(wallet.wallet.user, limit, offset)
total = await count_journal_entries_by_user(wallet.wallet.user)
# Enrich entries with username information
enriched_entries = []
@ -299,11 +320,13 @@ async def api_get_user_entries(
# Find user_id from entry lines (look for user-specific accounts)
entry_user_id = None
entry_username = None
entry_account_type = None
for line in entry.lines:
account = await get_account(line.account_id)
if account and account.user_id:
entry_user_id = account.user_id
entry_account_type = account.account_type.value if hasattr(account.account_type, 'value') else account.account_type
user = await get_user(account.user_id)
entry_username = user.username if user and user.username else account.user_id[:16] + "..."
break
@ -312,6 +335,7 @@ async def api_get_user_entries(
**entry.dict(),
"user_id": entry_user_id,
"username": entry_username,
"account_type": entry_account_type,
})
return {