merchant

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

pg_account_kyc_get_outdated.c (4132B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 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_account_kyc_get_outdated.c
     18  * @brief Implementation of the account_kyc_get_outdated 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_account_kyc_get_outdated.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for kyc_status_cb().
     30  */
     31 struct KycStatusContext
     32 {
     33   /**
     34    * Function to call with results.
     35    */
     36   TALER_MERCHANTDB_KycOutdatedCallback kyc_cb;
     37 
     38   /**
     39    * Closure for @e kyc_cb.
     40    */
     41   void *kyc_cb_cls;
     42 
     43   /**
     44    * Number of results found.
     45    */
     46   unsigned int count;
     47 
     48   /**
     49    * Set to true on failure(s).
     50    */
     51   bool failure;
     52 };
     53 
     54 
     55 /**
     56  * Function to be called with the results of a SELECT statement
     57  * that has returned @a num_results results about accounts.
     58  *
     59  * @param[in,out] cls of type `struct KycStatusContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 kyc_status_cb (void *cls,
     65                PGresult *result,
     66                unsigned int num_results)
     67 {
     68   struct KycStatusContext *ksc = cls;
     69 
     70   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     71               "Got %u outdated KYC records\n",
     72               num_results);
     73   for (unsigned int i = 0; i < num_results; i++)
     74   {
     75     struct TALER_MerchantWireHashP h_wire;
     76     char *exchange_url;
     77     char *instance_id;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
     80                                             &h_wire),
     81       GNUNET_PQ_result_spec_string ("exchange_url",
     82                                     &exchange_url),
     83       GNUNET_PQ_result_spec_string ("instance_id",
     84                                     &instance_id),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       ksc->failure = true;
     95       return;
     96     }
     97     ksc->kyc_cb (ksc->kyc_cb_cls,
     98                  instance_id,
     99                  exchange_url,
    100                  &h_wire);
    101     GNUNET_PQ_cleanup_result (rs);
    102   }
    103 }
    104 
    105 
    106 enum GNUNET_DB_QueryStatus
    107 TMH_PG_account_kyc_get_outdated (
    108   void *cls,
    109   TALER_MERCHANTDB_KycOutdatedCallback kyc_cb,
    110   void *kyc_cb_cls)
    111 {
    112   struct PostgresClosure *pg = cls;
    113   struct KycStatusContext ksc = {
    114     .kyc_cb = kyc_cb,
    115     .kyc_cb_cls = kyc_cb_cls
    116   };
    117   struct GNUNET_TIME_Absolute now
    118     = GNUNET_TIME_absolute_get ();
    119   struct GNUNET_PQ_QueryParam params[] = {
    120     GNUNET_PQ_query_param_absolute_time (&now),
    121     GNUNET_PQ_query_param_end
    122   };
    123   enum GNUNET_DB_QueryStatus qs;
    124 
    125   check_connection (pg);
    126   PREPARE (pg,
    127            "account_kyc_get_outdated",
    128            "SELECT "
    129            "  mi.merchant_id"
    130            " ,ma.h_wire"
    131            " ,kyc.exchange_url"
    132            " FROM merchant_kyc kyc"
    133            " JOIN merchant_accounts ma"
    134            "   USING (account_serial)"
    135            " JOIN merchant_instances mi"
    136            "   USING (merchant_serial)"
    137            " WHERE kyc.next_kyc_poll < $1"
    138            " ORDER BY kyc.next_kyc_poll ASC;");
    139   qs = GNUNET_PQ_eval_prepared_multi_select (
    140     pg->conn,
    141     "account_kyc_get_outdated",
    142     params,
    143     &kyc_status_cb,
    144     &ksc);
    145   if (ksc.failure)
    146   {
    147     GNUNET_break (0);
    148     return GNUNET_DB_STATUS_HARD_ERROR;
    149   }
    150   if (0 > qs)
    151     return qs;
    152   return ksc.count;
    153 }