iterate_unscreened_kyc_attributes.c (5203B)
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_unscreened_kyc_attributes.c 18 * @brief Implementation of the iterate_unscreened_kyc_attributes function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/iterate_unscreened_kyc_attributes.h" 23 #include "helper.h" 24 25 /** 26 * Closure for #get_all_attributes_cb(). 27 */ 28 struct GetAttributesContext 29 { 30 /** 31 * Function to call per result. 32 */ 33 TALER_EXCHANGEDB_UnscreenedAttributesCallback cb; 34 35 /** 36 * Closure for @e cb. 37 */ 38 void *cb_cls; 39 40 /** 41 * Plugin context. 42 */ 43 struct TALER_EXCHANGEDB_PostgresContext *pg; 44 45 /** 46 * Flag set to #GNUNET_OK as long as everything is fine. 47 */ 48 enum GNUNET_GenericReturnValue status; 49 50 }; 51 52 /** 53 * Invoke the callback for each result. 54 * 55 * @param cls a `struct GetAttributesContext *` 56 * @param result SQL result 57 * @param num_results number of rows in @a result 58 */ 59 static void 60 get_attributes_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct GetAttributesContext *ctx = cls; 65 66 for (unsigned int i = 0; i < num_results; i++) 67 { 68 uint64_t rowid; 69 struct GNUNET_TIME_Timestamp collection_time; 70 struct GNUNET_TIME_Timestamp expiration_time; 71 struct TALER_NormalizedPaytoHashP h_payto; 72 json_t *properties = NULL; 73 size_t enc_attributes_size; 74 void *enc_attributes; 75 char *provider; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_string ("provider_name", 78 &provider), 79 GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id", 80 &rowid), 81 GNUNET_PQ_result_spec_allow_null ( 82 TALER_PQ_result_spec_json ("jproperties", 83 &properties), 84 NULL), 85 GNUNET_PQ_result_spec_timestamp ("collection_time", 86 &collection_time), 87 GNUNET_PQ_result_spec_timestamp ("expiration_time", 88 &expiration_time), 89 GNUNET_PQ_result_spec_auto_from_type ("h_payto", 90 &h_payto), 91 GNUNET_PQ_result_spec_variable_size ("encrypted_attributes", 92 &enc_attributes, 93 &enc_attributes_size), 94 GNUNET_PQ_result_spec_end 95 }; 96 97 if (GNUNET_OK != 98 GNUNET_PQ_extract_result (result, 99 rs, 100 i)) 101 { 102 GNUNET_break (0); 103 ctx->status = GNUNET_SYSERR; 104 return; 105 } 106 { 107 bool cont; 108 109 cont = ctx->cb (ctx->cb_cls, 110 rowid, 111 &h_payto, 112 provider, 113 collection_time, 114 expiration_time, 115 properties, 116 enc_attributes_size, 117 enc_attributes); 118 GNUNET_PQ_cleanup_result (rs); 119 if (! cont) 120 return; 121 } 122 } 123 } 124 125 126 enum GNUNET_DB_QueryStatus 127 TALER_EXCHANGEDB_iterate_unscreened_kyc_attributes ( 128 struct TALER_EXCHANGEDB_PostgresContext *pg, 129 TALER_EXCHANGEDB_UnscreenedAttributesCallback cb, 130 void *cb_cls) 131 { 132 struct GNUNET_PQ_QueryParam params[] = { 133 GNUNET_PQ_query_param_end 134 }; 135 struct GetAttributesContext ctx = { 136 .cb = cb, 137 .cb_cls = cb_cls, 138 .pg = pg, 139 .status = GNUNET_OK 140 }; 141 enum GNUNET_DB_QueryStatus qs; 142 143 PREPARE (pg, 144 "iterate_unscreened_kyc_attributes", 145 "SELECT " 146 " lp.provider_name" 147 ",ka.h_payto" 148 ",ka.kyc_attributes_serial_id" 149 ",lo.jproperties::TEXT" 150 ",ka.collection_time" 151 ",ka.expiration_time" 152 ",ka.encrypted_attributes" 153 " FROM kyc_attributes ka" 154 " JOIN legitimization_processes lp" 155 " ON (ka.legitimization_serial = lp.legitimization_process_serial_id)" 156 " LEFT JOIN legitimization_outcomes lo" 157 " ON (ka.h_payto = lo.h_payto)" 158 /* **IF** we joined with 'lo', the lo must be active */ 159 " WHERE COALESCE(lo.is_active,TRUE)" 160 " AND NOT ka.screened" 161 " ORDER BY ka.kyc_attributes_serial_id ASC" 162 ); 163 qs = GNUNET_PQ_eval_prepared_multi_select ( 164 pg->conn, 165 "iterate_unscreened_kyc_attributes", 166 params, 167 &get_attributes_cb, 168 &ctx); 169 if (GNUNET_OK != ctx.status) 170 return GNUNET_DB_STATUS_HARD_ERROR; 171 return qs; 172 }