LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-transfers.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 31 45 68.9 %
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) 2014-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-transfers.c
      18             :  * @brief implement API for obtaining a list of wire transfers
      19             :  * @author Marcello Stanisci
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <jansson.h>
      24             : #include <taler/taler_json_lib.h>
      25             : #include "taler-merchant-httpd_private-get-transfers.h"
      26             : 
      27             : 
      28             : /**
      29             :  * Function called with information about a wire transfer.
      30             :  * Generate a response (array entry) based on the given arguments.
      31             :  *
      32             :  * @param cls closure with a `json_t *` array to build up the response
      33             :  * @param credit_amount how much was wired to the merchant (minus fees)
      34             :  * @param wtid wire transfer identifier
      35             :  * @param payto_uri target account that received the wire transfer
      36             :  * @param exchange_url base URL of the exchange that made the wire transfer
      37             :  * @param transfer_serial_id serial number identifying the transfer in the backend
      38             :  * @param execution_time when did the exchange make the transfer, #GNUNET_TIME_UNIT_FOREVER_ABS
      39             :  *           if it did not yet happen
      40             :  * @param verified YES if we checked the exchange's answer and liked it,
      41             :  *                 NO if we checked the exchange's answer and it is problematic,
      42             :  *                 ALL if we did not yet check
      43             :  * @param confirmed true if the merchant acknowledged the wire transfer reception
      44             :  */
      45             : static void
      46           2 : transfer_cb (void *cls,
      47             :              const struct TALER_Amount *credit_amount,
      48             :              const struct TALER_WireTransferIdentifierRawP *wtid,
      49             :              const char *payto_uri,
      50             :              const char *exchange_url,
      51             :              uint64_t transfer_serial_id,
      52             :              struct GNUNET_TIME_Absolute execution_time,
      53             :              bool verified,
      54             :              bool confirmed)
      55             : {
      56           2 :   json_t *ja = cls;
      57             :   json_t *r;
      58             : 
      59           2 :   r = GNUNET_JSON_PACK (
      60             :     TALER_JSON_pack_amount ("credit_amount",
      61             :                             credit_amount),
      62             :     GNUNET_JSON_pack_data_auto ("wtid",
      63             :                                 wtid),
      64             :     GNUNET_JSON_pack_string ("payto_uri",
      65             :                              payto_uri),
      66             :     GNUNET_JSON_pack_string ("exchange_url",
      67             :                              exchange_url),
      68             :     GNUNET_JSON_pack_uint64 ("transfer_serial_id",
      69             :                              transfer_serial_id),
      70             :     GNUNET_JSON_pack_bool ("verified",
      71             :                            verified),
      72             :     GNUNET_JSON_pack_bool ("confirmed",
      73             :                            confirmed),
      74             :     GNUNET_JSON_pack_allow_null (
      75             :       GNUNET_JSON_pack_time_abs (
      76             :         "execution_time",
      77             :         GNUNET_TIME_absolute_is_never (execution_time)
      78             :         ? GNUNET_TIME_UNIT_ZERO_ABS /* => field omitted */
      79             :         : execution_time) ));
      80           2 :   GNUNET_assert (0 ==
      81             :                  json_array_append_new (ja,
      82             :                                         r));
      83           2 : }
      84             : 
      85             : 
      86             : /**
      87             :  * Manages a GET /private/transfers call.
      88             :  *
      89             :  * @param rh context of the handler
      90             :  * @param connection the MHD connection to handle
      91             :  * @param[in,out] hc context with further information about the request
      92             :  * @return MHD result code
      93             :  */
      94             : MHD_RESULT
      95           1 : TMH_private_get_transfers (const struct TMH_RequestHandler *rh,
      96             :                            struct MHD_Connection *connection,
      97             :                            struct TMH_HandlerContext *hc)
      98             : {
      99             :   const char *payto_uri;
     100           1 :   struct GNUNET_TIME_Absolute before = GNUNET_TIME_UNIT_FOREVER_ABS;
     101           1 :   struct GNUNET_TIME_Absolute after = GNUNET_TIME_UNIT_ZERO_ABS;
     102           1 :   int64_t limit = -20;
     103             :   uint64_t offset;
     104             :   enum TALER_EXCHANGE_YesNoAll verified;
     105             : 
     106           1 :   payto_uri = MHD_lookup_connection_value (connection,
     107             :                                            MHD_GET_ARGUMENT_KIND,
     108             :                                            "payto_uri");
     109             :   {
     110             :     const char *before_s;
     111             : 
     112           1 :     before_s = MHD_lookup_connection_value (connection,
     113             :                                             MHD_GET_ARGUMENT_KIND,
     114             :                                             "before");
     115           1 :     if ( (NULL != before_s) &&
     116             :          (GNUNET_OK !=
     117           0 :           GNUNET_STRINGS_fancy_time_to_absolute (before_s,
     118             :                                                  &before)) )
     119           0 :       return TALER_MHD_reply_with_error (connection,
     120             :                                          MHD_HTTP_BAD_REQUEST,
     121             :                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
     122             :                                          "before");
     123             :   }
     124             :   {
     125             :     const char *after_s;
     126             : 
     127           1 :     after_s = MHD_lookup_connection_value (connection,
     128             :                                            MHD_GET_ARGUMENT_KIND,
     129             :                                            "after");
     130           1 :     if ( (NULL != after_s) &&
     131             :          (GNUNET_OK !=
     132           0 :           GNUNET_STRINGS_fancy_time_to_absolute (after_s,
     133             :                                                  &after)) )
     134           0 :       return TALER_MHD_reply_with_error (connection,
     135             :                                          MHD_HTTP_BAD_REQUEST,
     136             :                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
     137             :                                          "after");
     138             :   }
     139             :   {
     140             :     const char *limit_s;
     141             : 
     142           1 :     limit_s = MHD_lookup_connection_value (connection,
     143             :                                            MHD_GET_ARGUMENT_KIND,
     144             :                                            "limit");
     145           1 :     if (NULL != limit_s)
     146             :     {
     147             :       char dummy[2];
     148             :       long long l;
     149             : 
     150           1 :       if (1 !=
     151           1 :           sscanf (limit_s,
     152             :                   "%lld%1s",
     153             :                   &l,
     154             :                   dummy))
     155           0 :         return TALER_MHD_reply_with_error (connection,
     156             :                                            MHD_HTTP_BAD_REQUEST,
     157             :                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
     158             :                                            "limit");
     159           1 :       limit = (int64_t) l;
     160             :     }
     161             :   }
     162             :   {
     163             :     const char *offset_s;
     164             : 
     165           1 :     offset_s = MHD_lookup_connection_value (connection,
     166             :                                             MHD_GET_ARGUMENT_KIND,
     167             :                                             "offset");
     168           1 :     if (NULL != offset_s)
     169             :     {
     170             :       char dummy[2];
     171             :       unsigned long long o;
     172             : 
     173           0 :       if (1 !=
     174           0 :           sscanf (offset_s,
     175             :                   "%llu%1s",
     176             :                   &o,
     177             :                   dummy))
     178           0 :         return TALER_MHD_reply_with_error (connection,
     179             :                                            MHD_HTTP_BAD_REQUEST,
     180             :                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
     181             :                                            "offset");
     182           0 :       offset = (uint64_t) o;
     183             :     }
     184             :     else
     185             :     {
     186           1 :       if (limit < 0)
     187           0 :         offset = INT64_MAX;
     188             :       else
     189           1 :         offset = 0;
     190             :     }
     191             :   }
     192           1 :   if (! (TALER_arg_to_yna (connection,
     193             :                            "verified",
     194             :                            TALER_EXCHANGE_YNA_ALL,
     195             :                            &verified)) )
     196           0 :     return TALER_MHD_reply_with_error (connection,
     197             :                                        MHD_HTTP_BAD_REQUEST,
     198             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     199             :                                        "verified");
     200             : 
     201           1 :   TMH_db->preflight (TMH_db->cls);
     202             :   {
     203             :     json_t *ja;
     204             :     enum GNUNET_DB_QueryStatus qs;
     205             : 
     206           1 :     ja = json_array ();
     207           1 :     GNUNET_assert (NULL != ja);
     208           1 :     qs = TMH_db->lookup_transfers (TMH_db->cls,
     209           1 :                                    hc->instance->settings.id,
     210             :                                    payto_uri,
     211             :                                    before,
     212             :                                    after,
     213             :                                    limit,
     214             :                                    offset,
     215             :                                    verified,
     216             :                                    &transfer_cb,
     217             :                                    ja);
     218           1 :     if (0 > qs)
     219             :     {
     220             :       /* Simple select queries should not cause serialization issues */
     221           0 :       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
     222             :       /* Always report on hard error as well to enable diagnostics */
     223           0 :       GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
     224           0 :       return TALER_MHD_reply_with_error (connection,
     225             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     226             :                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
     227             :                                          "transfers");
     228             :     }
     229           1 :     return TALER_MHD_REPLY_JSON_PACK (
     230             :       connection,
     231             :       MHD_HTTP_OK,
     232             :       GNUNET_JSON_pack_array_steal ("transfers",
     233             :                                     ja));
     234             :   }
     235             : }
     236             : 
     237             : 
     238             : /* end of taler-merchant-httpd_track-transfer.c */

Generated by: LCOV version 1.14