merchant

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

lookup_product.c (7618B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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 src/backenddb/lookup_product.c
     18  * @brief Implementation of the lookup_product function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_product.h"
     26 #include "helper.h"
     27 
     28 
     29 enum GNUNET_DB_QueryStatus
     30 TALER_MERCHANTDB_lookup_product (struct TALER_MERCHANTDB_PostgresContext *pg,
     31                                  const char *instance_id,
     32                                  const char *product_id,
     33                                  struct TALER_MERCHANTDB_ProductDetails *pd,
     34                                  size_t *num_categories,
     35                                  uint64_t **categories)
     36 {
     37   struct GNUNET_PQ_QueryParam params[] = {
     38     GNUNET_PQ_query_param_string (instance_id),
     39     GNUNET_PQ_query_param_string (product_id),
     40     GNUNET_PQ_query_param_end
     41   };
     42 
     43   PREPARE (pg,
     44            "lookup_product",
     45            "SELECT"
     46            " mi.description"
     47            ",mi.description_i18n::TEXT"
     48            ",mi.product_name"
     49            ",mi.unit"
     50            ",mi.price_array"
     51            ",mi.taxes::TEXT"
     52            ",mi.total_stock"
     53            ",mi.total_stock_frac"
     54            ",mi.allow_fractional_quantity"
     55            ",mi.fractional_precision_level"
     56            ",mi.total_sold"
     57            ",mi.total_sold_frac"
     58            ",mi.total_lost"
     59            ",mi.total_lost_frac"
     60            ",mi.image"
     61            ",mi.address::TEXT"
     62            ",mi.next_restock"
     63            ",mi.minimum_age"
     64            ",mi.product_group_serial"
     65            ",mi.money_pot_serial"
     66            ",mi.price_is_net"
     67            ",t.category_array AS categories"
     68            " FROM merchant_inventory mi"
     69            " JOIN merchant_instances inst"
     70            "   USING (merchant_serial)"
     71            ",LATERAL ("
     72            "   SELECT ARRAY ("
     73            "     SELECT mpc.category_serial"
     74            "       FROM merchant_product_categories mpc"
     75            "      WHERE mpc.product_serial = mi.product_serial"
     76            "   ) AS category_array"
     77            " ) t"
     78            " WHERE inst.merchant_id=$1"
     79            "   AND mi.product_id=$2"
     80            );
     81   if (NULL == pd)
     82   {
     83     struct GNUNET_PQ_ResultSpec rs_null[] = {
     84       GNUNET_PQ_result_spec_end
     85     };
     86 
     87     check_connection (pg);
     88     return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     89                                                      "lookup_product",
     90                                                      params,
     91                                                      rs_null);
     92   }
     93   else
     94   {
     95     char *my_name = NULL;
     96     char *my_description = NULL;
     97     json_t *my_description_i18n = NULL;
     98     char *my_unit = NULL;
     99     char *my_image = NULL;
    100     json_t *my_address = NULL;
    101     json_t *my_taxes = NULL;
    102     uint64_t *my_categories = NULL;
    103     struct TALER_Amount *my_price_array = NULL;
    104     size_t my_price_array_length = 0;
    105     struct GNUNET_PQ_ResultSpec rs[] = {
    106       GNUNET_PQ_result_spec_string ("description",
    107                                     &my_description),
    108       TALER_PQ_result_spec_json ("description_i18n",
    109                                  &my_description_i18n),
    110       GNUNET_PQ_result_spec_string ("product_name",
    111                                     &my_name),
    112       GNUNET_PQ_result_spec_string ("unit",
    113                                     &my_unit),
    114       TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
    115                                                        "price_array",
    116                                                        &my_price_array_length,
    117                                                        &my_price_array),
    118       TALER_PQ_result_spec_json ("taxes",
    119                                  &my_taxes),
    120       GNUNET_PQ_result_spec_uint64 ("total_stock",
    121                                     &pd->total_stock),
    122       GNUNET_PQ_result_spec_uint32 ("total_stock_frac",
    123                                     &pd->total_stock_frac),
    124       GNUNET_PQ_result_spec_bool ("allow_fractional_quantity",
    125                                   &pd->allow_fractional_quantity),
    126       GNUNET_PQ_result_spec_uint32 ("fractional_precision_level",
    127                                     &pd->fractional_precision_level),
    128       GNUNET_PQ_result_spec_uint64 ("total_sold",
    129                                     &pd->total_sold),
    130       GNUNET_PQ_result_spec_uint32 ("total_sold_frac",
    131                                     &pd->total_sold_frac),
    132       GNUNET_PQ_result_spec_uint64 ("total_lost",
    133                                     &pd->total_lost),
    134       GNUNET_PQ_result_spec_uint32 ("total_lost_frac",
    135                                     &pd->total_lost_frac),
    136       GNUNET_PQ_result_spec_string ("image",
    137                                     &my_image),
    138       TALER_PQ_result_spec_json ("address",
    139                                  &my_address),
    140       GNUNET_PQ_result_spec_timestamp ("next_restock",
    141                                        &pd->next_restock),
    142       GNUNET_PQ_result_spec_uint32 ("minimum_age",
    143                                     &pd->minimum_age),
    144       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
    145                                           "categories",
    146                                           num_categories,
    147                                           &my_categories),
    148       GNUNET_PQ_result_spec_allow_null (
    149         GNUNET_PQ_result_spec_uint64 ("product_group_serial",
    150                                       &pd->product_group_id),
    151         NULL),
    152       GNUNET_PQ_result_spec_allow_null (
    153         GNUNET_PQ_result_spec_uint64 ("money_pot_serial",
    154                                       &pd->money_pot_id),
    155         NULL),
    156       GNUNET_PQ_result_spec_bool ("price_is_net",
    157                                   &pd->price_is_net),
    158       GNUNET_PQ_result_spec_end
    159     };
    160     enum GNUNET_DB_QueryStatus qs;
    161 
    162     check_connection (pg);
    163     pd->product_group_id = 0;
    164     pd->money_pot_id = 0;
    165     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    166                                                    "lookup_product",
    167                                                    params,
    168                                                    rs);
    169     pd->product_name = my_name;
    170     pd->description = my_description;
    171     pd->description_i18n = my_description_i18n;
    172     pd->unit = my_unit;
    173     pd->taxes = my_taxes;
    174     pd->image = my_image;
    175     pd->image_hash = NULL;
    176     pd->address = my_address;
    177     pd->price_array = my_price_array;
    178     pd->price_array_length = my_price_array_length;
    179     *categories = my_categories;
    180     /* Clear original pointers to that cleanup_result doesn't squash them */
    181     my_name = NULL;
    182     my_description = NULL;
    183     my_description_i18n = NULL;
    184     my_unit = NULL;
    185     my_taxes = NULL;
    186     my_image = NULL;
    187     my_address = NULL;
    188     my_price_array = NULL;
    189     my_price_array_length = 0;
    190     my_categories = NULL;
    191     GNUNET_PQ_cleanup_result (rs);
    192     return qs;
    193   }
    194 }