summaryrefslogtreecommitdiff
path: root/src/backenddb/pg_insert_deposit_to_transfer.sql
blob: ddc291c394e5c87d97d07c58241f8e8c5fa14336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 $$;