exchange

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

get_wire_accounts.c (5598B)


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