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


/**
 * Lookup pending payment for a certain challenge.
 *
 * @param truth_uuid identification of the challenge
 * @param[out] payment_secret set to the challenge payment secret
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_pending_challenge_payment (
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  struct ANASTASIS_PaymentSecretP *payment_secret)
{
  struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  struct GNUNET_TIME_Timestamp recent
    = GNUNET_TIME_absolute_to_timestamp (
        GNUNET_TIME_absolute_subtract (now,
                                       ANASTASIS_CHALLENGE_OFFER_LIFETIME));
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (truth_uuid),
    GNUNET_PQ_query_param_timestamp (&recent),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_auto_from_type ("payment_identifier",
                                          payment_secret),
    GNUNET_PQ_result_spec_end
  };

  PREPARE ("get_pending_challenge_payment",
           "SELECT"
           " creation_date"
           ",payment_identifier"
           " FROM anastasis_challenge_payment"
           " WHERE"
           "  paid=FALSE"
           " AND"
           "  refunded=FALSE"
           " AND"
           "  truth_uuid=$1"
           " AND"
           "  creation_date > $2"
           /* Nothing stops a truth from having more than one pending offer:
              the callers check for one and then insert after a round trip to
              the merchant, so two concurrent challenge requests race and both
              insert.  Without this the singleton select would then report a
              hard error.  Point the client at the most recent offer. */
           " ORDER BY creation_date DESC"
           " LIMIT 1;");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_pending_challenge_payment",
                                                   params,
                                                   rs);
}


/* end of get_pending_challenge_payment.c */
