donau-httpd_get-donation-statement-YEAR-HASH_DONOR_ID.c (4317B)
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_get-donation-statement-YEAR-HASH_DONOR_ID.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 <taler/taler_json_lib.h> 26 #include <taler/taler_mhd_lib.h> 27 #include <taler/taler_signatures.h> 28 #include "donau-httpd_get-keys.h" 29 #include "donau-httpd_get-donation-statement-YEAR-HASH_DONOR_ID.h" 30 #include "donau-database/iterate_submitted_receipts.h" 31 32 33 enum MHD_Result 34 DH_handler_get_donation_statement ( 35 struct DH_RequestContext *rc, 36 const char *const args[2]) 37 { 38 unsigned long long donation_year; 39 struct DONAU_HashDonorTaxId h_donor_tax_id; 40 char dummy; 41 42 if ( (NULL == args[0]) || 43 (1 != sscanf (args[0], 44 "%llu%c", 45 &donation_year, 46 &dummy)) ) 47 { 48 GNUNET_break_op (0); 49 return TALER_MHD_reply_with_error (rc->connection, 50 MHD_HTTP_BAD_REQUEST, 51 TALER_EC_GENERIC_PARAMETER_MALFORMED, 52 "donation_year"); 53 } 54 55 if (GNUNET_OK != 56 GNUNET_STRINGS_string_to_data (args[1], 57 strlen (args[1]), 58 &h_donor_tax_id, 59 sizeof (h_donor_tax_id))) 60 { 61 GNUNET_break_op (0); 62 return TALER_MHD_reply_with_error (rc->connection, 63 MHD_HTTP_BAD_REQUEST, 64 TALER_EC_GENERIC_PARAMETER_MALFORMED, 65 "h_donor_tax_id"); 66 } 67 68 { 69 struct TALER_Amount total_donations; 70 struct DONAU_DonauPublicKeyP donau_pub; 71 struct DONAU_DonauSignatureP donau_sig; 72 enum GNUNET_DB_QueryStatus qs; 73 enum MHD_Result result; 74 75 qs = DONAUDB_iterate_submitted_receipts (DH_context, 76 (uint64_t) donation_year, 77 &h_donor_tax_id, 78 &total_donations); 79 switch (qs) 80 { 81 case GNUNET_DB_STATUS_HARD_ERROR: 82 case GNUNET_DB_STATUS_SOFT_ERROR: 83 GNUNET_break (0); 84 return TALER_MHD_reply_with_error (rc->connection, 85 MHD_HTTP_INTERNAL_SERVER_ERROR, 86 TALER_EC_GENERIC_DB_FETCH_FAILED, 87 NULL); 88 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 89 return TALER_MHD_reply_static ( 90 rc->connection, 91 MHD_HTTP_NO_CONTENT, 92 NULL, 93 NULL, 94 0); 95 break; 96 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 97 enum TALER_ErrorCode ec; 98 ec = DONAU_donation_statement_sign ( 99 &DH_keys_donau_sign_, 100 &total_donations, 101 donation_year, 102 &h_donor_tax_id, 103 &donau_pub, 104 &donau_sig); 105 106 if (TALER_EC_NONE != ec) 107 { 108 GNUNET_break (0); 109 return TALER_MHD_reply_with_ec (rc->connection, 110 ec, 111 NULL); 112 } 113 break; 114 } 115 116 result = TALER_MHD_REPLY_JSON_PACK ( 117 rc->connection, 118 MHD_HTTP_OK, 119 TALER_JSON_pack_amount ("total", &total_donations), 120 GNUNET_JSON_pack_data_auto ("donation_statement_sig", 121 &donau_sig), 122 GNUNET_JSON_pack_data_auto ("donau_pub", &donau_pub)); 123 124 return result; 125 } 126 } 127 128 129 /* end of donau-httpd_get-donation-statement-YEAR-HASH_DONOR_ID.c */