merchant

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

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


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