exchange

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

pg_select_kyc_accounts.c (8451B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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_kyc_accounts.c
     18  * @brief Implementation of the select_kyc_accounts function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_select_kyc_accounts.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #handle_aml_result.
     31  */
     32 struct KycAccountResultContext
     33 {
     34   /**
     35    * Function to call on each result.
     36    */
     37   TALER_EXCHANGEDB_AmlAccountListCallback 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_kyc_accounts().
     60  *
     61  * @param cls closure of type `struct KycAccountResultContext *`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 handle_kyc_account_result (void *cls,
     67                            PGresult *result,
     68                            unsigned int num_results)
     69 {
     70   struct KycAccountResultContext *ctx = cls;
     71 
     72   for (unsigned int i = 0; i<num_results; i++)
     73   {
     74     uint64_t rowid;
     75     struct TALER_NormalizedPaytoHashP h_payto;
     76     char *comments = NULL;
     77     struct GNUNET_TIME_Timestamp open_time
     78       = GNUNET_TIME_UNIT_FOREVER_TS;
     79     struct GNUNET_TIME_Timestamp close_time
     80       = GNUNET_TIME_UNIT_FOREVER_TS;
     81     bool to_investigate;
     82     bool high_risk;
     83     struct TALER_FullPayto payto;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       GNUNET_PQ_result_spec_uint64 ("kyc_target_serial_id",
     86                                     &rowid),
     87       GNUNET_PQ_result_spec_auto_from_type ("h_payto",
     88                                             &h_payto),
     89       GNUNET_PQ_result_spec_allow_null (
     90         GNUNET_PQ_result_spec_string ("comments",
     91                                       &comments),
     92         NULL),
     93       GNUNET_PQ_result_spec_allow_null (
     94         GNUNET_PQ_result_spec_timestamp ("open_time",
     95                                          &open_time),
     96         NULL),
     97       GNUNET_PQ_result_spec_allow_null (
     98         GNUNET_PQ_result_spec_timestamp ("close_time",
     99                                          &close_time),
    100         NULL),
    101       GNUNET_PQ_result_spec_bool ("to_investigate",
    102                                   &to_investigate),
    103       GNUNET_PQ_result_spec_bool ("high_risk",
    104                                   &high_risk),
    105       GNUNET_PQ_result_spec_string ("payto_uri",
    106                                     &payto.full_payto),
    107       GNUNET_PQ_result_spec_end
    108     };
    109 
    110     if (GNUNET_OK !=
    111         GNUNET_PQ_extract_result (result,
    112                                   rs,
    113                                   i))
    114     {
    115       GNUNET_break (0);
    116       ctx->status = GNUNET_SYSERR;
    117       return;
    118     }
    119     ctx->cb (ctx->cb_cls,
    120              rowid,
    121              &h_payto,
    122              open_time,
    123              close_time,
    124              comments,
    125              high_risk,
    126              to_investigate,
    127              payto);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TEH_PG_select_kyc_accounts (
    135   void *cls,
    136   enum TALER_EXCHANGE_YesNoAll investigation_only,
    137   enum TALER_EXCHANGE_YesNoAll open_only,
    138   enum TALER_EXCHANGE_YesNoAll high_risk_only,
    139   uint64_t offset,
    140   int64_t limit,
    141   TALER_EXCHANGEDB_AmlAccountListCallback cb,
    142   void *cb_cls)
    143 {
    144   struct PostgresClosure *pg = cls;
    145   uint64_t ulimit = (limit > 0) ? limit : -limit;
    146   struct GNUNET_PQ_QueryParam params[] = {
    147     GNUNET_PQ_query_param_uint64 (&offset),
    148     GNUNET_PQ_query_param_uint64 (&ulimit),
    149     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    150                                  investigation_only)),
    151     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
    152                                  investigation_only)),
    153     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    154                                  open_only)),
    155     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
    156                                  open_only)),
    157     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    158                                  high_risk_only)),
    159     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
    160                                  high_risk_only)),
    161     GNUNET_PQ_query_param_end
    162   };
    163   struct KycAccountResultContext ctx = {
    164     .cb = cb,
    165     .cb_cls = cb_cls,
    166     .pg = pg,
    167     .status = GNUNET_OK
    168   };
    169   enum GNUNET_DB_QueryStatus qs;
    170   const char *stmt = (limit > 0)
    171     ? "select_kyc_accounts_inc"
    172     : "select_kyc_accounts_dec";
    173 
    174   PREPARE (pg,
    175            "select_kyc_accounts_inc",
    176            "SELECT"
    177            " kt.kyc_target_serial_id"
    178            ",kt.h_normalized_payto AS h_payto"
    179            ",kt.open_time"
    180            ",kt.close_time"
    181            ",lo.jproperties ->> 'FILE_NOTE' AS comments"
    182            ",lo.jproperties ->> 'open_date' AS open_time"
    183            ",COALESCE(lo.to_investigate,FALSE) AS to_investigate"
    184            ",COALESCE((lo.jproperties ->> 'HIGH_RISK_CUSTOMER')::bool,FALSE) AS high_risk"
    185            ",wt.payto_uri"
    186            " FROM kyc_targets kt"
    187            " JOIN wire_targets wt"
    188            "   ON (wt.h_normalized_payto = kt.h_normalized_payto)"
    189            " LEFT JOIN legitimization_outcomes lo"
    190            "   ON (lo.h_payto = kt.h_normalized_payto)"
    191            " WHERE (kyc_target_serial_id > $1)"
    192            // select most recent outcomes only
    193            "   AND COALESCE (lo.is_active, TRUE)"
    194            "   AND ($3 OR (COALESCE(lo.to_investigate,FALSE) = $4))"
    195            // Account is open if we had an AML outcome
    196            "   AND ($5 OR ((lo.outcome_serial_id IS NULL) = $6))"
    197            "   AND ($7 OR ((COALESCE((lo.jproperties ->>'high_risk')::bool,FALSE) = $8)))"
    198            " ORDER BY kt.kyc_target_serial_id ASC"
    199            " LIMIT $2");
    200   PREPARE (pg,
    201            "select_kyc_accounts_dec",
    202            "SELECT"
    203            " kt.kyc_target_serial_id"
    204            ",kt.h_normalized_payto AS h_payto"
    205            ",kt.open_time"
    206            ",kt.close_time"
    207            ",lo.jproperties ->> 'FILE_NOTE' AS comments"
    208            ",lo.jproperties ->> 'open_date' AS open_time"
    209            ",COALESCE(lo.to_investigate,FALSE) AS to_investigate"
    210            ",COALESCE((lo.jproperties ->> 'HIGH_RISK_CUSTOMER')::bool,FALSE) AS high_risk"
    211            ",wt.payto_uri"
    212            " FROM kyc_targets kt"
    213            " LEFT JOIN legitimization_outcomes lo"
    214            "   ON (lo.h_payto = kt.h_normalized_payto)"
    215            " LEFT JOIN LATERAL ("
    216            "   SELECT payto_uri"
    217            "     FROM wire_targets"
    218            "    WHERE h_normalized_payto = kt.h_normalized_payto"
    219            "    ORDER BY wire_target_serial_id DESC"
    220            "    LIMIT 1"
    221            " ) wt ON true"
    222            " WHERE (kyc_target_serial_id < $1)"
    223            // select most recent outcomes only
    224            "   AND COALESCE (lo.is_active, TRUE)"
    225            "   AND ($3 OR (COALESCE(lo.to_investigate,FALSE) = $4))"
    226            // Account is open if we had an AML outcome
    227            "   AND ($5 OR ((lo.outcome_serial_id IS NULL) = $6))"
    228            "   AND ($7 OR ((COALESCE((lo.jproperties ->>'high_risk')::bool,FALSE) = $8)))"
    229            " ORDER BY kt.kyc_target_serial_id DESC"
    230            " LIMIT $2");
    231   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    232                                              stmt,
    233                                              params,
    234                                              &handle_kyc_account_result,
    235                                              &ctx);
    236   if (GNUNET_OK != ctx.status)
    237     return GNUNET_DB_STATUS_HARD_ERROR;
    238   return qs;
    239 }