taler-merchant-httpd_patch-private-pots-POT_ID.c (5217B)
1 /* 2 This file is part of TALER 3 (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more 12 details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with TALER; see the file COPYING. If not, see 16 <http://www.gnu.org/licenses/> 17 */ 18 /** 19 * @file src/backend/taler-merchant-httpd_patch-private-pots-POT_ID.c 20 * @brief implementation of PATCH /private/pots/$POT_ID 21 * @author Christian Grothoff 22 */ 23 #include "platform.h" 24 #include "taler-merchant-httpd_patch-private-pots-POT_ID.h" 25 #include <taler/taler_json_lib.h> 26 #include "merchant-database/update_money_pot.h" 27 28 29 enum MHD_Result 30 TMH_private_patch_pot (const struct TMH_RequestHandler *rh, 31 struct MHD_Connection *connection, 32 struct TMH_HandlerContext *hc) 33 { 34 const char *pot_id_str = hc->infix; 35 unsigned long long pot_id; 36 const char *pot_name; 37 const char *description; 38 size_t expected_pot_total_len; 39 struct TALER_Amount *expected_pot_totals; 40 bool no_expected_total; 41 size_t new_pot_total_len; 42 struct TALER_Amount *new_pot_totals; 43 bool no_new_total; 44 enum GNUNET_DB_QueryStatus qs; 45 bool conflict_total; 46 bool conflict_name; 47 struct GNUNET_JSON_Specification spec[] = { 48 GNUNET_JSON_spec_string ("pot_name", 49 &pot_name), 50 GNUNET_JSON_spec_string ("description", 51 &description), 52 GNUNET_JSON_spec_mark_optional ( 53 TALER_JSON_spec_amount_any_array ("expected_pot_totals", 54 &expected_pot_total_len, 55 &expected_pot_totals), 56 &no_expected_total), 57 GNUNET_JSON_spec_mark_optional ( 58 TALER_JSON_spec_amount_any_array ("new_pot_totals", 59 &new_pot_total_len, 60 &new_pot_totals), 61 &no_new_total), 62 GNUNET_JSON_spec_end () 63 }; 64 65 (void) rh; 66 { 67 char dummy; 68 69 if (1 != sscanf (pot_id_str, 70 "%llu%c", 71 &pot_id, 72 &dummy)) 73 { 74 GNUNET_break_op (0); 75 return TALER_MHD_reply_with_error (connection, 76 MHD_HTTP_BAD_REQUEST, 77 TALER_EC_GENERIC_PARAMETER_MALFORMED, 78 "pot_id"); 79 } 80 } 81 82 { 83 enum GNUNET_GenericReturnValue res; 84 85 res = TALER_MHD_parse_json_data (connection, 86 hc->request_body, 87 spec); 88 if (GNUNET_OK != res) 89 { 90 GNUNET_break_op (0); 91 return (GNUNET_NO == res) 92 ? MHD_YES 93 : MHD_NO; 94 } 95 } 96 97 qs = TALER_MERCHANTDB_update_money_pot (TMH_db, 98 hc->instance->settings.id, 99 pot_id, 100 pot_name, 101 description, 102 expected_pot_total_len, 103 no_expected_total 104 ? NULL 105 : expected_pot_totals, 106 new_pot_total_len, 107 no_new_total 108 ? NULL 109 : new_pot_totals, 110 &conflict_total, 111 &conflict_name); 112 GNUNET_JSON_parse_free (spec); 113 if (qs < 0) 114 { 115 GNUNET_break (0); 116 return TALER_MHD_reply_with_error (connection, 117 MHD_HTTP_INTERNAL_SERVER_ERROR, 118 TALER_EC_GENERIC_DB_STORE_FAILED, 119 "update_money_pot"); 120 } 121 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 122 { 123 return TALER_MHD_reply_with_error ( 124 connection, 125 MHD_HTTP_NOT_FOUND, 126 TALER_EC_MERCHANT_GENERIC_MONEY_POT_UNKNOWN, 127 pot_id_str); 128 } 129 if (conflict_total) 130 { 131 /* Pot total mismatch - expected_pot_total didn't match current value */ 132 return TALER_MHD_reply_with_error ( 133 connection, 134 MHD_HTTP_CONFLICT, 135 TALER_EC_MERCHANT_PRIVATE_MONEY_POT_CONFLICTING_TOTAL, 136 NULL); 137 } 138 if (conflict_name) 139 { 140 /* Pot name conflict - name exists */ 141 return TALER_MHD_reply_with_error ( 142 connection, 143 MHD_HTTP_CONFLICT, 144 TALER_EC_MERCHANT_PRIVATE_MONEY_POT_CONFLICTING_NAME, 145 pot_name); 146 } 147 148 return TALER_MHD_reply_static (connection, 149 MHD_HTTP_NO_CONTENT, 150 NULL, 151 NULL, 152 0); 153 }