LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-post-products.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 78 115 67.8 %
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-post-products.c
      22             :  * @brief implementing POST /products request handling
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler-merchant-httpd_private-post-products.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             :  * Check if the two products are identical.
      39             :  *
      40             :  * @param p1 product to compare
      41             :  * @param p2 other product to compare
      42             :  * @return true if they are 'equal', false if not or of payto_uris is not an array
      43             :  */
      44             : static bool
      45           2 : products_equal (const struct TALER_MERCHANTDB_ProductDetails *p1,
      46             :                 const struct TALER_MERCHANTDB_ProductDetails *p2)
      47             : {
      48           2 :   return ( (0 == strcmp (p1->description,
      49           1 :                          p2->description)) &&
      50           1 :            (1 == json_equal (p1->description_i18n,
      51           1 :                              p2->description_i18n)) &&
      52           1 :            (0 == strcmp (p1->unit,
      53           2 :                          p2->unit)) &&
      54             :            (GNUNET_OK ==
      55           1 :             TALER_amount_cmp_currency (&p1->price,
      56           1 :                                        &p2->price)) &&
      57           1 :            (0 == TALER_amount_cmp (&p1->price,
      58           1 :                                    &p2->price)) &&
      59           1 :            (1 == json_equal (p1->taxes,
      60           1 :                              p2->taxes)) &&
      61           1 :            (p1->total_stock == p2->total_stock) &&
      62           1 :            (p1->total_sold == p2->total_sold) &&
      63           1 :            (p1->total_lost == p2->total_lost) &&
      64           1 :            (0 == strcmp (p1->image,
      65           2 :                          p2->image)) &&
      66           1 :            (1 == json_equal (p1->address,
      67           4 :                              p2->address)) &&
      68           1 :            (p1->next_restock.abs_value_us ==
      69           1 :             p2->next_restock.abs_value_us) );
      70             : }
      71             : 
      72             : 
      73             : MHD_RESULT
      74           5 : TMH_private_post_products (const struct TMH_RequestHandler *rh,
      75             :                            struct MHD_Connection *connection,
      76             :                            struct TMH_HandlerContext *hc)
      77             : {
      78           5 :   struct TMH_MerchantInstance *mi = hc->instance;
      79           5 :   struct TALER_MERCHANTDB_ProductDetails pd = { 0 };
      80             :   const char *product_id;
      81             :   int64_t total_stock;
      82             :   enum GNUNET_DB_QueryStatus qs;
      83             :   struct GNUNET_JSON_Specification spec[] = {
      84           5 :     GNUNET_JSON_spec_string ("product_id",
      85             :                              &product_id),
      86           5 :     GNUNET_JSON_spec_string ("description",
      87             :                              (const char **) &pd.description),
      88           5 :     GNUNET_JSON_spec_mark_optional (
      89             :       GNUNET_JSON_spec_json ("description_i18n",
      90             :                              &pd.description_i18n)),
      91           5 :     GNUNET_JSON_spec_string ("unit",
      92             :                              (const char **) &pd.unit),
      93           5 :     TALER_JSON_spec_amount ("price",
      94             :                             TMH_currency,
      95             :                             &pd.price),
      96           5 :     GNUNET_JSON_spec_mark_optional (
      97             :       GNUNET_JSON_spec_string ("image",
      98             :                                (const char **) &pd.image)),
      99           5 :     GNUNET_JSON_spec_mark_optional (
     100             :       GNUNET_JSON_spec_json ("taxes",
     101             :                              &pd.taxes)),
     102           5 :     GNUNET_JSON_spec_int64 ("total_stock",
     103             :                             &total_stock),
     104           5 :     GNUNET_JSON_spec_mark_optional (
     105             :       GNUNET_JSON_spec_json ("address",
     106             :                              &pd.address)),
     107           5 :     GNUNET_JSON_spec_mark_optional (
     108             :       TALER_JSON_spec_absolute_time ("next_restock",
     109             :                                      &pd.next_restock)),
     110           5 :     GNUNET_JSON_spec_end ()
     111             :   };
     112             : 
     113           5 :   GNUNET_assert (NULL != mi);
     114             :   {
     115             :     enum GNUNET_GenericReturnValue res;
     116             : 
     117           5 :     res = TALER_MHD_parse_json_data (connection,
     118           5 :                                      hc->request_body,
     119             :                                      spec);
     120           5 :     if (GNUNET_OK != res)
     121             :     {
     122           0 :       GNUNET_break_op (0);
     123             :       return (GNUNET_NO == res)
     124             :              ? MHD_YES
     125           0 :              : MHD_NO;
     126             :     }
     127             :   }
     128           5 :   if (total_stock < -1)
     129             :   {
     130           0 :     GNUNET_break_op (0);
     131           0 :     GNUNET_JSON_parse_free (spec);
     132           0 :     return TALER_MHD_reply_with_error (connection,
     133             :                                        MHD_HTTP_BAD_REQUEST,
     134             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     135             :                                        "total_stock");
     136             :   }
     137             : 
     138             : 
     139           5 :   if (-1 == total_stock)
     140           0 :     pd.total_stock = INT64_MAX;
     141             :   else
     142           5 :     pd.total_stock = (uint64_t) total_stock;
     143             : 
     144           5 :   if (NULL == pd.address)
     145           0 :     pd.address = json_object ();
     146           5 :   if (NULL == pd.description_i18n)
     147           0 :     pd.description_i18n = json_object ();
     148           5 :   if (NULL == pd.taxes)
     149           0 :     pd.taxes = json_array ();
     150             : 
     151             :   /* check taxes is well-formed */
     152           5 :   if (! TMH_taxes_array_valid (pd.taxes))
     153             :   {
     154           0 :     GNUNET_break_op (0);
     155           0 :     GNUNET_JSON_parse_free (spec);
     156           0 :     return TALER_MHD_reply_with_error (connection,
     157             :                                        MHD_HTTP_BAD_REQUEST,
     158             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     159             :                                        "taxes");
     160             :   }
     161             : 
     162           5 :   if (! TMH_location_object_valid (pd.address))
     163             :   {
     164           0 :     GNUNET_break_op (0);
     165           0 :     GNUNET_JSON_parse_free (spec);
     166           0 :     return TALER_MHD_reply_with_error (connection,
     167             :                                        MHD_HTTP_BAD_REQUEST,
     168             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     169             :                                        "address");
     170             :   }
     171             : 
     172           5 :   if (! TALER_JSON_check_i18n (pd.description_i18n))
     173             :   {
     174           0 :     GNUNET_break_op (0);
     175           0 :     GNUNET_JSON_parse_free (spec);
     176           0 :     return TALER_MHD_reply_with_error (connection,
     177             :                                        MHD_HTTP_BAD_REQUEST,
     178             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     179             :                                        "description_i18n");
     180             :   }
     181             : 
     182           5 :   if (NULL == pd.image)
     183           0 :     pd.image = "";
     184           5 :   if (! TMH_image_data_url_valid (pd.image))
     185             :   {
     186           0 :     GNUNET_break_op (0);
     187           0 :     GNUNET_JSON_parse_free (spec);
     188           0 :     return TALER_MHD_reply_with_error (connection,
     189             :                                        MHD_HTTP_BAD_REQUEST,
     190             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     191             :                                        "image");
     192             :   }
     193             : 
     194             :   /* finally, interact with DB until no serialization error */
     195           5 :   for (unsigned int i = 0; i<MAX_RETRIES; i++)
     196             :   {
     197             :     /* Test if an product of this id is known */
     198             :     struct TALER_MERCHANTDB_ProductDetails epd;
     199             : 
     200           5 :     if (GNUNET_OK !=
     201           5 :         TMH_db->start (TMH_db->cls,
     202             :                        "/post products"))
     203             :     {
     204           0 :       GNUNET_break (0);
     205           0 :       GNUNET_JSON_parse_free (spec);
     206           2 :       return TALER_MHD_reply_with_error (connection,
     207             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     208             :                                          TALER_EC_GENERIC_DB_START_FAILED,
     209             :                                          NULL);
     210             :     }
     211           5 :     qs = TMH_db->lookup_product (TMH_db->cls,
     212           5 :                                  mi->settings.id,
     213             :                                  product_id,
     214             :                                  &epd);
     215           5 :     switch (qs)
     216             :     {
     217           0 :     case GNUNET_DB_STATUS_HARD_ERROR:
     218             :       /* Clean up and fail hard */
     219           0 :       GNUNET_break (0);
     220           0 :       TMH_db->rollback (TMH_db->cls);
     221           0 :       GNUNET_JSON_parse_free (spec);
     222           0 :       return TALER_MHD_reply_with_error (connection,
     223             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     224             :                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
     225             :                                          NULL);
     226           0 :     case GNUNET_DB_STATUS_SOFT_ERROR:
     227             :       /* restart transaction */
     228           0 :       goto retry;
     229           3 :     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     230             :       /* Good, we can proceed! */
     231           3 :       break;
     232           2 :     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     233             :       /* idempotency check: is epd == pd? */
     234             :       {
     235             :         bool eq;
     236             : 
     237           2 :         eq = products_equal (&pd,
     238             :                              &epd);
     239           2 :         TALER_MERCHANTDB_product_details_free (&epd);
     240           2 :         TMH_db->rollback (TMH_db->cls);
     241           2 :         GNUNET_JSON_parse_free (spec);
     242             :         return eq
     243           1 :           ? TALER_MHD_reply_static (connection,
     244             :                                     MHD_HTTP_NO_CONTENT,
     245             :                                     NULL,
     246             :                                     NULL,
     247             :                                     0)
     248           3 :           : TALER_MHD_reply_with_error (connection,
     249             :                                         MHD_HTTP_CONFLICT,
     250             :                                         TALER_EC_MERCHANT_PRIVATE_POST_PRODUCTS_CONFLICT_PRODUCT_EXISTS,
     251             :                                         product_id);
     252             :       }
     253             :     } /* end switch (qs) */
     254             : 
     255           3 :     qs = TMH_db->insert_product (TMH_db->cls,
     256           3 :                                  mi->settings.id,
     257             :                                  product_id,
     258             :                                  &pd);
     259           3 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     260             :     {
     261           0 :       TMH_db->rollback (TMH_db->cls);
     262           3 :       break;
     263             :     }
     264           3 :     if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
     265             :     {
     266           3 :       qs = TMH_db->commit (TMH_db->cls);
     267           3 :       if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
     268           3 :         break;
     269             :     }
     270           0 : retry:
     271           0 :     GNUNET_assert (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     272           0 :     TMH_db->rollback (TMH_db->cls);
     273             :   } /* for RETRIES loop */
     274           3 :   GNUNET_JSON_parse_free (spec);
     275           3 :   if (qs < 0)
     276             :   {
     277           0 :     GNUNET_break (0);
     278           0 :     return TALER_MHD_reply_with_error (
     279             :       connection,
     280             :       MHD_HTTP_INTERNAL_SERVER_ERROR,
     281             :       (GNUNET_DB_STATUS_SOFT_ERROR == qs)
     282             :       ? TALER_EC_GENERIC_DB_SOFT_FAILURE
     283             :       : TALER_EC_GENERIC_DB_COMMIT_FAILED,
     284             :       NULL);
     285             :   }
     286           3 :   return TALER_MHD_reply_static (connection,
     287             :                                  MHD_HTTP_NO_CONTENT,
     288             :                                  NULL,
     289             :                                  NULL,
     290             :                                  0);
     291             : }
     292             : 
     293             : 
     294             : /* end of taler-merchant-httpd_private-post-products.c */

Generated by: LCOV version 1.14