exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_do_insert_successor_measure.sql (4397B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2023, 2024 Taler Systems SA
      4 --
      5 -- TALER is free software; you can redistribute it and/or modify it under the
      6 -- terms of the GNU General Public License as published by the Free Software
      7 -- Foundation; either version 3, or (at your option) any later version.
      8 --
      9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 -- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 --
     13 -- You should have received a copy of the GNU General Public License along with
     14 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 
     17 DROP FUNCTION IF EXISTS exchange_do_insert_successor_measure;
     18 CREATE FUNCTION exchange_do_insert_successor_measure(
     19   IN in_h_normalized_payto BYTEA,
     20   IN in_decision_time INT8,
     21   IN in_expiration_time INT8,
     22   IN in_new_measure_name TEXT, -- can be NULL
     23   IN in_jmeasures JSONB, -- can be NULL
     24   OUT out_last_date INT8,
     25   OUT out_account_unknown BOOLEAN,
     26   OUT out_legitimization_measure_serial_id INT8
     27 )
     28 LANGUAGE plpgsql
     29 AS $$
     30 DECLARE
     31   my_outcome_serial_id INT8;
     32   my_access_token BYTEA;
     33   my_is_wallet BOOL;
     34 BEGIN
     35 
     36 out_account_unknown=FALSE;
     37 out_legitimization_measure_serial_id=0;
     38 
     39 -- Check no more recent decision exists.
     40 SELECT decision_time
     41   INTO out_last_date
     42   FROM legitimization_outcomes
     43  WHERE h_payto=in_h_normalized_payto
     44    AND is_active
     45  ORDER BY decision_time DESC, outcome_serial_id DESC;
     46 
     47 IF FOUND
     48 THEN
     49   IF out_last_date > in_decision_time
     50   THEN
     51     -- Refuse to insert older decision.
     52     RETURN;
     53   END IF;
     54   UPDATE legitimization_outcomes
     55      SET is_active=FALSE
     56    WHERE h_payto=in_h_normalized_payto
     57      AND is_active;
     58 ELSE
     59   out_last_date = 0;
     60 END IF;
     61 
     62 SELECT access_token
     63   INTO my_access_token
     64   FROM kyc_targets
     65  WHERE h_normalized_payto=in_h_normalized_payto;
     66 
     67 IF NOT FOUND
     68 THEN
     69   IF in_payto_uri IS NULL
     70   THEN
     71     -- AML decision on an unknown account without payto_uri => fail.
     72     out_account_unknown=TRUE;
     73     RETURN;
     74   END IF;
     75 
     76   my_is_wallet
     77     = (LOWER (SUBSTRING (in_payto_uri, 0, 23)) =
     78        'payto://taler-reserve/') OR
     79       (LOWER (SUBSTRING (in_payto_uri, 0, 28)) =
     80        'payto://taler-reserve-http/');
     81   INSERT INTO kyc_targets
     82     (h_normalized_payto
     83     ,is_wallet)
     84     VALUES
     85     (in_h_normalized_payto
     86     ,my_is_wallet)
     87     RETURNING access_token
     88       INTO my_access_token;
     89   INSERT INTO wire_targets
     90     (wire_target_h_payto
     91     ,h_normalized_payto
     92     ,payto_uri)
     93     VALUES
     94     (in_h_full_payto
     95     ,in_h_normalized_payto
     96     ,in_payto_uri)
     97   ON CONFLICT DO NOTHING;
     98 END IF;
     99 
    100 
    101 -- First check if a perfectly equivalent legi measure
    102 -- already exists, to avoid creating tons of duplicates.
    103 SELECT legitimization_measure_serial_id
    104   INTO out_legitimization_measure_serial_id
    105   FROM legitimization_measures
    106   WHERE access_token=my_access_token
    107     AND jmeasures=in_jmeasures
    108     AND NOT is_finished;
    109 
    110 IF NOT FOUND
    111 THEN
    112   -- Enable new legitimization measure
    113   INSERT INTO legitimization_measures
    114     (access_token
    115     ,start_time
    116     ,jmeasures
    117     ,display_priority)
    118     VALUES
    119     (my_access_token
    120     ,in_decision_time
    121     ,in_jmeasures
    122     ,1)
    123     RETURNING
    124       legitimization_measure_serial_id
    125     INTO
    126       out_legitimization_measure_serial_id;
    127 END IF;
    128 
    129 -- AML decision: mark all other active measures finished!
    130 UPDATE legitimization_measures
    131   SET is_finished=TRUE
    132   WHERE access_token=my_access_token
    133     AND NOT is_finished
    134     AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
    135 
    136 UPDATE legitimization_outcomes
    137    SET is_active=FALSE
    138  WHERE h_payto=in_h_normalized_payto
    139    -- this clause is a minor optimization to avoid
    140    -- updating outcomes that have long expired.
    141    AND expiration_time >= in_decision_time;
    142 
    143 INSERT INTO legitimization_outcomes
    144   (h_payto
    145   ,decision_time
    146   ,expiration_time
    147   ,jproperties
    148   ,new_measure_name
    149   ,to_investigate
    150   ,jnew_rules
    151   )
    152   VALUES
    153   (in_h_normalized_payto
    154   ,in_decision_time
    155   ,in_expiration_time
    156   ,'{}'::JSONB
    157   ,in_new_measure_name
    158   ,FALSE
    159   ,NULL
    160   )
    161   RETURNING
    162     outcome_serial_id
    163   INTO
    164     my_outcome_serial_id;
    165 
    166 END $$;
    167 
    168 
    169 COMMENT ON FUNCTION exchange_do_insert_successor_measure(BYTEA, INT8, INT8, TEXT, JSONB)
    170   IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';