LCOV - code coverage report
Current view: top level - exchangedb - pg_select_aml_decisions.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 47 89.4 %
Date: 2025-06-05 21:03:14 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.16