diff --git a/BTCPayServer/Components/StoreNumbers/Default.cshtml b/BTCPayServer/Components/StoreNumbers/Default.cshtml index 89e5de5f6..e5b285c8b 100644 --- a/BTCPayServer/Components/StoreNumbers/Default.cshtml +++ b/BTCPayServer/Components/StoreNumbers/Default.cshtml @@ -1,3 +1,4 @@ +@using BTCPayServer.Client @model BTCPayServer.Components.StoreNumbers.StoreNumbersViewModel
@@ -21,26 +22,23 @@ } else { +
+
+
Paid invoices in the last @Model.TimeframeDays days
+ @if (Model.PaidInvoices > 0) + { + View All + } +
+
@Model.PaidInvoices
+
Payouts Pending
- Manage + Manage
@Model.PayoutsPending
- @if (Model.Transactions is not null) - { -
-
-
TXs in the last @Model.TransactionDays days
- @if (Model.Transactions.Value > 0) - { - View All - } -
-
@Model.Transactions.Value
-
- }
Refunds Issued
diff --git a/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs b/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs index 4a43a6956..27f2627ab 100644 --- a/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs +++ b/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs @@ -6,6 +6,7 @@ using BTCPayServer.Client.Models; using BTCPayServer.Components.StoreRecentTransactions; using BTCPayServer.Data; using BTCPayServer.Services; +using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Stores; using BTCPayServer.Services.Wallets; using Dapper; @@ -21,22 +22,16 @@ public class StoreNumbers : ViewComponent { private readonly StoreRepository _storeRepo; private readonly ApplicationDbContextFactory _dbContextFactory; - private readonly BTCPayWalletProvider _walletProvider; - private readonly NBXplorerConnectionFactory _nbxConnectionFactory; - private readonly BTCPayNetworkProvider _networkProvider; + private readonly InvoiceRepository _invoiceRepository; public StoreNumbers( StoreRepository storeRepo, ApplicationDbContextFactory dbContextFactory, - BTCPayNetworkProvider networkProvider, - BTCPayWalletProvider walletProvider, - NBXplorerConnectionFactory nbxConnectionFactory) + InvoiceRepository invoiceRepository) { _storeRepo = storeRepo; - _walletProvider = walletProvider; - _nbxConnectionFactory = nbxConnectionFactory; - _networkProvider = networkProvider; _dbContextFactory = dbContextFactory; + _invoiceRepository = invoiceRepository; } public async Task InvokeAsync(StoreNumbersViewModel vm) @@ -52,28 +47,17 @@ public class StoreNumbers : ViewComponent return View(vm); await using var ctx = _dbContextFactory.CreateContext(); - var payoutsCount = await ctx.Payouts + var offset = DateTimeOffset.Now.AddDays(-vm.TimeframeDays).ToUniversalTime(); + + vm.PaidInvoices = await _invoiceRepository.GetInvoiceCount( + new InvoiceQuery { StoreId = new [] { vm.Store.Id }, StartDate = offset, Status = new [] { "paid", "confirmed" } }); + vm.PayoutsPending = await ctx.Payouts .Where(p => p.PullPaymentData.StoreId == vm.Store.Id && !p.PullPaymentData.Archived && p.State == PayoutState.AwaitingApproval) .CountAsync(); - var refundsCount = await ctx.Invoices - .Where(i => i.StoreData.Id == vm.Store.Id && !i.Archived && i.CurrentRefundId != null) + vm.RefundsIssued = await ctx.Invoices + .Where(i => i.StoreData.Id == vm.Store.Id && !i.Archived && i.CurrentRefundId != null && i.Created >= offset) .CountAsync(); - var derivation = vm.Store.GetDerivationSchemeSettings(_networkProvider, vm.CryptoCode); - int? transactionsCount = null; - if (derivation != null && _nbxConnectionFactory.Available) - { - await using var conn = await _nbxConnectionFactory.OpenConnection(); - var wid = NBXplorer.Client.DBUtils.nbxv1_get_wallet_id(derivation.Network.CryptoCode, derivation.AccountDerivation.ToString()); - var afterDate = DateTimeOffset.UtcNow - TimeSpan.FromDays(vm.TransactionDays); - var count = await conn.ExecuteScalarAsync("SELECT COUNT(*) FROM wallets_history WHERE code=@code AND wallet_id=@wid AND seen_at > @afterDate", new { code = derivation.Network.CryptoCode, wid, afterDate }); - transactionsCount = (int)count; - } - - vm.PayoutsPending = payoutsCount; - vm.Transactions = transactionsCount; - vm.RefundsIssued = refundsCount; - return View(vm); } } diff --git a/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs b/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs index e234219f7..55ba51bd6 100644 --- a/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs +++ b/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs @@ -7,9 +7,9 @@ public class StoreNumbersViewModel public StoreData Store { get; set; } public WalletId WalletId { get; set; } public int PayoutsPending { get; set; } - public int? Transactions { get; set; } + public int TimeframeDays { get; set; } = 7; + public int? PaidInvoices { get; set; } public int RefundsIssued { get; set; } - public int TransactionDays { get; set; } = 7; public bool InitialRendering { get; set; } public string CryptoCode { get; set; } } diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 1c4a8dbd2..eb13f5512 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -738,6 +738,12 @@ namespace BTCPayServer.Services.Invoices var data = await query.ToArrayAsync(cancellationToken).ConfigureAwait(false); return data.Select(ToEntity).ToArray(); } + + public async Task GetInvoiceCount(InvoiceQuery queryObject) + { + await using var context = _applicationDbContextFactory.CreateContext(); + return await GetInvoiceQuery(context, queryObject).CountAsync(); + } private string NormalizeExceptionStatus(string status) {