exchange

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

taler-auditor-httpd_get-monitoring-reserves.c (4680B)


      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 #include <gnunet/gnunet_util_lib.h>
     17 #include <gnunet/gnunet_json_lib.h>
     18 #include <jansson.h>
     19 #include <microhttpd.h>
     20 #include <pthread.h>
     21 #include "taler/taler_json_lib.h"
     22 #include "taler/taler_mhd_lib.h"
     23 #include "taler-auditor-httpd.h"
     24 #include "taler-auditor-httpd_get-monitoring-reserves.h"
     25 #define TALER_AUDITORDB_RESERVES_RESULT_CLOSURE json_t
     26 #include "auditor-database/iterate_reserves.h"
     27 #include "auditor-database/preflight.h"
     28 
     29 
     30 /**
     31  * Add reserves to the list.
     32  *
     33  * @param[in,out] list a `json_t *` array to extend
     34  * @param serial_id location of the @a dc in the database
     35  * @param dc struct of inconsistencies
     36  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     37  */
     38 static enum GNUNET_GenericReturnValue
     39 process_reserves (
     40   json_t *list,
     41   uint64_t serial_id,
     42   const struct TALER_AUDITORDB_Reserves *dc)
     43 {
     44   json_t *obj;
     45 
     46   obj = GNUNET_JSON_PACK (
     47     GNUNET_JSON_pack_int64 ("auditor_reserves_rowid",
     48                             dc->auditor_reserves_rowid),
     49     GNUNET_JSON_pack_data_auto ("reserve_pub",
     50                                 &dc->reserve_pub),
     51     TALER_JSON_pack_amount ("reserve_balance",
     52                             &dc->reserve_balance),
     53     TALER_JSON_pack_amount ("reserve_loss",
     54                             &dc->reserve_loss),
     55     TALER_JSON_pack_amount ("withdraw_fee_balance",
     56                             &dc->withdraw_fee_balance),
     57     TALER_JSON_pack_amount ("close_fee_balance",
     58                             &dc->close_fee_balance),
     59     TALER_JSON_pack_amount ("purse_fee_balance",
     60                             &dc->purse_fee_balance),
     61     TALER_JSON_pack_amount ("open_fee_balance",
     62                             &dc->open_fee_balance),
     63     TALER_JSON_pack_amount ("history_fee_balance",
     64                             &dc->history_fee_balance),
     65     GNUNET_JSON_pack_timestamp ("expiration_date",
     66                                 GNUNET_TIME_absolute_to_timestamp (
     67                                   dc->expiration_date)),
     68     GNUNET_JSON_pack_allow_null (
     69       TALER_JSON_pack_full_payto ("origin_account",
     70                                   dc->origin_account))
     71     );
     72   GNUNET_break (0 ==
     73                 json_array_append_new (list,
     74                                        obj));
     75   return GNUNET_OK;
     76 }
     77 
     78 
     79 enum MHD_Result
     80 TAH_get_monitoring_reserves (
     81   struct TAH_RequestHandler *rh,
     82   struct MHD_Connection *connection,
     83   void **connection_cls,
     84   const char *upload_data,
     85   size_t *upload_data_size,
     86   const char *const args[])
     87 {
     88   json_t *ja;
     89   enum GNUNET_DB_QueryStatus qs;
     90   int64_t limit = -20;
     91   uint64_t offset;
     92 
     93   (void) rh;
     94   (void) connection_cls;
     95   (void) upload_data;
     96   (void) upload_data_size;
     97   if (GNUNET_SYSERR ==
     98       TALER_AUDITORDB_preflight (TAH_apg))
     99   {
    100     GNUNET_break (0);
    101     return TALER_MHD_reply_with_error (connection,
    102                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    103                                        TALER_EC_GENERIC_DB_SETUP_FAILED,
    104                                        NULL);
    105   }
    106   TAH_PARSE_REQUEST_LIMIT (connection,
    107                            "limit",
    108                            &limit);
    109 
    110   if (limit < 0)
    111     offset = TAH_SAFE_UINT64_MAX;
    112   else
    113     offset = 0;
    114   TAH_PARSE_SAFE_REQUEST_NUMBER (connection,
    115                                  "offset",
    116                                  &offset);
    117   ja = json_array ();
    118   GNUNET_break (NULL != ja);
    119   qs = TALER_AUDITORDB_iterate_reserves (
    120     TAH_apg,
    121     limit,
    122     offset,
    123     &process_reserves,
    124     ja);
    125   if (0 > qs)
    126   {
    127     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    128     json_decref (ja);
    129     TALER_LOG_WARNING (
    130       "Failed to handle GET /reserves");
    131     return TALER_MHD_reply_with_error (connection,
    132                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    133                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
    134                                        "reserves");
    135   }
    136   return TAH_reply_json_records (connection,
    137                                  ja);
    138 }