Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 : #include "taler/platform.h"
17 : #include <gnunet/gnunet_util_lib.h>
18 : #include <gnunet/gnunet_json_lib.h>
19 : #include <jansson.h>
20 : #include <microhttpd.h>
21 : #include <pthread.h>
22 : #include "taler/taler_json_lib.h"
23 : #include "taler/taler_mhd_lib.h"
24 : #include "taler-auditor-httpd.h"
25 : #include "taler-auditor-httpd_early-aggregation-get.h"
26 :
27 :
28 : /**
29 : * Add early aggregation record to the list.
30 : *
31 : * @param[in,out] cls a `json_t *` array to extend
32 : * @param ea early aggregation record
33 : * @return #GNUNET_OK
34 : */
35 : static enum GNUNET_GenericReturnValue
36 0 : add_early_aggregation (
37 : void *cls,
38 : const struct TALER_AUDITORDB_EarlyAggregation *ea)
39 : {
40 0 : json_t *list = cls;
41 : json_t *obj;
42 :
43 0 : obj = GNUNET_JSON_PACK (
44 : GNUNET_JSON_pack_uint64 ("row_id",
45 : ea->row_id),
46 : GNUNET_JSON_pack_uint64 ("batch_deposit_serial_id",
47 : ea->batch_deposit_serial_id),
48 : GNUNET_JSON_pack_uint64 ("tracking_serial_id",
49 : ea->tracking_serial_id),
50 : TALER_JSON_pack_amount ("balance",
51 : &ea->total),
52 : GNUNET_JSON_pack_bool ("suppressed",
53 : ea->suppressed)
54 : );
55 0 : GNUNET_break (0 ==
56 : json_array_append_new (list,
57 : obj));
58 0 : return GNUNET_OK;
59 : }
60 :
61 :
62 : MHD_RESULT
63 0 : TAH_early_aggregation_handler_get (
64 : struct TAH_RequestHandler *rh,
65 : struct MHD_Connection *connection,
66 : void **connection_cls,
67 : const char *upload_data,
68 : size_t *upload_data_size,
69 : const char *const args[])
70 : {
71 : json_t *ja;
72 : enum GNUNET_DB_QueryStatus qs;
73 0 : int64_t limit = -20;
74 : uint64_t offset;
75 0 : bool return_suppressed = false;
76 :
77 0 : if (GNUNET_SYSERR ==
78 0 : TAH_plugin->preflight (TAH_plugin->cls))
79 : {
80 0 : GNUNET_break (0);
81 0 : return TALER_MHD_reply_with_error (connection,
82 : MHD_HTTP_INTERNAL_SERVER_ERROR,
83 : TALER_EC_GENERIC_DB_SETUP_FAILED,
84 : NULL);
85 : }
86 0 : TALER_MHD_parse_request_snumber (connection,
87 : "limit",
88 : &limit);
89 0 : if (limit < 0)
90 0 : offset = INT64_MAX;
91 : else
92 0 : offset = 0;
93 0 : TALER_MHD_parse_request_number (connection,
94 : "offset",
95 : &offset);
96 : {
97 0 : const char *ret_s = MHD_lookup_connection_value (connection,
98 : MHD_GET_ARGUMENT_KIND,
99 : "return_suppressed");
100 0 : if ( (NULL != ret_s) &&
101 0 : (0 == strcmp (ret_s,
102 : "true")) )
103 : {
104 0 : return_suppressed = true;
105 : }
106 : }
107 0 : ja = json_array ();
108 0 : GNUNET_break (NULL != ja);
109 0 : qs = TAH_plugin->select_early_aggregations (
110 0 : TAH_plugin->cls,
111 : limit,
112 : offset,
113 : return_suppressed,
114 : &add_early_aggregation,
115 : ja);
116 0 : if (0 > qs)
117 : {
118 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
119 0 : json_decref (ja);
120 0 : TALER_LOG_WARNING (
121 : "Failed to handle GET /early-aggregation in database\n");
122 0 : return TALER_MHD_reply_with_error (connection,
123 : MHD_HTTP_INTERNAL_SERVER_ERROR,
124 : TALER_EC_GENERIC_DB_FETCH_FAILED,
125 : "select_early_aggregations");
126 : }
127 0 : return TALER_MHD_REPLY_JSON_PACK (
128 : connection,
129 : MHD_HTTP_OK,
130 : GNUNET_JSON_pack_array_steal ("early_aggregations",
131 : ja));
132 : }
|