exchange

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

pg_lookup_kyc_history.c (5762B)


      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_lookup_kyc_history.c
     18  * @brief Implementation of the lookup_kyc_history 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_lookup_kyc_history.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for callbacks called from #TEH_PG_lookup_kyc_history()
     30  */
     31 struct KycHistoryContext
     32 {
     33 
     34   /**
     35    * Function to call on each result.
     36    */
     37   TALER_EXCHANGEDB_KycHistoryCallback 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 'true' if the transaction failed.
     51    */
     52   bool failed;
     53 
     54 };
     55 
     56 
     57 /**
     58  * Function to be called with the results of a SELECT statement
     59  * that has returned @a num_results results.
     60  *
     61  * @param cls closure of type `struct KycHistoryContext`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 handle_kyc_entry (void *cls,
     67                   PGresult *result,
     68                   unsigned int num_results)
     69 {
     70   struct KycHistoryContext *khc = cls;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     char *provider_name = NULL;
     75     bool finished;
     76     uint32_t error_code;
     77     char *error_message = NULL;
     78     char *provider_user_id = NULL;
     79     char *provider_legitimization_id = NULL;
     80     struct GNUNET_TIME_Timestamp collection_time;
     81     struct GNUNET_TIME_Absolute expiration_time
     82       = GNUNET_TIME_UNIT_ZERO_ABS;
     83     void *encrypted_attributes;
     84     size_t encrypted_attributes_len;
     85 
     86     struct GNUNET_PQ_ResultSpec rs[] = {
     87       GNUNET_PQ_result_spec_allow_null (
     88         GNUNET_PQ_result_spec_string ("provider_name",
     89                                       &provider_name),
     90         NULL),
     91       GNUNET_PQ_result_spec_bool ("finished",
     92                                   &finished),
     93       GNUNET_PQ_result_spec_uint32 ("error_code",
     94                                     &error_code),
     95       GNUNET_PQ_result_spec_allow_null (
     96         GNUNET_PQ_result_spec_string ("error_message",
     97                                       &error_message),
     98         NULL),
     99       GNUNET_PQ_result_spec_allow_null (
    100         GNUNET_PQ_result_spec_string ("provider_user_id",
    101                                       &provider_user_id),
    102         NULL),
    103       GNUNET_PQ_result_spec_allow_null (
    104         GNUNET_PQ_result_spec_string ("provider_legitimization_id",
    105                                       &provider_legitimization_id),
    106         NULL),
    107       GNUNET_PQ_result_spec_timestamp ("collection_time",
    108                                        &collection_time),
    109       GNUNET_PQ_result_spec_allow_null (
    110         GNUNET_PQ_result_spec_absolute_time ("expiration_time",
    111                                              &expiration_time),
    112         NULL),
    113       GNUNET_PQ_result_spec_variable_size ("encrypted_attributes",
    114                                            &encrypted_attributes,
    115                                            &encrypted_attributes_len),
    116       GNUNET_PQ_result_spec_end
    117     };
    118 
    119     if (GNUNET_OK !=
    120         GNUNET_PQ_extract_result (result,
    121                                   rs,
    122                                   i))
    123     {
    124       GNUNET_break (0);
    125       khc->failed = true;
    126       return;
    127     }
    128     khc->cb (khc->cb_cls,
    129              provider_name,
    130              finished,
    131              (enum TALER_ErrorCode) error_code,
    132              error_message,
    133              provider_user_id,
    134              provider_legitimization_id,
    135              collection_time,
    136              expiration_time,
    137              encrypted_attributes_len,
    138              encrypted_attributes);
    139     GNUNET_PQ_cleanup_result (rs);
    140   }
    141 }
    142 
    143 
    144 enum GNUNET_DB_QueryStatus
    145 TEH_PG_lookup_kyc_history (
    146   void *cls,
    147   const struct TALER_NormalizedPaytoHashP *h_payto,
    148   TALER_EXCHANGEDB_KycHistoryCallback cb,
    149   void *cb_cls)
    150 {
    151   struct PostgresClosure *pg = cls;
    152   struct KycHistoryContext khc = {
    153     .pg = pg,
    154     .cb = cb,
    155     .cb_cls = cb_cls
    156   };
    157   struct GNUNET_PQ_QueryParam params[] = {
    158     GNUNET_PQ_query_param_auto_from_type (h_payto),
    159     GNUNET_PQ_query_param_end
    160   };
    161   enum GNUNET_DB_QueryStatus qs;
    162 
    163   PREPARE (pg,
    164            "lookup_kyc_history",
    165            "SELECT"
    166            " lp.provider_name"
    167            ",lp.finished"
    168            ",lp.error_code"
    169            ",lp.error_message"
    170            ",lp.provider_user_id"
    171            ",lp.provider_legitimization_id"
    172            ",ka.collection_time"
    173            ",ka.expiration_time"
    174            ",ka.encrypted_attributes"
    175            " FROM kyc_attributes ka"
    176            "    JOIN legitimization_processes lp"
    177            "      ON (ka.legitimization_serial = lp.legitimization_process_serial_id)"
    178            " WHERE ka.h_payto=$1"
    179            " ORDER BY ka.collection_time DESC;");
    180 
    181   qs = GNUNET_PQ_eval_prepared_multi_select (
    182     pg->conn,
    183     "lookup_kyc_history",
    184     params,
    185     &handle_kyc_entry,
    186     &khc);
    187   if (qs <= 0)
    188     return qs;
    189   if (khc.failed)
    190   {
    191     GNUNET_break (0);
    192     return GNUNET_DB_STATUS_HARD_ERROR;
    193   }
    194   return qs;
    195 }