merchant

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

lookup_pending_webhooks.c (5913B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023 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 src/backenddb/lookup_pending_webhooks.c
     18  * @brief Implementation of the lookup_pending_webhooks function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_pending_webhooks.h"
     24 #include "helper.h"
     25 
     26 /**
     27  * Context used for lookup_pending_webhooks_cb().
     28  */
     29 struct LookupPendingWebhookContext
     30 {
     31   /**
     32    * Function to call with the results.
     33    */
     34   TALER_MERCHANTDB_PendingWebhooksCallback cb;
     35 
     36   /**
     37    * Closure for @a cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Did database result extraction fail?
     43    */
     44   bool extract_failed;
     45 };
     46 
     47 
     48 /**
     49  * Function to be called with the results of a SELECT statement
     50  * that has returned @a num_results results about webhook.
     51  *
     52  * @param[in,out] cls of type `struct LookupPendingWebhookContext *`
     53  * @param result the postgres result
     54  * @param num_results the number of results in @a result
     55  */
     56 static void
     57 lookup_pending_webhooks_cb (void *cls,
     58                             PGresult *result,
     59                             unsigned int num_results)
     60 {
     61   struct LookupPendingWebhookContext *pwlc = cls;
     62 
     63   for (unsigned int i = 0; i < num_results; i++)
     64   {
     65     uint64_t webhook_pending_serial;
     66     struct GNUNET_TIME_Absolute next_attempt;
     67     uint32_t retries;
     68     char *url;
     69     char *http_method;
     70     char *header = NULL;
     71     char *body = NULL;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_uint64 ("webhook_pending_serial",
     74                                     &webhook_pending_serial),
     75       GNUNET_PQ_result_spec_absolute_time ("next_attempt",
     76                                            &next_attempt),
     77       GNUNET_PQ_result_spec_uint32 ("retries",
     78                                     &retries),
     79       GNUNET_PQ_result_spec_string ("url",
     80                                     &url),
     81       GNUNET_PQ_result_spec_string ("http_method",
     82                                     &http_method),
     83       GNUNET_PQ_result_spec_allow_null (
     84         GNUNET_PQ_result_spec_string ("header",
     85                                       &header),
     86         NULL),
     87       GNUNET_PQ_result_spec_allow_null (
     88         GNUNET_PQ_result_spec_string ("body",
     89                                       &body),
     90         NULL),
     91       GNUNET_PQ_result_spec_end
     92     };
     93 
     94     if (GNUNET_OK !=
     95         GNUNET_PQ_extract_result (result,
     96                                   rs,
     97                                   i))
     98     {
     99       GNUNET_break (0);
    100       pwlc->extract_failed = true;
    101       return;
    102     }
    103     pwlc->cb (pwlc->cb_cls,
    104               webhook_pending_serial,
    105               next_attempt,
    106               retries,
    107               url,
    108               http_method,
    109               header,
    110               body);
    111     GNUNET_PQ_cleanup_result (rs);
    112   }
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TALER_MERCHANTDB_lookup_pending_webhooks (
    118   struct TALER_MERCHANTDB_PostgresContext *pg,
    119   TALER_MERCHANTDB_PendingWebhooksCallback cb,
    120   void *cb_cls)
    121 {
    122   struct LookupPendingWebhookContext pwlc = {
    123     .cb = cb,
    124     .cb_cls = cb_cls,
    125     .extract_failed = false,
    126   };
    127   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
    128   struct GNUNET_PQ_QueryParam params[] = {
    129     GNUNET_PQ_query_param_absolute_time (&now),
    130     GNUNET_PQ_query_param_end
    131   };
    132   enum GNUNET_DB_QueryStatus qs;
    133 
    134   PREPARE (pg,
    135            "lookup_pending_webhooks",
    136            "SELECT"
    137            "  webhook_pending_serial"
    138            " ,next_attempt"
    139            " ,retries"
    140            " ,url"
    141            " ,http_method"
    142            " ,header"
    143            " ,body"
    144            "  FROM merchant.merchant_pending_webhooks"
    145            " WHERE next_attempt <= $1"
    146            " ORDER BY next_attempt ASC");
    147   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    148                                              "lookup_pending_webhooks",
    149                                              params,
    150                                              &lookup_pending_webhooks_cb,
    151                                              &pwlc);
    152   if (pwlc.extract_failed)
    153     return GNUNET_DB_STATUS_HARD_ERROR;
    154   return qs;
    155 }
    156 
    157 
    158 enum GNUNET_DB_QueryStatus
    159 TALER_MERCHANTDB_lookup_future_webhook (
    160   struct TALER_MERCHANTDB_PostgresContext *pg,
    161   TALER_MERCHANTDB_PendingWebhooksCallback cb,
    162   void *cb_cls)
    163 {
    164   struct LookupPendingWebhookContext pwlc = {
    165     .cb = cb,
    166     .cb_cls = cb_cls,
    167     .extract_failed = false,
    168   };
    169   struct GNUNET_PQ_QueryParam params_null[] = {
    170     GNUNET_PQ_query_param_end
    171   };
    172   enum GNUNET_DB_QueryStatus qs;
    173 
    174   PREPARE (pg,
    175            "lookup_future_webhook",
    176            "SELECT"
    177            "  webhook_pending_serial"
    178            " ,next_attempt"
    179            " ,retries"
    180            " ,url"
    181            " ,http_method"
    182            " ,header"
    183            " ,body"
    184            "  FROM merchant.merchant_pending_webhooks"
    185            " ORDER BY next_attempt ASC"
    186            " LIMIT 1");
    187   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    188                                              "lookup_future_webhook",
    189                                              params_null,
    190                                              &lookup_pending_webhooks_cb,
    191                                              &pwlc);
    192   if (pwlc.extract_failed)
    193     return GNUNET_DB_STATUS_HARD_ERROR;
    194   return qs;
    195 }