exchange

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

pg_select_all_kyc_attributes.c (5085B)


      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_all_kyc_attributes.c
     18  * @brief Implementation of the select_all_kyc_attributes function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/select_all_kyc_attributes.h"
     23 #include "helper.h"
     24 
     25 /**
     26  * Closure for #get_all_attributes_cb().
     27  */
     28 struct GetAttributesContext
     29 {
     30   /**
     31    * Function to call per result.
     32    */
     33   TALER_EXCHANGEDB_AllAttributesCallback cb;
     34 
     35   /**
     36    * Closure for @e cb.
     37    */
     38   void *cb_cls;
     39 
     40   /**
     41    * Plugin context.
     42    */
     43   struct TALER_EXCHANGEDB_PostgresContext *pg;
     44 
     45   /**
     46    * Flag set to #GNUNET_OK as long as everything is fine.
     47    */
     48   enum GNUNET_GenericReturnValue status;
     49 
     50 };
     51 
     52 /**
     53  * Invoke the callback for each result.
     54  *
     55  * @param cls a `struct GetAttributesContext *`
     56  * @param result SQL result
     57  * @param num_results number of rows in @a result
     58  */
     59 static void
     60 get_attributes_cb (void *cls,
     61                    PGresult *result,
     62                    unsigned int num_results)
     63 {
     64   struct GetAttributesContext *ctx = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     uint64_t rowid;
     69     struct GNUNET_TIME_Timestamp collection_time;
     70     struct GNUNET_TIME_Timestamp expiration_time;
     71     struct TALER_NormalizedPaytoHashP h_payto;
     72     json_t *properties = NULL;
     73     size_t enc_attributes_size;
     74     void *enc_attributes;
     75     char *provider;
     76     struct GNUNET_PQ_ResultSpec rs[] = {
     77       GNUNET_PQ_result_spec_string ("provider_name",
     78                                     &provider),
     79       GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id",
     80                                     &rowid),
     81       GNUNET_PQ_result_spec_allow_null (
     82         TALER_PQ_result_spec_json ("jproperties",
     83                                    &properties),
     84         NULL),
     85       GNUNET_PQ_result_spec_timestamp ("collection_time",
     86                                        &collection_time),
     87       GNUNET_PQ_result_spec_timestamp ("expiration_time",
     88                                        &expiration_time),
     89       GNUNET_PQ_result_spec_auto_from_type ("h_payto",
     90                                             &h_payto),
     91       GNUNET_PQ_result_spec_variable_size ("encrypted_attributes",
     92                                            &enc_attributes,
     93                                            &enc_attributes_size),
     94       GNUNET_PQ_result_spec_end
     95     };
     96 
     97     if (GNUNET_OK !=
     98         GNUNET_PQ_extract_result (result,
     99                                   rs,
    100                                   i))
    101     {
    102       GNUNET_break (0);
    103       ctx->status = GNUNET_SYSERR;
    104       return;
    105     }
    106     ctx->cb (ctx->cb_cls,
    107              rowid,
    108              &h_payto,
    109              provider,
    110              collection_time,
    111              expiration_time,
    112              properties,
    113              enc_attributes_size,
    114              enc_attributes);
    115     GNUNET_PQ_cleanup_result (rs);
    116   }
    117 }
    118 
    119 
    120 enum GNUNET_DB_QueryStatus
    121 TALER_EXCHANGEDB_select_all_kyc_attributes (
    122   struct TALER_EXCHANGEDB_PostgresContext *pg,
    123   uint64_t min_row_id,
    124   TALER_EXCHANGEDB_AllAttributesCallback cb,
    125   void *cb_cls)
    126 {
    127   struct GNUNET_PQ_QueryParam params[] = {
    128     GNUNET_PQ_query_param_uint64 (&min_row_id),
    129     GNUNET_PQ_query_param_end
    130   };
    131   struct GetAttributesContext ctx = {
    132     .cb = cb,
    133     .cb_cls = cb_cls,
    134     .pg = pg,
    135     .status = GNUNET_OK
    136   };
    137   enum GNUNET_DB_QueryStatus qs;
    138 
    139   PREPARE (pg,
    140            "select_all_kyc_attributes",
    141            "SELECT "
    142            " lp.provider_name"
    143            ",ka.h_payto"
    144            ",ka.kyc_attributes_serial_id"
    145            ",lo.jproperties::TEXT"
    146            ",ka.collection_time"
    147            ",ka.expiration_time"
    148            ",ka.encrypted_attributes"
    149            " FROM kyc_attributes ka"
    150            " JOIN legitimization_processes lp"
    151            "   ON (ka.legitimization_serial = lp.legitimization_process_serial_id)"
    152            " LEFT JOIN legitimization_outcomes lo"
    153            "   ON (ka.h_payto = lo.h_payto)"
    154            /* **IF** we joined with 'lo', the lo must be active */
    155            " WHERE COALESCE(lo.is_active,TRUE)"
    156            "   AND kyc_attributes_serial_id > $1"
    157            " ORDER BY kyc_attributes_serial_id ASC"
    158            );
    159   qs = GNUNET_PQ_eval_prepared_multi_select (
    160     pg->conn,
    161     "select_all_kyc_attributes",
    162     params,
    163     &get_attributes_cb,
    164     &ctx);
    165   if (GNUNET_OK != ctx.status)
    166     return GNUNET_DB_STATUS_HARD_ERROR;
    167   return qs;
    168 }