{"size": 1055, "ext": "js", "max_stars_repo_path": "src/response/response.js", "max_stars_repo_name": "Nevon/express-physical", "max_stars_repo_stars_event_min_datetime": "2017-04-04T10:29:23.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-29T14:29:34.000Z", "max_issues_repo_path": "src/response/response.js", "max_issues_repo_name": "Nevon/express-physical", "max_issues_repo_issues_event_min_datetime": "2017-04-20T13:09:59.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-11T08:08:26.000Z", "max_forks_repo_path": "src/response/response.js", "max_forks_repo_name": "Nevon/express-physical", "max_forks_repo_forks_event_min_datetime": "2019-04-02T07:45:31.000Z", "max_forks_repo_forks_event_max_datetime": "2019-04-02T07:45:31.000Z"}
{"max_stars_count": 22, "max_issues_count": 294, "max_forks_count": 1, "avg_line_length": 21.9791666667, "max_line_length": 77, "alphanum_fraction": 0.7194312796}
"use strict";
const R = require("ramda");

const InvalidHealthcheckResponse = require("./invalid-healthcheck-response");
const validate = require("./validate");

const RESPONSE_FORMAT = {
  name: undefined,
  healthy: undefined,
  actionable: undefined,
  type: undefined,
  severity: undefined,
  message: undefined,
  dependentOn: undefined,
  info: undefined,
  link: undefined
};

const createResponse = responseData =>
  R.pick(Object.keys(RESPONSE_FORMAT), responseData);

const someFail = results =>
  R.not(R.all(R.equals(true), Object.values(results)));

const isMessage = R.compose(
  R.not,
  R.is(Boolean)
);
const firstMessage = R.compose(
  R.head,
  R.filter(isMessage),
  R.values
);

module.exports = function(responseData) {
  const validatable = Object.assign({}, RESPONSE_FORMAT, responseData);
  const validationResult = validate(validatable);

  if (someFail(validationResult)) {
    throw new InvalidHealthcheckResponse(
      firstMessage(validationResult),
      responseData
    );
  }

  return createResponse(responseData);
};
