mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-01-02 21:54:23 +01:00
Refactor: Add GetMonitoredInvoices to fetch pending invoices or those with pending payments (#6235)
This commit is contained in:
@@ -5,5 +5,5 @@ $$ LANGUAGE sql IMMUTABLE;
|
||||
|
||||
CREATE INDEX "IX_Invoices_Pending" ON "Invoices"((1)) WHERE is_pending("Status");
|
||||
CREATE INDEX "IX_Payments_Pending" ON "Payments"((1)) WHERE is_pending("Status");
|
||||
|
||||
DROP TABLE "PendingInvoices";
|
||||
ANALYZE "Invoices";
|
||||
|
||||
22
BTCPayServer.Data/DBScripts/004.MonitoredInvoices.sql
Normal file
22
BTCPayServer.Data/DBScripts/004.MonitoredInvoices.sql
Normal file
@@ -0,0 +1,22 @@
|
||||
CREATE OR REPLACE FUNCTION get_prompt(invoice_blob JSONB, payment_method_id TEXT)
|
||||
RETURNS JSONB AS $$
|
||||
SELECT invoice_blob->'prompts'->payment_method_id
|
||||
$$ LANGUAGE sql IMMUTABLE;
|
||||
|
||||
|
||||
CREATE OR REPLACE FUNCTION get_monitored_invoices(payment_method_id TEXT)
|
||||
RETURNS TABLE (invoice_id TEXT, payment_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 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" 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"
|
||||
LEFT JOIN "Invoices" i ON cte.invoice_id=i."Id"
|
||||
WHERE (p."Type" IS NOT NULL AND p."Type" = payment_method_id) OR
|
||||
(p."Type" IS NULL AND get_prompt(i."Blob2", payment_method_id) IS NOT NULL AND (get_prompt(i."Blob2", payment_method_id)->'activated')::BOOLEAN IS NOT FALSE);
|
||||
$$ LANGUAGE SQL STABLE;
|
||||
Reference in New Issue
Block a user