/*
  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/get_challenge_payment.c
 * @brief Anastasis database: get challenge payment
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/get_challenge_payment.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Check payment identifier. Used to check if a payment identifier given by
 * the user is valid (existing and paid).
 *
 * @param payment_secret payment secret which the user must provide with every upload
 * @param truth_uuid which truth should we check the payment status of
 * @param[out] paid bool value to show if payment is paid
 * @param[out] amount set to the amount that was paid, in the currency it was
 *             paid in; only meaningful if @a paid is true, as the row still
 *             carries the asking price until the payment is confirmed
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_challenge_payment (
  const struct ANASTASIS_PaymentSecretP *payment_secret,
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  bool *paid,
  struct TALER_Amount *amount)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (payment_secret),
    GNUNET_PQ_query_param_auto_from_type (truth_uuid),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("paid",
                                paid),
    TALER_PQ_result_spec_amount_with_currency ("amount",
                                               amount),
    GNUNET_PQ_result_spec_end
  };

  /* The amount is read back because a refund has to return what was
     actually paid: with several currencies on offer the configured price
     no longer identifies it. */
  PREPARE ("get_challenge_payment",
           "SELECT"
           "  paid"
           " ,amount"
           "  FROM anastasis_challenge_payment"
           " WHERE payment_identifier=$1"
           "   AND truth_uuid=$2"
           "   AND refunded=FALSE"
           "   AND counter>0;");
  return GNUNET_PQ_eval_prepared_singleton_select (
    pg->conn,
    "get_challenge_payment",
    params,
    rs);
}


/* end of get_challenge_payment.c */
