exchange

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

insert_sanction_list_hit.sql (3078B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2025 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_sanction_list_hit;
     18 CREATE FUNCTION exchange_do_insert_sanction_list_hit(
     19   IN in_h_normalized_payto BYTEA,
     20   IN in_decision_time INT8,
     21   IN in_expiration_time INT8,
     22   IN in_properties JSONB, -- can be NULL
     23   IN in_new_rules JSONB, -- can be NULL
     24   IN in_to_investigate BOOLEAN,
     25   IN in_notify_s TEXT,
     26   IN ina_events TEXT[],
     27   OUT out_outcome_serial_id INT8)
     28 LANGUAGE plpgsql
     29 AS $$
     30 DECLARE
     31   my_i INT4;
     32   ini_event TEXT;
     33   my_rules JSONB;
     34   my_expiration INT8;
     35 BEGIN
     36 
     37 my_rules = in_new_rules;
     38 my_expiration = in_expiration_time;
     39 
     40 IF in_new_rules IS NULL
     41 THEN
     42   -- A NULL here means "flag this account, but do not change its rules".
     43   -- Storing the NULL would instead mean "no custom rules", silently
     44   -- reverting the account to the exchange defaults and revoking KYC the
     45   -- customer already passed.  So carry the rule set that currently applies
     46   -- (the one exchange_do_get_kyc_rules would pick) forward, together with
     47   -- its expiration.
     48   SELECT jnew_rules
     49         ,expiration_time
     50     INTO my_rules
     51         ,my_expiration
     52     FROM legitimization_outcomes
     53    WHERE h_payto=in_h_normalized_payto
     54      AND is_active
     55    ORDER BY expiration_time DESC
     56            ,outcome_serial_id DESC
     57    LIMIT 1;
     58   IF NOT FOUND OR my_rules IS NULL
     59   THEN
     60     -- Nothing to preserve: the account is already on the default rules.
     61     my_rules = NULL;
     62     my_expiration = in_expiration_time;
     63   END IF;
     64 END IF;
     65 
     66 -- Disable all previous legitimization outcomes.
     67 UPDATE legitimization_outcomes
     68    SET is_active=FALSE
     69  WHERE h_payto=in_h_normalized_payto;
     70 
     71 INSERT INTO legitimization_outcomes
     72   (h_payto
     73   ,decision_time
     74   ,expiration_time
     75   ,jproperties
     76   ,to_investigate
     77   ,jnew_rules
     78   )
     79   VALUES
     80   (in_h_normalized_payto
     81   ,in_decision_time
     82   ,my_expiration
     83   ,in_properties
     84   ,in_to_investigate
     85   ,my_rules
     86   )
     87   RETURNING
     88     outcome_serial_id
     89   INTO
     90     out_outcome_serial_id;
     91 
     92 -- Trigger events
     93 FOR i IN 1..COALESCE(array_length(ina_events,1),0)
     94 LOOP
     95   ini_event = ina_events[i];
     96   INSERT INTO kyc_events
     97     (event_timestamp
     98     ,event_type)
     99     VALUES
    100     (in_decision_time
    101     ,ini_event);
    102 END LOOP;
    103 
    104 EXECUTE FORMAT (
    105    'NOTIFY %s'
    106   ,in_notify_s);
    107 
    108 
    109 END $$;
    110 
    111 
    112 COMMENT ON FUNCTION exchange_do_insert_sanction_list_hit(BYTEA, INT8, INT8, JSONB, JSONB, BOOLEAN, TEXT, TEXT[])
    113   IS 'Insert result from sanction list check into the table';