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_historic-denomination-revenue-get.c (4274B)


      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 
     18 #include "taler/platform.h"
     19 #include <gnunet/gnunet_util_lib.h>
     20 #include <gnunet/gnunet_json_lib.h>
     21 #include <jansson.h>
     22 #include <microhttpd.h>
     23 #include <pthread.h>
     24 #include "taler/taler_json_lib.h"
     25 #include "taler/taler_mhd_lib.h"
     26 #include "taler-auditor-httpd.h"
     27 #include "taler-auditor-httpd_historic-denomination-revenue-get.h"
     28 
     29 
     30 /**
     31  * Add historic-denomination-revenue to the list.
     32  *
     33  * @param[in,out] cls a `json_t *` array to extend
     34  * @param serial_id location of the @a dc in the database
     35  * @param denom_pub_hash public key hash of the denomination
     36  * @param revenue_timestamp when was the revenue effective
     37  * @param revenue_balance how much in profit did we make from this denomination
     38  * @param loss_balance how much loss did we make from this denomination
     39  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     40  */
     41 static enum GNUNET_GenericReturnValue
     42 process_historic_denomination_revenue (
     43   void *cls,
     44   uint64_t serial_id,
     45   const struct TALER_DenominationHashP *denom_pub_hash,
     46   struct GNUNET_TIME_Timestamp revenue_timestamp,
     47   const struct TALER_Amount *revenue_balance,
     48   const struct TALER_Amount *loss_balance)
     49 {
     50   json_t *list = cls;
     51   json_t *obj;
     52 
     53   obj = GNUNET_JSON_PACK (
     54     GNUNET_JSON_pack_uint64 ("serial_id",
     55                              serial_id),
     56     GNUNET_JSON_pack_data_auto ("denom_pub_hash",
     57                                 denom_pub_hash),
     58     TALER_JSON_pack_time_abs_human ("revenue_timestamp",
     59                                     revenue_timestamp.abs_time),
     60     TALER_JSON_pack_amount ("revenue_balance",
     61                             revenue_balance),
     62     TALER_JSON_pack_amount ("loss_balance",
     63                             loss_balance)
     64     );
     65   GNUNET_break (0 ==
     66                 json_array_append_new (list,
     67                                        obj));
     68 
     69 
     70   return GNUNET_OK;
     71 }
     72 
     73 
     74 MHD_RESULT
     75 TAH_HISTORIC_DENOMINATION_REVENUE_handler_get (
     76   struct TAH_RequestHandler *rh,
     77   struct MHD_Connection *connection,
     78   void **connection_cls,
     79   const char *upload_data,
     80   size_t *upload_data_size,
     81   const char *const args[])
     82 {
     83   json_t *ja;
     84   int64_t limit = -20;
     85   uint64_t offset;
     86   enum GNUNET_DB_QueryStatus qs;
     87 
     88   (void) rh;
     89   (void) connection_cls;
     90   (void) upload_data;
     91   (void) upload_data_size;
     92   if (GNUNET_SYSERR ==
     93       TAH_plugin->preflight (TAH_plugin->cls))
     94   {
     95     GNUNET_break (0);
     96     return TALER_MHD_reply_with_error (
     97       connection,
     98       MHD_HTTP_INTERNAL_SERVER_ERROR,
     99       TALER_EC_GENERIC_DB_SETUP_FAILED,
    100       NULL);
    101   }
    102   TALER_MHD_parse_request_snumber (connection,
    103                                    "limit",
    104                                    &limit);
    105   if (limit < 0)
    106     offset = INT64_MAX;
    107   else
    108     offset = 0;
    109   TALER_MHD_parse_request_number (connection,
    110                                   "offset",
    111                                   &offset);
    112   ja = json_array ();
    113   GNUNET_break (NULL != ja);
    114   qs = TAH_plugin->select_historic_denom_revenue (
    115     TAH_plugin->cls,
    116     limit,
    117     offset,
    118     &process_historic_denomination_revenue,
    119     ja);
    120   if (0 > qs)
    121   {
    122     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
    123     json_decref (ja);
    124     TALER_LOG_WARNING (
    125       "Failed to handle GET /historic-denomination-revenue");
    126     return TALER_MHD_reply_with_error (
    127       connection,
    128       MHD_HTTP_INTERNAL_SERVER_ERROR,
    129       TALER_EC_GENERIC_DB_FETCH_FAILED,
    130       "select_historic_denom_revenue");
    131   }
    132   return TALER_MHD_REPLY_JSON_PACK (
    133     connection,
    134     MHD_HTTP_OK,
    135     GNUNET_JSON_pack_array_steal (
    136       "historic-denomination-revenue",
    137       ja));
    138 }