insert_aml_officer.sql (2499B)
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 17 CREATE OR REPLACE FUNCTION exchange_do_insert_aml_officer( 18 IN in_decider_pub BYTEA, 19 IN in_master_sig BYTEA, 20 IN in_decider_name TEXT, 21 IN in_is_active BOOLEAN, 22 IN in_read_only BOOLEAN, 23 IN in_last_change INT8, 24 OUT out_last_change INT8) 25 LANGUAGE plpgsql 26 AS $$ 27 BEGIN 28 -- aml_staff is append-only: one row per status change, the status in force 29 -- being the row with the greatest last_change. Never UPDATE a row here. 30 -- The history is what lets an auditor say who was allowed to decide when, 31 -- and an in-place update leaves aml_staff_uuid where it was, so it is 32 -- invisible to every cursor that walks the table by its serial ID -- 33 -- taler-auditor-sync would never carry the change to the auditor at all. 34 35 -- Check the new status is more recent than the one in force... 36 SELECT last_change 37 INTO out_last_change 38 FROM exchange.aml_staff 39 WHERE decider_pub=in_decider_pub 40 ORDER BY last_change DESC 41 LIMIT 1; 42 43 IF NOT FOUND 44 THEN 45 out_last_change=0; 46 ELSIF out_last_change >= in_last_change 47 THEN 48 -- Refuse to insert an older status; leave out_last_change at the 49 -- last_change we do have, so the caller can tell it was refused. 50 RETURN; 51 END IF; 52 53 INSERT INTO exchange.aml_staff 54 (decider_pub 55 ,master_sig 56 ,decider_name 57 ,is_active 58 ,read_only 59 ,last_change 60 ) VALUES 61 (in_decider_pub 62 ,in_master_sig 63 ,in_decider_name 64 ,in_is_active 65 ,in_read_only 66 ,in_last_change) 67 -- A concurrent transaction may have appended the very same status 68 -- between the SELECT above and here; that is a replay, not an error. 69 ON CONFLICT (decider_pub, last_change) DO NOTHING; 70 END $$; 71 72 73 COMMENT ON FUNCTION exchange_do_insert_aml_officer(BYTEA, BYTEA, TEXT, BOOL, BOOL, INT8) 74 IS 'Appends an AML staff status change, making sure it is more recent than the status currently in force';