1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
--
-- This file is part of TALER
-- Copyright (C) 2023, 2024 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
--
DROP FUNCTION IF EXISTS exchange_do_insert_successor_measure;
CREATE FUNCTION exchange_do_insert_successor_measure(
IN in_h_normalized_payto BYTEA,
IN in_decision_time INT8,
IN in_expiration_time INT8,
IN in_new_measure_name TEXT, -- can be NULL
IN in_jmeasures TEXT, -- can be NULL
OUT out_last_date INT8,
OUT out_account_unknown BOOLEAN,
OUT out_legitimization_measure_serial_id INT8
)
LANGUAGE plpgsql
AS $$
DECLARE
my_outcome_serial_id INT8;
my_access_token BYTEA;
BEGIN
out_account_unknown=FALSE;
out_legitimization_measure_serial_id=0;
-- Check no more recent decision exists.
SELECT decision_time
INTO out_last_date
FROM legitimization_outcomes
WHERE h_payto=in_h_normalized_payto
AND is_active
ORDER BY decision_time DESC, outcome_serial_id DESC;
IF FOUND
THEN
IF out_last_date >= in_decision_time
THEN
-- Refuse to insert older decision.
RETURN;
END IF;
UPDATE legitimization_outcomes
SET is_active=FALSE
WHERE h_payto=in_h_normalized_payto
AND is_active;
ELSE
out_last_date = 0;
END IF;
SELECT access_token
INTO my_access_token
FROM wire_targets
WHERE h_normalized_payto=in_h_normalized_payto;
IF NOT FOUND
THEN
IF in_payto_uri IS NULL
THEN
-- AML decision on an unknown account without payto_uri => fail.
out_account_unknown=TRUE;
RETURN;
END IF;
INSERT INTO wire_targets
(wire_target_h_payto
,h_normalized_payto
,payto_uri)
VALUES
(in_h_full_payto
,in_h_normalized_payto
,in_payto_uri)
RETURNING access_token
INTO my_access_token;
END IF;
-- First check if a perfectly equivalent legi measure
-- already exists, to avoid creating tons of duplicates.
SELECT legitimization_measure_serial_id
INTO out_legitimization_measure_serial_id
FROM legitimization_measures
WHERE access_token=my_access_token
AND jmeasures=in_jmeasures
AND NOT is_finished;
IF NOT FOUND
THEN
-- Enable new legitimization measure
INSERT INTO legitimization_measures
(access_token
,start_time
,jmeasures
,display_priority)
VALUES
(my_access_token
,in_decision_time
,in_jmeasures
,1)
RETURNING
legitimization_measure_serial_id
INTO
out_legitimization_measure_serial_id;
END IF;
-- AML decision: mark all other active measures finished!
UPDATE legitimization_measures
SET is_finished=TRUE
WHERE access_token=my_access_token
AND NOT is_finished
AND legitimization_measure_serial_id != out_legitimization_measure_serial_id;
UPDATE legitimization_outcomes
SET is_active=FALSE
WHERE h_payto=in_h_normalized_payto
-- this clause is a minor optimization to avoid
-- updating outcomes that have long expired.
AND expiration_time >= in_decision_time;
INSERT INTO legitimization_outcomes
(h_payto
,decision_time
,expiration_time
,jproperties
,new_measure_name
,to_investigate
,jnew_rules
)
VALUES
(in_h_normalized_payto
,in_decision_time
,in_expiration_time
,'{}'
,in_new_measure_name
,FALSE
,NULL
)
RETURNING
outcome_serial_id
INTO
my_outcome_serial_id;
END $$;
COMMENT ON FUNCTION exchange_do_insert_successor_measure(BYTEA, INT8, INT8, TEXT, TEXT)
IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';
|