merchant

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

pg_lookup_reports_pending.c (5710B)


      1 /*
      2    This file is part of TALER
      3    Copyright (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 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 General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 /**
     17  * @file backenddb/pg_lookup_reports_pending.c
     18  * @brief Implementation of the lookup_reports_pending function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "pg_lookup_reports_pending.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Context used for TMH_PG_lookup_reports_pending().
     31  */
     32 struct SelectReportsContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_MERCHANTDB_ReportsPendingCallback cb;
     38 
     39   /**
     40    * Closure for @a cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Did database result extraction fail?
     46    */
     47   bool extract_failed;
     48 };
     49 
     50 
     51 /**
     52  * Function to be called with the results of a SELECT statement
     53  * that has returned @a num_results results about reports.
     54  *
     55  * @param[in,out] cls of type `struct SelectReportsContext *`
     56  * @param result the postgres result
     57  * @param num_results the number of results in @a result
     58  */
     59 static void
     60 select_pending_reports_cb (void *cls,
     61                            PGresult *result,
     62                            unsigned int num_results)
     63 {
     64   struct SelectReportsContext *plc = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     char *instance_id;
     69     uint64_t report_serial;
     70     char *report_program_section;
     71     char *report_description;
     72     char *mime_type;
     73     struct TALER_MERCHANT_ReportToken report_token;
     74     char *target_address;
     75     struct GNUNET_TIME_Relative frequency;
     76     struct GNUNET_TIME_Relative frequency_shift;
     77     struct GNUNET_TIME_Absolute next_transmission;
     78     bool one_shot;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_string ("merchant_id",
     81                                     &instance_id),
     82       GNUNET_PQ_result_spec_uint64 ("report_serial",
     83                                     &report_serial),
     84       GNUNET_PQ_result_spec_string ("report_program_section",
     85                                     &report_program_section),
     86       GNUNET_PQ_result_spec_string ("report_description",
     87                                     &report_description),
     88       GNUNET_PQ_result_spec_string ("mime_type",
     89                                     &mime_type),
     90       GNUNET_PQ_result_spec_auto_from_type ("report_token",
     91                                             &report_token),
     92       GNUNET_PQ_result_spec_string ("target_address",
     93                                     &target_address),
     94       GNUNET_PQ_result_spec_relative_time ("frequency",
     95                                            &frequency),
     96       GNUNET_PQ_result_spec_relative_time ("frequency_shift",
     97                                            &frequency_shift),
     98       GNUNET_PQ_result_spec_absolute_time ("next_transmission",
     99                                            &next_transmission),
    100       GNUNET_PQ_result_spec_bool ("one_shot_hidden",
    101                                   &one_shot),
    102       GNUNET_PQ_result_spec_end
    103     };
    104 
    105     if (GNUNET_OK !=
    106         GNUNET_PQ_extract_result (result,
    107                                   rs,
    108                                   i))
    109     {
    110       GNUNET_break (0);
    111       plc->extract_failed = true;
    112       return;
    113     }
    114     plc->cb (plc->cb_cls,
    115              instance_id,
    116              report_serial,
    117              report_program_section,
    118              report_description,
    119              mime_type,
    120              &report_token,
    121              target_address,
    122              frequency,
    123              frequency_shift,
    124              next_transmission,
    125              one_shot);
    126     GNUNET_PQ_cleanup_result (rs);
    127   }
    128 }
    129 
    130 
    131 enum GNUNET_DB_QueryStatus
    132 TMH_PG_lookup_reports_pending (void *cls,
    133                                TALER_MERCHANTDB_ReportsPendingCallback cb,
    134                                void *cb_cls)
    135 {
    136   struct PostgresClosure *pg = cls;
    137   struct SelectReportsContext plc = {
    138     .cb = cb,
    139     .cb_cls = cb_cls,
    140     /* Can be overwritten by the lookup_reports_cb */
    141     .extract_failed = false,
    142   };
    143   struct GNUNET_PQ_QueryParam params[] = {
    144     GNUNET_PQ_query_param_end
    145   };
    146   enum GNUNET_DB_QueryStatus qs;
    147 
    148   check_connection (pg);
    149   PREPARE (pg,
    150            "lookup_reports_pending",
    151            "SELECT"
    152            "  mi.merchant_id"
    153            " ,mr.report_serial"
    154            " ,mr.report_program_section"
    155            " ,mr.report_description"
    156            " ,mr.mime_type"
    157            " ,mr.report_token"
    158            " ,mr.target_address"
    159            " ,mr.frequency"
    160            " ,mr.frequency_shift"
    161            " ,mr.next_transmission"
    162            " ,mr.one_shot_hidden"
    163            " FROM merchant_reports mr"
    164            " JOIN merchant_instances mi"
    165            "   USING (merchant_serial)"
    166            " ORDER BY next_transmission ASC"
    167            " LIMIT 1;");
    168   qs = GNUNET_PQ_eval_prepared_multi_select (
    169     pg->conn,
    170     "lookup_reports_pending",
    171     params,
    172     &select_pending_reports_cb,
    173     &plc);
    174   /* If there was an error inside select_pending_reports_cb, return a hard error. */
    175   if (plc.extract_failed)
    176   {
    177     GNUNET_break (0);
    178     return GNUNET_DB_STATUS_HARD_ERROR;
    179   }
    180   return qs;
    181 }