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