LCOV - code coverage report
Current view: top level - backenddb - get_external_refunds_total.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.0 % 32 24
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              :    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 src/backenddb/get_external_refunds_total.c
      18              :  * @brief Implementation of the get_external_refunds_total function for Postgres
      19              :  * @author Bohdan Potuzhnyi
      20              :  * @author Volodymyr Potuzhnyi
      21              :  */
      22              : #include "platform.h"
      23              : #include <taler/taler_pq_lib.h>
      24              : #include "merchant-database/get_external_refunds_total.h"
      25              : #include "helper.h"
      26              : 
      27              : 
      28              : enum GNUNET_DB_QueryStatus
      29           26 : TALER_MERCHANTDB_get_external_refunds_total (
      30              :   struct TALER_MERCHANTDB_PostgresContext *pg,
      31              :   const char *instance_id,
      32              :   const char *order_id,
      33              :   struct TALER_Amount *total,
      34              :   bool *currency_mismatch)
      35              : {
      36           26 :   struct GNUNET_PQ_QueryParam params[] = {
      37           26 :     GNUNET_PQ_query_param_string (order_id),
      38              :     GNUNET_PQ_query_param_end
      39              :   };
      40              :   uint64_t value_sum;
      41              :   uint64_t frac_sum;
      42              :   uint64_t currencies;
      43              :   char *currency;
      44           26 :   struct GNUNET_PQ_ResultSpec rs[] = {
      45           26 :     GNUNET_PQ_result_spec_uint64 ("refund_value_sum",
      46              :                                   &value_sum),
      47           26 :     GNUNET_PQ_result_spec_uint64 ("refund_frac_sum",
      48              :                                   &frac_sum),
      49           26 :     GNUNET_PQ_result_spec_uint64 ("currencies",
      50              :                                   &currencies),
      51           26 :     GNUNET_PQ_result_spec_string ("currency",
      52              :                                   &currency),
      53              :     GNUNET_PQ_result_spec_end
      54              :   };
      55              :   enum GNUNET_DB_QueryStatus qs;
      56              : 
      57           26 :   GNUNET_assert (NULL != pg->current_merchant_id);
      58           26 :   GNUNET_assert (0 == strcmp (instance_id,
      59              :                               pg->current_merchant_id));
      60           26 :   *currency_mismatch = false;
      61              :   /* no preflight check here, run in transaction by caller! */
      62           26 :   TMH_PQ_prepare_anon (pg,
      63              :                        "SELECT"
      64              :                        " SUM((refund_amount).val)::INT8"
      65              :                        "   AS refund_value_sum"
      66              :                        ",SUM((refund_amount).frac)::INT8"
      67              :                        "   AS refund_frac_sum"
      68              :                        ",COUNT(DISTINCT (refund_amount).curr)::INT8"
      69              :                        "   AS currencies"
      70              :                        ",MIN((refund_amount).curr) AS currency"
      71              :                        " FROM merchant_refunds_external"
      72              :                        " WHERE order_serial="
      73              :                        "  (SELECT order_serial"
      74              :                        "     FROM merchant_contract_terms"
      75              :                        "    WHERE order_id=$1)"
      76              :                        " HAVING COUNT(*) > 0");
      77           26 :   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
      78              :                                                  "",
      79              :                                                  params,
      80              :                                                  rs);
      81           26 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
      82           18 :     return qs;
      83            8 :   if (1 != currencies)
      84              :   {
      85              :     /* Refunds in different currencies were recorded; their sum is
      86              :        meaningless, so we only report the mismatch. */
      87            0 :     *currency_mismatch = true;
      88            0 :     GNUNET_PQ_cleanup_result (rs);
      89            0 :     return qs;
      90              :   }
      91            8 :   if (GNUNET_OK !=
      92            8 :       TALER_amount_set_zero (currency,
      93              :                              total))
      94              :   {
      95            0 :     GNUNET_break (0);
      96            0 :     GNUNET_PQ_cleanup_result (rs);
      97            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
      98              :   }
      99            8 :   GNUNET_PQ_cleanup_result (rs);
     100            8 :   total->value = value_sum + frac_sum / TALER_AMOUNT_FRAC_BASE;
     101            8 :   total->fraction = (uint32_t) (frac_sum % TALER_AMOUNT_FRAC_BASE);
     102            8 :   if (GNUNET_SYSERR ==
     103            8 :       TALER_amount_normalize (total))
     104              :   {
     105              :     /* Only reachable if the recorded refunds sum beyond the maximum
     106              :        representable amount, which the per-order cap prevents. */
     107            0 :     GNUNET_break (0);
     108            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     109              :   }
     110            8 :   return qs;
     111              : }
        

Generated by: LCOV version 2.0-1