LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-tips-ID.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 26 48 54.2 %
Date: 2021-08-30 06:54:17 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2017-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 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_get-tips-ID.c
      18             :  * @brief implementation of a GET /tips/ID handler
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <microhttpd.h>
      23             : #include <jansson.h>
      24             : #include <taler/taler_json_lib.h>
      25             : #include <taler/taler_signatures.h>
      26             : #include "taler-merchant-httpd.h"
      27             : #include "taler-merchant-httpd_mhd.h"
      28             : #include "taler-merchant-httpd_exchanges.h"
      29             : 
      30             : 
      31             : MHD_RESULT
      32           2 : TMH_private_get_tips_ID (const struct TMH_RequestHandler *rh,
      33             :                          struct MHD_Connection *connection,
      34             :                          struct TMH_HandlerContext *hc)
      35             : {
      36             :   struct GNUNET_HashCode tip_id;
      37             :   struct TALER_Amount total_authorized;
      38             :   struct TALER_Amount total_picked_up;
      39             :   char *reason;
      40             :   struct GNUNET_TIME_Absolute expiration;
      41             :   struct TALER_ReservePublicKeyP reserve_pub;
      42           2 :   unsigned int pickups_length = 0;
      43           2 :   struct TALER_MERCHANTDB_PickupDetails *pickups = NULL;
      44             :   enum GNUNET_DB_QueryStatus qs;
      45             :   bool fpu;
      46           2 :   json_t *pickups_json = NULL;
      47             : 
      48           2 :   GNUNET_assert (NULL != hc->infix);
      49           2 :   if (GNUNET_OK !=
      50           2 :       GNUNET_CRYPTO_hash_from_string (hc->infix,
      51             :                                       &tip_id))
      52             :   {
      53             :     /* tip_id has wrong encoding */
      54           0 :     GNUNET_break_op (0);
      55           0 :     return TALER_MHD_reply_with_error (connection,
      56             :                                        MHD_HTTP_BAD_REQUEST,
      57             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
      58           0 :                                        hc->infix);
      59             :   }
      60             :   {
      61             :     const char *pstr;
      62             : 
      63           2 :     pstr = MHD_lookup_connection_value (connection,
      64             :                                         MHD_GET_ARGUMENT_KIND,
      65             :                                         "pickups");
      66           2 :     fpu = (NULL != pstr)
      67           1 :           ? 0 == strcasecmp (pstr, "yes")
      68           2 :           : false;
      69             :   }
      70           2 :   TMH_db->preflight (TMH_db->cls);
      71           2 :   qs = TMH_db->lookup_tip_details (TMH_db->cls,
      72           2 :                                    hc->instance->settings.id,
      73             :                                    &tip_id,
      74             :                                    fpu,
      75             :                                    &total_authorized,
      76             :                                    &total_picked_up,
      77             :                                    &reason,
      78             :                                    &expiration,
      79             :                                    &reserve_pub,
      80             :                                    &pickups_length,
      81             :                                    &pickups);
      82           2 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
      83             :   {
      84             :     unsigned int response_code;
      85             :     enum TALER_ErrorCode ec;
      86             : 
      87           0 :     switch (qs)
      88             :     {
      89           0 :     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
      90           0 :       ec = TALER_EC_MERCHANT_GENERIC_TIP_ID_UNKNOWN;
      91           0 :       response_code = MHD_HTTP_NOT_FOUND;
      92           0 :       break;
      93           0 :     case GNUNET_DB_STATUS_SOFT_ERROR:
      94           0 :       ec = TALER_EC_GENERIC_DB_SOFT_FAILURE;
      95           0 :       response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
      96           0 :       break;
      97           0 :     case GNUNET_DB_STATUS_HARD_ERROR:
      98           0 :       ec = TALER_EC_GENERIC_DB_COMMIT_FAILED;
      99           0 :       response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
     100           0 :       break;
     101           0 :     default:
     102           0 :       GNUNET_break (0);
     103           0 :       ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     104           0 :       response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
     105           0 :       break;
     106             :     }
     107           0 :     return TALER_MHD_reply_with_error (connection,
     108             :                                        response_code,
     109             :                                        ec,
     110             :                                        NULL);
     111             :   }
     112           2 :   if (fpu)
     113             :   {
     114           1 :     pickups_json = json_array ();
     115           1 :     GNUNET_assert (NULL != pickups_json);
     116           3 :     for (unsigned int i = 0; i<pickups_length; i++)
     117             :     {
     118           2 :       GNUNET_assert (0 ==
     119             :                      json_array_append_new (
     120             :                        pickups_json,
     121             :                        GNUNET_JSON_PACK (
     122             :                          GNUNET_JSON_pack_data_auto ("pickup_id",
     123             :                                                      &pickups[i].pickup_id),
     124             :                          GNUNET_JSON_pack_uint64 ("num_planchets",
     125             :                                                   pickups[i].num_planchets),
     126             :                          TALER_JSON_pack_amount ("requested_amount",
     127             :                                                  &pickups[i].requested_amount))));
     128             :     }
     129             :   }
     130           2 :   GNUNET_array_grow (pickups,
     131             :                      pickups_length,
     132             :                      0);
     133             :   {
     134           2 :     struct GNUNET_TIME_Absolute expiration_round = expiration;
     135             :     MHD_RESULT ret;
     136             : 
     137           2 :     GNUNET_TIME_round_abs (&expiration_round);
     138             : 
     139           2 :     ret = TALER_MHD_REPLY_JSON_PACK (
     140             :       connection,
     141             :       MHD_HTTP_OK,
     142             :       TALER_JSON_pack_amount ("total_authorized",
     143             :                               &total_authorized),
     144             :       TALER_JSON_pack_amount ("total_picked_up",
     145             :                               &total_picked_up),
     146             :       GNUNET_JSON_pack_string ("reason",
     147             :                                reason),
     148             :       GNUNET_JSON_pack_time_abs ("expiration",
     149             :                                  expiration_round),
     150             :       GNUNET_JSON_pack_data_auto ("reserve_pub",
     151             :                                   &reserve_pub),
     152             :       GNUNET_JSON_pack_allow_null (
     153             :         GNUNET_JSON_pack_array_steal ("pickups",
     154             :                                       pickups_json)));
     155           2 :     GNUNET_free (reason);
     156           2 :     return ret;
     157             :   }
     158             : }

Generated by: LCOV version 1.14