mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 18:11:36 +01:00
Make a batch query for all pending invoice (Fix #2022)
This commit is contained in:
parent
440ce0221a
commit
b9ca02088d
@ -138,9 +138,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
||||
switch (newEvent)
|
||||
{
|
||||
case NBXplorer.Models.NewBlockEvent evt:
|
||||
await Task.WhenAll((await _InvoiceRepository.GetPendingInvoices())
|
||||
.Select(invoiceId => UpdatePaymentStates(wallet, invoiceId))
|
||||
.ToArray());
|
||||
await UpdatePaymentStates(wallet, await _InvoiceRepository.GetPendingInvoices());
|
||||
_Aggregator.Publish(new Events.NewBlockEvent() { CryptoCode = evt.CryptoCode });
|
||||
break;
|
||||
case NBXplorer.Models.NewTransactionEvent evt:
|
||||
@ -206,11 +204,21 @@ namespace BTCPayServer.Payments.Bitcoin
|
||||
}
|
||||
}
|
||||
|
||||
async Task UpdatePaymentStates(BTCPayWallet wallet, string[] invoiceIds)
|
||||
{
|
||||
var invoices = await _InvoiceRepository.GetInvoices(invoiceIds);
|
||||
await Task.WhenAll(invoices.Select(i => UpdatePaymentStates(wallet, i)).ToArray());
|
||||
}
|
||||
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, string invoiceId)
|
||||
{
|
||||
var invoice = await _InvoiceRepository.GetInvoice(invoiceId, false);
|
||||
if (invoice == null)
|
||||
return null;
|
||||
return await UpdatePaymentStates(wallet, invoice);
|
||||
}
|
||||
async Task<InvoiceEntity> UpdatePaymentStates(BTCPayWallet wallet, InvoiceEntity invoice)
|
||||
{
|
||||
|
||||
List<PaymentEntity> updatedPaymentEntities = new List<PaymentEntity>();
|
||||
var transactions = await wallet.GetTransactions(invoice.GetAllBitcoinPaymentData()
|
||||
.Select(p => p.Outpoint.Hash)
|
||||
|
@ -467,6 +467,20 @@ retry:
|
||||
var res = await GetInvoiceRaw(id, inludeAddressData);
|
||||
return res == null ? null : ToEntity(res);
|
||||
}
|
||||
public async Task<InvoiceEntity[]> GetInvoices(string[] invoiceIds)
|
||||
{
|
||||
var invoiceIdSet = invoiceIds.ToHashSet();
|
||||
using (var context = _ContextFactory.CreateContext())
|
||||
{
|
||||
IQueryable<Data.InvoiceData> query =
|
||||
context
|
||||
.Invoices
|
||||
.Include(o => o.Payments)
|
||||
.Where(o => invoiceIdSet.Contains(o.Id));
|
||||
|
||||
return (await query.ToListAsync()).Select(o => ToEntity(o)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<InvoiceData> GetInvoiceRaw(string id, bool inludeAddressData = false)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user