merchant

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

taler-merchant-httpd_patch-private-reports-REPORT_ID.c (5358B)


      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_patch-private-reports-REPORT_ID.c
     20  * @brief implementation of PATCH /private/reports/$REPORT_ID
     21  * @author Christian Grothoff
     22  */
     23 #include "platform.h"
     24 #include "taler-merchant-httpd_patch-private-reports-REPORT_ID.h"
     25 #include <taler/taler_json_lib.h>
     26 #include <taler/taler_dbevents.h>
     27 #include "merchant-database/update_report.h"
     28 #include "merchant-database/event_notify.h"
     29 
     30 
     31 enum MHD_Result
     32 TMH_private_patch_report (const struct TMH_RequestHandler *rh,
     33                           struct MHD_Connection *connection,
     34                           struct TMH_HandlerContext *hc)
     35 {
     36   const char *report_id_str = hc->infix;
     37   unsigned long long report_id;
     38   const char *description;
     39   const char *program_section;
     40   const char *mime_type;
     41   const char *data_source;
     42   const char *target_address;
     43   struct GNUNET_TIME_Relative frequency;
     44   struct GNUNET_TIME_Relative frequency_shift
     45     = GNUNET_TIME_UNIT_ZERO;
     46   enum GNUNET_DB_QueryStatus qs;
     47   struct GNUNET_JSON_Specification spec[] = {
     48     GNUNET_JSON_spec_string ("description",
     49                              &description),
     50     GNUNET_JSON_spec_string ("program_section",
     51                              &program_section),
     52     GNUNET_JSON_spec_string ("mime_type",
     53                              &mime_type),
     54     GNUNET_JSON_spec_string ("data_source",
     55                              &data_source),
     56     GNUNET_JSON_spec_string ("target_address",
     57                              &target_address),
     58     GNUNET_JSON_spec_relative_time ("report_frequency",
     59                                     &frequency),
     60     GNUNET_JSON_spec_mark_optional (
     61       GNUNET_JSON_spec_relative_time ("report_frequency_shift",
     62                                       &frequency_shift),
     63       NULL),
     64     GNUNET_JSON_spec_end ()
     65   };
     66 
     67   (void) rh;
     68   {
     69     char dummy;
     70 
     71     if (1 != sscanf (report_id_str,
     72                      "%llu%c",
     73                      &report_id,
     74                      &dummy))
     75     {
     76       GNUNET_break_op (0);
     77       return TALER_MHD_reply_with_error (connection,
     78                                          MHD_HTTP_BAD_REQUEST,
     79                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
     80                                          "report_id");
     81     }
     82   }
     83 
     84   {
     85     enum GNUNET_GenericReturnValue res;
     86 
     87     res = TALER_MHD_parse_json_data (connection,
     88                                      hc->request_body,
     89                                      spec);
     90     if (GNUNET_OK != res)
     91     {
     92       GNUNET_break_op (0);
     93       return (GNUNET_NO == res)
     94              ? MHD_YES
     95              : MHD_NO;
     96     }
     97   }
     98   if ('/' != data_source[0])
     99   {
    100     GNUNET_break_op (0);
    101     return TALER_MHD_reply_with_error (connection,
    102                                        MHD_HTTP_BAD_REQUEST,
    103                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
    104                                        "data_source");
    105 
    106   }
    107 
    108   qs = TALER_MERCHANTDB_update_report (TMH_db,
    109                                        hc->instance->settings.id,
    110                                        report_id,
    111                                        program_section,
    112                                        description,
    113                                        mime_type,
    114                                        data_source,
    115                                        target_address,
    116                                        frequency,
    117                                        frequency_shift);
    118   if (qs < 0)
    119   {
    120     GNUNET_break (0);
    121     return TALER_MHD_reply_with_error (connection,
    122                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    123                                        TALER_EC_GENERIC_DB_STORE_FAILED,
    124                                        "update_report");
    125   }
    126   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
    127   {
    128     return TALER_MHD_reply_with_error (connection,
    129                                        MHD_HTTP_NOT_FOUND,
    130                                        TALER_EC_MERCHANT_GENERIC_REPORT_UNKNOWN,
    131                                        report_id_str);
    132   }
    133 
    134   /* FIXME-Optimization: Trigger MERCHANT_REPORT_UPDATE event inside of UPDATE transaction */
    135   {
    136     struct GNUNET_DB_EventHeaderP ev = {
    137       .size = htons (sizeof (ev)),
    138       .type = htons (TALER_DBEVENT_MERCHANT_REPORT_UPDATE)
    139     };
    140 
    141     TALER_MERCHANTDB_event_notify (TMH_db,
    142                                    &ev,
    143                                    NULL,
    144                                    0);
    145   }
    146 
    147   return TALER_MHD_reply_static (connection,
    148                                  MHD_HTTP_NO_CONTENT,
    149                                  NULL,
    150                                  NULL,
    151                                  0);
    152 }