taler-merchant-httpd_post-private-reports.c (4877B)
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 taler-merchant-httpd_post-private-reports.c 20 * @brief implementation of POST /private/reports 21 * @author Christian Grothoff 22 */ 23 #include "taler/platform.h" 24 #include "taler-merchant-httpd_post-private-reports.h" 25 #include <taler/taler_json_lib.h> 26 #include <taler/taler_dbevents.h> 27 28 MHD_RESULT 29 TMH_private_post_reports (const struct TMH_RequestHandler *rh, 30 struct MHD_Connection *connection, 31 struct TMH_HandlerContext *hc) 32 { 33 const char *description; 34 const char *program_section; 35 const char *mime_type; 36 const char *data_source; 37 const char *target_address; 38 struct GNUNET_TIME_Relative frequency; 39 struct GNUNET_TIME_Relative frequency_shift 40 = GNUNET_TIME_UNIT_ZERO; 41 enum GNUNET_DB_QueryStatus qs; 42 struct GNUNET_JSON_Specification spec[] = { 43 GNUNET_JSON_spec_string ("description", 44 &description), 45 GNUNET_JSON_spec_string ("program_section", 46 &program_section), 47 GNUNET_JSON_spec_string ("mime_type", 48 &mime_type), 49 GNUNET_JSON_spec_string ("data_source", 50 &data_source), 51 GNUNET_JSON_spec_string ("target_address", 52 &target_address), 53 GNUNET_JSON_spec_relative_time ("report_frequency", 54 &frequency), 55 GNUNET_JSON_spec_mark_optional ( 56 GNUNET_JSON_spec_relative_time ("report_frequency_shift", 57 &frequency_shift), 58 NULL), 59 GNUNET_JSON_spec_end () 60 }; 61 uint64_t report_id; 62 63 (void) rh; 64 65 { 66 enum GNUNET_GenericReturnValue res; 67 68 res = TALER_MHD_parse_json_data (connection, 69 hc->request_body, 70 spec); 71 if (GNUNET_OK != res) 72 { 73 GNUNET_break_op (0); 74 return (GNUNET_NO == res) 75 ? MHD_YES 76 : MHD_NO; 77 } 78 } 79 { 80 char *section; 81 82 /* Check program_section exists in config! */ 83 GNUNET_asprintf (§ion, 84 "report-generator-%s", 85 program_section); 86 if (GNUNET_YES != 87 GNUNET_CONFIGURATION_have_value (TMH_cfg, 88 section, 89 "BINARY")) 90 { 91 GNUNET_free (section); 92 GNUNET_break (0); 93 return TALER_MHD_reply_with_error ( 94 connection, 95 MHD_HTTP_NOT_IMPLEMENTED, 96 TALER_EC_MERCHANT_GENERIC_REPORT_GENERATOR_UNCONFIGURED, 97 program_section); 98 } 99 GNUNET_free (section); 100 } 101 if ('/' != data_source[0]) 102 { 103 GNUNET_break_op (0); 104 return TALER_MHD_reply_with_error (connection, 105 MHD_HTTP_BAD_REQUEST, 106 TALER_EC_GENERIC_PARAMETER_MALFORMED, 107 "data_source"); 108 109 } 110 qs = TMH_db->insert_report (TMH_db->cls, 111 hc->instance->settings.id, 112 program_section, 113 description, 114 mime_type, 115 data_source, 116 target_address, 117 frequency, 118 frequency_shift, 119 &report_id); 120 if (qs < 0) 121 { 122 GNUNET_break (0); 123 return TALER_MHD_reply_with_error (connection, 124 MHD_HTTP_INTERNAL_SERVER_ERROR, 125 TALER_EC_GENERIC_DB_STORE_FAILED, 126 "insert_report"); 127 } 128 129 /* FIXME-Optimization: do trigger inside of transaction above... */ 130 { 131 struct GNUNET_DB_EventHeaderP ev = { 132 .size = htons (sizeof (ev)), 133 .type = htons (TALER_DBEVENT_MERCHANT_REPORT_UPDATE) 134 }; 135 136 TMH_db->event_notify (TMH_db->cls, 137 &ev, 138 NULL, 139 0); 140 } 141 142 return TALER_MHD_REPLY_JSON_PACK ( 143 connection, 144 MHD_HTTP_OK, 145 GNUNET_JSON_pack_uint64 ("report_serial_id", 146 report_id)); 147 }