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_aggregation_amounts_for_kyc_check.c
18 : * @brief Implementation of the select_aggregation_amounts_for_kyc_check function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_aggregation_amounts_for_kyc_check.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #get_kyc_amounts_cb().
28 : */
29 : struct KycAmountCheckContext
30 : {
31 : /**
32 : * Function to call per result.
33 : */
34 : TALER_KYCLOGIC_KycAmountCallback cb;
35 :
36 : /**
37 : * Closure for @e cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_EXCHANGEDB_PostgresContext *pg;
45 :
46 : /**
47 : * Flag set to #GNUNET_OK as long as everything is fine.
48 : */
49 : enum GNUNET_GenericReturnValue status;
50 :
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 2 : get_kyc_amounts_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 2 : struct KycAmountCheckContext *ctx = cls;
67 2 : struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
68 :
69 2 : for (unsigned int i = 0; i < num_results; i++)
70 : {
71 : struct GNUNET_TIME_Absolute date;
72 : struct TALER_Amount amount;
73 0 : struct GNUNET_PQ_ResultSpec rs[] = {
74 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
75 : &amount),
76 0 : GNUNET_PQ_result_spec_absolute_time ("date",
77 : &date),
78 : GNUNET_PQ_result_spec_end
79 : };
80 : enum GNUNET_GenericReturnValue ret;
81 :
82 0 : if (GNUNET_OK !=
83 0 : 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 0 : ret = ctx->cb (ctx->cb_cls,
92 : &amount,
93 : date);
94 0 : GNUNET_PQ_cleanup_result (rs);
95 0 : switch (ret)
96 : {
97 0 : case GNUNET_OK:
98 0 : 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 2 : TALER_EXCHANGEDB_select_aggregation_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 2 : struct GNUNET_PQ_QueryParam params[] = {
119 2 : GNUNET_PQ_query_param_auto_from_type (h_payto),
120 2 : GNUNET_PQ_query_param_absolute_time (&time_limit),
121 : GNUNET_PQ_query_param_end
122 : };
123 2 : 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 2 : PREPARE (pg,
132 : "select_kyc_relevant_aggregation_events",
133 : "SELECT"
134 : " amount"
135 : ",execution_date AS date"
136 : " FROM wire_out"
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 execution_date >= $2"
143 : " ORDER BY execution_date DESC");
144 :
145 2 : qs = GNUNET_PQ_eval_prepared_multi_select (
146 : pg->conn,
147 : "select_kyc_relevant_aggregation_events",
148 : params,
149 : &get_kyc_amounts_cb,
150 : &ctx);
151 2 : if (GNUNET_OK != ctx.status)
152 0 : return GNUNET_DB_STATUS_HARD_ERROR;
153 2 : return qs;
154 : }
|