merchant

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

update_product.c (5655B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 2024, 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/update_product.c
     18  * @brief Implementation of the update_product function for Postgres
     19  * @author Christian Grothoff
     20  * @author Iván Ávalos
     21  */
     22 #include "platform.h"
     23 #include <taler/taler_error_codes.h>
     24 #include <taler/taler_dbevents.h>
     25 #include <taler/taler_pq_lib.h>
     26 #include "merchant-database/update_product.h"
     27 #include "helper.h"
     28 
     29 
     30 enum GNUNET_DB_QueryStatus
     31 TALER_MERCHANTDB_update_product (struct TALER_MERCHANTDB_PostgresContext *pg,
     32                                  const char *instance_id,
     33                                  const char *product_id,
     34                                  const struct TALER_MERCHANTDB_ProductDetails *pd,
     35                                  size_t num_cats,
     36                                  const uint64_t *cats,
     37                                  bool *no_instance,
     38                                  ssize_t *no_cat,
     39                                  bool *no_product,
     40                                  bool *lost_reduced,
     41                                  bool *sold_reduced,
     42                                  bool *stocked_reduced,
     43                                  bool *no_group,
     44                                  bool *no_pot)
     45 {
     46   struct GNUNET_PQ_QueryParam params[] = {
     47     GNUNET_PQ_query_param_string (instance_id), /* $1 */
     48     GNUNET_PQ_query_param_string (product_id),
     49     GNUNET_PQ_query_param_string (pd->description),
     50     TALER_PQ_query_param_json (pd->description_i18n), /* $4 */
     51     GNUNET_PQ_query_param_string (pd->unit),
     52     GNUNET_PQ_query_param_string (pd->image), /* $6 */
     53     TALER_PQ_query_param_json (pd->taxes), /* $7 */
     54     TALER_PQ_query_param_array_amount_with_currency (
     55       pd->price_array_length,
     56       pd->price_array,
     57       pg->conn), /* $8 */
     58     GNUNET_PQ_query_param_uint64 (&pd->total_stock),  /* $9 */
     59     GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $10 */
     60     GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity), /* $11 */
     61     GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level),
     62     GNUNET_PQ_query_param_uint64 (&pd->total_lost),
     63     TALER_PQ_query_param_json (pd->address), /* $14 */
     64     GNUNET_PQ_query_param_timestamp (&pd->next_restock),
     65     GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
     66     GNUNET_PQ_query_param_array_uint64 (num_cats,
     67                                         cats,
     68                                         pg->conn), /* $17 */
     69     GNUNET_PQ_query_param_string (pd->product_name), /* $18 */
     70     (0 == pd->product_group_id)
     71     ? GNUNET_PQ_query_param_null ()
     72     : GNUNET_PQ_query_param_uint64 (&pd->product_group_id),
     73     (0 == pd->money_pot_id)
     74     ? GNUNET_PQ_query_param_null ()
     75     : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $20 */
     76     GNUNET_PQ_query_param_bool (pd->price_is_net),
     77     GNUNET_PQ_query_param_end
     78   };
     79   uint64_t ncat;
     80   bool cats_found = true;
     81   struct GNUNET_PQ_ResultSpec rs[] = {
     82     GNUNET_PQ_result_spec_bool ("no_instance",
     83                                 no_instance),
     84     GNUNET_PQ_result_spec_bool ("no_product",
     85                                 no_product),
     86     GNUNET_PQ_result_spec_bool ("lost_reduced",
     87                                 lost_reduced),
     88     GNUNET_PQ_result_spec_bool ("sold_reduced",
     89                                 sold_reduced),
     90     GNUNET_PQ_result_spec_bool ("stocked_reduced",
     91                                 stocked_reduced),
     92     GNUNET_PQ_result_spec_allow_null (
     93       GNUNET_PQ_result_spec_uint64 ("no_cat",
     94                                     &ncat),
     95       &cats_found),
     96     GNUNET_PQ_result_spec_bool ("no_group",
     97                                 no_group),
     98     GNUNET_PQ_result_spec_bool ("no_pot",
     99                                 no_pot),
    100     GNUNET_PQ_result_spec_end
    101   };
    102   enum GNUNET_DB_QueryStatus qs;
    103 
    104   if ( (pd->total_stock < pd->total_lost + pd->total_sold) ||
    105        (pd->total_lost < pd->total_lost
    106         + pd->total_sold) /* integer overflow */)
    107   {
    108     GNUNET_break (0);
    109     return GNUNET_DB_STATUS_HARD_ERROR;
    110   }
    111   check_connection (pg);
    112   PREPARE (pg,
    113            "update_product",
    114            "SELECT"
    115            " out_lost_reduced AS lost_reduced"
    116            ",out_sold_reduced AS sold_reduced"
    117            ",out_stocked_reduced AS stocked_reduced"
    118            ",out_no_product AS no_product"
    119            ",out_no_cat AS no_cat"
    120            ",out_no_instance AS no_instance"
    121            ",out_no_group AS no_group"
    122            ",out_no_pot AS no_pot"
    123            " FROM merchant_do_update_product"
    124            "($1,$2,$3,$4::TEXT::JSONB,$5,$6,$7::TEXT::JSONB,$8,$9"
    125            ",$10,$11,$12,$13,$14::TEXT::JSONB,$15,$16,$17,$18"
    126            ",$19,$20,$21);");
    127   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    128                                                  "update_product",
    129                                                  params,
    130                                                  rs);
    131   GNUNET_PQ_cleanup_query_params_closures (params);
    132   *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
    133   return qs;
    134 }