exchange

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

exchange_do_select_justification_for_missing_wire.sql (2901B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2023 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 -- @author: Christian Grothoff
     17 
     18 CREATE OR REPLACE FUNCTION exchange_do_select_justification_missing_wire(
     19   IN in_wire_target_h_payto BYTEA,
     20   IN in_current_time INT8,
     21   OUT out_payto_uri TEXT, -- NULL allowed
     22   OUT out_kyc_pending TEXT, -- NULL allowed
     23   OUT out_aml_status INT4, -- NULL allowed
     24   OUT out_aml_limit taler_amount) -- NULL allowed!
     25 LANGUAGE plpgsql
     26 AS $$
     27 DECLARE
     28   my_required_checks TEXT[];
     29 DECLARE
     30   my_aml_data RECORD;
     31 DECLARE
     32   satisfied CURSOR FOR
     33   SELECT satisfied_checks
     34     FROM kyc_attributes
     35    WHERE h_payto=in_wire_target_h_payto
     36      AND expiration_time < in_current_time;
     37 DECLARE
     38   i RECORD;
     39 BEGIN
     40 
     41   -- Fetch payto URI
     42   out_payto_uri = NULL;
     43   SELECT payto_uri
     44     INTO out_payto_uri
     45     FROM wire_targets
     46    WHERE wire_target_h_payto=my_wire_target_h_payto;
     47 
     48   -- Check KYC status
     49   my_required_checks = NULL;
     50   SELECT string_to_array (required_checks, ' ')
     51     INTO my_required_checks
     52     FROM legitimization_requirements
     53     WHERE h_payto=my_wire_target_h_payto;
     54 
     55   -- Get last AML decision
     56   SELECT
     57       new_threshold
     58      ,kyc_requirements
     59      ,new_status
     60     INTO
     61       my_aml_data
     62      FROM aml_history
     63     WHERE h_payto=in_wire_target_h_payto
     64     ORDER BY aml_history_serial_id -- get last decision
     65       DESC LIMIT 1;
     66   IF FOUND
     67   THEN
     68     out_aml_limit=my_aml_data.new_threshold;
     69     out_aml_status=my_aml_data.kyc_status;
     70     -- Combine KYC requirements
     71     my_required_checks
     72        = array_cat (my_required_checks,
     73                     my_aml_data.kyc_requirements);
     74   ELSE
     75     out_aml_limit=NULL;
     76     out_aml_status=0; -- or NULL? Style question!
     77   END IF;
     78 
     79   OPEN satisfied;
     80   LOOP
     81     FETCH NEXT FROM satisfied INTO i;
     82     EXIT WHEN NOT FOUND;
     83 
     84     -- remove all satisfied checks from the list
     85     FOR i in 1..array_length(i.satisfied_checks)
     86     LOOP
     87       my_required_checks
     88         = array_remove (my_required_checks,
     89                         i.satisfied_checks[i]);
     90     END LOOP;
     91   END LOOP;
     92 
     93   -- Return remaining required checks as one string
     94   IF ( (my_required_checks IS NOT NULL) AND
     95        (0 < array_length(my_satisfied_checks)) )
     96   THEN
     97     out_kyc_pending
     98       = array_to_string (my_required_checks, ' ');
     99   END IF;
    100 
    101   RETURN;
    102 END $$;