LCOV - code coverage report
Current view: top level - backenddb - insert_contract_terms.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 85.2 % 27 23
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) 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 src/backenddb/insert_contract_terms.c
      18              :  * @brief Implementation of the insert_contract_terms function for Postgres
      19              :  * @author Iván Ávalos
      20              :  */
      21              : #include "platform.h"
      22              : #include <taler/taler_pq_lib.h>
      23              : #include "merchant-database/insert_contract_terms.h"
      24              : #include "helper.h"
      25              : 
      26              : enum GNUNET_DB_QueryStatus
      27          129 : TALER_MERCHANTDB_insert_contract_terms (
      28              :   struct TALER_MERCHANTDB_PostgresContext *pg,
      29              :   const char *instance_id,
      30              :   const char *order_id,
      31              :   json_t *contract_terms,
      32              :   uint64_t *order_serial)
      33              : {
      34              :   struct GNUNET_TIME_Timestamp pay_deadline;
      35              :   struct GNUNET_TIME_Timestamp refund_deadline;
      36              :   const char *fulfillment_url;
      37              :   struct TALER_PrivateContractHashP h_contract_terms;
      38              : 
      39          129 :   if (GNUNET_OK !=
      40          129 :       TALER_JSON_contract_hash (contract_terms,
      41              :                                 &h_contract_terms))
      42              :   {
      43            0 :     GNUNET_break (0);
      44            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
      45              :   }
      46              : 
      47              :   {
      48              :     struct GNUNET_JSON_Specification spec[] = {
      49          129 :       GNUNET_JSON_spec_timestamp ("pay_deadline",
      50              :                                   &pay_deadline),
      51          129 :       GNUNET_JSON_spec_timestamp ("refund_deadline",
      52              :                                   &refund_deadline),
      53          129 :       GNUNET_JSON_spec_end ()
      54              :     };
      55              :     enum GNUNET_GenericReturnValue res;
      56              :     const char *error_json_name;
      57              :     unsigned int error_line;
      58              : 
      59          129 :     res = GNUNET_JSON_parse (contract_terms,
      60              :                              spec,
      61              :                              &error_json_name,
      62              :                              &error_line);
      63          129 :     if (GNUNET_OK != res)
      64              :     {
      65            0 :       GNUNET_break (0);
      66            0 :       return GNUNET_DB_STATUS_HARD_ERROR;
      67              :     }
      68              :   }
      69              : 
      70              :   fulfillment_url =
      71          129 :     json_string_value (json_object_get (contract_terms,
      72              :                                         "fulfillment_url"));
      73          129 :   GNUNET_assert (NULL != pg->current_merchant_id);
      74          129 :   GNUNET_assert (0 == strcmp (instance_id,
      75              :                               pg->current_merchant_id));
      76              :   {
      77          129 :     struct GNUNET_PQ_QueryParam params[] = {
      78          129 :       GNUNET_PQ_query_param_string (order_id),
      79          129 :       TALER_PQ_query_param_json (contract_terms),
      80          129 :       GNUNET_PQ_query_param_auto_from_type (&h_contract_terms),
      81          129 :       GNUNET_PQ_query_param_timestamp (&pay_deadline),
      82          129 :       GNUNET_PQ_query_param_timestamp (&refund_deadline),
      83              :       (NULL == fulfillment_url)
      84           30 :       ? GNUNET_PQ_query_param_null ()
      85          129 :       : GNUNET_PQ_query_param_string (fulfillment_url),
      86              :       GNUNET_PQ_query_param_end
      87              :     };
      88          129 :     struct GNUNET_PQ_ResultSpec rs[] = {
      89          129 :       GNUNET_PQ_result_spec_uint64 ("order_serial",
      90              :                                     order_serial),
      91              :       GNUNET_PQ_result_spec_end
      92              :     };
      93              : 
      94          129 :     TMH_PQ_prepare_anon (pg,
      95              :                          "INSERT INTO merchant_contract_terms"
      96              :                          "(order_serial"
      97              :                          ",order_id"
      98              :                          ",contract_terms"
      99              :                          ",h_contract_terms"
     100              :                          ",creation_time"
     101              :                          ",pay_deadline"
     102              :                          ",refund_deadline"
     103              :                          ",fulfillment_url"
     104              :                          ",claim_token"
     105              :                          ",pos_key"
     106              :                          ",pos_algorithm)"
     107              :                          "SELECT"
     108              :                          " mo.order_serial"
     109              :                          ",mo.order_id"
     110              :                          ",$2::TEXT::JSONB" /* contract_terms */
     111              :                          ",$3" /* h_contract_terms */
     112              :                          ",mo.creation_time"
     113              :                          ",$4" /* pay_deadline */
     114              :                          ",$5" /* refund_deadline */
     115              :                          ",$6" /* fulfillment_url */
     116              :                          ",mo.claim_token"
     117              :                          ",mo.pos_key"
     118              :                          ",mo.pos_algorithm"
     119              :                          " FROM merchant_orders mo"
     120              :                          " WHERE order_id=$1"
     121              :                          " RETURNING order_serial");
     122          129 :     return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     123              :                                                      "",
     124              :                                                      params,
     125              :                                                      rs);
     126              :   }
     127              : }
        

Generated by: LCOV version 2.0-1