/*
  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_recdoc_payment.c
 * @brief Anastasis database: get recdoc payment
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/get_recdoc_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 account_pub account the payment must belong to
 * @param payment_secret payment secret which the user must provide with every upload
 * @param[out] paid bool value to show if payment is paid
 * @param[out] valid_counter bool value to show if post_counter is > 0
 * @param[out] creation_date when was the payment record created
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_recdoc_payment (
  const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
  const struct ANASTASIS_PaymentSecretP *payment_secret,
  bool *paid,
  bool *valid_counter,
  struct GNUNET_TIME_Timestamp *creation_date)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (account_pub),
    GNUNET_PQ_query_param_auto_from_type (payment_secret),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("paid",
                                paid),
    GNUNET_PQ_result_spec_bool ("valid_counter",
                                valid_counter),
    GNUNET_PQ_result_spec_timestamp ("creation_date",
                                     creation_date),
    GNUNET_PQ_result_spec_end
  };

  *creation_date = GNUNET_TIME_UNIT_ZERO_TS;
  /* The payment identifier is chosen by the client, so it is only meaningful
     together with the account it was issued for: without the user_id an
     identifier belonging to one account would be accepted as credit for
     another, and a lookup would answer with a row that the caller then goes
     on to treat as its own. */
  PREPARE ("get_recdoc_payment",
           "SELECT"
           " creation_date"
           ",post_counter > 0 AS valid_counter"
           ",paid"
           " FROM anastasis_recdoc_payment"
           " WHERE user_id=$1"
           "   AND payment_identifier=$2;");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_recdoc_payment",
                                                   params,
                                                   rs);
}


/* end of get_recdoc_payment.c */
