pg_lookup_transfer_details_by_order.c (7436B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023, 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 backenddb/pg_lookup_transfer_details_by_order.c 18 * @brief Implementation of the lookup_transfer_details_by_order function for Postgres 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <taler/taler_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "pg_lookup_transfer_details_by_order.h" 26 #include "pg_helper.h" 27 28 /** 29 * Closure for lookup_transfer_details_by_order_cb(). 30 */ 31 struct LookupTransferDetailsByOrderContext 32 { 33 34 /** 35 * Plugin context. 36 */ 37 struct PostgresClosure *pg; 38 39 /** 40 * Function to call with all results. 41 */ 42 TALER_MERCHANTDB_OrderTransferDetailsCallback cb; 43 44 /** 45 * Closure for @e cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Set to the query result. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 53 }; 54 55 56 /** 57 * Function to be called with the results of a SELECT statement 58 * that has returned @a num_results results. We SELECT by coin, 59 * but because that's not useful for the UI, we combine all coins 60 * that were deposited in the same wire transfer into a single 61 * record before calling the callback. 62 * 63 * @param cls of type `struct LookupTransferDetailsByOrderContext *` 64 * @param result the postgres result 65 * @param num_results the number of results in @a result 66 */ 67 static void 68 lookup_transfer_details_by_order_cb (void *cls, 69 PGresult *result, 70 unsigned int num_results) 71 { 72 struct LookupTransferDetailsByOrderContext *ltdo = cls; 73 struct TALER_WireTransferIdentifierRawP last_wtid; 74 char *last_exchange_url = NULL; 75 struct GNUNET_TIME_Timestamp last_execution_time; 76 struct TALER_Amount last_deposit_value; 77 struct TALER_Amount last_deposit_fee; 78 bool last_confirmed; 79 80 for (unsigned int i = 0; i<num_results; i++) 81 { 82 struct TALER_WireTransferIdentifierRawP wtid; 83 char *exchange_url; 84 struct GNUNET_TIME_Timestamp execution_time; 85 struct TALER_Amount deposit_value; 86 struct TALER_Amount deposit_fee; 87 bool confirmed; 88 struct GNUNET_PQ_ResultSpec rs[] = { 89 GNUNET_PQ_result_spec_timestamp ("deposit_timestamp", 90 &execution_time), 91 GNUNET_PQ_result_spec_string ("exchange_url", 92 &exchange_url), 93 GNUNET_PQ_result_spec_bool ("confirmed", 94 &confirmed), 95 GNUNET_PQ_result_spec_auto_from_type ("wtid", 96 &wtid), 97 TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value", 98 &deposit_value), 99 TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee", 100 &deposit_fee), 101 GNUNET_PQ_result_spec_end 102 }; 103 104 if (GNUNET_OK != 105 GNUNET_PQ_extract_result (result, 106 rs, 107 i)) 108 { 109 GNUNET_break (0); 110 ltdo->qs = GNUNET_DB_STATUS_HARD_ERROR; 111 return; 112 } 113 if (0 == i) 114 { 115 last_wtid = wtid; 116 last_exchange_url = exchange_url; 117 last_execution_time = execution_time; 118 last_deposit_value = deposit_value; 119 last_deposit_fee = deposit_fee; 120 last_confirmed = confirmed; 121 continue; 122 } 123 if ( (0 == 124 GNUNET_memcmp (&wtid, 125 &last_wtid)) && 126 (0 == strcmp (exchange_url, 127 last_exchange_url)) && 128 (GNUNET_TIME_timestamp_cmp (execution_time, 129 ==, 130 last_execution_time)) && 131 (last_confirmed == confirmed) && 132 (GNUNET_OK == 133 TALER_amount_cmp_currency (&deposit_value, 134 &last_deposit_value)) ) 135 { 136 GNUNET_assert (0 <= 137 TALER_amount_add (&last_deposit_value, 138 &last_deposit_value, 139 &deposit_value)); 140 GNUNET_assert (0 <= 141 TALER_amount_add (&last_deposit_fee, 142 &last_deposit_fee, 143 &deposit_fee)); 144 continue; 145 } 146 ltdo->cb (ltdo->cb_cls, 147 &last_wtid, 148 last_exchange_url, 149 last_execution_time, 150 &last_deposit_value, 151 &last_deposit_fee, 152 last_confirmed); 153 GNUNET_free (exchange_url); 154 last_wtid = wtid; 155 last_exchange_url = exchange_url; 156 last_execution_time = execution_time; 157 last_deposit_value = deposit_value; 158 last_deposit_fee = deposit_fee; 159 last_confirmed = confirmed; 160 } 161 if (num_results > 0) 162 { 163 ltdo->cb (ltdo->cb_cls, 164 &last_wtid, 165 last_exchange_url, 166 last_execution_time, 167 &last_deposit_value, 168 &last_deposit_fee, 169 last_confirmed); 170 } 171 GNUNET_free (last_exchange_url); 172 ltdo->qs = num_results; 173 } 174 175 176 enum GNUNET_DB_QueryStatus 177 TMH_PG_lookup_transfer_details_by_order ( 178 void *cls, 179 uint64_t order_serial, 180 TALER_MERCHANTDB_OrderTransferDetailsCallback cb, 181 void *cb_cls) 182 { 183 struct PostgresClosure *pg = cls; 184 struct LookupTransferDetailsByOrderContext ltdo = { 185 .pg = pg, 186 .cb = cb, 187 .cb_cls = cb_cls 188 }; 189 struct GNUNET_PQ_QueryParam params[] = { 190 GNUNET_PQ_query_param_uint64 (&order_serial), 191 GNUNET_PQ_query_param_end 192 }; 193 enum GNUNET_DB_QueryStatus qs; 194 195 check_connection (pg); 196 PREPARE (pg, 197 "lookup_transfer_details_by_order", 198 "SELECT" 199 " md.deposit_serial" 200 ",mcon.exchange_url" 201 ",met.wtid" 202 ",mtc.exchange_deposit_value" 203 ",mtc.exchange_deposit_fee" 204 ",mcon.deposit_timestamp" 205 ",met.confirmed" 206 " FROM merchant_expected_transfer_to_coin mtc" 207 " JOIN merchant_deposits md" 208 " USING (deposit_serial)" 209 " JOIN merchant_deposit_confirmations mcon" 210 " USING (deposit_confirmation_serial)" 211 " JOIN merchant_expected_transfers met" 212 " USING (expected_credit_serial)" 213 " JOIN merchant_accounts acc" 214 " ON (acc.account_serial = met.account_serial)" 215 /* Check that all this is for the same instance */ 216 " JOIN merchant_contract_terms contracts" 217 " USING (merchant_serial, order_serial)" 218 " WHERE mcon.order_serial=$1" 219 " ORDER BY met.wtid"); 220 221 qs = GNUNET_PQ_eval_prepared_multi_select ( 222 pg->conn, 223 "lookup_transfer_details_by_order", 224 params, 225 &lookup_transfer_details_by_order_cb, 226 <do); 227 if (qs < 0) 228 return qs; 229 return ltdo.qs; 230 }