merchant

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

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


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