exchange

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

iterate_aml_decisions.c (8848B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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  * @file exchangedb/iterate_aml_decisions.c
     18  * @brief Implementation of the iterate_aml_decisions function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/taler_kyclogic_lib.h"
     23 #include "exchange-database/iterate_aml_decisions.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Closure for #handle_aml_result.
     29  */
     30 struct AmlProcessResultContext
     31 {
     32   /**
     33    * Function to call on each result.
     34    */
     35   TALER_EXCHANGEDB_AmlDecisionCallback cb;
     36 
     37   /**
     38    * Closure for @e cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Plugin context.
     44    */
     45   struct TALER_EXCHANGEDB_PostgresContext *pg;
     46 
     47   /**
     48    * Set to #GNUNET_SYSERR on serious errors.
     49    */
     50   enum GNUNET_GenericReturnValue status;
     51 };
     52 
     53 
     54 /**
     55  * Function to be called with the results of a SELECT statement
     56  * that has returned @a num_results results.  Helper function
     57  * for #TALER_EXCHANGEDB_iterate_aml_decisions().
     58  *
     59  * @param cls closure of type `struct AmlProcessResultContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 handle_aml_result (void *cls,
     65                    PGresult *result,
     66                    unsigned int num_results)
     67 {
     68   struct AmlProcessResultContext *ctx = cls;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     struct TALER_NormalizedPaytoHashP h_payto;
     73     uint64_t rowid;
     74     char *justification = NULL;
     75     struct GNUNET_TIME_Timestamp decision_time;
     76     struct GNUNET_TIME_Absolute expiration_time;
     77     json_t *jproperties = NULL;
     78     bool is_wallet;
     79     bool to_investigate;
     80     bool is_active;
     81     json_t *account_rules = NULL;
     82     json_t *default_rules = NULL;
     83     struct TALER_FullPayto payto;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       GNUNET_PQ_result_spec_uint64 ("outcome_serial_id",
     86                                     &rowid),
     87       GNUNET_PQ_result_spec_auto_from_type ("h_payto",
     88                                             &h_payto),
     89       GNUNET_PQ_result_spec_bool ("is_wallet",
     90                                   &is_wallet),
     91       GNUNET_PQ_result_spec_allow_null (
     92         GNUNET_PQ_result_spec_string ("justification",
     93                                       &justification),
     94         NULL),
     95       GNUNET_PQ_result_spec_timestamp ("decision_time",
     96                                        &decision_time),
     97       GNUNET_PQ_result_spec_absolute_time ("expiration_time",
     98                                            &expiration_time),
     99       GNUNET_PQ_result_spec_allow_null (
    100         TALER_PQ_result_spec_json ("jproperties",
    101                                    &jproperties),
    102         NULL),
    103       GNUNET_PQ_result_spec_allow_null (
    104         TALER_PQ_result_spec_json ("jnew_rules",
    105                                    &account_rules),
    106         NULL),
    107       GNUNET_PQ_result_spec_bool ("to_investigate",
    108                                   &to_investigate),
    109       GNUNET_PQ_result_spec_bool ("is_active",
    110                                   &is_active),
    111       GNUNET_PQ_result_spec_string ("payto_uri",
    112                                     &payto.full_payto),
    113       GNUNET_PQ_result_spec_end
    114     };
    115 
    116     if (GNUNET_OK !=
    117         GNUNET_PQ_extract_result (result,
    118                                   rs,
    119                                   i))
    120     {
    121       GNUNET_break (0);
    122       ctx->status = GNUNET_SYSERR;
    123       return;
    124     }
    125     if (GNUNET_TIME_absolute_is_past (expiration_time))
    126       is_active = false;
    127     if (NULL == account_rules)
    128     {
    129       /* A NULL rule set means the account is on the exchange's default
    130          rules (see exchange_do_insert_successor_measure), not that it has
    131          no limits.  Report the defaults so the caller always gets a valid
    132          rule set. */
    133       default_rules = TALER_KYCLOGIC_get_default_legi_rules (is_wallet);
    134       account_rules = default_rules;
    135     }
    136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    137                 "Returning AML decisions for `%s' (%s)\n",
    138                 TALER_B2S (&h_payto),
    139                 is_wallet
    140                 ? "wallet"
    141                 : "account");
    142     ctx->cb (ctx->cb_cls,
    143              rowid,
    144              justification,
    145              &h_payto,
    146              decision_time,
    147              expiration_time,
    148              jproperties,
    149              to_investigate,
    150              is_active,
    151              is_wallet,
    152              payto,
    153              account_rules);
    154     json_decref (default_rules);
    155     GNUNET_PQ_cleanup_result (rs);
    156   }
    157 }
    158 
    159 
    160 enum GNUNET_DB_QueryStatus
    161 TALER_EXCHANGEDB_iterate_aml_decisions (
    162   struct TALER_EXCHANGEDB_PostgresContext *pg,
    163   const struct TALER_NormalizedPaytoHashP *h_payto,
    164   enum TALER_EXCHANGE_YesNoAll investigation_only,
    165   enum TALER_EXCHANGE_YesNoAll active_only,
    166   uint64_t offset,
    167   int64_t limit,
    168   TALER_EXCHANGEDB_AmlDecisionCallback cb,
    169   void *cb_cls)
    170 {
    171   uint64_t ulimit = (limit > 0) ? limit : -limit;
    172   struct GNUNET_PQ_QueryParam params[] = {
    173     GNUNET_PQ_query_param_bool (NULL == h_payto),
    174     NULL == h_payto
    175     ? GNUNET_PQ_query_param_null ()
    176     : GNUNET_PQ_query_param_auto_from_type (h_payto),
    177     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    178                                  investigation_only)),
    179     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
    180                                  investigation_only)),
    181     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    182                                  active_only)),
    183     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
    184                                  active_only)),
    185     GNUNET_PQ_query_param_uint64 (&offset),
    186     GNUNET_PQ_query_param_uint64 (&ulimit),
    187     GNUNET_PQ_query_param_end
    188   };
    189   struct AmlProcessResultContext ctx = {
    190     .cb = cb,
    191     .cb_cls = cb_cls,
    192     .pg = pg,
    193     .status = GNUNET_OK
    194   };
    195   enum GNUNET_DB_QueryStatus qs;
    196   const char *stmt = (limit > 0)
    197     ? "iterate_aml_decisions_inc"
    198     : "iterate_aml_decisions_dec";
    199 
    200   PREPARE (pg,
    201            "iterate_aml_decisions_inc",
    202            "SELECT"
    203            " lo.outcome_serial_id"
    204            ",lo.h_payto"
    205            ",ah.justification"
    206            ",lo.decision_time"
    207            ",lo.expiration_time"
    208            ",lo.jproperties::TEXT"
    209            ",lo.to_investigate"
    210            ",lo.is_active"
    211            ",lo.jnew_rules::TEXT"
    212            ",kt.is_wallet"
    213            ",wt.payto_uri"
    214            " FROM legitimization_outcomes lo"
    215            " JOIN kyc_targets kt"
    216            "   ON (lo.h_payto = kt.h_normalized_payto)"
    217            " JOIN wire_targets wt"
    218            "   ON (lo.h_payto = wt.h_normalized_payto)"
    219            " LEFT JOIN aml_history ah"
    220            "   USING (outcome_serial_id)"
    221            " WHERE (outcome_serial_id > $7)"
    222            "   AND ($1 OR (lo.h_payto = $2))"
    223            "   AND ($3 OR (lo.to_investigate = $4))"
    224            "   AND ($5 OR (lo.is_active = $6))"
    225            " ORDER BY lo.outcome_serial_id ASC"
    226            " LIMIT $8");
    227   PREPARE (pg,
    228            "iterate_aml_decisions_dec",
    229            "SELECT"
    230            " lo.outcome_serial_id"
    231            ",lo.h_payto"
    232            ",ah.justification"
    233            ",lo.decision_time"
    234            ",lo.expiration_time"
    235            ",lo.jproperties::TEXT"
    236            ",lo.to_investigate"
    237            ",lo.is_active"
    238            ",lo.jnew_rules::TEXT"
    239            ",kt.is_wallet"
    240            ",wt.payto_uri"
    241            " FROM legitimization_outcomes lo"
    242            " JOIN kyc_targets kt"
    243            "   ON (lo.h_payto = kt.h_normalized_payto)"
    244            " JOIN wire_targets wt"
    245            "   ON (lo.h_payto = wt.h_normalized_payto)"
    246            " LEFT JOIN aml_history ah"
    247            "   USING (outcome_serial_id)"
    248            " WHERE lo.outcome_serial_id < $7"
    249            "  AND ($1 OR (lo.h_payto = $2))"
    250            "  AND ($3 OR (lo.to_investigate = $4))"
    251            "  AND ($5 OR (lo.is_active = $6))"
    252            " ORDER BY lo.outcome_serial_id DESC"
    253            " LIMIT $8");
    254   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    255                                              stmt,
    256                                              params,
    257                                              &handle_aml_result,
    258                                              &ctx);
    259   if (GNUNET_OK != ctx.status)
    260     return GNUNET_DB_STATUS_HARD_ERROR;
    261   return qs;
    262 }