summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_history.c
blob: 967aa04bb1104ae6208a23904f235495143e972b (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
  This file is part of TALER
  (C) 2014, 2015, 2016 INRIA

  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_history.c
 * @brief HTTP serving layer mainly intended to communicate with the frontend
 * @author Marcello Stanisci
 */
#include "platform.h"
#include <jansson.h>
#include <taler/taler_signatures.h>
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd.h"
#include "taler-merchant-httpd_responses.h"

/**
 * Index to the first row to return in response to /history.
 */
static int start = -1;

/**
 * How many rows we are to return in response to /history.
 */
static unsigned int delta;

/**
 * Function called with information about a transaction.
 *
 * @param cls closure
 * @param order_id transaction's order ID.
 * @param row_id serial numer of the transaction in the table,
 * used as index by the frontend to skip previous results.
 */
static void
pd_cb (void *cls,
       const char *order_id,
       uint64_t row_id,
       const json_t *proposal_data)
{
  json_t *response = cls;
  json_t *entry;
  json_t *amount;
  json_t *timestamp;
  json_t *instance;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "/history's row_id: %llu\n",
              row_id);

  GNUNET_assert (-1 != json_unpack ((json_t *) proposal_data,
                                    "{s:o, s:o, s:{s:o}}",
                                    "amount", &amount,
                                    "timestamp", &timestamp,
                                    "merchant", "instance", &instance));

  GNUNET_break (NULL != (entry = json_pack ("{s:I, s:s, s:O, s:O, s:O}",
                                            "row_id", row_id,
                                            "order_id", order_id,
                                            "amount", amount,
                                            "timestamp", timestamp,
                                            "instance", instance)));

  GNUNET_break (0 == json_array_append_new (response,
                                            entry));
}


/**
 * Manage a /history request. Query the db and returns transactions
 * younger than the date given as parameter
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] connection_cls the connection's closure (can be updated)
 * @param upload_data upload data
 * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
 * @return MHD result code
 */
int
MH_handler_history (struct TMH_RequestHandler *rh,
                    struct MHD_Connection *connection,
                    void **connection_cls,
                    const char *upload_data,
                    size_t *upload_data_size)
{
  #define LOG_INFO(...) GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__)
  const char *str;
  struct GNUNET_TIME_Absolute date;
  json_t *response;
  unsigned int ret;
  unsigned long long seconds;
  struct MerchantInstance *mi;

  response = json_array (); /*FIXME who decrefs this?*/
  str = MHD_lookup_connection_value (connection,
                                     MHD_GET_ARGUMENT_KIND,
                                     "date");

  date = GNUNET_TIME_absolute_get ();

  if (NULL != str)
  {
    if (1 != sscanf (str, "%llu", &seconds))
    {
      json_decref (response);
      return TMH_RESPONSE_reply_arg_invalid (connection,
                                             TALER_EC_PARAMETER_MALFORMED,
                                             "date");
    }

  date.abs_value_us = seconds * 1000LL * 1000LL;

  if (date.abs_value_us / 1000LL / 1000LL != seconds)
  {
    json_decref (response);
    return TMH_RESPONSE_reply_bad_request (connection,
                                           TALER_EC_HISTORY_TIMESTAMP_OVERFLOW,
                                           "Timestamp overflowed");
  }


  }

  mi = TMH_lookup_instance ("default");
  str = MHD_lookup_connection_value (connection,
                                     MHD_GET_ARGUMENT_KIND,
                                     "instance");
  if (NULL != str)
    mi = TMH_lookup_instance (str);

  if (NULL == mi)
  {
    json_decref (response);
    return TMH_RESPONSE_reply_not_found (connection,
                                         TALER_EC_HISTORY_INSTANCE_UNKNOWN,
                                         "instance");
  }

  delta = 20;

  str = MHD_lookup_connection_value (connection,
                                     MHD_GET_ARGUMENT_KIND,
                                     "start");
  if (NULL != str)
  {
    if ((1 != sscanf (str, "%d", &start)) ||
        start < 0)
    {
      json_decref (response);
      return TMH_RESPONSE_reply_arg_invalid (connection,
                                             TALER_EC_PARAMETER_MALFORMED,
                                             "start");
    }
  }

  str = MHD_lookup_connection_value (connection,
                                     MHD_GET_ARGUMENT_KIND,
                                     "delta");

  if (NULL != str)
  {
    if ((1 != sscanf (str, "%d", &delta)) ||
        delta < 0)
      return TMH_RESPONSE_reply_arg_invalid (connection,
                                             TALER_EC_PARAMETER_MALFORMED,
                                             "delta");
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Querying history back to %s\n",
              GNUNET_STRINGS_absolute_time_to_string (date));

  if (0 > start)
    ret = db->find_proposal_data_by_date (db->cls,
                                          date,
                                          &mi->pubkey,
                                          delta,
                                          pd_cb,
                                          response);
  else
    ret = db->find_proposal_data_by_date_and_range (db->cls,
                                                    date,
                                                    &mi->pubkey,
                                                    start,
                                                    delta,
                                                    pd_cb,
                                                    response);
  if (GNUNET_SYSERR == ret)
  {
    json_decref (response);
    return TMH_RESPONSE_reply_internal_error (connection,
					      TALER_EC_HISTORY_DB_FETCH_ERROR,
					      "db error to get history");
  }
  ret = TMH_RESPONSE_reply_json (connection,
                                 response,
                                 MHD_HTTP_OK);
  json_decref (response);
  return ret;
}

/* end of taler-merchant-httpd_history.c */