exchange

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

exchange_do_persist_kyc_attributes.sql (3174B)


      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_persist_kyc_attributes;
     18 CREATE FUNCTION exchange_do_persist_kyc_attributes(
     19   IN in_process_row INT8,
     20   IN in_h_payto BYTEA,
     21   IN in_birthday INT4,
     22   IN in_provider_name TEXT,
     23   IN in_provider_account_id TEXT,  -- can be NULL
     24   IN in_provider_legitimization_id TEXT, -- can be NULL
     25   IN in_collection_time_ts INT8,
     26   IN in_expiration_time INT8, -- not rounded
     27   IN in_expiration_time_ts INT8, -- rounded to timestamp
     28   IN in_enc_attributes BYTEA,
     29   IN in_kyc_completed_notify_s TEXT,
     30   IN in_form_name TEXT, -- can be NULL
     31   OUT out_ok BOOLEAN) -- set to true if we had a legi process matching in_process_row and in_provider_name for this account
     32 LANGUAGE plpgsql
     33 AS $$
     34 BEGIN
     35 
     36 INSERT INTO kyc_attributes
     37   (h_payto
     38   ,collection_time
     39   ,expiration_time
     40   ,form_name
     41   ,by_aml_officer
     42   ,encrypted_attributes
     43   ,legitimization_serial
     44  ) VALUES
     45   (in_h_payto
     46   ,in_collection_time_ts
     47   ,in_expiration_time_ts
     48   ,in_form_name
     49   ,FALSE
     50   ,in_enc_attributes
     51   ,in_process_row);
     52 
     53 -- Wake up taler-exchange-sanctionscheck to check new attributes
     54 -- This is value for TALER_DBEVENT_EXCHANGE_NEW_KYC_ATTRIBUTES.
     55 NOTIFY XSX9Z5XGWWYFKXTAYCES63B62527JKNX9XD0131Z08THVV8YW5BZG;
     56 
     57 UPDATE legitimization_processes
     58   SET provider_user_id=in_provider_account_id
     59      ,provider_legitimization_id=in_provider_legitimization_id
     60      ,expiration_time=GREATEST(expiration_time,in_expiration_time)
     61      ,finished=TRUE
     62  WHERE h_payto=in_h_payto
     63    AND legitimization_process_serial_id=in_process_row
     64    AND provider_name=in_provider_name;
     65 out_ok=FOUND;
     66 
     67 UPDATE reserves
     68    SET birthday=in_birthday
     69  WHERE (reserve_pub IN
     70     (SELECT reserve_pub
     71        FROM reserves_in
     72       WHERE wire_source_h_payto IN
     73         (SELECT wire_source_h_payto
     74            FROM wire_targets
     75           WHERE h_normalized_payto=in_h_payto) ) )
     76 -- The next 3 clauses primarily serve to limit
     77 -- unnecessary updates for reserves we do not
     78 -- care about anymore.
     79   AND ( ((current_balance).frac > 0) OR
     80         ((current_balance).val > 0 ) )
     81   AND (expiration_date > in_collection_time_ts);
     82 
     83 
     84 EXECUTE FORMAT (
     85  'NOTIFY %s'
     86  ,in_kyc_completed_notify_s);
     87 
     88 INSERT INTO kyc_alerts
     89  (h_payto
     90  ,trigger_type)
     91  VALUES
     92  (in_h_payto,1)
     93  ON CONFLICT DO NOTHING;
     94 
     95 END $$;
     96 
     97 
     98 COMMENT ON FUNCTION exchange_do_persist_kyc_attributes(INT8, BYTEA, INT4, TEXT, TEXT, TEXT, INT8, INT8, INT8, BYTEA, TEXT, TEXT)
     99   IS 'Inserts new KYC attributes and updates the status of the legitimization process';