Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-2025 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_get_kyc_rules.c
18 : * @brief Implementation of the get_kyc_rules function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler_error_codes.h"
23 : #include "taler_dbevents.h"
24 : #include "taler_pq_lib.h"
25 : #include "pg_get_kyc_rules.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : enum GNUNET_DB_QueryStatus
30 144 : TEH_PG_get_kyc_rules (
31 : void *cls,
32 : const struct TALER_NormalizedPaytoHashP *h_payto,
33 : bool *no_account_pub,
34 : union TALER_AccountPublicKeyP *account_pub,
35 : bool *no_reserve_pub,
36 : struct TALER_ReservePublicKeyP *reserve_pub,
37 : json_t **jrules)
38 : {
39 144 : struct PostgresClosure *pg = cls;
40 : struct GNUNET_TIME_Timestamp now
41 144 : = GNUNET_TIME_timestamp_get ();
42 144 : struct GNUNET_PQ_QueryParam params[] = {
43 144 : GNUNET_PQ_query_param_auto_from_type (h_payto),
44 144 : GNUNET_PQ_query_param_timestamp (&now),
45 : GNUNET_PQ_query_param_end
46 : };
47 144 : struct GNUNET_PQ_ResultSpec rs[] = {
48 144 : GNUNET_PQ_result_spec_allow_null (
49 : GNUNET_PQ_result_spec_auto_from_type ("target_pub",
50 : account_pub),
51 : no_account_pub),
52 144 : GNUNET_PQ_result_spec_allow_null (
53 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
54 : reserve_pub),
55 : no_reserve_pub),
56 144 : GNUNET_PQ_result_spec_allow_null (
57 : TALER_PQ_result_spec_json ("jnew_rules",
58 : jrules),
59 : NULL),
60 : GNUNET_PQ_result_spec_end
61 : };
62 :
63 144 : *jrules = NULL;
64 144 : *no_account_pub = true;
65 144 : *no_reserve_pub = true;
66 144 : memset (account_pub,
67 : 0,
68 : sizeof (*account_pub));
69 144 : memset (reserve_pub,
70 : 0,
71 : sizeof (*reserve_pub));
72 144 : PREPARE (pg,
73 : "get_kyc_rules",
74 : "SELECT"
75 : " kt.target_pub"
76 : " ,lo.jnew_rules"
77 : " ,ri.reserve_pub"
78 : " FROM kyc_targets kt"
79 : /* This may result in multiple matches */
80 : " JOIN wire_targets wt"
81 : " USING (h_normalized_payto)"
82 : /* zero or more matches, reserve_pub will be NULL if no match */
83 : " LEFT JOIN reserves_in ri"
84 : " ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
85 : /* zero or more matches, jnew_rules will be NULL if no match */
86 : " LEFT JOIN legitimization_outcomes lo"
87 : " ON (lo.h_payto = kt.h_normalized_payto)"
88 : " WHERE kt.h_normalized_payto=$1"
89 : " AND COALESCE(lo.expiration_time >= $2, TRUE)"
90 : " AND COALESCE(lo.is_active, TRUE)"
91 : /* If multiple reserves_in match, we pick the latest one */
92 : " ORDER BY ri.execution_date DESC"
93 : " LIMIT 1;");
94 144 : return GNUNET_PQ_eval_prepared_singleton_select (
95 : pg->conn,
96 : "get_kyc_rules",
97 : params,
98 : rs);
99 : }
100 :
101 :
102 : enum GNUNET_DB_QueryStatus
103 0 : TEH_PG_get_kyc_rules2 (
104 : void *cls,
105 : const struct TALER_NormalizedPaytoHashP *h_payto,
106 : json_t **jrules)
107 : {
108 0 : struct PostgresClosure *pg = cls;
109 : struct GNUNET_TIME_Timestamp now
110 0 : = GNUNET_TIME_timestamp_get ();
111 0 : struct GNUNET_PQ_QueryParam params[] = {
112 0 : GNUNET_PQ_query_param_auto_from_type (h_payto),
113 0 : GNUNET_PQ_query_param_timestamp (&now),
114 : GNUNET_PQ_query_param_end
115 : };
116 0 : struct GNUNET_PQ_ResultSpec rs[] = {
117 0 : GNUNET_PQ_result_spec_allow_null (
118 : TALER_PQ_result_spec_json ("jnew_rules",
119 : jrules),
120 : NULL),
121 : GNUNET_PQ_result_spec_end
122 : };
123 :
124 0 : *jrules = NULL;
125 0 : PREPARE (pg,
126 : "get_kyc_rules2",
127 : "SELECT"
128 : " jnew_rules"
129 : " FROM legitimization_outcomes"
130 : " WHERE h_payto=$1"
131 : " AND expiration_time >= $2"
132 : " AND is_active"
133 : " ORDER BY expiration_time DESC"
134 : " LIMIT 1;");
135 0 : return GNUNET_PQ_eval_prepared_singleton_select (
136 : pg->conn,
137 : "get_kyc_rules2",
138 : params,
139 : rs);
140 : }
|