merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_post-private-reports.c (5094B)


      1 /*
      2   This file is part of TALER
      3   (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 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 Affero General Public License for more
     12   details.
     13 
     14   You should have received a copy of the GNU Affero General Public License
     15   along with TALER; see the file COPYING.  If not, see
     16   <http://www.gnu.org/licenses/>
     17 */
     18 /**
     19  * @file src/backend/taler-merchant-httpd_post-private-reports.c
     20  * @brief implementation of POST /private/reports
     21  * @author Christian Grothoff
     22  */
     23 #include "platform.h"
     24 #include "taler-merchant-httpd_post-private-reports.h"
     25 #include <taler/taler_json_lib.h>
     26 #include <taler/taler_dbevents.h>
     27 #include "merchant-database/insert_report.h"
     28 #include "merchant-database/event_notify.h"
     29 
     30 
     31 enum MHD_Result
     32 TMH_private_post_reports (const struct TMH_RequestHandler *rh,
     33                           struct MHD_Connection *connection,
     34                           struct TMH_HandlerContext *hc)
     35 {
     36   const char *description;
     37   const char *program_section;
     38   const char *mime_type;
     39   const char *data_source;
     40   const char *target_address;
     41   struct GNUNET_TIME_Relative frequency;
     42   struct GNUNET_TIME_Relative frequency_shift
     43     = GNUNET_TIME_UNIT_ZERO;
     44   enum GNUNET_DB_QueryStatus qs;
     45   struct GNUNET_JSON_Specification spec[] = {
     46     GNUNET_JSON_spec_string ("description",
     47                              &description),
     48     GNUNET_JSON_spec_string ("program_section",
     49                              &program_section),
     50     GNUNET_JSON_spec_string ("mime_type",
     51                              &mime_type),
     52     GNUNET_JSON_spec_string ("data_source",
     53                              &data_source),
     54     GNUNET_JSON_spec_string ("target_address",
     55                              &target_address),
     56     GNUNET_JSON_spec_relative_time ("report_frequency",
     57                                     &frequency),
     58     GNUNET_JSON_spec_mark_optional (
     59       GNUNET_JSON_spec_relative_time ("report_frequency_shift",
     60                                       &frequency_shift),
     61       NULL),
     62     GNUNET_JSON_spec_end ()
     63   };
     64   uint64_t report_id;
     65 
     66   (void) rh;
     67 
     68   {
     69     enum GNUNET_GenericReturnValue res;
     70 
     71     res = TALER_MHD_parse_json_data (connection,
     72                                      hc->request_body,
     73                                      spec);
     74     if (GNUNET_OK != res)
     75     {
     76       GNUNET_break_op (0);
     77       return (GNUNET_NO == res)
     78              ? MHD_YES
     79              : MHD_NO;
     80     }
     81   }
     82   {
     83     char *section;
     84 
     85     /* Check program_section exists in config! */
     86     GNUNET_asprintf (&section,
     87                      "report-generator-%s",
     88                      program_section);
     89     if (GNUNET_YES !=
     90         GNUNET_CONFIGURATION_have_value (TMH_cfg,
     91                                          section,
     92                                          "BINARY"))
     93     {
     94       GNUNET_free (section);
     95       GNUNET_break (0);
     96       return TALER_MHD_reply_with_error (
     97         connection,
     98         MHD_HTTP_NOT_IMPLEMENTED,
     99         TALER_EC_MERCHANT_GENERIC_REPORT_GENERATOR_UNCONFIGURED,
    100         program_section);
    101     }
    102     GNUNET_free (section);
    103   }
    104   if ('/' != data_source[0])
    105   {
    106     GNUNET_break_op (0);
    107     return TALER_MHD_reply_with_error (connection,
    108                                        MHD_HTTP_BAD_REQUEST,
    109                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
    110                                        "data_source");
    111 
    112   }
    113   qs = TALER_MERCHANTDB_insert_report (TMH_db,
    114                                        hc->instance->settings.id,
    115                                        program_section,
    116                                        description,
    117                                        mime_type,
    118                                        data_source,
    119                                        target_address,
    120                                        frequency,
    121                                        frequency_shift,
    122                                        &report_id);
    123   if (qs < 0)
    124   {
    125     GNUNET_break (0);
    126     return TALER_MHD_reply_with_error (connection,
    127                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    128                                        TALER_EC_GENERIC_DB_STORE_FAILED,
    129                                        "insert_report");
    130   }
    131 
    132   /* FIXME-Optimization: do trigger inside of transaction above... */
    133   {
    134     struct GNUNET_DB_EventHeaderP ev = {
    135       .size = htons (sizeof (ev)),
    136       .type = htons (TALER_DBEVENT_MERCHANT_REPORT_UPDATE)
    137     };
    138 
    139     TALER_MERCHANTDB_event_notify (TMH_db,
    140                                    &ev,
    141                                    NULL,
    142                                    0);
    143   }
    144 
    145   return TALER_MHD_REPLY_JSON_PACK (
    146     connection,
    147     MHD_HTTP_OK,
    148     GNUNET_JSON_pack_uint64 ("report_serial_id",
    149                              report_id));
    150 }