From c0fc31c69a5a36aa4ae4268413880046a9b2170d Mon Sep 17 00:00:00 2001 From: d11n Date: Thu, 10 Aug 2023 21:23:18 +0300 Subject: [PATCH] Improve invoices status filter (#5248) --- .../Services/Invoices/InvoiceRepository.cs | 74 +++++++++---------- .../Views/UIInvoice/ListInvoices.cshtml | 7 +- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 1d5201cc5..1c4a8dbd2 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -662,54 +662,52 @@ namespace BTCPayServer.Services.Invoices if (queryObject.OrderId is { Length: > 0 }) { - var statusSet = queryObject.OrderId.ToHashSet().ToArray(); - query = query.Where(i => statusSet.Contains(i.OrderId)); + var orderIdSet = queryObject.OrderId.ToHashSet().ToArray(); + query = query.Where(i => orderIdSet.Contains(i.OrderId)); } if (queryObject.ItemCode is { Length: > 0 }) { - var statusSet = queryObject.ItemCode.ToHashSet().ToArray(); - query = query.Where(i => statusSet.Contains(i.ItemCode)); + var itemCodeSet = queryObject.ItemCode.ToHashSet().ToArray(); + query = query.Where(i => itemCodeSet.Contains(i.ItemCode)); } - if (queryObject.Status is { Length: > 0 }) + var statusSet = queryObject.Status is { Length: > 0 } + ? queryObject.Status.Select(s => s.ToLowerInvariant()).ToHashSet() + : new HashSet(); + var exceptionStatusSet = queryObject.ExceptionStatus is { Length: > 0 } + ? queryObject.ExceptionStatus.Select(NormalizeExceptionStatus).ToHashSet() + : new HashSet(); + + // We make sure here that the old filters still work + if (statusSet.Contains("paid")) + statusSet.Add("processing"); + if (statusSet.Contains("processing")) + statusSet.Add("paid"); + if (statusSet.Contains("confirmed")) { - var statusSet = queryObject.Status.Select(s => s.ToLowerInvariant()).ToHashSet(); - // We make sure here that the old filters still work - foreach (var status in queryObject.Status) - { - if (status == "paid") - statusSet.Add("processing"); - if (status == "processing") - statusSet.Add("paid"); - if (status == "confirmed") - { - statusSet.Add("complete"); - statusSet.Add("settled"); - } - if (status == "settled") - { - statusSet.Add("complete"); - statusSet.Add("confirmed"); - } - if (status == "complete") - { - statusSet.Add("settled"); - statusSet.Add("confirmed"); - } - } - query = query.Where(i => statusSet.Contains(i.Status)); + statusSet.Add("complete"); + statusSet.Add("settled"); + } + if (statusSet.Contains("settled")) + { + statusSet.Add("complete"); + statusSet.Add("confirmed"); + } + if (statusSet.Contains("complete")) + { + statusSet.Add("settled"); + statusSet.Add("confirmed"); + } + + if (statusSet.Any() || exceptionStatusSet.Any()) + { + query = query.Where(i => statusSet.Contains(i.Status) || exceptionStatusSet.Contains(i.ExceptionStatus)); } if (queryObject.Unusual != null) { - var unused = queryObject.Unusual.Value; - query = query.Where(i => unused == (i.Status == "invalid" || !string.IsNullOrEmpty(i.ExceptionStatus))); - } - - if (queryObject.ExceptionStatus is { Length: > 0 }) - { - var exceptionStatusSet = queryObject.ExceptionStatus.Select(NormalizeExceptionStatus).ToHashSet().ToArray(); - query = query.Where(i => exceptionStatusSet.Contains(i.ExceptionStatus)); + var unusual = queryObject.Unusual.Value; + query = query.Where(i => unusual == (i.Status == "invalid" || !string.IsNullOrEmpty(i.ExceptionStatus))); } query = query.OrderByDescending(q => q.Created); diff --git a/BTCPayServer/Views/UIInvoice/ListInvoices.cshtml b/BTCPayServer/Views/UIInvoice/ListInvoices.cshtml index 96ec94d9d..cdc539c7b 100644 --- a/BTCPayServer/Views/UIInvoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/UIInvoice/ListInvoices.cshtml @@ -251,15 +251,16 @@ }