exchange

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

iterate_kycauth_in_inconsistencies.c (5743B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 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 src/auditordb/iterate_kycauth_in_inconsistencies.c
     18  * @brief Implementation of the iterate_kycauth_in_inconsistencies function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_error_codes.h"
     22 #include "taler/taler_dbevents.h"
     23 #include "taler/taler_pq_lib.h"
     24 #include "pg_helper.h"
     25 #include "auditor-database/iterate_kycauth_in_inconsistencies.h"
     26 
     27 
     28 struct KycauthInInconsistencyContext
     29 {
     30 
     31   /**
     32    * Function to call for each inconsistency.
     33    */
     34   TALER_AUDITORDB_KycauthInInconsistencyCallback cb;
     35 
     36   /**
     37    * Closure for @e cb
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Plugin context.
     43    */
     44   struct TALER_AUDITORDB_PostgresContext *pg;
     45 
     46   /**
     47    * Query status to return.
     48    */
     49   enum GNUNET_DB_QueryStatus qs;
     50 };
     51 
     52 
     53 /**
     54  * Helper function for #TALER_AUDITORDB_iterate_kycauth_in_inconsistencies().
     55  * To be called with the results of a SELECT statement
     56  * that has returned @a num_results results.
     57  *
     58  * @param cls closure of type `struct KycauthInInconsistencyContext *`
     59  * @param result the postgres result
     60  * @param num_results the number of results in @a result
     61  */
     62 static void
     63 kycauth_in_inconsistency_cb (void *cls,
     64                              PGresult *result,
     65                              unsigned int num_results)
     66 {
     67   struct KycauthInInconsistencyContext *dcc = cls;
     68   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     69 
     70   for (unsigned int i = 0; i < num_results; i++)
     71   {
     72     struct TALER_AUDITORDB_KycauthInInconsistency dc;
     73     struct GNUNET_PQ_ResultSpec rs[] = {
     74       GNUNET_PQ_result_spec_uint64 ("row_id",
     75                                     &dc.serial_id),
     76       GNUNET_PQ_result_spec_uint64 ("bank_row_id",
     77                                     &dc.bank_row_id),
     78       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_exchange_expected",
     79                                    &dc.amount_exchange_expected),
     80       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_wired",
     81                                    &dc.amount_wired),
     82       GNUNET_PQ_result_spec_auto_from_type ("account_pub",
     83                                             &dc.account_pub),
     84       GNUNET_PQ_result_spec_absolute_time ("timestamp",
     85                                            &dc.timestamp),
     86       GNUNET_PQ_result_spec_string ("account",
     87                                     &dc.account.full_payto),
     88       GNUNET_PQ_result_spec_string ("diagnostic",
     89                                     &dc.diagnostic),
     90       GNUNET_PQ_result_spec_bool ("suppressed",
     91                                   &dc.suppressed),
     92       GNUNET_PQ_result_spec_end
     93     };
     94     enum GNUNET_GenericReturnValue rval;
     95 
     96     if (GNUNET_OK !=
     97         GNUNET_PQ_extract_result (result,
     98                                   rs,
     99                                   i))
    100     {
    101       GNUNET_break (0);
    102       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    103       return;
    104     }
    105 
    106     dcc->qs = i + 1;
    107     rval = dcc->cb (dcc->cb_cls,
    108                     &dc);
    109     GNUNET_PQ_cleanup_result (rs);
    110     if (GNUNET_OK != rval)
    111       break;
    112   }
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TALER_AUDITORDB_iterate_kycauth_in_inconsistencies (
    118   struct TALER_AUDITORDB_PostgresContext *pg,
    119   int64_t limit,
    120   uint64_t offset,
    121   bool return_suppressed,
    122   TALER_AUDITORDB_KycauthInInconsistencyCallback cb,
    123   void *cb_cls)
    124 {
    125   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    126   struct GNUNET_PQ_QueryParam params[] = {
    127     GNUNET_PQ_query_param_uint64 (&offset),
    128     GNUNET_PQ_query_param_bool (return_suppressed),
    129     GNUNET_PQ_query_param_uint64 (&plimit),
    130     GNUNET_PQ_query_param_end
    131   };
    132   struct KycauthInInconsistencyContext dcc = {
    133     .cb = cb,
    134     .cb_cls = cb_cls,
    135     .pg = pg
    136   };
    137   enum GNUNET_DB_QueryStatus qs;
    138 
    139   PREPARE (pg,
    140            "iterate_kycauth_in_inconsistencies_get_desc",
    141            "SELECT"
    142            " row_id"
    143            ",bank_row_id"
    144            ",amount_exchange_expected"
    145            ",amount_wired"
    146            ",account_pub"
    147            ",timestamp"
    148            ",account"
    149            ",diagnostic"
    150            ",suppressed"
    151            " FROM auditor_kycauth_in_inconsistency"
    152            " WHERE (row_id < $1)"
    153            " AND ($2 OR suppressed is false)"
    154            " ORDER BY row_id DESC"
    155            " LIMIT $3"
    156            );
    157   PREPARE (pg,
    158            "iterate_kycauth_in_inconsistencies_get_asc",
    159            "SELECT"
    160            " row_id"
    161            ",bank_row_id"
    162            ",amount_exchange_expected"
    163            ",amount_wired"
    164            ",account_pub"
    165            ",timestamp"
    166            ",account"
    167            ",diagnostic"
    168            ",suppressed"
    169            " FROM auditor_kycauth_in_inconsistency"
    170            " WHERE (row_id > $1)"
    171            " AND ($2 OR suppressed is false)"
    172            " ORDER BY row_id ASC"
    173            " LIMIT $3"
    174            );
    175   qs = GNUNET_PQ_eval_prepared_multi_select (
    176     pg->conn,
    177     (limit > 0)
    178     ? "iterate_kycauth_in_inconsistencies_get_asc"
    179     : "iterate_kycauth_in_inconsistencies_get_desc",
    180     params,
    181     &kycauth_in_inconsistency_cb,
    182     &dcc);
    183   if (qs > 0)
    184     return dcc.qs;
    185   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    186   return qs;
    187 }