fix InvoiceRepository.GetMonitoredInvoices (#6243)

This commit is contained in:
Vincent Bouzon 2024-09-24 08:44:51 +02:00 committed by GitHub
parent 587d3aa612
commit fe48cd4236
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 6 deletions

View file

@ -24,5 +24,6 @@
<None Remove="DBScripts\004.MonitoredInvoices.sql" />
<None Remove="DBScripts\005.PaymentsRenaming.sql" />
<None Remove="DBScripts\006.PaymentsRenaming.sql" />
<None Remove="DBScripts\007.PaymentsRenaming.sql" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,18 @@
DROP FUNCTION get_monitored_invoices;
CREATE OR REPLACE FUNCTION get_monitored_invoices(arg_payment_method_id TEXT, include_non_activated BOOLEAN)
RETURNS TABLE (invoice_id TEXT, payment_id TEXT, payment_method_id TEXT) AS $$
WITH cte AS (
-- Get all the invoices which are pending. Even if no payments.
SELECT i."Id" invoice_id, p."Id" payment_id, p."PaymentMethodId" payment_method_id FROM "Invoices" i LEFT JOIN "Payments" p ON i."Id" = p."InvoiceDataId"
WHERE is_pending(i."Status")
UNION ALL
-- For invoices not pending, take all of those which have pending payments
SELECT i."Id", p."Id", p."PaymentMethodId" payment_method_id FROM "Invoices" i INNER JOIN "Payments" p ON i."Id" = p."InvoiceDataId"
WHERE is_pending(p."Status") AND NOT is_pending(i."Status"))
SELECT cte.* FROM cte
LEFT JOIN "Payments" p ON cte.payment_id=p."Id" AND cte.payment_id=p."PaymentMethodId"
LEFT JOIN "Invoices" i ON cte.invoice_id=i."Id"
WHERE (p."PaymentMethodId" IS NOT NULL AND p."PaymentMethodId" = arg_payment_method_id) OR
(p."PaymentMethodId" IS NULL AND get_prompt(i."Blob2", arg_payment_method_id) IS NOT NULL AND
(include_non_activated IS TRUE OR (get_prompt(i."Blob2", arg_payment_method_id)->'activated')::BOOLEAN IS NOT FALSE));
$$ LANGUAGE SQL STABLE;

View file

@ -0,0 +1,15 @@
using BTCPayServer.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BTCPayServer.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20240924071444_temprefactor4")]
[DBScript("007.PaymentsRenaming.sql")]
public partial class temprefactor4 : DBScriptsMigration
{
}
}

View file

@ -104,14 +104,9 @@ namespace BTCPayServer.Services.Invoices
/// <returns></returns>
public async Task<InvoiceEntity[]> GetMonitoredInvoices(PaymentMethodId paymentMethodId, bool includeNonActivated, CancellationToken cancellationToken = default)
{
var pmi = paymentMethodId.ToString();
using var ctx = _applicationDbContextFactory.CreateContext();
var conn = ctx.Database.GetDbConnection();
string includeNonActivateQuery = String.Empty;
if (includeNonActivated)
includeNonActivateQuery = " AND (get_prompt(i.\"Blob2\", @pmi)->'activated')::BOOLEAN IS NOT FALSE)";
var rows = await conn.QueryAsync<(string Id, uint xmin, string[] addresses, string[] payments, string invoice)>(new("""
SELECT
i."Id",
@ -123,7 +118,6 @@ namespace BTCPayServer.Services.Invoices
LEFT JOIN "Payments" p ON p."Id" = m.payment_id AND p."PaymentMethodId" = m.payment_method_id
LEFT JOIN "Invoices" i ON i."Id" = m.invoice_id
LEFT JOIN "AddressInvoices" ai ON i."Id" = ai."InvoiceDataId"
WHERE ai."PaymentMethodId" = @pmi
GROUP BY i."Id";
"""
, new { pmi = paymentMethodId.ToString(), includeNonActivated }));