LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_post-private-reports.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.0 % 36 27
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 1 1

            Line data    Source code
       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            1 : 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            1 :   struct GNUNET_TIME_Relative frequency_shift
      43              :     = GNUNET_TIME_UNIT_ZERO;
      44              :   enum GNUNET_DB_QueryStatus qs;
      45              :   struct GNUNET_JSON_Specification spec[] = {
      46            1 :     GNUNET_JSON_spec_string ("description",
      47              :                              &description),
      48            1 :     GNUNET_JSON_spec_string ("program_section",
      49              :                              &program_section),
      50            1 :     GNUNET_JSON_spec_string ("mime_type",
      51              :                              &mime_type),
      52            1 :     GNUNET_JSON_spec_string ("data_source",
      53              :                              &data_source),
      54            1 :     GNUNET_JSON_spec_string ("target_address",
      55              :                              &target_address),
      56            1 :     GNUNET_JSON_spec_relative_time ("report_frequency",
      57              :                                     &frequency),
      58            1 :     GNUNET_JSON_spec_mark_optional (
      59              :       GNUNET_JSON_spec_relative_time ("report_frequency_shift",
      60              :                                       &frequency_shift),
      61              :       NULL),
      62            1 :     GNUNET_JSON_spec_end ()
      63              :   };
      64            1 :   uint64_t report_id = 0;
      65              : 
      66              :   (void) rh;
      67              : 
      68              :   {
      69              :     enum GNUNET_GenericReturnValue res;
      70              : 
      71            1 :     res = TALER_MHD_parse_json_data (connection,
      72            1 :                                      hc->request_body,
      73              :                                      spec);
      74            1 :     if (GNUNET_OK != res)
      75              :     {
      76            0 :       GNUNET_break_op (0);
      77              :       return (GNUNET_NO == res)
      78              :              ? MHD_YES
      79            0 :              : MHD_NO;
      80              :     }
      81              :   }
      82              :   {
      83              :     char *section;
      84              : 
      85              :     /* Check program_section exists in config! */
      86            1 :     GNUNET_asprintf (&section,
      87              :                      "report-generator-%s",
      88              :                      program_section);
      89            1 :     if (GNUNET_YES !=
      90            1 :         GNUNET_CONFIGURATION_have_value (TMH_cfg,
      91              :                                          section,
      92              :                                          "BINARY"))
      93              :     {
      94            0 :       GNUNET_free (section);
      95            0 :       GNUNET_break (0);
      96            0 :       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            1 :     GNUNET_free (section);
     103              :   }
     104            1 :   if ('/' != data_source[0])
     105              :   {
     106            0 :     GNUNET_break_op (0);
     107            0 :     return TALER_MHD_reply_with_error (connection,
     108              :                                        MHD_HTTP_BAD_REQUEST,
     109              :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     110              :                                        "data_source");
     111              : 
     112              :   }
     113            1 :   qs = TALER_MERCHANTDB_insert_report (TMH_db,
     114            1 :                                        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            1 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     124              :   {
     125              :     /* The INSERT either stores the row and returns its serial, or it
     126              :        fails; anything else would leave @e report_id unset. */
     127            0 :     GNUNET_break (0);
     128            0 :     return TALER_MHD_reply_with_error (connection,
     129              :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     130              :                                        TALER_EC_GENERIC_DB_STORE_FAILED,
     131              :                                        "insert_report");
     132              :   }
     133              : 
     134              :   /* FIXME-Optimization: do trigger inside of transaction above... */
     135              :   {
     136            1 :     struct GNUNET_DB_EventHeaderP ev = {
     137            1 :       .size = htons (sizeof (ev)),
     138            1 :       .type = htons (TALER_DBEVENT_MERCHANT_REPORT_UPDATE)
     139              :     };
     140              : 
     141            1 :     TALER_MERCHANTDB_event_notify (TMH_db,
     142              :                                    &ev,
     143              :                                    NULL,
     144              :                                    0);
     145              :   }
     146              : 
     147            1 :   return TALER_MHD_REPLY_JSON_PACK (
     148              :     connection,
     149              :     MHD_HTTP_OK,
     150              :     GNUNET_JSON_pack_uint64 ("report_serial_id",
     151              :                              report_id));
     152              : }
        

Generated by: LCOV version 2.0-1