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
{
2022-04-20 10:20:39 +09:00
private string CryptoCode ;
2022-04-12 09:55:10 +02:00
private const int TransactionDays = 7 ;
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-04-20 10:20:39 +09:00
CryptoCode = networkProvider . DefaultNetwork . CryptoCode ;
2022-04-12 09:55:10 +02:00
}
public async Task < IViewComponentResult > InvokeAsync ( StoreData store )
{
await using var ctx = _dbContextFactory . CreateContext ( ) ;
var payoutsCount = await ctx . Payouts
. Where ( p = > p . PullPaymentData . StoreId = = store . Id & & ! p . PullPaymentData . Archived & & p . State = = PayoutState . AwaitingApproval )
. CountAsync ( ) ;
var refundsCount = await ctx . Invoices
. Where ( i = > i . StoreData . Id = = store . Id & & ! i . Archived & & i . CurrentRefundId ! = null )
. CountAsync ( ) ;
var walletId = new WalletId ( store . Id , CryptoCode ) ;
var derivation = store . GetDerivationSchemeSettings ( _networkProvider , walletId . 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-04-12 09:55:10 +02:00
var afterDate = DateTimeOffset . UtcNow - TimeSpan . FromDays ( 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
}
var vm = new StoreNumbersViewModel
{
Store = store ,
WalletId = walletId ,
PayoutsPending = payoutsCount ,
Transactions = transactionsCount ,
TransactionDays = TransactionDays ,
RefundsIssued = refundsCount
} ;
return View ( vm ) ;
}
}