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