do_reserve_legitimization_process.sql (3278B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2026 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_reserve_legitimization_process; 18 CREATE FUNCTION exchange_do_reserve_legitimization_process( 19 IN in_h_payto BYTEA, 20 IN in_measure_index INT4, 21 IN in_legitimization_measure_serial_id INT8, 22 IN in_provider_name TEXT, 23 IN in_now INT8, 24 IN in_lease_expiration INT8, 25 OUT out_process_row INT8, 26 OUT out_start_time INT8, 27 OUT out_redirect_url TEXT, 28 OUT out_state INT4) 29 LANGUAGE plpgsql 30 AS $$ 31 DECLARE 32 my_rec RECORD; 33 BEGIN 34 -- Keep these INT4 values synchronized with 35 -- enum TALER_EXCHANGEDB_KycProcessReservationState: 36 -- 0 = KPRS_READY, 1 = KPRS_OWNER, 2 = KPRS_BUSY. 37 PERFORM FROM kyc_targets 38 WHERE h_normalized_payto=in_h_payto 39 FOR UPDATE; 40 41 SELECT legitimization_process_serial_id, 42 start_time, 43 redirect_url 44 INTO my_rec 45 FROM legitimization_processes 46 WHERE h_payto=in_h_payto 47 AND provider_name=in_provider_name 48 AND NOT finished 49 AND process_expiration_time > in_now 50 ORDER BY start_time DESC 51 LIMIT 1 52 FOR UPDATE; 53 54 IF FOUND 55 THEN 56 out_process_row=my_rec.legitimization_process_serial_id; 57 out_start_time=my_rec.start_time; 58 out_redirect_url=my_rec.redirect_url; 59 IF out_redirect_url IS NULL 60 THEN 61 out_state=2; 62 ELSE 63 out_state=0; 64 END IF; 65 RETURN; 66 END IF; 67 68 UPDATE legitimization_processes 69 SET finished=TRUE 70 WHERE h_payto=in_h_payto 71 AND provider_name=in_provider_name 72 AND NOT finished; 73 74 INSERT INTO legitimization_processes 75 (h_payto, 76 start_time, 77 process_expiration_time, 78 expiration_time, 79 provider_name, 80 provider_user_id, 81 provider_legitimization_id, 82 redirect_url, 83 finished, 84 legitimization_measure_serial_id, 85 measure_index, 86 error_code, 87 error_message) 88 VALUES 89 (in_h_payto, 90 in_now, 91 in_lease_expiration, 92 0, 93 in_provider_name, 94 NULL, 95 NULL, 96 NULL, 97 FALSE, 98 in_legitimization_measure_serial_id, 99 in_measure_index, 100 0, 101 NULL) 102 ON CONFLICT (legitimization_measure_serial_id,measure_index) 103 DO UPDATE SET 104 h_payto=in_h_payto, 105 start_time=in_now, 106 process_expiration_time=in_lease_expiration, 107 expiration_time=0, 108 provider_name=in_provider_name, 109 provider_user_id=NULL, 110 provider_legitimization_id=NULL, 111 redirect_url=NULL, 112 finished=FALSE, 113 error_code=0, 114 error_message=NULL 115 RETURNING legitimization_process_serial_id, 116 start_time 117 INTO out_process_row, 118 out_start_time; 119 out_redirect_url=NULL; 120 out_state=1; 121 END $$;