summaryrefslogtreecommitdiff
path: root/src/backenddb/pg_insert_deposit_to_transfer.sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/backenddb/pg_insert_deposit_to_transfer.sql')
-rw-r--r--src/backenddb/pg_insert_deposit_to_transfer.sql75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/backenddb/pg_insert_deposit_to_transfer.sql b/src/backenddb/pg_insert_deposit_to_transfer.sql
new file mode 100644
index 00000000..ddc291c3
--- /dev/null
+++ b/src/backenddb/pg_insert_deposit_to_transfer.sql
@@ -0,0 +1,75 @@
+--
+-- This file is part of TALER
+-- Copyright (C) 2024 Taler Systems SA
+--
+-- TALER is free software; you can redistribute it and/or modify it under the
+-- terms of the GNU General Public License as published by the Free Software
+-- Foundation; either version 3, or (at your option) any later version.
+--
+-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along with
+-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+--
+
+
+CREATE OR REPLACE FUNCTION merchant_insert_deposit_to_transfer (
+ IN in_deposit_serial INT8,
+ IN in_amount_with_fee taler_amount_currency,
+ IN in_execution_time INT8,
+ IN in_exchange_sig BYTEA,
+ IN in_exchange_pub BYTEA,
+ IN in_wtid BYTEA,
+ OUT out_wire_pending_cleared BOOL)
+LANGUAGE plpgsql
+AS $$
+DECLARE
+ decose INT8;
+BEGIN
+
+out_wire_pending_cleared=FALSE;
+INSERT INTO merchant_deposit_to_transfer
+ (deposit_serial
+ ,coin_contribution_value
+ ,credit_serial
+ ,execution_time
+ ,signkey_serial
+ ,exchange_sig
+ )
+ SELECT
+ in_deposit_serial
+ ,in_amount_with_fee
+ ,credit_serial
+ ,in_execution_time
+ ,signkey_serial
+ ,in_exchange_sig
+ FROM merchant_transfers
+ CROSS JOIN merchant_exchange_signing_keys
+ WHERE exchange_pub=in_exchange_pub
+ AND wtid=in_wtid
+ ON CONFLICT DO NOTHING;
+
+IF NOT FOUND
+THEN
+ SELECT deposit_confirmation_serial
+ INTO decose
+ FROM merchant_deposits
+ WHERE deposit_serial=in_deposit_serial;
+
+ -- we made a change, check about clearing wire_pending
+ UPDATE merchant_deposit_confirmations
+ SET wire_pending=FALSE
+ WHERE (deposit_confirmation_serial=decose)
+ AND NOT EXISTS (
+ SELECT *
+ FROM merchant_deposits md
+ LEFT JOIN merchant_deposit_to_transfer mdtt
+ USING (deposit_serial)
+ WHERE md.deposit_confirmation_serial=decose
+ AND mdtt.credit_serial IS NULL);
+ out_wire_pending_cleared=FOUND;
+END IF;
+
+END $$;