taler-merchant-httpd_patch-private-products-PRODUCT_ID.c (16157B)
1 /* 2 This file is part of TALER 3 (C) 2020--2026 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 src/backend/taler-merchant-httpd_patch-private-products-PRODUCT_ID.c 22 * @brief implementing PATCH /products/$ID request handling 23 * @author Christian Grothoff 24 */ 25 #include "platform.h" 26 #include "taler-merchant-httpd_patch-private-products-PRODUCT_ID.h" 27 #include "taler-merchant-httpd_helper.h" 28 #include <taler/taler_json_lib.h> 29 #include "merchant-database/update_product.h" 30 31 32 /** 33 * PATCH configuration of an existing instance, given its configuration. 34 * 35 * @param rh context of the handler 36 * @param connection the MHD connection to handle 37 * @param[in,out] hc context with further information about the request 38 * @return MHD result code 39 */ 40 enum MHD_Result 41 TMH_private_patch_products_ID ( 42 const struct TMH_RequestHandler *rh, 43 struct MHD_Connection *connection, 44 struct TMH_HandlerContext *hc) 45 { 46 struct TMH_MerchantInstance *mi = hc->instance; 47 const char *product_id = hc->infix; 48 struct TALER_MERCHANTDB_ProductDetails pd = {0}; 49 const json_t *categories = NULL; 50 int64_t total_stock; 51 const char *unit_total_stock = NULL; 52 bool unit_total_stock_missing; 53 bool total_stock_missing; 54 struct TALER_Amount price; 55 bool price_missing; 56 bool unit_price_missing; 57 bool unit_allow_fraction; 58 bool unit_allow_fraction_missing; 59 uint32_t unit_precision_level; 60 bool unit_precision_missing; 61 enum GNUNET_DB_QueryStatus qs; 62 struct GNUNET_JSON_Specification spec[] = { 63 /* new in protocol v20, thus optional for backwards-compatibility */ 64 GNUNET_JSON_spec_mark_optional ( 65 GNUNET_JSON_spec_string ("product_name", 66 (const char **) &pd.product_name), 67 NULL), 68 GNUNET_JSON_spec_string ("description", 69 (const char **) &pd.description), 70 GNUNET_JSON_spec_mark_optional ( 71 GNUNET_JSON_spec_json ("description_i18n", 72 &pd.description_i18n), 73 NULL), 74 GNUNET_JSON_spec_string ("unit", 75 (const char **) &pd.unit), 76 // FIXME: deprecated API 77 GNUNET_JSON_spec_mark_optional ( 78 TALER_JSON_spec_amount_any ("price", 79 &price), 80 &price_missing), 81 GNUNET_JSON_spec_mark_optional ( 82 TALER_JSON_spec_amount_any_array ("unit_price", 83 &pd.price_array_length, 84 &pd.price_array), 85 &unit_price_missing), 86 GNUNET_JSON_spec_mark_optional ( 87 GNUNET_JSON_spec_string ("image", 88 (const char **) &pd.image), 89 NULL), 90 GNUNET_JSON_spec_mark_optional ( 91 GNUNET_JSON_spec_json ("taxes", 92 &pd.taxes), 93 NULL), 94 GNUNET_JSON_spec_mark_optional ( 95 GNUNET_JSON_spec_array_const ("categories", 96 &categories), 97 NULL), 98 GNUNET_JSON_spec_mark_optional ( 99 GNUNET_JSON_spec_string ("unit_total_stock", 100 &unit_total_stock), 101 &unit_total_stock_missing), 102 GNUNET_JSON_spec_mark_optional ( 103 GNUNET_JSON_spec_int64 ("total_stock", 104 &total_stock), 105 &total_stock_missing), 106 GNUNET_JSON_spec_mark_optional ( 107 GNUNET_JSON_spec_bool ("unit_allow_fraction", 108 &unit_allow_fraction), 109 &unit_allow_fraction_missing), 110 GNUNET_JSON_spec_mark_optional ( 111 GNUNET_JSON_spec_uint32 ("unit_precision_level", 112 &unit_precision_level), 113 &unit_precision_missing), 114 GNUNET_JSON_spec_mark_optional ( 115 GNUNET_JSON_spec_uint64 ("total_lost", 116 &pd.total_lost), 117 NULL), 118 GNUNET_JSON_spec_mark_optional ( 119 GNUNET_JSON_spec_uint64 ("product_group_id", 120 &pd.product_group_id), 121 NULL), 122 GNUNET_JSON_spec_mark_optional ( 123 GNUNET_JSON_spec_uint64 ("money_pot_id", 124 &pd.money_pot_id), 125 NULL), 126 GNUNET_JSON_spec_mark_optional ( 127 GNUNET_JSON_spec_json ("address", 128 &pd.address), 129 NULL), 130 GNUNET_JSON_spec_mark_optional ( 131 GNUNET_JSON_spec_timestamp ("next_restock", 132 &pd.next_restock), 133 NULL), 134 GNUNET_JSON_spec_mark_optional ( 135 GNUNET_JSON_spec_uint32 ("minimum_age", 136 &pd.minimum_age), 137 NULL), 138 GNUNET_JSON_spec_end () 139 }; 140 enum MHD_Result ret; 141 size_t num_cats = 0; 142 uint64_t *cats = NULL; 143 bool no_instance; 144 ssize_t no_cat; 145 bool no_product; 146 bool lost_reduced; 147 bool sold_reduced; 148 bool stock_reduced; 149 bool no_group; 150 bool no_pot; 151 152 GNUNET_assert (NULL != mi); 153 GNUNET_assert (NULL != product_id); 154 { 155 enum GNUNET_GenericReturnValue res; 156 157 res = TALER_MHD_parse_json_data (connection, 158 hc->request_body, 159 spec); 160 if (GNUNET_OK != res) 161 return (GNUNET_NO == res) 162 ? MHD_YES 163 : MHD_NO; 164 /* For pre-v20 clients, we use the description given as the 165 product name; remove once we make product_name mandatory. */ 166 if (NULL == pd.product_name) 167 pd.product_name = pd.description; 168 } 169 if (! unit_price_missing) 170 { 171 if (! price_missing) 172 { 173 if (0 != TALER_amount_cmp (&price, 174 &pd.price_array[0])) 175 { 176 ret = TALER_MHD_reply_with_error (connection, 177 MHD_HTTP_BAD_REQUEST, 178 TALER_EC_GENERIC_PARAMETER_MALFORMED, 179 "price,unit_price mismatch"); 180 goto cleanup; 181 } 182 } 183 if (GNUNET_OK != 184 TMH_validate_unit_price_array (pd.price_array, 185 pd.price_array_length)) 186 { 187 ret = TALER_MHD_reply_with_error (connection, 188 MHD_HTTP_BAD_REQUEST, 189 TALER_EC_GENERIC_PARAMETER_MALFORMED, 190 "unit_price"); 191 goto cleanup; 192 } 193 } 194 else 195 { 196 if (price_missing) 197 { 198 ret = TALER_MHD_reply_with_error (connection, 199 MHD_HTTP_BAD_REQUEST, 200 TALER_EC_GENERIC_PARAMETER_MALFORMED, 201 "price missing"); 202 goto cleanup; 203 } 204 pd.price_array = GNUNET_new_array (1, 205 struct TALER_Amount); 206 pd.price_array[0] = price; 207 pd.price_array_length = 1; 208 } 209 if (! unit_precision_missing) 210 { 211 if (unit_precision_level > TMH_MAX_FRACTIONAL_PRECISION_LEVEL) 212 { 213 ret = TALER_MHD_reply_with_error (connection, 214 MHD_HTTP_BAD_REQUEST, 215 TALER_EC_GENERIC_PARAMETER_MALFORMED, 216 "unit_precision_level"); 217 goto cleanup; 218 } 219 } 220 { 221 bool default_allow_fractional; 222 uint32_t default_precision_level; 223 224 if (GNUNET_OK != 225 TMH_unit_defaults_for_instance (mi, 226 pd.unit, 227 &default_allow_fractional, 228 &default_precision_level)) 229 { 230 GNUNET_break (0); 231 ret = TALER_MHD_reply_with_error (connection, 232 MHD_HTTP_INTERNAL_SERVER_ERROR, 233 TALER_EC_GENERIC_DB_FETCH_FAILED, 234 "unit defaults"); 235 goto cleanup; 236 } 237 if (unit_allow_fraction_missing) 238 unit_allow_fraction = default_allow_fractional; 239 if (unit_precision_missing) 240 unit_precision_level = default_precision_level; 241 242 if (! unit_allow_fraction) 243 unit_precision_level = 0; 244 pd.fractional_precision_level = unit_precision_level; 245 } 246 { 247 const char *eparam; 248 if (GNUNET_OK != 249 TALER_MERCHANT_vk_process_quantity_inputs ( 250 TALER_MERCHANT_VK_STOCK, 251 unit_allow_fraction, 252 total_stock_missing, 253 total_stock, 254 unit_total_stock_missing, 255 unit_total_stock, 256 &pd.total_stock, 257 &pd.total_stock_frac, 258 &eparam)) 259 { 260 ret = TALER_MHD_reply_with_error ( 261 connection, 262 MHD_HTTP_BAD_REQUEST, 263 TALER_EC_GENERIC_PARAMETER_MALFORMED, 264 eparam); 265 goto cleanup; 266 } 267 pd.allow_fractional_quantity = unit_allow_fraction; 268 } 269 if (NULL == pd.address) 270 pd.address = json_object (); 271 272 if (! TMH_location_object_valid (pd.address)) 273 { 274 GNUNET_break_op (0); 275 ret = TALER_MHD_reply_with_error (connection, 276 MHD_HTTP_BAD_REQUEST, 277 TALER_EC_GENERIC_PARAMETER_MALFORMED, 278 "address"); 279 goto cleanup; 280 } 281 num_cats = json_array_size (categories); 282 cats = GNUNET_new_array (num_cats, 283 uint64_t); 284 { 285 size_t idx; 286 json_t *val; 287 288 json_array_foreach (categories, idx, val) 289 { 290 if (! json_is_integer (val)) 291 { 292 GNUNET_break_op (0); 293 ret = TALER_MHD_reply_with_error (connection, 294 MHD_HTTP_BAD_REQUEST, 295 TALER_EC_GENERIC_PARAMETER_MALFORMED, 296 "categories"); 297 goto cleanup; 298 } 299 cats[idx] = json_integer_value (val); 300 } 301 } 302 303 if (NULL == pd.description_i18n) 304 pd.description_i18n = json_object (); 305 306 if (! TALER_JSON_check_i18n (pd.description_i18n)) 307 { 308 GNUNET_break_op (0); 309 ret = TALER_MHD_reply_with_error (connection, 310 MHD_HTTP_BAD_REQUEST, 311 TALER_EC_GENERIC_PARAMETER_MALFORMED, 312 "description_i18n"); 313 goto cleanup; 314 } 315 316 if (NULL == pd.taxes) 317 pd.taxes = json_array (); 318 /* check taxes is well-formed */ 319 if (! TALER_MERCHANT_taxes_array_valid (pd.taxes)) 320 { 321 GNUNET_break_op (0); 322 ret = TALER_MHD_reply_with_error (connection, 323 MHD_HTTP_BAD_REQUEST, 324 TALER_EC_GENERIC_PARAMETER_MALFORMED, 325 "taxes"); 326 goto cleanup; 327 } 328 329 if (NULL == pd.image) 330 pd.image = (char *) ""; 331 if (! TALER_MERCHANT_image_data_url_valid (pd.image)) 332 { 333 GNUNET_break_op (0); 334 ret = TALER_MHD_reply_with_error (connection, 335 MHD_HTTP_BAD_REQUEST, 336 TALER_EC_GENERIC_PARAMETER_MALFORMED, 337 "image"); 338 goto cleanup; 339 } 340 341 if ( (pd.total_stock < pd.total_sold + pd.total_lost) || 342 (pd.total_sold + pd.total_lost < pd.total_sold) /* integer overflow */) 343 { 344 GNUNET_break_op (0); 345 ret = TALER_MHD_reply_with_error ( 346 connection, 347 MHD_HTTP_BAD_REQUEST, 348 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_EXCEEDS_STOCKS, 349 NULL); 350 goto cleanup; 351 } 352 353 qs = TALER_MERCHANTDB_update_product (TMH_db, 354 mi->settings.id, 355 product_id, 356 &pd, 357 num_cats, 358 cats, 359 &no_instance, 360 &no_cat, 361 &no_product, 362 &lost_reduced, 363 &sold_reduced, 364 &stock_reduced, 365 &no_group, 366 &no_pot); 367 switch (qs) 368 { 369 case GNUNET_DB_STATUS_HARD_ERROR: 370 GNUNET_break (0); 371 ret = TALER_MHD_reply_with_error (connection, 372 MHD_HTTP_INTERNAL_SERVER_ERROR, 373 TALER_EC_GENERIC_DB_STORE_FAILED, 374 NULL); 375 goto cleanup; 376 case GNUNET_DB_STATUS_SOFT_ERROR: 377 GNUNET_break (0); 378 ret = TALER_MHD_reply_with_error (connection, 379 MHD_HTTP_INTERNAL_SERVER_ERROR, 380 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 381 "unexpected serialization problem"); 382 goto cleanup; 383 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 384 GNUNET_break (0); 385 ret = TALER_MHD_reply_with_error (connection, 386 MHD_HTTP_INTERNAL_SERVER_ERROR, 387 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 388 "unexpected problem in stored procedure"); 389 goto cleanup; 390 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 391 break; 392 } 393 394 if (no_instance) 395 { 396 ret = TALER_MHD_reply_with_error (connection, 397 MHD_HTTP_NOT_FOUND, 398 TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN, 399 mi->settings.id); 400 goto cleanup; 401 } 402 if (-1 != no_cat) 403 { 404 char cat_str[24]; 405 406 GNUNET_snprintf (cat_str, 407 sizeof (cat_str), 408 "%llu", 409 (unsigned long long) no_cat); 410 ret = TALER_MHD_reply_with_error (connection, 411 MHD_HTTP_NOT_FOUND, 412 TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN, 413 cat_str); 414 goto cleanup; 415 } 416 if (no_product) 417 { 418 ret = TALER_MHD_reply_with_error (connection, 419 MHD_HTTP_NOT_FOUND, 420 TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN, 421 product_id); 422 goto cleanup; 423 } 424 if (no_group) 425 { 426 ret = TALER_MHD_reply_with_error ( 427 connection, 428 MHD_HTTP_NOT_FOUND, 429 TALER_EC_MERCHANT_GENERIC_PRODUCT_GROUP_UNKNOWN, 430 NULL); 431 goto cleanup; 432 } 433 if (no_pot) 434 { 435 ret = TALER_MHD_reply_with_error ( 436 connection, 437 MHD_HTTP_NOT_FOUND, 438 TALER_EC_MERCHANT_GENERIC_MONEY_POT_UNKNOWN, 439 NULL); 440 goto cleanup; 441 } 442 if (lost_reduced) 443 { 444 ret = TALER_MHD_reply_with_error ( 445 connection, 446 MHD_HTTP_CONFLICT, 447 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_REDUCED, 448 NULL); 449 goto cleanup; 450 } 451 if (sold_reduced) 452 { 453 ret = TALER_MHD_reply_with_error ( 454 connection, 455 MHD_HTTP_CONFLICT, 456 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_SOLD_REDUCED, 457 NULL); 458 goto cleanup; 459 } 460 if (stock_reduced) 461 { 462 ret = TALER_MHD_reply_with_error ( 463 connection, 464 MHD_HTTP_CONFLICT, 465 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_STOCKED_REDUCED, 466 NULL); 467 goto cleanup; 468 } 469 /* success! */ 470 ret = TALER_MHD_reply_static (connection, 471 MHD_HTTP_NO_CONTENT, 472 NULL, 473 NULL, 474 0); 475 cleanup: 476 GNUNET_free (cats); 477 GNUNET_free (pd.price_array); 478 GNUNET_JSON_parse_free (spec); 479 return ret; 480 } 481 482 483 /* end of taler-merchant-httpd_patch-private-products-PRODUCT_ID.c */