exchange

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

pg_get_wire_accounts.c (5610B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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_get_wire_accounts.c
     18  * @brief Implementation of the get_wire_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_get_wire_accounts.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #get_wire_accounts_cb().
     31  */
     32 struct GetWireAccountsContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   TALER_EXCHANGEDB_WireAccountCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Flag set to #GNUNET_OK as long as everything is fine.
     46    */
     47   enum GNUNET_GenericReturnValue status;
     48 
     49 };
     50 
     51 
     52 /**
     53  * Invoke the callback for each result.
     54  *
     55  * @param cls a `struct MissingWireContext *`
     56  * @param result SQL result
     57  * @param num_results number of rows in @a result
     58  */
     59 static void
     60 get_wire_accounts_cb (void *cls,
     61                       PGresult *result,
     62                       unsigned int num_results)
     63 {
     64   struct GetWireAccountsContext *ctx = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct TALER_FullPayto payto_uri;
     69     char *conversion_url = NULL;
     70     char *open_banking_gateway = NULL;
     71     char *wire_transfer_gateway = NULL;
     72     json_t *debit_restrictions = NULL;
     73     json_t *credit_restrictions = NULL;
     74     struct TALER_MasterSignatureP master_sig;
     75     char *bank_label = NULL;
     76     int64_t priority;
     77     struct GNUNET_PQ_ResultSpec rs[] = {
     78       GNUNET_PQ_result_spec_string ("payto_uri",
     79                                     &payto_uri.full_payto),
     80       GNUNET_PQ_result_spec_allow_null (
     81         GNUNET_PQ_result_spec_string ("conversion_url",
     82                                       &conversion_url),
     83         NULL),
     84       GNUNET_PQ_result_spec_allow_null (
     85         GNUNET_PQ_result_spec_string ("open_banking_gateway",
     86                                       &open_banking_gateway),
     87         NULL),
     88       GNUNET_PQ_result_spec_allow_null (
     89         GNUNET_PQ_result_spec_string ("wire_transfer_gateway",
     90                                       &wire_transfer_gateway),
     91         NULL),
     92       GNUNET_PQ_result_spec_allow_null (
     93         GNUNET_PQ_result_spec_string ("bank_label",
     94                                       &bank_label),
     95         NULL),
     96       GNUNET_PQ_result_spec_int64 ("priority",
     97                                    &priority),
     98       GNUNET_PQ_result_spec_allow_null (
     99         TALER_PQ_result_spec_json ("debit_restrictions",
    100                                    &debit_restrictions),
    101         NULL),
    102       GNUNET_PQ_result_spec_allow_null (
    103         TALER_PQ_result_spec_json ("credit_restrictions",
    104                                    &credit_restrictions),
    105         NULL),
    106       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
    107                                             &master_sig),
    108       GNUNET_PQ_result_spec_end
    109     };
    110 
    111     if (GNUNET_OK !=
    112         GNUNET_PQ_extract_result (result,
    113                                   rs,
    114                                   i))
    115     {
    116       GNUNET_break (0);
    117       ctx->status = GNUNET_SYSERR;
    118       return;
    119     }
    120     if (NULL == debit_restrictions)
    121     {
    122       debit_restrictions = json_array ();
    123       GNUNET_assert (NULL != debit_restrictions);
    124     }
    125     if (NULL == credit_restrictions)
    126     {
    127       credit_restrictions = json_array ();
    128       GNUNET_assert (NULL != credit_restrictions);
    129     }
    130     ctx->cb (ctx->cb_cls,
    131              payto_uri,
    132              conversion_url,
    133              open_banking_gateway,
    134              wire_transfer_gateway,
    135              debit_restrictions,
    136              credit_restrictions,
    137              &master_sig,
    138              bank_label,
    139              priority);
    140     GNUNET_PQ_cleanup_result (rs);
    141   }
    142 }
    143 
    144 
    145 enum GNUNET_DB_QueryStatus
    146 TEH_PG_get_wire_accounts (void *cls,
    147                           TALER_EXCHANGEDB_WireAccountCallback cb,
    148                           void *cb_cls)
    149 {
    150   struct PostgresClosure *pg = cls;
    151   struct GetWireAccountsContext ctx = {
    152     .cb = cb,
    153     .cb_cls = cb_cls,
    154     .status = GNUNET_OK
    155   };
    156   struct GNUNET_PQ_QueryParam params[] = {
    157     GNUNET_PQ_query_param_end
    158   };
    159   enum GNUNET_DB_QueryStatus qs;
    160 
    161   PREPARE (pg,
    162            "get_wire_accounts",
    163            "SELECT"
    164            " payto_uri"
    165            ",conversion_url"
    166            ",open_banking_gateway"
    167            ",wire_transfer_gateway"
    168            ",debit_restrictions::TEXT"
    169            ",credit_restrictions::TEXT"
    170            ",master_sig"
    171            ",bank_label"
    172            ",priority"
    173            " FROM wire_accounts"
    174            " WHERE is_active");
    175   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    176                                              "get_wire_accounts",
    177                                              params,
    178                                              &get_wire_accounts_cb,
    179                                              &ctx);
    180   if (GNUNET_OK != ctx.status)
    181     return GNUNET_DB_STATUS_HARD_ERROR;
    182   return qs;
    183 }