Add Search tab with cross-account transaction search
Backend: GET /api/organizations/{orgId}/search/transactions
- Text search on transaction description (case-insensitive)
- Tx# / sequence ID lookup
- Amount range (min/max in cents, matches absolute row amounts)
- Date range (from/to)
- Optional account filter
- Returns transactions with nested rows, same format as General Journal
- Results ordered by date descending, capped at 500
Frontend: SearchTab component
- Search bar with description field (autofocus, Enter to search),
Tx# field, amount min/max, date from/to pickers, Search button
- Results displayed in General Journal format: transaction headers
with account rows, clickable for transaction detail dialog
- "N transactions found" with query echo
- Empty state before first search, "No transactions found" after
- Same visual style as General Journal (badges, colSpan, monospace)
Search tab now enabled in the Inspect Ledgers tab bar.
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
using UsingCash.Api.Support;
|
||||
using UsingCash.Database.Repositories;
|
||||
|
||||
namespace UsingCash.Api.Endpoints;
|
||||
|
||||
public static class SearchEndpoints
|
||||
{
|
||||
public static RouteGroupBuilder MapSearchEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
var group = app.MapGroup("/api/organizations/{orgId:int}/search");
|
||||
|
||||
group.MapGet("/transactions", async (int orgId, string? q, int? txId,
|
||||
long? amountMin, long? amountMax, string? dateFrom, string? dateTo,
|
||||
int? accountId, int? limit, FinancialRepository repo) =>
|
||||
{
|
||||
var maxResults = Math.Min(limit ?? 100, 500);
|
||||
var allAccounts = await repo.GetAccountsForOrganization(orgId);
|
||||
var accountMap = allAccounts.ToDictionary(a => a.FinancialAccountId, a => AccountNameResolver.Resolve(a.Name));
|
||||
|
||||
string AccountPath(int acctId)
|
||||
{
|
||||
if (!accountMap.TryGetValue(acctId, out var name)) return $"#{acctId}";
|
||||
var acct = allAccounts.FirstOrDefault(a => a.FinancialAccountId == acctId);
|
||||
if (acct != null && acct.ParentFinancialAccountId != 0 && accountMap.TryGetValue(acct.ParentFinancialAccountId, out var parentName))
|
||||
return $"{parentName} » {name}";
|
||||
return name;
|
||||
}
|
||||
|
||||
// Build date range
|
||||
DateTime from = DateTime.MinValue, to = DateTime.MaxValue;
|
||||
if (!string.IsNullOrEmpty(dateFrom) && DateTime.TryParse(dateFrom, out var df)) from = df;
|
||||
if (!string.IsNullOrEmpty(dateTo) && DateTime.TryParse(dateTo, out var dt)) to = dt.AddDays(1);
|
||||
|
||||
// Determine which accounts to search
|
||||
var searchAccountIds = accountId.HasValue
|
||||
? new List<int> { accountId.Value }
|
||||
: allAccounts.Select(a => a.FinancialAccountId).ToList();
|
||||
|
||||
// Collect matching rows
|
||||
var matchingTxIds = new HashSet<int>();
|
||||
var allRows = new List<(Basic.Types.BasicFinancialTransactionRow Row, int AccountId)>();
|
||||
|
||||
foreach (var acctId in searchAccountIds)
|
||||
{
|
||||
var rows = await repo.GetRowsForAccountInRange(acctId, from, to);
|
||||
foreach (var row in rows)
|
||||
{
|
||||
allRows.Add((row, acctId));
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate rows by rowId (same row can appear if account is in multiple search paths)
|
||||
var seenRowIds = new HashSet<int>();
|
||||
var uniqueRows = new List<(Basic.Types.BasicFinancialTransactionRow Row, int AccountId)>();
|
||||
foreach (var (row, acctId) in allRows)
|
||||
{
|
||||
if (seenRowIds.Add(row.FinancialTransactionRowId))
|
||||
uniqueRows.Add((row, acctId));
|
||||
}
|
||||
|
||||
// Load transactions
|
||||
var txCache = new Dictionary<int, Basic.Types.BasicFinancialTransaction>();
|
||||
foreach (var (row, _) in uniqueRows)
|
||||
{
|
||||
if (!txCache.ContainsKey(row.FinancialTransactionId))
|
||||
{
|
||||
var tx = await repo.GetTransaction(row.FinancialTransactionId);
|
||||
if (tx != null && tx.OrganizationId == orgId)
|
||||
txCache[tx.FinancialTransactionId] = tx;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by search criteria
|
||||
var matchedTxs = txCache.Values.AsEnumerable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(q))
|
||||
{
|
||||
var terms = q.ToLowerInvariant();
|
||||
matchedTxs = matchedTxs.Where(tx =>
|
||||
tx.Comment.Contains(terms, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
if (txId.HasValue)
|
||||
{
|
||||
matchedTxs = matchedTxs.Where(tx =>
|
||||
tx.OrganizationSequenceId == txId.Value || tx.FinancialTransactionId == txId.Value);
|
||||
}
|
||||
|
||||
if (amountMin.HasValue || amountMax.HasValue)
|
||||
{
|
||||
var txIdsWithMatchingAmounts = new HashSet<int>();
|
||||
foreach (var (row, _) in uniqueRows)
|
||||
{
|
||||
var absAmount = Math.Abs(row.AmountCents);
|
||||
if (amountMin.HasValue && absAmount < amountMin.Value) continue;
|
||||
if (amountMax.HasValue && absAmount > amountMax.Value) continue;
|
||||
txIdsWithMatchingAmounts.Add(row.FinancialTransactionId);
|
||||
}
|
||||
matchedTxs = matchedTxs.Where(tx => txIdsWithMatchingAmounts.Contains(tx.FinancialTransactionId));
|
||||
}
|
||||
|
||||
// Build results
|
||||
var results = matchedTxs
|
||||
.OrderByDescending(tx => tx.DateTime)
|
||||
.ThenByDescending(tx => tx.FinancialTransactionId)
|
||||
.Take(maxResults)
|
||||
.Select(tx =>
|
||||
{
|
||||
var txRows = uniqueRows
|
||||
.Where(r => r.Row.FinancialTransactionId == tx.FinancialTransactionId)
|
||||
.Select(r => new
|
||||
{
|
||||
rowId = r.Row.FinancialTransactionRowId,
|
||||
accountId = r.AccountId,
|
||||
accountName = AccountPath(r.AccountId),
|
||||
accountType = allAccounts.FirstOrDefault(a => a.FinancialAccountId == r.AccountId)?.AccountType.ToString() ?? "",
|
||||
debitCents = r.Row.AmountCents > 0 ? r.Row.AmountCents : 0,
|
||||
creditCents = r.Row.AmountCents < 0 ? -r.Row.AmountCents : 0,
|
||||
foreignCurrencyCode = r.Row.ForeignCurrencyCode,
|
||||
foreignCurrencyAmountCents = r.Row.ForeignCurrencyAmountCents,
|
||||
}).ToList();
|
||||
|
||||
return new
|
||||
{
|
||||
transactionId = tx.FinancialTransactionId,
|
||||
sequenceId = tx.OrganizationSequenceId,
|
||||
dateTime = tx.DateTime,
|
||||
description = tx.Comment,
|
||||
blockchainHash = tx.BlockchainHash,
|
||||
hasDocumentation = tx.HasDocumentation,
|
||||
rows = txRows,
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
return Results.Ok(new
|
||||
{
|
||||
query = q,
|
||||
resultCount = results.Count,
|
||||
transactions = results,
|
||||
});
|
||||
});
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ app.MapUserEndpoints();
|
||||
app.MapEndOfMonthEndpoints();
|
||||
app.MapTreeReportEndpoints();
|
||||
app.MapInspectLedgerEndpoints();
|
||||
app.MapSearchEndpoints();
|
||||
app.MapDevEndpoints();
|
||||
|
||||
app.UseDefaultFiles();
|
||||
|
||||
Reference in New Issue
Block a user