From f3aa67e0f1f2a597d58734f8b0523d5723aba384 Mon Sep 17 00:00:00 2001 From: Kukks Date: Sat, 21 Sep 2019 17:07:48 +0200 Subject: [PATCH] make invoice repository able to query more extensively --- .../Controllers/InvoiceController.API.cs | 2 +- .../Controllers/InvoiceController.UI.cs | 4 +- .../Payments/Bitcoin/NBXplorerListener.cs | 3 +- .../Services/Invoices/InvoiceRepository.cs | 43 ++++++++++--------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.API.cs b/BTCPayServer/Controllers/InvoiceController.API.cs index c949f8e26..6e90700a7 100644 --- a/BTCPayServer/Controllers/InvoiceController.API.cs +++ b/BTCPayServer/Controllers/InvoiceController.API.cs @@ -41,7 +41,7 @@ namespace BTCPayServer.Controllers { var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() { - InvoiceId = id, + InvoiceId = new[] {id}, StoreId = new[] { HttpContext.GetStoreData().Id } })).FirstOrDefault(); if (invoice == null) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index bb1b76014..7c4cb3be2 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -37,7 +37,7 @@ namespace BTCPayServer.Controllers { var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() { - InvoiceId = invoiceId, + InvoiceId = new[] {invoiceId}, UserId = GetUserId(), IncludeAddresses = true, IncludeEvents = true @@ -582,7 +582,7 @@ namespace BTCPayServer.Controllers { var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery() { - InvoiceId = invoiceId, + InvoiceId = new[] {invoiceId}, UserId = GetUserId() })).FirstOrDefault(); diff --git a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index bd01ab410..94b7672c3 100644 --- a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -149,7 +149,8 @@ namespace BTCPayServer.Payments.Bitcoin foreach (var txCoin in evt.TransactionData.Transaction.Outputs.AsCoins() .Where(o => o.ScriptPubKey == output.ScriptPubKey)) { - var invoice = await _InvoiceRepository.GetInvoiceFromScriptPubKey(output.ScriptPubKey, network.CryptoCode); + var key = output.ScriptPubKey.Hash + "#" + network.CryptoCode; + var invoice = (await _InvoiceRepository.GetInvoicesFromAddresses(new [] {key})).FirstOrDefault(); if (invoice != null) { var paymentData = new BitcoinLikePaymentData(txCoin, evt.TransactionData.Transaction.RBF); diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 0a578c185..e0ce31cad 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -1,6 +1,7 @@ using DBriize; using Microsoft.Extensions.Logging; using System; +using System.Collections; using System.Collections.Generic; using System.Text; using NBitpayClient; @@ -80,30 +81,31 @@ retry: } } - public async Task GetInvoiceFromScriptPubKey(Script scriptPubKey, string cryptoCode) + public async Task> GetInvoicesFromAddresses(string[] addresses) { using (var db = _ContextFactory.CreateContext()) { - var key = scriptPubKey.Hash.ToString() + "#" + cryptoCode; - var result = (await db.AddressInvoices + return (await db.AddressInvoices #pragma warning disable CS0618 - .Where(a => a.Address == key) + .Where(a => addresses.Contains(a.Address)) #pragma warning restore CS0618 - .Select(a => a.InvoiceData) - .Include(a => a.Payments) - .Include(a => a.RefundAddresses) - .ToListAsync()).FirstOrDefault(); - if (result == null) - return null; - return ToEntity(result); + .Select(a => a.InvoiceData) + .Include(a => a.Payments) + .Include(a => a.RefundAddresses) + .ToListAsync()).Select(ToEntity); } } - public async Task GetPendingInvoices() + public async Task GetPendingInvoices(Func, IQueryable> filter = null ) { using (var ctx = _ContextFactory.CreateContext()) { - return await ctx.PendingInvoices.Select(p => p.Id).ToArrayAsync(); + var queryable = ctx.PendingInvoices.AsQueryable(); + if (filter != null) + { + queryable = filter.Invoke(queryable); + } + return await queryable.Select(p => p.Id).ToArrayAsync(); } } @@ -239,7 +241,7 @@ retry: return paymentMethod.GetPaymentMethodDetails().GetPaymentDestination(); } - public async Task NewAddress(string invoiceId, Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod paymentMethod, BTCPayNetworkBase network) + public async Task NewAddress(string invoiceId, IPaymentMethodDetails paymentMethod, BTCPayNetworkBase network) { using (var context = _ContextFactory.CreateContext()) { @@ -252,7 +254,7 @@ retry: if (currencyData == null) return false; - var existingPaymentMethod = (Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)currencyData.GetPaymentMethodDetails(); + var existingPaymentMethod = currencyData.GetPaymentMethodDetails(); if (existingPaymentMethod.GetPaymentDestination() != null) { MarkUnassigned(invoiceId, invoiceEntity, context, currencyData.GetId()); @@ -487,11 +489,12 @@ retry: { IQueryable query = context.Invoices; - if (!string.IsNullOrEmpty(queryObject.InvoiceId)) + if (queryObject.InvoiceId != null && queryObject.InvoiceId.Length > 0) { - query = query.Where(i => i.Id == queryObject.InvoiceId); + var statusSet = queryObject.InvoiceId.ToHashSet(); + query = query.Where(i => statusSet.Contains(i.Id)); } - + if (queryObject.StoreId != null && queryObject.StoreId.Length > 0) { var stores = queryObject.StoreId.ToHashSet(); @@ -666,7 +669,7 @@ retry: Network = network }; entity.SetCryptoPaymentData(paymentData); - + //TODO: abstract if (paymentMethodDetails is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod && bitcoinPaymentMethod.NetworkFeeMode == NetworkFeeMode.MultiplePaymentsOnly && bitcoinPaymentMethod.NextNetworkFee == Money.Zero) @@ -811,7 +814,7 @@ retry: get; set; } - public string InvoiceId + public string[] InvoiceId { get; set;