iterate_kyc_accounts.c (8698B)
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 exchangedb/iterate_kyc_accounts.c 18 * @brief Implementation of the iterate_kyc_accounts function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/iterate_kyc_accounts.h" 23 #include "helper.h" 24 25 26 /** 27 * Hard upper bound on the number of records returned by a single 28 * call, regardless of the limit requested by the client. 29 */ 30 #define MAX_RECORDS 50000 31 32 33 /** 34 * Closure for #handle_aml_result. 35 */ 36 struct KycAccountResultContext 37 { 38 /** 39 * Function to call on each result. 40 */ 41 TALER_EXCHANGEDB_AmlAccountListCallback cb; 42 43 /** 44 * Closure for @e cb. 45 */ 46 void *cb_cls; 47 48 /** 49 * Plugin context. 50 */ 51 struct TALER_EXCHANGEDB_PostgresContext *pg; 52 53 /** 54 * Set to #GNUNET_SYSERR on serious errors. 55 */ 56 enum GNUNET_GenericReturnValue status; 57 }; 58 59 60 /** 61 * Function to be called with the results of a SELECT statement 62 * that has returned @a num_results results. Helper function 63 * for #TALER_EXCHANGEDB_iterate_kyc_accounts(). 64 * 65 * @param cls closure of type `struct KycAccountResultContext *` 66 * @param result the postgres result 67 * @param num_results the number of results in @a result 68 */ 69 static void 70 handle_kyc_account_cb (void *cls, 71 PGresult *result, 72 unsigned int num_results) 73 { 74 struct KycAccountResultContext *ctx = cls; 75 76 for (unsigned int i = 0; i<num_results; i++) 77 { 78 uint64_t rowid; 79 struct TALER_NormalizedPaytoHashP h_payto; 80 char *comments = NULL; 81 struct GNUNET_TIME_Timestamp open_time 82 = GNUNET_TIME_UNIT_FOREVER_TS; 83 struct GNUNET_TIME_Timestamp close_time 84 = GNUNET_TIME_UNIT_FOREVER_TS; 85 bool to_investigate; 86 bool high_risk; 87 struct TALER_FullPayto payto; 88 struct GNUNET_PQ_ResultSpec rs[] = { 89 GNUNET_PQ_result_spec_uint64 ("kyc_target_serial_id", 90 &rowid), 91 GNUNET_PQ_result_spec_auto_from_type ("h_payto", 92 &h_payto), 93 GNUNET_PQ_result_spec_allow_null ( 94 GNUNET_PQ_result_spec_string ("comments", 95 &comments), 96 NULL), 97 GNUNET_PQ_result_spec_allow_null ( 98 GNUNET_PQ_result_spec_timestamp ("open_time", 99 &open_time), 100 NULL), 101 GNUNET_PQ_result_spec_allow_null ( 102 GNUNET_PQ_result_spec_timestamp ("close_time", 103 &close_time), 104 NULL), 105 GNUNET_PQ_result_spec_bool ("to_investigate", 106 &to_investigate), 107 GNUNET_PQ_result_spec_bool ("high_risk", 108 &high_risk), 109 GNUNET_PQ_result_spec_string ("payto_uri", 110 &payto.full_payto), 111 GNUNET_PQ_result_spec_end 112 }; 113 114 if (GNUNET_OK != 115 GNUNET_PQ_extract_result (result, 116 rs, 117 i)) 118 { 119 GNUNET_break (0); 120 ctx->status = GNUNET_SYSERR; 121 return; 122 } 123 ctx->cb (ctx->cb_cls, 124 rowid, 125 &h_payto, 126 open_time, 127 close_time, 128 comments, 129 high_risk, 130 to_investigate, 131 payto); 132 GNUNET_PQ_cleanup_result (rs); 133 } 134 } 135 136 137 enum GNUNET_DB_QueryStatus 138 TALER_EXCHANGEDB_iterate_kyc_accounts ( 139 struct TALER_EXCHANGEDB_PostgresContext *pg, 140 enum TALER_EXCHANGE_YesNoAll investigation_only, 141 enum TALER_EXCHANGE_YesNoAll open_only, 142 enum TALER_EXCHANGE_YesNoAll high_risk_only, 143 uint64_t offset, 144 int64_t limit, 145 TALER_EXCHANGEDB_AmlAccountListCallback cb, 146 void *cb_cls) 147 { 148 uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 149 TALER_EXCHANGEDB_abs_limit (limit)); 150 struct GNUNET_PQ_QueryParam params[] = { 151 GNUNET_PQ_query_param_uint64 (&offset), 152 GNUNET_PQ_query_param_uint64 (&ulimit), 153 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 154 investigation_only)), 155 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == 156 investigation_only)), 157 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 158 open_only)), 159 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == 160 open_only)), 161 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 162 high_risk_only)), 163 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == 164 high_risk_only)), 165 GNUNET_PQ_query_param_end 166 }; 167 struct KycAccountResultContext ctx = { 168 .cb = cb, 169 .cb_cls = cb_cls, 170 .pg = pg, 171 .status = GNUNET_OK 172 }; 173 enum GNUNET_DB_QueryStatus qs; 174 const char *stmt = (limit > 0) 175 ? "iterate_kyc_accounts_inc" 176 : "iterate_kyc_accounts_dec"; 177 178 PREPARE (pg, 179 "iterate_kyc_accounts_inc", 180 "SELECT" 181 " kt.kyc_target_serial_id" 182 ",kt.h_normalized_payto AS h_payto" 183 ",kt.open_time" 184 ",kt.close_time" 185 ",lo.jproperties ->> 'FILE_NOTE' AS comments" 186 ",COALESCE(lo.to_investigate,FALSE) AS to_investigate" 187 ",COALESCE((lo.jproperties ->> 'HIGH_RISK_CUSTOMER')::bool,FALSE) AS high_risk" 188 ",wt.payto_uri" 189 " FROM kyc_targets kt" 190 " LEFT JOIN legitimization_outcomes lo" 191 " ON (lo.h_payto = kt.h_normalized_payto)" 192 " LEFT JOIN LATERAL (" 193 " SELECT payto_uri" 194 " FROM wire_targets" 195 " WHERE h_normalized_payto = kt.h_normalized_payto" 196 " ORDER BY wire_target_serial_id DESC" 197 " LIMIT 1" 198 " ) wt ON true" 199 " WHERE (kyc_target_serial_id > $1)" 200 // select most recent outcomes only 201 " AND COALESCE (lo.is_active, TRUE)" 202 " AND ($3 OR (COALESCE(lo.to_investigate,FALSE) = $4))" 203 // Account is open if we had an AML outcome 204 " AND ($5 OR ((lo.outcome_serial_id IS NULL) = $6))" 205 " AND ($7 OR ((COALESCE((lo.jproperties ->>'HIGH_RISK_CUSTOMER')::bool,FALSE) = $8)))" 206 " ORDER BY kt.kyc_target_serial_id ASC" 207 " LIMIT $2"); 208 PREPARE (pg, 209 "iterate_kyc_accounts_dec", 210 "SELECT" 211 " kt.kyc_target_serial_id" 212 ",kt.h_normalized_payto AS h_payto" 213 ",kt.open_time" 214 ",kt.close_time" 215 ",lo.jproperties ->> 'FILE_NOTE' AS comments" 216 ",COALESCE(lo.to_investigate,FALSE) AS to_investigate" 217 ",COALESCE((lo.jproperties ->> 'HIGH_RISK_CUSTOMER')::bool,FALSE) AS high_risk" 218 ",wt.payto_uri" 219 " FROM kyc_targets kt" 220 " LEFT JOIN legitimization_outcomes lo" 221 " ON (lo.h_payto = kt.h_normalized_payto)" 222 " LEFT JOIN LATERAL (" 223 " SELECT payto_uri" 224 " FROM wire_targets" 225 " WHERE h_normalized_payto = kt.h_normalized_payto" 226 " ORDER BY wire_target_serial_id DESC" 227 " LIMIT 1" 228 " ) wt ON true" 229 " WHERE (kyc_target_serial_id < $1)" 230 // select most recent outcomes only 231 " AND COALESCE (lo.is_active, TRUE)" 232 " AND ($3 OR (COALESCE(lo.to_investigate,FALSE) = $4))" 233 // Account is open if we had an AML outcome 234 " AND ($5 OR ((lo.outcome_serial_id IS NULL) = $6))" 235 " AND ($7 OR ((COALESCE((lo.jproperties ->>'HIGH_RISK_CUSTOMER')::bool,FALSE) = $8)))" 236 " ORDER BY kt.kyc_target_serial_id DESC" 237 " LIMIT $2"); 238 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 239 stmt, 240 params, 241 &handle_kyc_account_cb, 242 &ctx); 243 if (GNUNET_OK != ctx.status) 244 return GNUNET_DB_STATUS_HARD_ERROR; 245 return qs; 246 }