/*
  This file is part of Anastasis
  Copyright (C) 2020-2022 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/update_to_challenge_payment_paid.c
 * @brief Anastasis database: update to challenge payment paid
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/update_to_challenge_payment_paid.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Update payment status of challenge
 *
 * @param truth_uuid which challenge received a payment
 * @param payment_identifier proof of payment, must be unique and match pending payment
 * @param amount what was actually paid, in the currency it was paid in
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_update_to_challenge_payment_paid (
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  const struct ANASTASIS_PaymentSecretP *payment_identifier,
  const struct TALER_Amount *amount)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (payment_identifier),
    GNUNET_PQ_query_param_auto_from_type (truth_uuid),
    TALER_PQ_query_param_amount_with_currency (pg->conn,
                                               amount),
    GNUNET_PQ_query_param_end
  };

  /* Until now the row held the asking price in the provider's primary
     currency, because at order creation time it was not yet known which
     of the offered currencies the wallet would settle in.  Now it is, and
     a later refund must return that one. */
  PREPARE ("update_to_challenge_payment_paid",
           "UPDATE anastasis_challenge_payment "
           "SET"
           " paid=TRUE"
           ",amount=$3 "
           "WHERE"
           "  payment_identifier=$1"
           " AND"
           "  refunded=FALSE"
           " AND"
           "  truth_uuid=$2"
           " AND"
           "  paid=FALSE;");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "update_to_challenge_payment_paid",
                                             params);
}


/* end of update_to_challenge_payment_paid.c */
