taler-auditor-httpd_get-monitoring-aml-holds.c (5346B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU 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 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 #include <gnunet/gnunet_util_lib.h> 17 #include <gnunet/gnunet_json_lib.h> 18 #include <jansson.h> 19 #include <microhttpd.h> 20 #include <pthread.h> 21 #include "taler/taler_json_lib.h" 22 #include "taler/taler_mhd_lib.h" 23 #include "exchangedb_lib.h" 24 #include "taler-auditor-httpd.h" 25 #include "taler-auditor-httpd_get-monitoring-aml-holds.h" 26 #define TALER_AUDITORDB_AUDITOR_AML_HOLD_RESULT_CLOSURE json_t 27 #include "auditor-database/iterate_auditor_aml_holds.h" 28 #include "auditor-database/preflight.h" 29 30 31 /** 32 * Convert the reason the exchange gave for not making a transfer into the 33 * string the REST API uses for it. 34 * 35 * @param reason an `enum TALER_EXCHANGEDB_DeferralReason` value 36 * @return human-readable name of @a reason 37 */ 38 static const char * 39 reason2s (uint32_t reason) 40 { 41 switch ((enum TALER_EXCHANGEDB_DeferralReason) reason) 42 { 43 case TALER_EXCHANGEDB_DR_NONE: 44 return "NONE"; 45 case TALER_EXCHANGEDB_DR_AMOUNT_TOO_SMALL: 46 return "AMOUNT_TOO_SMALL"; 47 case TALER_EXCHANGEDB_DR_KYC: 48 return "KYC"; 49 } 50 /* the exchange wrote a reason this auditor does not know; say so rather 51 than guess, the row is still a hold either way */ 52 GNUNET_break (0); 53 return "INVALID"; 54 } 55 56 57 /** 58 * Add an AML hold to the list. 59 * 60 * @param[in,out] list a `json_t *` array to extend 61 * @param ah the hold 62 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating 63 */ 64 static enum GNUNET_GenericReturnValue 65 process_aml_holds ( 66 json_t *list, 67 const struct TALER_AUDITORDB_AmlHold *ah) 68 { 69 json_t *obj; 70 71 obj = GNUNET_JSON_PACK ( 72 GNUNET_JSON_pack_uint64 ("row_id", 73 ah->row_id), 74 GNUNET_JSON_pack_data_auto ("wtid", 75 &ah->wtid), 76 GNUNET_JSON_pack_data_auto ("wire_target_h_payto", 77 &ah->wire_target_h_payto), 78 TALER_JSON_pack_full_payto ("account", 79 ah->account), 80 TALER_JSON_pack_amount ("amount", 81 &ah->amount), 82 GNUNET_JSON_pack_string ("deferral_reason", 83 reason2s (ah->deferral_reason)), 84 GNUNET_JSON_pack_uint64 ("legitimization_measure_serial_id", 85 ah->legitimization_measure_serial_id), 86 GNUNET_JSON_pack_timestamp ("first_seen", 87 GNUNET_TIME_absolute_to_timestamp ( 88 ah->creation_date)), 89 GNUNET_JSON_pack_bool ("suppressed", 90 ah->suppressed) 91 ); 92 GNUNET_break (0 == 93 json_array_append_new (list, 94 obj)); 95 return GNUNET_OK; 96 } 97 98 99 enum MHD_Result 100 TAH_get_monitoring_aml_holds ( 101 struct TAH_RequestHandler *rh, 102 struct MHD_Connection *connection, 103 void **connection_cls, 104 const char *upload_data, 105 size_t *upload_data_size, 106 const char *const args[]) 107 { 108 json_t *ja; 109 enum GNUNET_DB_QueryStatus qs; 110 int64_t limit = -20; 111 uint64_t offset; 112 bool return_suppressed = false; 113 114 if (GNUNET_SYSERR == 115 TALER_AUDITORDB_preflight (TAH_apg)) 116 { 117 GNUNET_break (0); 118 return TALER_MHD_reply_with_error (connection, 119 MHD_HTTP_INTERNAL_SERVER_ERROR, 120 TALER_EC_GENERIC_DB_SETUP_FAILED, 121 NULL); 122 } 123 TAH_PARSE_REQUEST_LIMIT (connection, 124 "limit", 125 &limit); 126 if (limit < 0) 127 offset = TAH_SAFE_UINT64_MAX; 128 else 129 offset = 0; 130 TAH_PARSE_SAFE_REQUEST_NUMBER (connection, 131 "offset", 132 &offset); 133 { 134 const char *ret_s 135 = MHD_lookup_connection_value (connection, 136 MHD_GET_ARGUMENT_KIND, 137 "return_suppressed"); 138 if (ret_s != NULL && strcmp (ret_s, "true") == 0) 139 { 140 return_suppressed = true; 141 } 142 } 143 ja = json_array (); 144 GNUNET_break (NULL != ja); 145 qs = TALER_AUDITORDB_iterate_auditor_aml_holds ( 146 TAH_apg, 147 limit, 148 offset, 149 return_suppressed, 150 &process_aml_holds, 151 ja); 152 if (0 > qs) 153 { 154 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs); 155 json_decref (ja); 156 TALER_LOG_WARNING ( 157 "Failed to handle GET /monitoring/aml-holds\n"); 158 return TALER_MHD_reply_with_error (connection, 159 MHD_HTTP_INTERNAL_SERVER_ERROR, 160 TALER_EC_GENERIC_DB_FETCH_FAILED, 161 "iterate_auditor_aml_holds"); 162 } 163 return TAH_reply_json_records (connection, 164 ja); 165 }