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 Affero 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 src/backend/taler-merchant-httpd_get-private-donau.c
18 : * @brief implementation of GET /donau
19 : * @author Bohdan Potuzhnyi
20 : * @author Vlada Svirsh
21 : */
22 : #include "platform.h"
23 : #include <jansson.h>
24 : #include <taler/taler_json_lib.h>
25 : #include <taler/taler_dbevents.h>
26 : #include "taler/taler_merchant_service.h"
27 : #include "taler-merchant-httpd_get-private-donau.h"
28 : #include "merchant-database/iterate_donau_instances.h"
29 : #include "merchant-database/preflight.h"
30 :
31 :
32 : /**
33 : * Add details about a Donau instance to the JSON array.
34 : *
35 : * @param cls json array to which the Donau instance details will be added
36 : * @param donau_instance_serial the serial number of the Donau instance
37 : * @param donau_url the URL of the Donau instance
38 : * @param charity_name the name of the charity
39 : * @param charity_pub_key the public key of the charity
40 : * @param charity_id the ID of the charity
41 : * @param charity_max_per_year the maximum donation amount per year
42 : * @param charity_receipts_to_date the total donations received so far this year
43 : * @param current_year the current year being tracked for donations
44 : * @param donau_keys_json JSON object with key information specific to the Donau instance, NULL if not (yet) available.
45 : */
46 : static void
47 4 : add_donau_instance (void *cls,
48 : uint64_t donau_instance_serial,
49 : const char *donau_url,
50 : const char *charity_name,
51 : const struct DONAU_CharityPublicKeyP *charity_pub_key,
52 : uint64_t charity_id,
53 : const struct TALER_Amount *charity_max_per_year,
54 : const struct TALER_Amount *charity_receipts_to_date,
55 : int64_t current_year,
56 : const json_t *donau_keys_json)
57 : {
58 4 : json_t *json_instances = cls;
59 :
60 4 : GNUNET_assert (
61 : 0 == json_array_append_new (
62 : json_instances,
63 : GNUNET_JSON_PACK (
64 : GNUNET_JSON_pack_uint64 ("donau_instance_serial",
65 : donau_instance_serial),
66 : GNUNET_JSON_pack_string ("donau_url",
67 : donau_url),
68 : GNUNET_JSON_pack_string ("charity_name",
69 : charity_name),
70 : GNUNET_JSON_pack_data_auto ("charity_pub_key",
71 : charity_pub_key),
72 : GNUNET_JSON_pack_uint64 ("charity_id",
73 : charity_id),
74 : TALER_JSON_pack_amount ("charity_max_per_year",
75 : charity_max_per_year),
76 : TALER_JSON_pack_amount ("charity_receipts_to_date",
77 : charity_receipts_to_date),
78 : GNUNET_JSON_pack_int64 ("current_year",
79 : current_year),
80 : GNUNET_JSON_pack_allow_null (
81 : GNUNET_JSON_pack_object_incref ("donau_keys_json",
82 : (json_t *) donau_keys_json))
83 : )));
84 4 : }
85 :
86 :
87 : /**
88 : * Handle a GET "/donau" request.
89 : *
90 : * @param rh context of the handler
91 : * @param connection the MHD connection to handle
92 : * @param[in,out] hc context with further information about the request
93 : * @return MHD result code
94 : */
95 : enum MHD_Result
96 6 : TMH_private_get_donau_instances (const struct TMH_RequestHandler *rh,
97 : struct MHD_Connection *connection,
98 : struct TMH_HandlerContext *hc)
99 : {
100 6 : json_t *json_donau_instances = json_array ();
101 : enum GNUNET_DB_QueryStatus qs;
102 :
103 6 : TALER_MERCHANTDB_preflight (TMH_db);
104 6 : qs = TALER_MERCHANTDB_iterate_donau_instances (TMH_db,
105 6 : hc->instance->settings.id,
106 : &add_donau_instance,
107 : json_donau_instances);
108 6 : if (0 > qs)
109 : {
110 0 : GNUNET_break (0);
111 0 : json_decref (json_donau_instances);
112 0 : return TALER_MHD_reply_with_error (connection,
113 : MHD_HTTP_INTERNAL_SERVER_ERROR,
114 : TALER_EC_GENERIC_DB_FETCH_FAILED,
115 : NULL);
116 : }
117 :
118 6 : return TALER_MHD_REPLY_JSON_PACK (connection,
119 : MHD_HTTP_OK,
120 : GNUNET_JSON_pack_array_steal (
121 : "donau_instances",
122 : json_donau_instances));
123 : }
|