LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-reserves.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 18 23 78.3 %
Date: 2021-08-30 06:54:17 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2019-2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero 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 taler-merchant-httpd_private-get-reserves.c
      18             :  * @brief implement GET /reserves
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <taler/taler_json_lib.h>
      23             : #include "taler-merchant-httpd_private-get-reserves.h"
      24             : 
      25             : 
      26             : /**
      27             :  * Add reserve details to our JSON array.
      28             :  *
      29             :  * @param cls a `json_t *` JSON array to build
      30             :  * @param reserve_pub public key of the reserve
      31             :  * @param creation_time time when the reserve was setup
      32             :  * @param expiration_time time when the reserve will be closed by the exchange
      33             :  * @param merchant_initial_amount initial amount that the merchant claims to have filled the
      34             :  *           reserve with
      35             :  * @param exchange_initial_amount initial amount that the exchange claims to have received
      36             :  * @param pickup_amount total of tips that were picked up from this reserve
      37             :  * @param committed_amount total of tips that the merchant committed to, but that were not
      38             :  *           picked up yet
      39             :  * @param active  true if the reserve is still active (we have the private key)
      40             :  */
      41             : static void
      42           3 : add_reserve (void *cls,
      43             :              const struct TALER_ReservePublicKeyP *reserve_pub,
      44             :              struct GNUNET_TIME_Absolute creation_time,
      45             :              struct GNUNET_TIME_Absolute expiration_time,
      46             :              const struct TALER_Amount *merchant_initial_amount,
      47             :              const struct TALER_Amount *exchange_initial_amount,
      48             :              const struct TALER_Amount *pickup_amount,
      49             :              const struct TALER_Amount *committed_amount,
      50             :              bool active)
      51             : {
      52           3 :   json_t *pa = cls;
      53           3 :   struct GNUNET_TIME_Absolute creation_time_round = creation_time;
      54           3 :   struct GNUNET_TIME_Absolute expiration_time_round = expiration_time;
      55             : 
      56           3 :   GNUNET_TIME_round_abs (&creation_time_round);
      57           3 :   GNUNET_TIME_round_abs (&expiration_time_round);
      58           3 :   GNUNET_assert (0 ==
      59             :                  json_array_append_new (
      60             :                    pa,
      61             :                    GNUNET_JSON_PACK (
      62             :                      GNUNET_JSON_pack_data_auto ("reserve_pub",
      63             :                                                  reserve_pub),
      64             :                      GNUNET_JSON_pack_time_abs ("creation_time",
      65             :                                                 creation_time_round),
      66             :                      GNUNET_JSON_pack_time_abs ("expiration_time",
      67             :                                                 expiration_time_round),
      68             :                      TALER_JSON_pack_amount ("merchant_initial_amount",
      69             :                                              merchant_initial_amount),
      70             :                      TALER_JSON_pack_amount ("exchange_initial_amount",
      71             :                                              exchange_initial_amount),
      72             :                      TALER_JSON_pack_amount ("pickup_amount",
      73             :                                              pickup_amount),
      74             :                      TALER_JSON_pack_amount ("committed_amount",
      75             :                                              committed_amount),
      76             :                      GNUNET_JSON_pack_bool ("active",
      77             :                                             active))));
      78           3 : }
      79             : 
      80             : 
      81             : /**
      82             :  * Handle a GET "/reserves" request.
      83             :  *
      84             :  * @param rh context of the handler
      85             :  * @param connection the MHD connection to handle
      86             :  * @param[in,out] hc context with further information about the request
      87             :  * @return MHD result code
      88             :  */
      89             : MHD_RESULT
      90           2 : TMH_private_get_reserves (const struct TMH_RequestHandler *rh,
      91             :                           struct MHD_Connection *connection,
      92             :                           struct TMH_HandlerContext *hc)
      93             : {
      94             :   json_t *ra;
      95             :   enum GNUNET_DB_QueryStatus qs;
      96           2 :   struct GNUNET_TIME_Absolute created_after = { 0 };
      97             :   enum TALER_EXCHANGE_YesNoAll active;
      98             :   enum TALER_EXCHANGE_YesNoAll failures;
      99             : 
     100           2 :   if (! (TALER_arg_to_yna (connection,
     101             :                            "active",
     102             :                            TALER_EXCHANGE_YNA_ALL,
     103             :                            &active)) )
     104             :   {
     105           0 :     return TALER_MHD_reply_with_error (connection,
     106             :                                        MHD_HTTP_BAD_REQUEST,
     107             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     108             :                                        "active");
     109             :   }
     110             : 
     111           2 :   if (! (TALER_arg_to_yna (connection,
     112             :                            "failures",
     113             :                            TALER_EXCHANGE_YNA_ALL,
     114             :                            &failures)) )
     115             :   {
     116           0 :     return TALER_MHD_reply_with_error (connection,
     117             :                                        MHD_HTTP_BAD_REQUEST,
     118             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     119             :                                        "failures");
     120             :   }
     121             : 
     122           2 :   ra = json_array ();
     123           2 :   GNUNET_assert (NULL != ra);
     124           2 :   qs = TMH_db->lookup_reserves (TMH_db->cls,
     125           2 :                                 hc->instance->settings.id,
     126             :                                 created_after,
     127             :                                 active,
     128             :                                 failures,
     129             :                                 &add_reserve,
     130             :                                 ra);
     131           2 :   if (0 > qs)
     132             :   {
     133           0 :     GNUNET_break (0);
     134           0 :     json_decref (ra);
     135           0 :     return TALER_MHD_reply_with_error (connection,
     136             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     137             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     138             :                                        "reserves");
     139             :   }
     140           2 :   return TALER_MHD_REPLY_JSON_PACK (
     141             :     connection,
     142             :     MHD_HTTP_OK,
     143             :     GNUNET_JSON_pack_array_steal ("reserves",
     144             :                                   ra));
     145             : }
     146             : 
     147             : 
     148             : /* end of taler-merchant-httpd_private-get-reserves.c */

Generated by: LCOV version 1.14