diff --git a/BTCPayServer.Data/BTCPayServer.Data.csproj b/BTCPayServer.Data/BTCPayServer.Data.csproj
index f2042776f..757cb9c49 100644
--- a/BTCPayServer.Data/BTCPayServer.Data.csproj
+++ b/BTCPayServer.Data/BTCPayServer.Data.csproj
@@ -24,5 +24,6 @@
+
diff --git a/BTCPayServer.Data/DBScripts/007.PaymentsRenaming.sql b/BTCPayServer.Data/DBScripts/007.PaymentsRenaming.sql
new file mode 100644
index 000000000..ae31a9853
--- /dev/null
+++ b/BTCPayServer.Data/DBScripts/007.PaymentsRenaming.sql
@@ -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;
diff --git a/BTCPayServer.Data/Migrations/20240924071444_temprefactor4.cs b/BTCPayServer.Data/Migrations/20240924071444_temprefactor4.cs
new file mode 100644
index 000000000..968a5c6ce
--- /dev/null
+++ b/BTCPayServer.Data/Migrations/20240924071444_temprefactor4.cs
@@ -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
+ {
+ }
+}
diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs
index 7c06a1070..af2142a9b 100644
--- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs
+++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs
@@ -104,14 +104,9 @@ namespace BTCPayServer.Services.Invoices
///
public async Task 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 }));