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