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/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/taler_pq_lib.h"
22 : #include "exchange-database/select_deposit_amounts_for_kyc_check.h"
23 : #include "helper.h"
24 :
25 : /**
26 : * Closure for #get_kyc_amounts_cb().
27 : */
28 : struct KycAmountCheckContext
29 : {
30 : /**
31 : * Function to call per result.
32 : */
33 : TALER_KYCLOGIC_KycAmountCallback 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 KycAmountCheckContext *`
56 : * @param result SQL result
57 : * @param num_results number of rows in @a result
58 : */
59 : static void
60 0 : get_kyc_amounts_cb (void *cls,
61 : PGresult *result,
62 : unsigned int num_results)
63 : {
64 0 : struct KycAmountCheckContext *ctx = cls;
65 0 : struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
66 :
67 0 : for (unsigned int i = 0; i < num_results; i++)
68 : {
69 : struct GNUNET_TIME_Absolute date;
70 : struct TALER_Amount amount;
71 0 : struct GNUNET_PQ_ResultSpec rs[] = {
72 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
73 : &amount),
74 0 : GNUNET_PQ_result_spec_absolute_time ("date",
75 : &date),
76 : GNUNET_PQ_result_spec_end
77 : };
78 : enum GNUNET_GenericReturnValue ret;
79 :
80 0 : if (GNUNET_OK !=
81 0 : GNUNET_PQ_extract_result (result,
82 : rs,
83 : i))
84 : {
85 0 : GNUNET_break (0);
86 0 : ctx->status = GNUNET_SYSERR;
87 0 : return;
88 : }
89 0 : ret = ctx->cb (ctx->cb_cls,
90 : &amount,
91 : date);
92 0 : GNUNET_PQ_cleanup_result (rs);
93 0 : switch (ret)
94 : {
95 0 : case GNUNET_OK:
96 0 : continue;
97 0 : case GNUNET_NO:
98 0 : break;
99 0 : case GNUNET_SYSERR:
100 0 : ctx->status = GNUNET_SYSERR;
101 0 : break;
102 : }
103 0 : break;
104 : }
105 : }
106 :
107 :
108 : enum GNUNET_DB_QueryStatus
109 0 : TALER_EXCHANGEDB_select_deposit_amounts_for_kyc_check (
110 : struct TALER_EXCHANGEDB_PostgresContext *pg,
111 : const struct TALER_NormalizedPaytoHashP *h_payto,
112 : struct GNUNET_TIME_Absolute time_limit,
113 : TALER_KYCLOGIC_KycAmountCallback kac,
114 : void *kac_cls)
115 : {
116 0 : struct GNUNET_PQ_QueryParam params[] = {
117 0 : GNUNET_PQ_query_param_auto_from_type (h_payto),
118 0 : GNUNET_PQ_query_param_absolute_time (&time_limit),
119 : GNUNET_PQ_query_param_end
120 : };
121 0 : struct KycAmountCheckContext ctx = {
122 : .cb = kac,
123 : .cb_cls = kac_cls,
124 : .pg = pg,
125 : .status = GNUNET_OK
126 : };
127 : enum GNUNET_DB_QueryStatus qs;
128 :
129 0 : PREPARE (pg,
130 : "select_kyc_relevant_deposit_events",
131 : "SELECT"
132 : " cd.amount_with_fee AS amount"
133 : ",bd.exchange_timestamp AS date"
134 : " FROM batch_deposits bd"
135 : " JOIN coin_deposits cd"
136 : " USING (batch_deposit_serial_id)"
137 : " WHERE wire_target_h_payto IN ("
138 : " SELECT wire_target_h_payto"
139 : " FROM wire_targets"
140 : " WHERE h_normalized_payto=$1"
141 : " )"
142 : " AND bd.exchange_timestamp >= $2"
143 : " ORDER BY bd.exchange_timestamp DESC");
144 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
145 : pg->conn,
146 : "select_kyc_relevant_deposit_events",
147 : params,
148 : &get_kyc_amounts_cb,
149 : &ctx);
150 0 : if (GNUNET_OK != ctx.status)
151 0 : return GNUNET_DB_STATUS_HARD_ERROR;
152 0 : return qs;
153 : }
|