exchange

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

insert_successor_measure.sql (4186B)


      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   IN in_notify_s TEXT,
     25   OUT out_last_date INT8,
     26   OUT out_account_unknown BOOLEAN,
     27   OUT out_legitimization_measure_serial_id INT8
     28 )
     29 LANGUAGE plpgsql
     30 AS $$
     31 DECLARE
     32   my_outcome_serial_id INT8;
     33   my_access_token BYTEA;
     34   my_is_wallet BOOL;
     35   my_properties JSONB;
     36   my_to_investigate BOOL;
     37 BEGIN
     38 
     39 out_account_unknown=FALSE;
     40 out_legitimization_measure_serial_id=0;
     41 
     42 -- Check no more recent decision exists.  Also remember the account
     43 -- properties and investigation state: expiring rules must not silently
     44 -- drop what AML staff recorded about the account.
     45 SELECT decision_time
     46       ,jproperties
     47       ,to_investigate
     48   INTO out_last_date
     49       ,my_properties
     50       ,my_to_investigate
     51   FROM legitimization_outcomes
     52  WHERE h_payto=in_h_normalized_payto
     53    AND is_active
     54  ORDER BY decision_time DESC, outcome_serial_id DESC;
     55 
     56 IF FOUND
     57 THEN
     58   IF out_last_date > in_decision_time
     59   THEN
     60     -- Refuse to insert older decision.
     61     RETURN;
     62   END IF;
     63   UPDATE legitimization_outcomes
     64      SET is_active=FALSE
     65    WHERE h_payto=in_h_normalized_payto
     66      AND is_active;
     67 ELSE
     68   out_last_date = 0;
     69 END IF;
     70 
     71 SELECT access_token
     72   INTO my_access_token
     73   FROM kyc_targets
     74  WHERE h_normalized_payto=in_h_normalized_payto;
     75 
     76 IF NOT FOUND
     77 THEN
     78   -- AML decision on an unknown account => fail.
     79   out_account_unknown=TRUE;
     80   RETURN;
     81 END IF;
     82 
     83 
     84 -- First check if a perfectly equivalent legi measure
     85 -- already exists, to avoid creating tons of duplicates.
     86 SELECT legitimization_measure_serial_id
     87   INTO out_legitimization_measure_serial_id
     88   FROM legitimization_measures
     89   WHERE access_token=my_access_token
     90     AND jmeasures=in_jmeasures
     91     AND NOT is_finished;
     92 
     93 IF NOT FOUND
     94 THEN
     95   -- Enable new legitimization measure
     96   INSERT INTO legitimization_measures
     97     (access_token
     98     ,start_time
     99     ,jmeasures
    100     ,display_priority)
    101     VALUES
    102     (my_access_token
    103     ,in_decision_time
    104     ,in_jmeasures
    105     ,1)
    106     RETURNING
    107       legitimization_measure_serial_id
    108     INTO
    109       out_legitimization_measure_serial_id;
    110 END IF;
    111 
    112 -- AML decision: mark all other active measures finished!
    113 UPDATE legitimization_measures
    114   SET is_finished=TRUE
    115   WHERE access_token=my_access_token
    116     AND NOT is_finished
    117     AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
    118 
    119 UPDATE legitimization_outcomes
    120    SET is_active=FALSE
    121  WHERE h_payto=in_h_normalized_payto
    122    -- this clause is a minor optimization to avoid
    123    -- updating outcomes that have long expired.
    124    AND expiration_time >= in_decision_time;
    125 
    126 INSERT INTO legitimization_outcomes
    127   (h_payto
    128   ,decision_time
    129   ,expiration_time
    130   ,jproperties
    131   ,new_measure_name
    132   ,to_investigate
    133   ,jnew_rules
    134   )
    135   VALUES
    136   (in_h_normalized_payto
    137   ,in_decision_time
    138   ,in_expiration_time
    139   ,COALESCE(my_properties, '{}'::JSONB)
    140   ,in_new_measure_name
    141   ,COALESCE(my_to_investigate, FALSE)
    142   ,NULL
    143   )
    144   RETURNING
    145     outcome_serial_id
    146   INTO
    147     my_outcome_serial_id;
    148 
    149 EXECUTE FORMAT (
    150    'NOTIFY %s'
    151   ,in_notify_s);
    152 
    153 END $$;
    154 
    155 
    156 COMMENT ON FUNCTION exchange_do_insert_successor_measure(BYTEA, INT8, INT8, TEXT, JSONB, TEXT)
    157   IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';