{"size": 1328, "ext": "js", "max_stars_repo_path": "lib/dataContract/stateTransition/validation/validateDataContractSTDataFactory.js", "max_stars_repo_name": "jawid-h/js-dpp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/dataContract/stateTransition/validation/validateDataContractSTDataFactory.js", "max_issues_repo_name": "jawid-h/js-dpp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/dataContract/stateTransition/validation/validateDataContractSTDataFactory.js", "max_forks_repo_name": "jawid-h/js-dpp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.6666666667, "max_line_length": 99, "alphanum_fraction": 0.7371987952}
const ValidationResult = require('../../../validation/ValidationResult');

const DataContractAlreadyPresentError = require('../../../errors/DataContractAlreadyPresentError');

/**
 *
 * @param {DataProvider} dataProvider
 * @param {validateBlockchainUser} validateBlockchainUser
 * @return {validateDataContractSTData}
 */
function validateDataContractSTDataFactory(dataProvider, validateBlockchainUser) {
  /**
   * @typedef validateDataContractSTData
   * @param {DataContractStateTransition} stateTransition
   * @return {ValidationResult}
   */
  async function validateDataContractSTData(stateTransition) {
    const result = new ValidationResult();

    const dataContract = stateTransition.getDataContract();
    const dataContractId = dataContract.getId();

    // Data Contract identity must exists and confirmed
    result.merge(
      await validateBlockchainUser(dataContractId),
    );

    if (!result.isValid()) {
      return result;
    }

    // Data contract shouldn't exist
    const existingDataContract = await dataProvider.fetchDataContract(dataContractId);

    if (existingDataContract) {
      result.addError(
        new DataContractAlreadyPresentError(dataContract),
      );
    }

    return result;
  }

  return validateDataContractSTData;
}

module.exports = validateDataContractSTDataFactory;
