summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_private-get-reserves.c
blob: 780f88c888085bf654562d2524b22a65029be8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
  This file is part of TALER
  (C) 2019, 2020 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file backend/taler-merchant-httpd_private-get-reserves.c
 * @brief implement GET /reserves
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd_private-get-reserves.h"


/**
 * Add reserve details to our JSON array.
 *
 * @param cls a `json_t *` JSON array to build
 * @param reserve_pub public key of the reserve
 * @param creation_time time when the reserve was setup
 * @param expiration_time time when the reserve will be closed by the exchange
 * @param merchant_initial_amount initial amount that the merchant claims to have filled the
 *           reserve with
 * @param exchange_initial_amount initial amount that the exchange claims to have received
 * @param pickup_amount total of tips that were picked up from this reserve
 * @param committed_amount total of tips that the merchant committed to, but that were not
 *           picked up yet
 * @param active  true if the reserve is still active (we have the private key)
 */
static void
add_reserve (void *cls,
             const struct TALER_ReservePublicKeyP *reserve_pub,
             struct GNUNET_TIME_Absolute creation_time,
             struct GNUNET_TIME_Absolute expiration_time,
             const struct TALER_Amount *merchant_initial_amount,
             const struct TALER_Amount *exchange_initial_amount,
             const struct TALER_Amount *pickup_amount,
             const struct TALER_Amount *committed_amount,
             bool active)
{
  json_t *pa = cls;

  GNUNET_assert (0 ==
                 json_array_append_new (
                   pa,
                   json_pack (
                     "{s:o,s:o,s:o,"
                     " s:o,s:o,s:o,s:o,"
                     " s:b}",
                     "reserve_pub",
                     GNUNET_JSON_from_data_auto (reserve_pub),
                     "creation_time",
                     GNUNET_JSON_from_time_abs (creation_time),
                     "expiration_time",
                     GNUNET_JSON_from_time_abs (expiration_time),
                     "merchant_initial_amount",
                     TALER_JSON_from_amount (merchant_initial_amount),
                     "exchange_initial_amount",
                     TALER_JSON_from_amount (exchange_initial_amount),
                     "pickup_amount",
                     TALER_JSON_from_amount (pickup_amount),
                     "commited_amount",
                     TALER_JSON_from_amount (committed_amount),
                     "active",
                     active)));
}


/**
 * Handle a GET "/reserves" request.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] hc context with further information about the request
 * @return MHD result code
 */
MHD_RESULT
TMH_private_get_reserves (const struct TMH_RequestHandler *rh,
                          struct MHD_Connection *connection,
                          struct TMH_HandlerContext *hc)
{
  json_t *ra;
  enum GNUNET_DB_QueryStatus qs;
  struct GNUNET_TIME_Absolute created_after = { 0 };
  enum TALER_MERCHANTDB_YesNoAll active;
  enum TALER_MERCHANTDB_YesNoAll failures;

  {
    const char *active_s;

    active_s = MHD_lookup_connection_value (connection,
                                            MHD_GET_ARGUMENT_KIND,
                                            "active");
    if (NULL == active_s)
    {
      active = TALER_MERCHANTDB_YNA_ALL;
    }
    else if (0 == strcasecmp (active_s,
                              "yes"))
    {
      active = TALER_MERCHANTDB_YNA_YES;
    }
    else if (0 == strcasecmp (active_s,
                              "no"))
    {
      active = TALER_MERCHANTDB_YNA_NO;
    }
    else if (0 == strcasecmp (active_s,
                              "all"))
    {
      active = TALER_MERCHANTDB_YNA_ALL;
    }
    else
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_PARAMETER_MALFORMED,
                                         "active");
  }

  {
    const char *failures_s;

    failures_s = MHD_lookup_connection_value (connection,
                                              MHD_GET_ARGUMENT_KIND,
                                              "failures");
    if (NULL == failures_s)
    {
      failures = TALER_MERCHANTDB_YNA_ALL;
    }
    else if (0 == strcasecmp (failures_s,
                              "yes"))
    {
      failures = TALER_MERCHANTDB_YNA_YES;
    }
    else if (0 == strcasecmp (failures_s,
                              "no"))
    {
      failures = TALER_MERCHANTDB_YNA_NO;
    }
    else if (0 == strcasecmp (failures_s,
                              "all"))
    {
      failures = TALER_MERCHANTDB_YNA_ALL;
    }
    else
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_PARAMETER_MALFORMED,
                                         "failures");
  }

  ra = json_array ();
  GNUNET_assert (NULL != ra);
  qs = TMH_db->lookup_reserves (TMH_db->cls,
                                hc->instance->settings.id,
                                created_after,
                                active,
                                failures,
                                &add_reserve,
                                ra);
  if (0 > qs)
  {
    GNUNET_break (0);
    json_decref (ra);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_GET_RESERVES_DB_LOOKUP_ERROR,
                                       "failed to lookup reserves in database");
  }
  return TALER_MHD_reply_json_pack (connection,
                                    MHD_HTTP_OK,
                                    "{s:o}",
                                    "reserves", ra);
}


/* end of taler-merchant-httpd_private-get-reserves.c */