summaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_insert_aml_decision.sql
blob: b3f77c8c4ea30ad4bf231d2ce0cf4b6058ebde23 (plain)
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
--
-- This file is part of TALER
-- Copyright (C) 2023 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/>
--

CREATE OR REPLACE FUNCTION exchange_do_insert_aml_decision(
  IN in_h_payto BYTEA,
  IN in_new_threshold_val INT8,
  IN in_new_threshold_frac INT4,
  IN in_new_status INT4,
  IN in_decision_time INT8,
  IN in_justification VARCHAR,
  IN in_decider_pub BYTEA,
  IN in_decider_sig BYTEA,
  OUT out_invalid_officer BOOLEAN,
  OUT out_last_date INT8)
LANGUAGE plpgsql
AS $$
BEGIN
-- Check officer is eligible to make decisions.
PERFORM
  FROM exchange.aml_staff
  WHERE decider_pub=in_decider_pub
    AND is_active
    AND NOT read_only;
IF NOT FOUND
THEN
  out_invalid_officer=TRUE;
  out_last_date=0;
  RETURN;
END IF;
out_invalid_officer=FALSE;

-- Check no more recent decision exists.
SELECT decision_time
  INTO out_last_date
  FROM exchange.aml_history
  WHERE h_payto=in_h_payto
  ORDER BY decision_time DESC;
IF FOUND
THEN
  IF out_last_date >= in_decision_time
  THEN
    -- Refuse to insert older decision.
    RETURN;
  END IF;
  UPDATE exchange.aml_status
    SET threshold_val=in_threshold_val
       ,threshold_frac=in_threshold_frac
       ,status=in_new_status
   WHERE h_payto=in_h_payto;
  ASSERT FOUND, 'cannot have AML decision history but no AML status';
ELSE
  out_last_date = 0;
  INSERT INTO exchange.aml_status
    (h_payto
    ,threshold_val
    ,threshold_frac
    ,status)
    VALUES
    (in_h_payto
    ,in_threshold_val
    ,in_threshold_frac
    ,in_new_status);
END IF;


INSERT INTO exchange.aml_history
  (h_payto
  ,new_threshold_val
  ,new_threshold_frac
  ,new_status
  ,decision_time
  ,justification
  ,decider_pub
  ,decider_sig
  ) VALUES
  (in_h_payto
  ,in_new_threshold_val
  ,in_new_threshold_frac
  ,in_new_status
  ,in_decision_time
  ,in_justification
  ,in_decider_pub
  ,in_decider_sig);

END $$;


COMMENT ON FUNCTION exchange_do_insert_aml_decision(BYTEA, INT8, INT4, INT4, INT8, VARCHAR, BYTEA, BYTEA)
  IS 'Checks whether the AML officer is eligible to make AML decisions and if so inserts the decision into the table';