/**
 * ----------------------------------------------------------------------------------------------------
 * Get Quorum Transaction Receipt [Run]
 *
 * @description - Get quorum transaction receipt using the Tatum API
 *
 * @author    Buildable Technologies Inc.
 * @access    open
 * @license   MIT
 * @docs      https://tatum.io/apidoc.php#operation/QuorumGetTransactionReceipt
 *
 * ----------------------------------------------------------------------------------------------------
 */

const axios = require("axios");

/**
 * The Node’s executable function
 *
 * @param {Run} input - Data passed to your Node from the input function
 */
const run = async (input) => {
  const { TATUM_API_KEY, TATUM_API_URL, quorumEndpoint, hash } = input;

  verifyInput(input);

  try {
    const { data } = await axios({
      method: "get",
      url: `${TATUM_API_URL}/v3/quorum/transaction/${hash}/receipt`,
      headers: {
        "x-quorum-endpoint": quorumEndpoint,
        "x-api-key": TATUM_API_KEY,
      },
    });

    return data;
  } catch (error) {
    return {
      failed: true,
      message: error.message,
      data: error.response.data,
    };
  }
};

/**
 * Verifies the input parameters
 */
const verifyInput = ({
  TATUM_API_KEY,
  TATUM_API_URL,
  quorumEndpoint,
  hash,
}) => {
  const ERRORS = {
    INVALID_TATUM_API_KEY:
      "A valid TATUM_API_KEY field (string) was not provided in the input.",
    INVALID_TATUM_API_URL:
      "A valid TATUM_API_URL field (string) was not provided in the input.",
    INVALID_QUORUM_ENDPOINT:
      "A valid quorumEndpoint field (string) was not provided in the input.",
    INVALID_HASH: "A valid hash field (string) was not provided in the input.",
  };

  if (typeof TATUM_API_KEY !== "string")
    throw new Error(ERRORS.INVALID_TATUM_API_KEY);
  if (typeof TATUM_API_URL !== "string")
    throw new Error(ERRORS.INVALID_TATUM_API_URL);
  if (typeof quorumEndpoint !== "string")
    throw new Error(ERRORS.INVALID_QUORUM_ENDPOINT);
  if (typeof hash !== "string") throw new Error(ERRORS.INVALID_HASH);
};
