/*
   This file is part of Challenger
   Copyright (C) 2023 Taler Systems SA

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

   Challenger 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   Challenger; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file src/challengerdb/do_insert_validation.c
 * @brief Implementation of the do_insert_validation function for Postgres
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "do_insert_validation.h"
#include "pg_helper.h"


enum GNUNET_DB_QueryStatus
CHALLENGERDB_do_insert_validation (struct CHALLENGERDB_PostgresContext *ctx,
                                   uint64_t client_id,
                                   const char *client_secret,
                                   const struct
                                   CHALLENGER_ValidationNonceP *nonce,
                                   struct GNUNET_TIME_Absolute expiration_time,
                                   const json_t *initial_address)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_uint64 (&client_id),
    GNUNET_PQ_query_param_string (client_secret),
    GNUNET_PQ_query_param_auto_from_type (nonce),
    GNUNET_PQ_query_param_absolute_time (&expiration_time),
    NULL == initial_address
    ? GNUNET_PQ_query_param_null ()
    : TALER_PQ_query_param_json (initial_address),
    GNUNET_PQ_query_param_end
  };

  /* Authenticating the client, charging it for the validation and creating
     the validation row must happen together: the counter may only move if
     the validation is really created.  Doing it in one statement also saves
     the round trips of an explicit transaction.  A client that does not
     exist (or presents the wrong secret) leaves the CTE empty, so nothing is
     inserted and the caller sees NO_RESULTS. */
  PREPARE (ctx,
           "do_insert_validation",
           "WITH charged AS ("
           "  UPDATE clients"
           "     SET validation_counter=validation_counter+1"
           "   WHERE client_serial_id=$1"
           "     AND client_secret=$2"
           "  RETURNING client_serial_id, uri"
           ") INSERT INTO validations"
           " (client_serial_id"
           " ,nonce"
           " ,expiration_time"
           " ,client_redirect_uri"
           " ,address"
           ") SELECT client_serial_id, $3, $4, uri, $5"
           " FROM charged;");
  return GNUNET_PQ_eval_prepared_non_select (ctx->conn,
                                             "do_insert_validation",
                                             params);
}
