merchant

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

pg_lookup_inventory_products.c (7951B)


      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_inventory_products.c
     18  * @brief Implementation of the lookup_inventory_products function for Postgres
     19  * @author Bohdan Potuzhnyi
     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_inventory_products.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Context used for TMH_PG_lookup_inventory_products().
     30  */
     31 struct LookupInventoryProductsContext
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_InventoryProductCallback cb;
     37 
     38   /**
     39    * Closure for @a cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Postgres context.
     45    */
     46   struct PostgresClosure *pg;
     47 
     48   /**
     49    * Did database result extraction fail?
     50    */
     51   bool extract_failed;
     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 products.
     58  *
     59  * @param[in,out] cls of type `struct LookupInventoryProductsContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 lookup_inventory_products_cb (void *cls,
     65                               PGresult *result,
     66                               unsigned int num_results)
     67 {
     68   struct LookupInventoryProductsContext *plc = cls;
     69   struct PostgresClosure *pg = plc->pg;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     char *product_id;
     74     struct TALER_MERCHANTDB_InventoryProductDetails pd;
     75     size_t num_categories;
     76     uint64_t *categories;
     77     bool no_image_hash;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_string ("product_id",
     80                                     &product_id),
     81       GNUNET_PQ_result_spec_string ("product_name",
     82                                     &pd.product_name),
     83       GNUNET_PQ_result_spec_string ("description",
     84                                     &pd.description),
     85       TALER_PQ_result_spec_json ("description_i18n",
     86                                  &pd.description_i18n),
     87       GNUNET_PQ_result_spec_string ("unit",
     88                                     &pd.unit),
     89       TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
     90                                                        "price_array",
     91                                                        &pd.price_array_length,
     92                                                        &pd.price_array),
     93       GNUNET_PQ_result_spec_uint64 ("remaining_stock",
     94                                     &pd.remaining_stock),
     95       GNUNET_PQ_result_spec_uint32 ("remaining_stock_frac",
     96                                     &pd.remaining_stock_frac),
     97       TALER_PQ_result_spec_json ("taxes",
     98                                  &pd.taxes),
     99       GNUNET_PQ_result_spec_allow_null (
    100         GNUNET_PQ_result_spec_string ("image_hash",
    101                                       &pd.image_hash),
    102         &no_image_hash),
    103       GNUNET_PQ_result_spec_bool ("allow_fractional_quantity",
    104                                   &pd.allow_fractional_quantity),
    105       GNUNET_PQ_result_spec_uint32 ("fractional_precision_level",
    106                                     &pd.fractional_precision_level),
    107       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
    108                                           "categories",
    109                                           &num_categories,
    110                                           &categories),
    111       GNUNET_PQ_result_spec_end
    112     };
    113 
    114     if (GNUNET_OK !=
    115         GNUNET_PQ_extract_result (result,
    116                                   rs,
    117                                   i))
    118     {
    119       GNUNET_break (0);
    120       plc->extract_failed = true;
    121       return;
    122     }
    123     plc->cb (plc->cb_cls,
    124              product_id,
    125              &pd,
    126              num_categories,
    127              categories);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TMH_PG_lookup_inventory_products (void *cls,
    135                                   const char *instance_id,
    136                                   TALER_MERCHANTDB_InventoryProductCallback cb,
    137                                   void *cb_cls)
    138 {
    139   struct PostgresClosure *pg = cls;
    140   struct LookupInventoryProductsContext plc = {
    141     .cb = cb,
    142     .cb_cls = cb_cls,
    143     .pg = pg,
    144     /* Can be overwritten by the lookup_inventory_products_cb */
    145     .extract_failed = false,
    146   };
    147   struct GNUNET_PQ_QueryParam params[] = {
    148     GNUNET_PQ_query_param_string (instance_id),
    149     GNUNET_PQ_query_param_end
    150   };
    151   enum GNUNET_DB_QueryStatus qs;
    152 
    153   check_connection (pg);
    154   PREPARE (pg,
    155            "lookup_inventory_products",
    156            "SELECT"
    157            " description"
    158            ",description_i18n::TEXT"
    159            ",product_name"
    160            ",unit"
    161            ",price_array"
    162            ",CASE WHEN minv.total_stock = 9223372036854775807"
    163            "      THEN minv.total_stock"
    164            "      ELSE (GREATEST(0, rt.remaining_total) / 1000000)::INT8"
    165            "      END AS remaining_stock"
    166            ",CASE WHEN minv.total_stock = 9223372036854775807"
    167            "      THEN 2147483647"
    168            "      ELSE mod(GREATEST(0, rt.remaining_total), 1000000)::INT4"
    169            "      END AS remaining_stock_frac"
    170            ",taxes::TEXT"
    171            ",image_hash"
    172            ",allow_fractional_quantity"
    173            ",fractional_precision_level"
    174            ",product_id"
    175            ",t.category_array AS categories"
    176            " FROM merchant_inventory minv"
    177            " JOIN merchant_instances inst"
    178            "   USING (merchant_serial)"
    179            ",LATERAL ("
    180            "   SELECT ARRAY ("
    181            "     SELECT mpc.category_serial"
    182            "       FROM merchant_product_categories mpc"
    183            "      WHERE mpc.product_serial = minv.product_serial"
    184            "   ) AS category_array"
    185            " ) t"
    186            ",LATERAL ("
    187            "   SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
    188            "                        + total_locked_frac::NUMERIC), 0)"
    189            "          AS total_locked"
    190            "     FROM merchant_inventory_locks mil"
    191            "    WHERE mil.product_serial = minv.product_serial"
    192            " ) il"
    193            ",LATERAL ("
    194            "   SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
    195            "                        + total_locked_frac::NUMERIC), 0)"
    196            "          AS total_locked"
    197            "     FROM merchant_order_locks mol"
    198            "    WHERE mol.product_serial = minv.product_serial"
    199            " ) ol"
    200            ",LATERAL ("
    201            "   SELECT"
    202            "     (minv.total_stock::NUMERIC * 1000000"
    203            "      + minv.total_stock_frac::NUMERIC)"
    204            "     - (minv.total_sold::NUMERIC * 1000000"
    205            "        + minv.total_sold_frac::NUMERIC)"
    206            "     - (minv.total_lost::NUMERIC * 1000000"
    207            "        + minv.total_lost_frac::NUMERIC)"
    208            "     - il.total_locked"
    209            "     - ol.total_locked"
    210            "     AS remaining_total"
    211            " ) rt"
    212            " WHERE inst.merchant_id=$1");
    213   qs = GNUNET_PQ_eval_prepared_multi_select (
    214     pg->conn,
    215     "lookup_inventory_products",
    216     params,
    217     &lookup_inventory_products_cb,
    218     &plc);
    219   /* If there was an error inside lookup_inventory_products_cb, return a hard error. */
    220   if (plc.extract_failed)
    221     return GNUNET_DB_STATUS_HARD_ERROR;
    222   return qs;
    223 }