2022-04-12 09:55:10 +02:00
using System ;
2022-04-30 12:54:12 +09:00
using Dapper ;
2022-04-12 09:55:10 +02:00
using System.Globalization ;
using System.Linq ;
using System.Threading.Tasks ;
using BTCPayServer.Client.Models ;
using BTCPayServer.Components.StoreRecentTransactions ;
using BTCPayServer.Data ;
2022-04-30 12:54:12 +09:00
using BTCPayServer.Services ;
2022-04-12 09:55:10 +02:00
using BTCPayServer.Services.Stores ;
using BTCPayServer.Services.Wallets ;
using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.EntityFrameworkCore ;
using Npgsql ;
using StoreData = BTCPayServer . Data . StoreData ;
namespace BTCPayServer.Components.StoreNumbers ;
public class StoreNumbers : ViewComponent
{
private readonly StoreRepository _storeRepo ;
private readonly ApplicationDbContextFactory _dbContextFactory ;
private readonly BTCPayWalletProvider _walletProvider ;
2022-04-30 12:54:12 +09:00
private readonly NBXplorerConnectionFactory _nbxConnectionFactory ;
2022-04-12 09:55:10 +02:00
private readonly BTCPayNetworkProvider _networkProvider ;
public StoreNumbers (
StoreRepository storeRepo ,
ApplicationDbContextFactory dbContextFactory ,
BTCPayNetworkProvider networkProvider ,
2022-04-30 12:54:12 +09:00
BTCPayWalletProvider walletProvider ,
NBXplorerConnectionFactory nbxConnectionFactory )
2022-04-12 09:55:10 +02:00
{
_storeRepo = storeRepo ;
_walletProvider = walletProvider ;
2022-04-30 12:54:12 +09:00
_nbxConnectionFactory = nbxConnectionFactory ;
2022-04-12 09:55:10 +02:00
_networkProvider = networkProvider ;
_dbContextFactory = dbContextFactory ;
}
2022-07-06 05:40:16 +02:00
public async Task < IViewComponentResult > InvokeAsync ( StoreNumbersViewModel vm )
2022-04-12 09:55:10 +02:00
{
2022-07-06 05:40:16 +02:00
if ( vm . Store = = null ) throw new ArgumentNullException ( nameof ( vm . Store ) ) ;
if ( vm . CryptoCode = = null ) throw new ArgumentNullException ( nameof ( vm . CryptoCode ) ) ;
vm . WalletId = new WalletId ( vm . Store . Id , vm . CryptoCode ) ;
if ( vm . InitialRendering ) return View ( vm ) ;
2022-04-12 09:55:10 +02:00
await using var ctx = _dbContextFactory . CreateContext ( ) ;
var payoutsCount = await ctx . Payouts
2022-07-06 05:40:16 +02:00
. Where ( p = > p . PullPaymentData . StoreId = = vm . Store . Id & & ! p . PullPaymentData . Archived & & p . State = = PayoutState . AwaitingApproval )
2022-04-12 09:55:10 +02:00
. CountAsync ( ) ;
var refundsCount = await ctx . Invoices
2022-07-06 05:40:16 +02:00
. Where ( i = > i . StoreData . Id = = vm . Store . Id & & ! i . Archived & & i . CurrentRefundId ! = null )
2022-04-12 09:55:10 +02:00
. CountAsync ( ) ;
2022-07-06 05:40:16 +02:00
var derivation = vm . Store . GetDerivationSchemeSettings ( _networkProvider , vm . CryptoCode ) ;
2022-04-30 12:54:12 +09:00
int? transactionsCount = null ;
if ( derivation ! = null & & _nbxConnectionFactory . Available )
2022-04-12 09:55:10 +02:00
{
2022-04-30 12:54:12 +09:00
await using var conn = await _nbxConnectionFactory . OpenConnection ( ) ;
var wid = NBXplorer . Client . DBUtils . nbxv1_get_wallet_id ( derivation . Network . CryptoCode , derivation . AccountDerivation . ToString ( ) ) ;
2022-07-06 05:40:16 +02:00
var afterDate = DateTimeOffset . UtcNow - TimeSpan . FromDays ( vm . TransactionDays ) ;
2022-04-30 12:54:12 +09:00
var count = await conn . ExecuteScalarAsync < long > ( "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 ;
2022-04-12 09:55:10 +02:00
}
2022-07-06 05:40:16 +02:00
vm . PayoutsPending = payoutsCount ;
vm . Transactions = transactionsCount ;
vm . RefundsIssued = refundsCount ;
2022-04-12 09:55:10 +02:00
return View ( vm ) ;
}
}