Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 2024 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/pg_select_withdraw_amounts_for_kyc_check.c
18 : * @brief Implementation of the select_withdraw_amounts_for_kyc_check function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/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_select_withdraw_amounts_for_kyc_check.h"
26 : #include "pg_select_aggregation_amounts_for_kyc_check.h"
27 : #include "pg_helper.h"
28 :
29 :
30 : /**
31 : * Closure for #get_kyc_amounts_cb().
32 : */
33 : struct KycAmountCheckContext
34 : {
35 : /**
36 : * Function to call per result.
37 : */
38 : TALER_EXCHANGEDB_KycAmountCallback cb;
39 :
40 : /**
41 : * Closure for @e cb.
42 : */
43 : void *cb_cls;
44 :
45 : /**
46 : * Plugin context.
47 : */
48 : struct PostgresClosure *pg;
49 :
50 : /**
51 : * Flag set to #GNUNET_OK as long as everything is fine.
52 : */
53 : enum GNUNET_GenericReturnValue status;
54 :
55 : };
56 :
57 : /**
58 : * Invoke the callback for each result.
59 : *
60 : * @param cls a `struct KycAmountCheckContext *`
61 : * @param result SQL result
62 : * @param num_results number of rows in @a result
63 : */
64 : static void
65 19 : get_kyc_amounts_cb (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 19 : struct KycAmountCheckContext *ctx = cls;
70 19 : struct PostgresClosure *pg = ctx->pg;
71 :
72 43 : for (unsigned int i = 0; i < num_results; i++)
73 : {
74 : struct GNUNET_TIME_Absolute date;
75 : struct TALER_Amount amount;
76 24 : struct GNUNET_PQ_ResultSpec rs[] = {
77 24 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
78 : &amount),
79 24 : GNUNET_PQ_result_spec_absolute_time ("date",
80 : &date),
81 : GNUNET_PQ_result_spec_end
82 : };
83 : enum GNUNET_GenericReturnValue ret;
84 :
85 24 : if (GNUNET_OK !=
86 24 : GNUNET_PQ_extract_result (result,
87 : rs,
88 : i))
89 : {
90 0 : GNUNET_break (0);
91 0 : ctx->status = GNUNET_SYSERR;
92 0 : return;
93 : }
94 24 : ret = ctx->cb (ctx->cb_cls,
95 : &amount,
96 : date);
97 24 : GNUNET_PQ_cleanup_result (rs);
98 24 : switch (ret)
99 : {
100 24 : case GNUNET_OK:
101 24 : continue;
102 0 : case GNUNET_NO:
103 0 : break;
104 0 : case GNUNET_SYSERR:
105 0 : ctx->status = GNUNET_SYSERR;
106 0 : break;
107 : }
108 0 : break;
109 : }
110 : }
111 :
112 :
113 : enum GNUNET_DB_QueryStatus
114 19 : TEH_PG_select_withdraw_amounts_for_kyc_check (
115 : void *cls,
116 : const struct TALER_NormalizedPaytoHashP *h_payto,
117 : struct GNUNET_TIME_Absolute time_limit,
118 : TALER_EXCHANGEDB_KycAmountCallback kac,
119 : void *kac_cls)
120 : {
121 19 : struct PostgresClosure *pg = cls;
122 19 : struct GNUNET_PQ_QueryParam params[] = {
123 19 : GNUNET_PQ_query_param_auto_from_type (h_payto),
124 19 : GNUNET_PQ_query_param_absolute_time (&time_limit),
125 : GNUNET_PQ_query_param_end
126 : };
127 19 : struct KycAmountCheckContext ctx = {
128 : .cb = kac,
129 : .cb_cls = kac_cls,
130 : .pg = pg,
131 : .status = GNUNET_OK
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 19 : PREPARE (pg,
136 : "select_withdraw_amounts_for_kyc_check",
137 : "SELECT"
138 : " wd.amount_with_fee AS amount"
139 : ",wd.execution_date AS date"
140 : " FROM reserves_in ri"
141 : " JOIN reserve_history rh"
142 : " ON (rh.reserve_pub = ri.reserve_pub)"
143 : " JOIN withdraw wd"
144 : " ON (wd.withdraw_id = rh.serial_id)"
145 : " WHERE ri.wire_source_h_payto IN ("
146 : " SELECT wire_target_h_payto"
147 : " FROM wire_targets"
148 : " WHERE h_normalized_payto=$1"
149 : " )"
150 : " AND rh.table_name='withdraw'"
151 : " AND wd.execution_date >= $2"
152 : " ORDER BY rh.reserve_history_serial_id DESC");
153 19 : qs = GNUNET_PQ_eval_prepared_multi_select (
154 : pg->conn,
155 : "select_withdraw_amounts_for_kyc_check",
156 : params,
157 : &get_kyc_amounts_cb,
158 : &ctx);
159 19 : if (GNUNET_OK != ctx.status)
160 0 : return GNUNET_DB_STATUS_HARD_ERROR;
161 19 : return qs;
162 : }
|