Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2022 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_mark_contract_paid.c 18 : * @brief Implementation of the mark_contract_paid 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 "pg_mark_contract_paid.h" 26 : #include "pg_helper.h" 27 : 28 : enum GNUNET_DB_QueryStatus 29 32 : TMH_PG_mark_contract_paid ( 30 : void *cls, 31 : const char *instance_id, 32 : const struct TALER_PrivateContractHashP *h_contract_terms, 33 : const char *session_id, 34 : int16_t choice_index) 35 : { 36 32 : struct PostgresClosure *pg = cls; 37 32 : struct GNUNET_PQ_QueryParam params[] = { 38 32 : GNUNET_PQ_query_param_string (instance_id), 39 32 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 40 32 : GNUNET_PQ_query_param_string (session_id), 41 32 : (choice_index >= 0) 42 4 : ? GNUNET_PQ_query_param_int16 (&choice_index) 43 32 : : GNUNET_PQ_query_param_null (), 44 : GNUNET_PQ_query_param_end 45 : }; 46 32 : struct GNUNET_PQ_QueryParam uparams[] = { 47 32 : GNUNET_PQ_query_param_string (instance_id), 48 32 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 49 : GNUNET_PQ_query_param_end 50 : }; 51 : enum GNUNET_DB_QueryStatus qs; 52 : 53 : /* Session ID must always be given by the caller. */ 54 32 : GNUNET_assert (NULL != session_id); 55 : 56 : /* no preflight check here, run in transaction by caller! */ 57 32 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 58 : "Marking h_contract_terms '%s' of %s as paid for session `%s'\n", 59 : GNUNET_h2s (&h_contract_terms->hash), 60 : instance_id, 61 : session_id); 62 32 : PREPARE (pg, 63 : "mark_contract_paid", 64 : "UPDATE merchant_contract_terms SET" 65 : " paid=TRUE" 66 : ",session_id=$3" 67 : ",choice_index=$4" 68 : " WHERE h_contract_terms=$2" 69 : " AND merchant_serial=" 70 : " (SELECT merchant_serial" 71 : " FROM merchant_instances" 72 : " WHERE merchant_id=$1)"); 73 32 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 74 : "mark_contract_paid", 75 : params); 76 32 : if (qs <= 0) 77 0 : return qs; 78 32 : PREPARE (pg, 79 : "mark_inventory_sold", 80 : "UPDATE merchant_inventory SET" 81 : " total_sold=total_sold + order_locks.total_locked" 82 : " FROM (SELECT total_locked,product_serial" 83 : " FROM merchant_order_locks" 84 : " WHERE order_serial=" 85 : " (SELECT order_serial" 86 : " FROM merchant_contract_terms" 87 : " WHERE h_contract_terms=$2" 88 : " AND merchant_serial=" 89 : " (SELECT merchant_serial" 90 : " FROM merchant_instances" 91 : " WHERE merchant_id=$1))" 92 : " ) AS order_locks" 93 : " WHERE merchant_inventory.product_serial" 94 : " =order_locks.product_serial"); 95 32 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 96 : "mark_inventory_sold", 97 : uparams); 98 32 : if (qs < 0) 99 0 : return qs; /* 0: no inventory management, that's OK! */ 100 : /* ON DELETE CASCADE deletes from merchant_order_locks */ 101 32 : PREPARE (pg, 102 : "delete_completed_order", 103 : "WITH md AS" 104 : " (SELECT merchant_serial" 105 : " FROM merchant_instances" 106 : " WHERE merchant_id=$1) " 107 : "DELETE" 108 : " FROM merchant_orders" 109 : " WHERE order_serial=" 110 : " (SELECT order_serial" 111 : " FROM merchant_contract_terms" 112 : " JOIN md USING (merchant_serial)" 113 : " WHERE h_contract_terms=$2)"); 114 32 : return GNUNET_PQ_eval_prepared_non_select (pg->conn, 115 : "delete_completed_order", 116 : uparams); 117 : }