LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-patch-products-ID.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 52 103 50.5 %
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) 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Affero General Public License as
       7             :   published by the Free Software Foundation; either version 3,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file taler-merchant-httpd_private-patch-products-ID.c
      22             :  * @brief implementing PATCH /products/$ID request handling
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler-merchant-httpd_private-patch-products-ID.h"
      27             : #include "taler-merchant-httpd_helper.h"
      28             : #include <taler/taler_json_lib.h>
      29             : 
      30             : 
      31             : /**
      32             :  * How often do we retry the simple INSERT database transaction?
      33             :  */
      34             : #define MAX_RETRIES 3
      35             : 
      36             : 
      37             : /**
      38             :  * Determine the cause of the PATCH failure in more detail and report.
      39             :  *
      40             :  * @param connection connection to report on
      41             :  * @param instance_id instance we are processing
      42             :  * @param product_id ID of the product to patch
      43             :  * @param pd product details we failed to set
      44             :  */
      45             : static MHD_RESULT
      46           1 : determine_cause (struct MHD_Connection *connection,
      47             :                  const char *instance_id,
      48             :                  const char *product_id,
      49             :                  const struct TALER_MERCHANTDB_ProductDetails *pd)
      50             : {
      51             :   struct TALER_MERCHANTDB_ProductDetails pdx;
      52             :   enum GNUNET_DB_QueryStatus qs;
      53             : 
      54           1 :   qs = TMH_db->lookup_product (TMH_db->cls,
      55             :                                instance_id,
      56             :                                product_id,
      57             :                                &pdx);
      58           1 :   switch (qs)
      59             :   {
      60           0 :   case GNUNET_DB_STATUS_HARD_ERROR:
      61           0 :     GNUNET_break (0);
      62           0 :     return TALER_MHD_reply_with_error (connection,
      63             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      64             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      65             :                                        NULL);
      66           0 :   case GNUNET_DB_STATUS_SOFT_ERROR:
      67           0 :     GNUNET_break (0);
      68           0 :     return TALER_MHD_reply_with_error (connection,
      69             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      70             :                                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
      71             :                                        "unexpected serialization problem");
      72           1 :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
      73           1 :     return TALER_MHD_reply_with_error (connection,
      74             :                                        MHD_HTTP_NOT_FOUND,
      75             :                                        TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
      76             :                                        product_id);
      77           0 :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
      78           0 :     break; /* do below */
      79             :   }
      80             : 
      81           0 :   {
      82             :     enum TALER_ErrorCode ec;
      83             : 
      84           0 :     ec = TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
      85           0 :     if (pdx.total_lost > pd->total_lost)
      86           0 :       ec = TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_REDUCED;
      87           0 :     if (pdx.total_sold > pd->total_sold)
      88           0 :       ec = TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_SOLD_REDUCED;
      89           0 :     if (pdx.total_stock > pd->total_stock)
      90           0 :       ec = TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_STOCKED_REDUCED;
      91           0 :     TALER_MERCHANTDB_product_details_free (&pdx);
      92           0 :     GNUNET_break (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE != ec);
      93           0 :     return TALER_MHD_reply_with_error (connection,
      94             :                                        MHD_HTTP_CONFLICT,
      95             :                                        ec,
      96             :                                        NULL);
      97             :   }
      98             : }
      99             : 
     100             : 
     101             : /**
     102             :  * PATCH configuration of an existing instance, given its configuration.
     103             :  *
     104             :  * @param rh context of the handler
     105             :  * @param connection the MHD connection to handle
     106             :  * @param[in,out] hc context with further information about the request
     107             :  * @return MHD result code
     108             :  */
     109             : MHD_RESULT
     110           3 : TMH_private_patch_products_ID (const struct TMH_RequestHandler *rh,
     111             :                                struct MHD_Connection *connection,
     112             :                                struct TMH_HandlerContext *hc)
     113             : {
     114           3 :   struct TMH_MerchantInstance *mi = hc->instance;
     115           3 :   const char *product_id = hc->infix;
     116           3 :   struct TALER_MERCHANTDB_ProductDetails pd = {0};
     117             :   int64_t total_stock;
     118             :   enum GNUNET_DB_QueryStatus qs;
     119             :   struct GNUNET_JSON_Specification spec[] = {
     120           3 :     GNUNET_JSON_spec_string ("description",
     121             :                              (const char **) &pd.description),
     122           3 :     GNUNET_JSON_spec_mark_optional (
     123             :       GNUNET_JSON_spec_json ("description_i18n",
     124             :                              &pd.description_i18n)),
     125           3 :     GNUNET_JSON_spec_string ("unit",
     126             :                              (const char **) &pd.unit),
     127           3 :     TALER_JSON_spec_amount ("price",
     128             :                             TMH_currency,
     129             :                             &pd.price),
     130           3 :     GNUNET_JSON_spec_mark_optional (
     131             :       GNUNET_JSON_spec_string ("image",
     132             :                                (const char **) &pd.image)),
     133           3 :     GNUNET_JSON_spec_mark_optional (
     134             :       GNUNET_JSON_spec_json ("taxes",
     135             :                              &pd.taxes)),
     136           3 :     GNUNET_JSON_spec_int64 ("total_stock",
     137             :                             &total_stock),
     138           3 :     GNUNET_JSON_spec_mark_optional (
     139             :       GNUNET_JSON_spec_uint64 ("total_lost",
     140             :                                &pd.total_lost)),
     141           3 :     GNUNET_JSON_spec_mark_optional (
     142             :       GNUNET_JSON_spec_json ("address",
     143             :                              &pd.address)),
     144           3 :     GNUNET_JSON_spec_mark_optional (
     145             :       TALER_JSON_spec_absolute_time ("next_restock",
     146             :                                      &pd.next_restock)),
     147           3 :     GNUNET_JSON_spec_end ()
     148             :   };
     149             : 
     150           3 :   pd.total_sold = 0; /* will be ignored anyway */
     151           3 :   GNUNET_assert (NULL != mi);
     152           3 :   GNUNET_assert (NULL != product_id);
     153             :   {
     154             :     enum GNUNET_GenericReturnValue res;
     155             : 
     156           3 :     res = TALER_MHD_parse_json_data (connection,
     157           3 :                                      hc->request_body,
     158             :                                      spec);
     159           3 :     if (GNUNET_OK != res)
     160             :       return (GNUNET_NO == res)
     161             :              ? MHD_YES
     162           0 :              : MHD_NO;
     163             :   }
     164           3 :   if (total_stock < -1)
     165             :   {
     166           0 :     GNUNET_break_op (0);
     167           0 :     GNUNET_JSON_parse_free (spec);
     168           0 :     return TALER_MHD_reply_with_error (connection,
     169             :                                        MHD_HTTP_BAD_REQUEST,
     170             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     171             :                                        "total_stock");
     172             :   }
     173           3 :   if (-1 == total_stock)
     174           0 :     pd.total_stock = INT64_MAX;
     175             :   else
     176           3 :     pd.total_stock = (uint64_t) total_stock;
     177           3 :   if (NULL == pd.address)
     178           0 :     pd.address = json_object ();
     179             : 
     180           3 :   if (! TMH_location_object_valid (pd.address))
     181             :   {
     182           0 :     GNUNET_break_op (0);
     183           0 :     GNUNET_JSON_parse_free (spec);
     184           0 :     return TALER_MHD_reply_with_error (connection,
     185             :                                        MHD_HTTP_BAD_REQUEST,
     186             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     187             :                                        "address");
     188             :   }
     189           3 :   if (NULL == pd.description_i18n)
     190           0 :     pd.description_i18n = json_object ();
     191             : 
     192           3 :   if (! TALER_JSON_check_i18n (pd.description_i18n))
     193             :   {
     194           0 :     GNUNET_break_op (0);
     195           0 :     GNUNET_JSON_parse_free (spec);
     196           0 :     return TALER_MHD_reply_with_error (connection,
     197             :                                        MHD_HTTP_BAD_REQUEST,
     198             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     199             :                                        "description_i18n");
     200             :   }
     201             : 
     202           3 :   if (NULL == pd.taxes)
     203           0 :     pd.taxes = json_array ();
     204             :   /* check taxes is well-formed */
     205           3 :   if (! TMH_taxes_array_valid (pd.taxes))
     206             :   {
     207           0 :     GNUNET_break_op (0);
     208           0 :     GNUNET_JSON_parse_free (spec);
     209           0 :     return TALER_MHD_reply_with_error (connection,
     210             :                                        MHD_HTTP_BAD_REQUEST,
     211             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     212             :                                        "taxes");
     213             :   }
     214           3 :   if (NULL == pd.image)
     215           0 :     pd.image = "";
     216           3 :   if (! TMH_image_data_url_valid (pd.image))
     217             :   {
     218           0 :     GNUNET_break_op (0);
     219           0 :     GNUNET_JSON_parse_free (spec);
     220           0 :     return TALER_MHD_reply_with_error (connection,
     221             :                                        MHD_HTTP_BAD_REQUEST,
     222             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     223             :                                        "image");
     224             :   }
     225           3 :   if ( (pd.total_stock < pd.total_sold + pd.total_lost) ||
     226           3 :        (pd.total_sold + pd.total_lost < pd.total_sold) /* integer overflow */)
     227             :   {
     228           0 :     GNUNET_break_op (0);
     229           0 :     GNUNET_JSON_parse_free (spec);
     230           0 :     return TALER_MHD_reply_with_error (
     231             :       connection,
     232             :       MHD_HTTP_BAD_REQUEST,
     233             :       TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_EXCEEDS_STOCKS,
     234             :       NULL);
     235             :   }
     236           3 :   qs = TMH_db->update_product (TMH_db->cls,
     237           3 :                                mi->settings.id,
     238             :                                product_id,
     239             :                                &pd);
     240             :   {
     241           3 :     MHD_RESULT ret = MHD_NO;
     242             : 
     243           3 :     switch (qs)
     244             :     {
     245           0 :     case GNUNET_DB_STATUS_HARD_ERROR:
     246           0 :       GNUNET_break (0);
     247           0 :       ret = TALER_MHD_reply_with_error (connection,
     248             :                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     249             :                                         TALER_EC_GENERIC_DB_STORE_FAILED,
     250             :                                         NULL);
     251           0 :       break;
     252           0 :     case GNUNET_DB_STATUS_SOFT_ERROR:
     253           0 :       GNUNET_break (0);
     254           0 :       ret = TALER_MHD_reply_with_error (connection,
     255             :                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     256             :                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     257             :                                         "unexpected serialization problem");
     258           0 :       break;
     259           1 :     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     260           1 :       ret = determine_cause (connection,
     261           1 :                              mi->settings.id,
     262             :                              product_id,
     263             :                              &pd);
     264           1 :       break;
     265           2 :     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     266           2 :       ret = TALER_MHD_reply_static (connection,
     267             :                                     MHD_HTTP_NO_CONTENT,
     268             :                                     NULL,
     269             :                                     NULL,
     270             :                                     0);
     271           2 :       break;
     272             :     }
     273           3 :     GNUNET_JSON_parse_free (spec);
     274           3 :     return ret;
     275             :   }
     276             : }
     277             : 
     278             : 
     279             : /* end of taler-merchant-httpd_private-patch-products-ID.c */

Generated by: LCOV version 1.14