exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

0002-aml_history.sql (4392B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2022 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 create_table_aml_history(
     18   IN partition_suffix TEXT DEFAULT NULL
     19 )
     20 RETURNS VOID
     21 LANGUAGE plpgsql
     22 AS $$
     23 DECLARE
     24   table_name TEXT DEFAULT 'aml_history';
     25 BEGIN
     26   PERFORM create_partitioned_table(
     27     'CREATE TABLE IF NOT EXISTS %I'
     28       '(aml_history_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY'
     29       ',h_payto BYTEA CHECK (LENGTH(h_payto)=32)'
     30       ',justification TEXT NOT NULL'
     31       ',decider_pub BYTEA CHECK (LENGTH(decider_pub)=32)'
     32       ',decider_sig BYTEA CHECK (LENGTH(decider_sig)=64)'
     33       ',outcome_serial_id INT8 NOT NULL'
     34       ',kyc_attributes_hash BYTEA CHECK(LENGTH(kyc_attributes_hash)=64) DEFAULT NULL'
     35       ',kyc_attributes_serial_id INT8 DEFAULT NULL'
     36     ') %s ;'
     37     ,table_name
     38     ,'PARTITION BY HASH (h_payto)'
     39     ,partition_suffix
     40   );
     41   PERFORM comment_partitioned_table(
     42      'AML decision history for a particular payment destination'
     43     ,table_name
     44     ,partition_suffix
     45   );
     46   PERFORM comment_partitioned_column(
     47      'hash of the payto://-URI this AML history is about'
     48     ,'h_payto'
     49     ,table_name
     50     ,partition_suffix
     51   );
     52   PERFORM comment_partitioned_column(
     53      'human-readable justification for the status change'
     54     ,'justification'
     55     ,table_name
     56     ,partition_suffix
     57   );
     58   PERFORM comment_partitioned_column(
     59      'Public key of the staff member who made the AML decision'
     60     ,'decider_pub'
     61     ,table_name
     62     ,partition_suffix
     63   );
     64   PERFORM comment_partitioned_column(
     65      'Signature key of the staff member affirming the AML decision; of type AML_DECISION'
     66     ,'decider_sig'
     67     ,table_name
     68     ,partition_suffix
     69   );
     70   PERFORM comment_partitioned_column(
     71      'Actual outcome for the account (included in what decider_sig signs over)'
     72     ,'outcome_serial_id'
     73     ,table_name
     74     ,partition_suffix
     75   );
     76   PERFORM comment_partitioned_column(
     77      'Hash of the new attributes inserted by the AML officer.'
     78     ,'kyc_attributes_hash'
     79     ,'aml_history'
     80     ,NULL
     81   );
     82 
     83   PERFORM comment_partitioned_column(
     84      'Attributes inserted by the AML officer.'
     85     ,'kyc_attributes_serial_id'
     86     ,'aml_history'
     87     ,NULL
     88   );
     89 END $$;
     90 
     91 COMMENT ON FUNCTION create_table_aml_history
     92   IS 'Creates the aml_history table';
     93 
     94 
     95 CREATE OR REPLACE FUNCTION constrain_table_aml_history(
     96   IN partition_suffix TEXT
     97 )
     98 RETURNS void
     99 LANGUAGE plpgsql
    100 AS $$
    101 DECLARE
    102   table_name TEXT DEFAULT 'aml_history';
    103 BEGIN
    104   table_name = concat_ws('_', table_name, partition_suffix);
    105   EXECUTE FORMAT (
    106     'ALTER TABLE ' || table_name ||
    107       ' ADD CONSTRAINT ' || table_name || '_serial_key '
    108         'UNIQUE (aml_history_serial_id)'
    109   );
    110   EXECUTE FORMAT (
    111     'CREATE INDEX ' || table_name || '_main_index '
    112     'ON ' || table_name || ' '
    113     '(h_payto);'
    114   );
    115 END $$;
    116 
    117 CREATE FUNCTION foreign_table_aml_history()
    118 RETURNS void
    119 LANGUAGE plpgsql
    120 AS $$
    121 DECLARE
    122   table_name TEXT DEFAULT 'aml_history';
    123 BEGIN
    124   EXECUTE FORMAT (
    125     'ALTER TABLE ' || table_name ||
    126     ' ADD CONSTRAINT ' || table_name || '_legitimization_outcome'
    127     ' FOREIGN KEY (outcome_serial_id)'
    128     ' REFERENCES legitimization_outcomes (outcome_serial_id)'
    129   );
    130   EXECUTE FORMAT (
    131     'ALTER TABLE ' || table_name ||
    132     ' ADD CONSTRAINT ' || table_name || '_foreign_key_kyc_attributes'
    133     ' FOREIGN KEY (kyc_attributes_serial_id)'
    134     ' REFERENCES kyc_attributes (kyc_attributes_serial_id)');
    135 END $$;
    136 
    137 
    138 INSERT INTO exchange_tables
    139     (name
    140     ,version
    141     ,action
    142     ,partitioned
    143     ,by_range)
    144   VALUES
    145     ('aml_history'
    146     ,'exchange-0002'
    147     ,'create'
    148     ,TRUE
    149     ,FALSE),
    150     ('aml_history'
    151     ,'exchange-0002'
    152     ,'constrain'
    153     ,TRUE
    154     ,FALSE),
    155     ('aml_history'
    156     ,'exchange-0002'
    157     ,'foreign'
    158     ,TRUE
    159     ,FALSE);
    160