summaryrefslogtreecommitdiff
path: root/src/exchangedb/exchange_do_insert_aml_decision.sql
blob: c8ed7e9280827d67f8882be0a2c3f7708ad1d756 (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
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
--
-- 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 taler_amount,
  IN in_new_status INT4,
  IN in_decision_time INT8,
  IN in_justification TEXT,
  IN in_decider_pub BYTEA,
  IN in_decider_sig BYTEA,
  IN in_notify_s TEXT,
  IN in_kyc_requirements TEXT,
  IN in_requirement_row INT8,
  OUT out_invalid_officer BOOLEAN,
  OUT out_last_date INT8)
LANGUAGE plpgsql
AS $$
BEGIN
-- Check officer is eligible to make decisions.
PERFORM
  FROM 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 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 aml_status
    SET threshold=in_new_threshold
       ,status=in_new_status
       ,kyc_requirement=in_requirement_row
   WHERE h_payto=in_h_payto;
  ASSERT FOUND, 'cannot have AML decision history but no AML status';
ELSE
  out_last_date = 0;
  INSERT INTO aml_status
    (h_payto
    ,threshold
    ,status
    ,kyc_requirement)
    VALUES
    (in_h_payto
    ,in_new_threshold
    ,in_new_status
    ,in_requirement_row)
    ON CONFLICT (h_payto) DO
    UPDATE SET
       threshold=in_new_threshold
      ,status=in_new_status;
END IF;


INSERT INTO aml_history
  (h_payto
  ,new_threshold
  ,new_status
  ,decision_time
  ,justification
  ,kyc_requirements
  ,kyc_req_row
  ,decider_pub
  ,decider_sig
  ) VALUES
  (in_h_payto
  ,in_new_threshold
  ,in_new_status
  ,in_decision_time
  ,in_justification
  ,in_kyc_requirements
  ,in_requirement_row
  ,in_decider_pub
  ,in_decider_sig);


-- wake up taler-exchange-aggregator
IF 0 = in_new_status
THEN
  INSERT INTO kyc_alerts
    (h_payto
    ,trigger_type)
    VALUES
    (in_h_payto,1);

   EXECUTE FORMAT (
     'NOTIFY %s'
    ,in_notify_s);

END IF;


END $$;


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