LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-patch-orders-ID-forget.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 63 87 72.4 %
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-orders-ID-forget.c
      22             :  * @brief implementing PATCH /orders/$ORDER_ID/forget request handling
      23             :  * @author Jonathan Buchanan
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler-merchant-httpd_private-patch-instances-ID.h"
      27             : #include <taler/taler_json_lib.h>
      28             : 
      29             : 
      30             : /**
      31             :  * How often do we retry the UPDATE database transaction?
      32             :  */
      33             : #define MAX_RETRIES 3
      34             : 
      35             : 
      36             : /**
      37             :  * Forget part of the contract terms.
      38             :  *
      39             :  * @param cls pointer to the result of the forget operation.
      40             :  * @param object_id name of the object to forget.
      41             :  * @param parent parent of the object at @e object_id.
      42             :  */
      43             : static void
      44           5 : forget (void *cls,
      45             :         const char *object_id,
      46             :         json_t *parent)
      47             : {
      48           5 :   int *res = cls;
      49             :   int ret;
      50             : 
      51           5 :   ret = TALER_JSON_contract_part_forget (parent,
      52             :                                          object_id);
      53           5 :   if (GNUNET_SYSERR == ret)
      54             :   {
      55           1 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      56             :                 "Matching path `%s' not forgettable!\n",
      57             :                 object_id);
      58           1 :     *res = GNUNET_SYSERR;
      59             :   }
      60           5 :   if (GNUNET_NO == ret)
      61             :   {
      62           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      63             :                 "Matching path `%s' already forgotten!\n",
      64             :                 object_id);
      65             :   }
      66             :   else
      67             :   {
      68           5 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      69             :                 "Forgot `%s'\n",
      70             :                 object_id);
      71           5 :     if (GNUNET_NO == *res)
      72           3 :       *res = GNUNET_OK;
      73             :   }
      74           5 : }
      75             : 
      76             : 
      77             : /**
      78             :  * Forget fields of an order's contract terms.
      79             :  *
      80             :  * @param rh context of the handler
      81             :  * @param connection the MHD connection to handle
      82             :  * @param[in,out] hc context with further information about the request
      83             :  * @return MHD result code
      84             :  */
      85             : MHD_RESULT
      86           6 : TMH_private_patch_orders_ID_forget (const struct TMH_RequestHandler *rh,
      87             :                                     struct MHD_Connection *connection,
      88             :                                     struct TMH_HandlerContext *hc)
      89             : {
      90           6 :   const char *order_id = hc->infix;
      91             :   enum GNUNET_DB_QueryStatus qs;
      92             :   uint64_t order_serial;
      93             : 
      94           6 :   for (unsigned int i = 0; i<MAX_RETRIES; i++)
      95             :   {
      96             :     json_t *fields;
      97             :     json_t *contract_terms;
      98           6 :     bool changed = false;
      99             : 
     100           6 :     if (GNUNET_OK !=
     101           6 :         TMH_db->start (TMH_db->cls,
     102             :                        "forget order"))
     103             :     {
     104           3 :       return TALER_MHD_reply_with_error (connection,
     105             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     106             :                                          TALER_EC_GENERIC_DB_START_FAILED,
     107             :                                          NULL);
     108             :     }
     109           6 :     qs = TMH_db->lookup_contract_terms (TMH_db->cls,
     110           6 :                                         hc->instance->settings.id,
     111             :                                         order_id,
     112             :                                         &contract_terms,
     113             :                                         &order_serial,
     114             :                                         NULL);
     115           6 :     switch (qs)
     116             :     {
     117           0 :     case GNUNET_DB_STATUS_HARD_ERROR:
     118           0 :       TMH_db->rollback (TMH_db->cls);
     119           0 :       return TALER_MHD_reply_with_error (connection,
     120             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     121             :                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
     122             :                                          "contract terms");
     123           0 :     case GNUNET_DB_STATUS_SOFT_ERROR:
     124           0 :       TMH_db->rollback (TMH_db->cls);
     125           0 :       continue;
     126           1 :     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     127           1 :       TMH_db->rollback (TMH_db->cls);
     128           1 :       return TALER_MHD_reply_with_error (connection,
     129             :                                          MHD_HTTP_NOT_FOUND,
     130             :                                          TALER_EC_MERCHANT_GENERIC_ORDER_UNKNOWN,
     131             :                                          order_id);
     132           5 :     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     133           5 :       GNUNET_assert (NULL != contract_terms);
     134           5 :       break;
     135             :     }
     136             : 
     137           5 :     {
     138             :       struct GNUNET_JSON_Specification spec[] = {
     139           5 :         GNUNET_JSON_spec_json ("fields",
     140             :                                &fields),
     141           5 :         GNUNET_JSON_spec_end ()
     142             :       };
     143             :       enum GNUNET_GenericReturnValue res;
     144             : 
     145           5 :       res = TALER_MHD_parse_json_data (connection,
     146           5 :                                        hc->request_body,
     147             :                                        spec);
     148           5 :       if (GNUNET_OK != res)
     149             :       {
     150           0 :         TMH_db->rollback (TMH_db->cls);
     151           0 :         json_decref (contract_terms);
     152             :         return (GNUNET_NO == res)
     153             :                ? MHD_YES
     154           0 :                : MHD_NO;
     155             :       }
     156             :     }
     157           5 :     if (! (json_is_array (fields)))
     158             :     {
     159           0 :       TMH_db->rollback (TMH_db->cls);
     160           0 :       json_decref (contract_terms);
     161           0 :       json_decref (fields);
     162           0 :       return TALER_MHD_reply_with_error (connection,
     163             :                                          MHD_HTTP_BAD_REQUEST,
     164             :                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
     165             :                                          "fields");
     166             :     }
     167             : 
     168             :     {
     169             :       size_t index;
     170             :       json_t *value;
     171             : 
     172           8 :       json_array_foreach (fields, index, value) {
     173           5 :         int forget_status = GNUNET_NO;
     174             :         int expand_status;
     175             : 
     176           5 :         if (! (json_is_string (value)))
     177             :         {
     178           0 :           TMH_db->rollback (TMH_db->cls);
     179           0 :           json_decref (contract_terms);
     180           0 :           json_decref (fields);
     181           2 :           return TALER_MHD_reply_with_error (connection,
     182             :                                              MHD_HTTP_BAD_REQUEST,
     183             :                                              TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_SYNTAX_INCORRECT,
     184             :                                              "field is not a string");
     185             :         }
     186           5 :         expand_status = TALER_JSON_expand_path (contract_terms,
     187             :                                                 json_string_value (value),
     188             :                                                 &forget,
     189             :                                                 &forget_status);
     190           5 :         if (GNUNET_SYSERR == forget_status)
     191             :         {
     192             :           /* We tried to forget a field that isn't forgettable */
     193           1 :           TMH_db->rollback (TMH_db->cls);
     194           1 :           json_decref (contract_terms);
     195           1 :           json_decref (fields);
     196           1 :           return TALER_MHD_reply_with_error (connection,
     197             :                                              MHD_HTTP_CONFLICT,
     198             :                                              TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_NOT_FORGETTABLE,
     199             :                                              json_string_value (value));
     200             :         }
     201           4 :         if (GNUNET_OK == forget_status)
     202           3 :           changed = true;
     203           4 :         if (GNUNET_SYSERR == expand_status)
     204             :         {
     205             :           /* One of the paths was malformed and couldn't be expanded */
     206           1 :           TMH_db->rollback (TMH_db->cls);
     207           1 :           json_decref (contract_terms);
     208           1 :           json_decref (fields);
     209           1 :           return TALER_MHD_reply_with_error (connection,
     210             :                                              MHD_HTTP_BAD_REQUEST,
     211             :                                              TALER_EC_MERCHANT_PRIVATE_PATCH_ORDERS_ID_FORGET_PATH_SYNTAX_INCORRECT,
     212             :                                              json_string_value (value));
     213             :         }
     214             :       }
     215             :     }
     216             : 
     217           3 :     if (! changed)
     218             :     {
     219           0 :       TMH_db->rollback (TMH_db->cls);
     220           0 :       json_decref (contract_terms);
     221           0 :       json_decref (fields);
     222           0 :       return TALER_MHD_reply_static (connection,
     223             :                                      MHD_HTTP_NO_CONTENT,
     224             :                                      NULL,
     225             :                                      NULL,
     226             :                                      0);
     227             :     }
     228           3 :     qs = TMH_db->update_contract_terms (TMH_db->cls,
     229           3 :                                         hc->instance->settings.id,
     230             :                                         order_id,
     231             :                                         contract_terms);
     232           3 :     json_decref (contract_terms);
     233           3 :     json_decref (fields);
     234           3 :     if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     235             :     {
     236           0 :       TMH_db->rollback (TMH_db->cls);
     237           0 :       if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
     238           3 :         break;
     239             :     }
     240             :     else
     241             :     {
     242           3 :       qs = TMH_db->commit (TMH_db->cls);
     243           3 :       if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
     244           3 :         break;
     245             :     }
     246             :   }
     247           3 :   if (0 > qs)
     248             :   {
     249           0 :     return TALER_MHD_reply_with_error (connection,
     250             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     251             :                                        TALER_EC_GENERIC_DB_COMMIT_FAILED,
     252             :                                        NULL);
     253             :   }
     254             : 
     255           3 :   return TALER_MHD_reply_static (connection,
     256             :                                  MHD_HTTP_OK,
     257             :                                  NULL,
     258             :                                  NULL,
     259             :                                  0);
     260             : }

Generated by: LCOV version 1.14