donau-httpd_donation-statement.c (4332B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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 details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file donau-httpd_donation-statement_get.c 18 * @brief Return donation statement 19 * @author Johannes Casaburi 20 */ 21 #include <donau_config.h> 22 #include <gnunet/gnunet_util_lib.h> 23 #include <jansson.h> 24 #include <microhttpd.h> 25 #include <pthread.h> 26 #include <taler/taler_json_lib.h> 27 #include <taler/taler_mhd_lib.h> 28 #include <taler/taler_signatures.h> 29 #include "donaudb_plugin.h" 30 #include "donau-httpd_keys.h" 31 #include "donau-httpd_donation-statement.h" 32 33 34 MHD_RESULT 35 DH_handler_donation_statement_get ( 36 struct DH_RequestContext *rc, 37 const char *const args[2]) 38 { 39 unsigned long long donation_year; 40 struct DONAU_HashDonorTaxId h_donor_tax_id; 41 char dummy; 42 43 if ( (NULL == args[0]) || 44 (1 != sscanf (args[0], 45 "%llu%c", 46 &donation_year, 47 &dummy)) ) 48 { 49 GNUNET_break_op (0); 50 return TALER_MHD_reply_with_error (rc->connection, 51 MHD_HTTP_BAD_REQUEST, 52 TALER_EC_GENERIC_PARAMETER_MALFORMED, 53 "donation_year"); 54 } 55 56 if (GNUNET_OK != 57 GNUNET_STRINGS_string_to_data (args[1], 58 strlen (args[1]), 59 &h_donor_tax_id, 60 sizeof (h_donor_tax_id))) 61 { 62 GNUNET_break_op (0); 63 return TALER_MHD_reply_with_error (rc->connection, 64 MHD_HTTP_BAD_REQUEST, 65 TALER_EC_GENERIC_PARAMETER_MALFORMED, 66 "h_donor_tax_id"); 67 } 68 69 { 70 struct TALER_Amount total_donations; 71 struct DONAU_DonauPublicKeyP donau_pub; 72 struct DONAU_DonauSignatureP donau_sig; 73 enum GNUNET_DB_QueryStatus qs; 74 MHD_RESULT result; 75 76 qs = DH_plugin->iterate_submitted_receipts (DH_plugin->cls, 77 (uint64_t) donation_year, 78 &h_donor_tax_id, 79 &total_donations); 80 switch (qs) 81 { 82 case GNUNET_DB_STATUS_HARD_ERROR: 83 case GNUNET_DB_STATUS_SOFT_ERROR: 84 GNUNET_break (0); 85 return TALER_MHD_reply_with_error (rc->connection, 86 MHD_HTTP_INTERNAL_SERVER_ERROR, 87 TALER_EC_GENERIC_DB_FETCH_FAILED, 88 NULL); 89 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 90 return TALER_MHD_reply_static ( 91 rc->connection, 92 MHD_HTTP_NO_CONTENT, 93 NULL, 94 NULL, 95 0); 96 break; 97 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 98 enum TALER_ErrorCode ec; 99 ec = DONAU_donation_statement_sign ( 100 &DH_keys_donau_sign_, 101 &total_donations, 102 donation_year, 103 &h_donor_tax_id, 104 &donau_pub, 105 &donau_sig); 106 107 if (TALER_EC_NONE != ec) 108 { 109 GNUNET_break (0); 110 return TALER_MHD_reply_with_error (rc->connection, 111 MHD_HTTP_INTERNAL_SERVER_ERROR, 112 ec, 113 NULL); 114 } 115 break; 116 } 117 118 result = TALER_MHD_REPLY_JSON_PACK ( 119 rc->connection, 120 MHD_HTTP_OK, 121 TALER_JSON_pack_amount ("total", &total_donations), 122 GNUNET_JSON_pack_data_auto ("donation_statement_sig", 123 &donau_sig), 124 GNUNET_JSON_pack_data_auto ("donau_pub", &donau_pub)); 125 126 return result; 127 } 128 } 129 130 131 /* end of donau-httpd_donation-statement.c */