exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

pg_select_merge_amounts_for_kyc_check.c (4170B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 2024 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 exchangedb/pg_select_merge_amounts_for_kyc_check.c
     18  * @brief Implementation of the select_merge_amounts_for_kyc_check function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/select_merge_amounts_for_kyc_check.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #get_kyc_amounts_cb().
     28  */
     29 struct KycAmountCheckContext
     30 {
     31   /**
     32    * Function to call per result.
     33    */
     34   TALER_KYCLOGIC_KycAmountCallback cb;
     35 
     36   /**
     37    * Closure for @e cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Plugin context.
     43    */
     44   struct TALER_EXCHANGEDB_PostgresContext *pg;
     45 
     46   /**
     47    * Flag set to #GNUNET_OK as long as everything is fine.
     48    */
     49   enum GNUNET_GenericReturnValue status;
     50 
     51 };
     52 
     53 /**
     54  * Invoke the callback for each result.
     55  *
     56  * @param cls a `struct KycAmountCheckContext *`
     57  * @param result SQL result
     58  * @param num_results number of rows in @a result
     59  */
     60 static void
     61 get_kyc_amounts_cb (void *cls,
     62                     PGresult *result,
     63                     unsigned int num_results)
     64 {
     65   struct KycAmountCheckContext *ctx = cls;
     66   struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
     67 
     68   for (unsigned int i = 0; i < num_results; i++)
     69   {
     70     struct GNUNET_TIME_Absolute date;
     71     struct TALER_Amount amount;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     74                                    &amount),
     75       GNUNET_PQ_result_spec_absolute_time ("date",
     76                                            &date),
     77       GNUNET_PQ_result_spec_end
     78     };
     79     enum GNUNET_GenericReturnValue ret;
     80 
     81     if (GNUNET_OK !=
     82         GNUNET_PQ_extract_result (result,
     83                                   rs,
     84                                   i))
     85     {
     86       GNUNET_break (0);
     87       ctx->status = GNUNET_SYSERR;
     88       return;
     89     }
     90     ret = ctx->cb (ctx->cb_cls,
     91                    &amount,
     92                    date);
     93     GNUNET_PQ_cleanup_result (rs);
     94     switch (ret)
     95     {
     96     case GNUNET_OK:
     97       continue;
     98     case GNUNET_NO:
     99       break;
    100     case GNUNET_SYSERR:
    101       ctx->status = GNUNET_SYSERR;
    102       break;
    103     }
    104     break;
    105   }
    106 }
    107 
    108 
    109 enum GNUNET_DB_QueryStatus
    110 TALER_EXCHANGEDB_select_merge_amounts_for_kyc_check (
    111   struct TALER_EXCHANGEDB_PostgresContext *pg,
    112   const struct TALER_NormalizedPaytoHashP *h_payto,
    113   struct GNUNET_TIME_Absolute time_limit,
    114   TALER_KYCLOGIC_KycAmountCallback kac,
    115   void *kac_cls)
    116 {
    117   struct GNUNET_PQ_QueryParam params[] = {
    118     GNUNET_PQ_query_param_auto_from_type (h_payto),
    119     GNUNET_PQ_query_param_absolute_time (&time_limit),
    120     GNUNET_PQ_query_param_end
    121   };
    122   struct KycAmountCheckContext ctx = {
    123     .cb = kac,
    124     .cb_cls = kac_cls,
    125     .pg = pg,
    126     .status = GNUNET_OK
    127   };
    128   enum GNUNET_DB_QueryStatus qs;
    129 
    130   PREPARE (pg,
    131            "select_kyc_relevant_merge_events",
    132            "SELECT"
    133            " amount_with_fee AS amount"
    134            ",merge_timestamp AS date"
    135            " FROM account_merges"
    136            " JOIN purse_merges USING (purse_pub)"
    137            " JOIN purse_requests USING (purse_pub)"
    138            " JOIN purse_decision USING (purse_pub)"
    139            " WHERE wallet_h_payto=$1"
    140            "   AND merge_timestamp >= $2"
    141            "   AND NOT refunded"
    142            " ORDER BY merge_timestamp DESC");
    143   qs = GNUNET_PQ_eval_prepared_multi_select (
    144     pg->conn,
    145     "select_kyc_relevant_merge_events",
    146     params,
    147     &get_kyc_amounts_cb,
    148     &ctx);
    149   if (GNUNET_OK != ctx.status)
    150     return GNUNET_DB_STATUS_HARD_ERROR;
    151   return qs;
    152 }