lookup_expected_transfers.c (10193B)
1 /* 2 This file is part of TALER 3 Copyright (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 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 /** 17 * @file src/backenddb/lookup_expected_transfers.c 18 * @brief Implementation of the lookup_expected_transfers function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_expected_transfers.h" 24 #include "helper.h" 25 #include <microhttpd.h> /* for HTTP status codes */ 26 27 /** 28 * Closure for #lookup_expected_transfers_cb(). 29 */ 30 struct LookupExpectedTransfersContext 31 { 32 /** 33 * Function to call on results. 34 */ 35 TALER_MERCHANTDB_IncomingCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Postgres context. 44 */ 45 struct TALER_MERCHANTDB_PostgresContext *pg; 46 47 /** 48 * Transaction status (set). 49 */ 50 enum GNUNET_DB_QueryStatus qs; 51 52 }; 53 54 55 /** 56 * Function to be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param cls of type `struct LookupExpectedTransfersContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 lookup_expected_transfers_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct LookupExpectedTransfersContext *ltc = cls; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 struct TALER_Amount expected_credit_amount; 73 struct TALER_WireTransferIdentifierRawP wtid; 74 struct TALER_FullPayto payto_uri; 75 char *exchange_url; 76 uint64_t expected_transfer_serial_id; 77 struct GNUNET_TIME_Timestamp execution_time 78 = GNUNET_TIME_UNIT_ZERO_TS; 79 bool confirmed; 80 bool validated; 81 bool no_amount; 82 char *last_detail = NULL; 83 uint32_t last_http_status = 0; 84 uint32_t last_ec = TALER_EC_NONE; 85 struct GNUNET_PQ_ResultSpec rs[] = { 86 GNUNET_PQ_result_spec_allow_null ( 87 TALER_PQ_result_spec_amount_with_currency ("expected_credit_amount", 88 &expected_credit_amount), 89 &no_amount), 90 GNUNET_PQ_result_spec_auto_from_type ("wtid", 91 &wtid), 92 GNUNET_PQ_result_spec_string ("payto_uri", 93 &payto_uri.full_payto), 94 GNUNET_PQ_result_spec_string ("exchange_url", 95 &exchange_url), 96 GNUNET_PQ_result_spec_uint64 ("expected_credit_serial", 97 &expected_transfer_serial_id), 98 GNUNET_PQ_result_spec_allow_null ( 99 GNUNET_PQ_result_spec_timestamp ("execution_time", 100 &execution_time), 101 NULL), 102 GNUNET_PQ_result_spec_bool ("confirmed", 103 &confirmed), 104 GNUNET_PQ_result_spec_allow_null ( 105 GNUNET_PQ_result_spec_uint32 ("last_http_status", 106 &last_http_status), 107 NULL), 108 GNUNET_PQ_result_spec_allow_null ( 109 GNUNET_PQ_result_spec_uint32 ("last_ec", 110 &last_ec), 111 NULL), 112 GNUNET_PQ_result_spec_allow_null ( 113 GNUNET_PQ_result_spec_string ("last_detail", 114 &last_detail), 115 NULL), 116 GNUNET_PQ_result_spec_end 117 }; 118 119 if (GNUNET_OK != 120 GNUNET_PQ_extract_result (result, 121 rs, 122 i)) 123 { 124 GNUNET_break (0); 125 ltc->qs = GNUNET_DB_STATUS_HARD_ERROR; 126 return; 127 } 128 validated = ( (MHD_HTTP_OK == last_http_status) && 129 (TALER_EC_NONE == last_ec) ); 130 ltc->cb (ltc->cb_cls, 131 no_amount 132 ? NULL 133 : &expected_credit_amount, 134 &wtid, 135 payto_uri, 136 exchange_url, 137 expected_transfer_serial_id, 138 execution_time, 139 confirmed, 140 validated, 141 last_http_status, 142 last_ec, 143 last_detail); 144 GNUNET_PQ_cleanup_result (rs); 145 } 146 ltc->qs = num_results; 147 } 148 149 150 enum GNUNET_DB_QueryStatus 151 TALER_MERCHANTDB_lookup_expected_transfers ( 152 struct TALER_MERCHANTDB_PostgresContext *pg, 153 const char *instance_id, 154 struct TALER_FullPayto payto_uri, 155 struct GNUNET_TIME_Timestamp before, 156 struct GNUNET_TIME_Timestamp after, 157 int64_t limit, 158 uint64_t offset, 159 enum TALER_EXCHANGE_YesNoAll confirmed, 160 enum TALER_EXCHANGE_YesNoAll verified, 161 TALER_MERCHANTDB_IncomingCallback cb, 162 void *cb_cls) 163 { 164 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 165 bool by_time = ( (! GNUNET_TIME_absolute_is_never (before.abs_time)) || 166 (! GNUNET_TIME_absolute_is_zero (after.abs_time)) ); 167 struct LookupExpectedTransfersContext ltc = { 168 .cb = cb, 169 .cb_cls = cb_cls, 170 .pg = pg 171 }; 172 struct GNUNET_PQ_QueryParam params[] = { 173 GNUNET_PQ_query_param_timestamp (&before), 174 GNUNET_PQ_query_param_timestamp (&after), 175 GNUNET_PQ_query_param_uint64 (&offset), 176 GNUNET_PQ_query_param_uint64 (&plimit), 177 NULL == payto_uri.full_payto 178 ? GNUNET_PQ_query_param_null () /* NULL: do not filter by payto URI */ 179 : GNUNET_PQ_query_param_string (payto_uri.full_payto), 180 GNUNET_PQ_query_param_bool (! by_time), /* $6: filter by time? */ 181 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_ALL == confirmed), /* filter by confirmed? */ 182 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_YES == confirmed), 183 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_ALL == verified), /* filter by verified? */ 184 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_YES == verified), 185 186 GNUNET_PQ_query_param_end 187 }; 188 enum GNUNET_DB_QueryStatus qs; 189 190 GNUNET_assert (NULL != pg->current_merchant_id); 191 GNUNET_assert (0 == strcmp (instance_id, 192 pg->current_merchant_id)); 193 if (limit > 0) 194 { 195 TMH_PQ_prepare_anon (pg, 196 "SELECT" 197 " met.expected_credit_amount" 198 ",met.wtid" 199 ",mac.payto_uri" 200 ",met.exchange_url" 201 ",met.expected_credit_serial" 202 ",mts.execution_time" 203 ",met.confirmed" 204 ",met.last_http_status" 205 ",met.last_ec" 206 ",met.last_detail" 207 " FROM merchant_expected_transfers met" 208 " JOIN merchant_accounts mac" 209 " USING (account_serial)" 210 " LEFT JOIN merchant_transfer_signatures mts" 211 " USING (expected_credit_serial)" 212 " WHERE ( $6 OR " 213 " (mts.execution_time IS NOT NULL AND" 214 " mts.execution_time < $1 AND" 215 " mts.execution_time >= $2) )" 216 " AND ( (CAST($5 AS TEXT) IS NULL) OR " 217 " (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')" 218 " =REGEXP_REPLACE($5,'\\?.*','')) )" 219 " AND ( $7 OR " 220 " (met.confirmed = $8) )" 221 " AND ( $9 OR " 222 " ($10 = (200=met.last_http_status) AND" 223 " (0=met.last_ec) ) )" 224 " AND (met.expected_credit_serial > $3)" 225 " ORDER BY met.expected_credit_serial ASC" 226 " LIMIT $4"); 227 } 228 else 229 { 230 TMH_PQ_prepare_anon (pg, 231 "SELECT" 232 " met.expected_credit_amount" 233 ",met.wtid" 234 ",mac.payto_uri" 235 ",met.exchange_url" 236 ",met.expected_credit_serial" 237 ",mts.execution_time" 238 ",met.confirmed" 239 ",met.last_http_status" 240 ",met.last_ec" 241 ",met.last_detail" 242 " FROM merchant_expected_transfers met" 243 " JOIN merchant_accounts mac" 244 " USING (account_serial)" 245 " LEFT JOIN merchant_transfer_signatures mts" 246 " USING (expected_credit_serial)" 247 " WHERE ( $6 OR " 248 " (mts.execution_time IS NOT NULL AND" 249 " mts.execution_time < $1 AND" 250 " mts.execution_time >= $2) )" 251 " AND ( (CAST($5 AS TEXT) IS NULL) OR " 252 " (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')" 253 " =REGEXP_REPLACE($5,'\\?.*','')) )" 254 " AND ( $7 OR " 255 " (met.confirmed = $8) )" 256 " AND ( $9 OR " 257 " ($10 = (200=met.last_http_status) AND" 258 " (0=met.last_ec) ) )" 259 " AND (met.expected_credit_serial < $3)" 260 " ORDER BY met.expected_credit_serial DESC" 261 " LIMIT $4"); 262 } 263 qs = GNUNET_PQ_eval_prepared_multi_select ( 264 pg->conn, 265 "", 266 params, 267 &lookup_expected_transfers_cb, 268 <c); 269 if (0 >= qs) 270 return qs; 271 return ltc.qs; 272 }