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 (3846B)


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