taler-merchant-httpd_patch-private-products-PRODUCT_ID.c (15792B)
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 ssize_t no_cat; 144 bool no_product; 145 bool lost_reduced; 146 bool sold_reduced; 147 bool stock_reduced; 148 bool no_group; 149 bool no_pot; 150 151 GNUNET_assert (NULL != mi); 152 GNUNET_assert (NULL != product_id); 153 { 154 enum GNUNET_GenericReturnValue res; 155 156 res = TALER_MHD_parse_json_data (connection, 157 hc->request_body, 158 spec); 159 if (GNUNET_OK != res) 160 return (GNUNET_NO == res) 161 ? MHD_YES 162 : MHD_NO; 163 /* For pre-v20 clients, we use the description given as the 164 product name; remove once we make product_name mandatory. */ 165 if (NULL == pd.product_name) 166 pd.product_name = pd.description; 167 } 168 if (! unit_price_missing) 169 { 170 if (! price_missing) 171 { 172 if (0 != TALER_amount_cmp (&price, 173 &pd.price_array[0])) 174 { 175 ret = TALER_MHD_reply_with_error (connection, 176 MHD_HTTP_BAD_REQUEST, 177 TALER_EC_GENERIC_PARAMETER_MALFORMED, 178 "price,unit_price mismatch"); 179 goto cleanup; 180 } 181 } 182 if (GNUNET_OK != 183 TMH_validate_unit_price_array (pd.price_array, 184 pd.price_array_length)) 185 { 186 ret = TALER_MHD_reply_with_error (connection, 187 MHD_HTTP_BAD_REQUEST, 188 TALER_EC_GENERIC_PARAMETER_MALFORMED, 189 "unit_price"); 190 goto cleanup; 191 } 192 } 193 else 194 { 195 if (price_missing) 196 { 197 ret = TALER_MHD_reply_with_error (connection, 198 MHD_HTTP_BAD_REQUEST, 199 TALER_EC_GENERIC_PARAMETER_MALFORMED, 200 "price missing"); 201 goto cleanup; 202 } 203 pd.price_array = GNUNET_new_array (1, 204 struct TALER_Amount); 205 pd.price_array[0] = price; 206 pd.price_array_length = 1; 207 } 208 if (! unit_precision_missing) 209 { 210 if (unit_precision_level > TMH_MAX_FRACTIONAL_PRECISION_LEVEL) 211 { 212 ret = TALER_MHD_reply_with_error (connection, 213 MHD_HTTP_BAD_REQUEST, 214 TALER_EC_GENERIC_PARAMETER_MALFORMED, 215 "unit_precision_level"); 216 goto cleanup; 217 } 218 } 219 { 220 bool default_allow_fractional; 221 uint32_t default_precision_level; 222 223 if (GNUNET_OK != 224 TMH_unit_defaults_for_instance (mi, 225 pd.unit, 226 &default_allow_fractional, 227 &default_precision_level)) 228 { 229 GNUNET_break (0); 230 ret = TALER_MHD_reply_with_error (connection, 231 MHD_HTTP_INTERNAL_SERVER_ERROR, 232 TALER_EC_GENERIC_DB_FETCH_FAILED, 233 "unit defaults"); 234 goto cleanup; 235 } 236 if (unit_allow_fraction_missing) 237 unit_allow_fraction = default_allow_fractional; 238 if (unit_precision_missing) 239 unit_precision_level = default_precision_level; 240 241 if (! unit_allow_fraction) 242 unit_precision_level = 0; 243 pd.fractional_precision_level = unit_precision_level; 244 } 245 { 246 const char *eparam; 247 if (GNUNET_OK != 248 TALER_MERCHANT_vk_process_quantity_inputs ( 249 TALER_MERCHANT_VK_STOCK, 250 unit_allow_fraction, 251 total_stock_missing, 252 total_stock, 253 unit_total_stock_missing, 254 unit_total_stock, 255 &pd.total_stock, 256 &pd.total_stock_frac, 257 &eparam)) 258 { 259 ret = TALER_MHD_reply_with_error ( 260 connection, 261 MHD_HTTP_BAD_REQUEST, 262 TALER_EC_GENERIC_PARAMETER_MALFORMED, 263 eparam); 264 goto cleanup; 265 } 266 pd.allow_fractional_quantity = unit_allow_fraction; 267 } 268 if (NULL == pd.address) 269 pd.address = json_object (); 270 271 if (! TMH_location_object_valid (pd.address)) 272 { 273 GNUNET_break_op (0); 274 ret = TALER_MHD_reply_with_error (connection, 275 MHD_HTTP_BAD_REQUEST, 276 TALER_EC_GENERIC_PARAMETER_MALFORMED, 277 "address"); 278 goto cleanup; 279 } 280 num_cats = json_array_size (categories); 281 cats = GNUNET_new_array (num_cats, 282 uint64_t); 283 { 284 size_t idx; 285 json_t *val; 286 287 json_array_foreach (categories, idx, val) 288 { 289 if (! json_is_integer (val)) 290 { 291 GNUNET_break_op (0); 292 ret = TALER_MHD_reply_with_error (connection, 293 MHD_HTTP_BAD_REQUEST, 294 TALER_EC_GENERIC_PARAMETER_MALFORMED, 295 "categories"); 296 goto cleanup; 297 } 298 cats[idx] = json_integer_value (val); 299 } 300 } 301 302 if (NULL == pd.description_i18n) 303 pd.description_i18n = json_object (); 304 305 if (! TALER_JSON_check_i18n (pd.description_i18n)) 306 { 307 GNUNET_break_op (0); 308 ret = TALER_MHD_reply_with_error (connection, 309 MHD_HTTP_BAD_REQUEST, 310 TALER_EC_GENERIC_PARAMETER_MALFORMED, 311 "description_i18n"); 312 goto cleanup; 313 } 314 315 if (NULL == pd.taxes) 316 pd.taxes = json_array (); 317 /* check taxes is well-formed */ 318 if (! TALER_MERCHANT_taxes_array_valid (pd.taxes)) 319 { 320 GNUNET_break_op (0); 321 ret = TALER_MHD_reply_with_error (connection, 322 MHD_HTTP_BAD_REQUEST, 323 TALER_EC_GENERIC_PARAMETER_MALFORMED, 324 "taxes"); 325 goto cleanup; 326 } 327 328 if (NULL == pd.image) 329 pd.image = (char *) ""; 330 if (! TALER_MERCHANT_image_data_url_valid (pd.image)) 331 { 332 GNUNET_break_op (0); 333 ret = TALER_MHD_reply_with_error (connection, 334 MHD_HTTP_BAD_REQUEST, 335 TALER_EC_GENERIC_PARAMETER_MALFORMED, 336 "image"); 337 goto cleanup; 338 } 339 340 if ( (pd.total_stock < pd.total_sold + pd.total_lost) || 341 (pd.total_sold + pd.total_lost < pd.total_sold) /* integer overflow */) 342 { 343 GNUNET_break_op (0); 344 ret = TALER_MHD_reply_with_error ( 345 connection, 346 MHD_HTTP_BAD_REQUEST, 347 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_EXCEEDS_STOCKS, 348 NULL); 349 goto cleanup; 350 } 351 352 qs = TALER_MERCHANTDB_update_product (TMH_db, 353 mi->settings.id, 354 product_id, 355 &pd, 356 num_cats, 357 cats, 358 &no_cat, 359 &no_product, 360 &lost_reduced, 361 &sold_reduced, 362 &stock_reduced, 363 &no_group, 364 &no_pot); 365 switch (qs) 366 { 367 case GNUNET_DB_STATUS_HARD_ERROR: 368 GNUNET_break (0); 369 ret = TALER_MHD_reply_with_error (connection, 370 MHD_HTTP_INTERNAL_SERVER_ERROR, 371 TALER_EC_GENERIC_DB_STORE_FAILED, 372 NULL); 373 goto cleanup; 374 case GNUNET_DB_STATUS_SOFT_ERROR: 375 GNUNET_break (0); 376 ret = TALER_MHD_reply_with_error (connection, 377 MHD_HTTP_INTERNAL_SERVER_ERROR, 378 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 379 "unexpected serialization problem"); 380 goto cleanup; 381 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 382 GNUNET_break (0); 383 ret = TALER_MHD_reply_with_error (connection, 384 MHD_HTTP_INTERNAL_SERVER_ERROR, 385 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 386 "unexpected problem in stored procedure"); 387 goto cleanup; 388 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 389 break; 390 } 391 392 if (-1 != no_cat) 393 { 394 char cat_str[24]; 395 396 GNUNET_snprintf (cat_str, 397 sizeof (cat_str), 398 "%llu", 399 (unsigned long long) no_cat); 400 ret = TALER_MHD_reply_with_error (connection, 401 MHD_HTTP_NOT_FOUND, 402 TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN, 403 cat_str); 404 goto cleanup; 405 } 406 if (no_product) 407 { 408 ret = TALER_MHD_reply_with_error (connection, 409 MHD_HTTP_NOT_FOUND, 410 TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN, 411 product_id); 412 goto cleanup; 413 } 414 if (no_group) 415 { 416 ret = TALER_MHD_reply_with_error ( 417 connection, 418 MHD_HTTP_NOT_FOUND, 419 TALER_EC_MERCHANT_GENERIC_PRODUCT_GROUP_UNKNOWN, 420 NULL); 421 goto cleanup; 422 } 423 if (no_pot) 424 { 425 ret = TALER_MHD_reply_with_error ( 426 connection, 427 MHD_HTTP_NOT_FOUND, 428 TALER_EC_MERCHANT_GENERIC_MONEY_POT_UNKNOWN, 429 NULL); 430 goto cleanup; 431 } 432 if (lost_reduced) 433 { 434 ret = TALER_MHD_reply_with_error ( 435 connection, 436 MHD_HTTP_CONFLICT, 437 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_LOST_REDUCED, 438 NULL); 439 goto cleanup; 440 } 441 if (sold_reduced) 442 { 443 ret = TALER_MHD_reply_with_error ( 444 connection, 445 MHD_HTTP_CONFLICT, 446 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_SOLD_REDUCED, 447 NULL); 448 goto cleanup; 449 } 450 if (stock_reduced) 451 { 452 ret = TALER_MHD_reply_with_error ( 453 connection, 454 MHD_HTTP_CONFLICT, 455 TALER_EC_MERCHANT_PRIVATE_PATCH_PRODUCTS_TOTAL_STOCKED_REDUCED, 456 NULL); 457 goto cleanup; 458 } 459 /* success! */ 460 ret = TALER_MHD_reply_static (connection, 461 MHD_HTTP_NO_CONTENT, 462 NULL, 463 NULL, 464 0); 465 cleanup: 466 GNUNET_free (cats); 467 GNUNET_free (pd.price_array); 468 GNUNET_JSON_parse_free (spec); 469 return ret; 470 } 471 472 473 /* end of taler-merchant-httpd_patch-private-products-PRODUCT_ID.c */