{"size": 4604, "ext": "js", "max_stars_repo_path": "service/film.validate.js", "max_stars_repo_name": "sungmino/film-store", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "service/film.validate.js", "max_issues_repo_name": "sungmino/film-store", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "service/film.validate.js", "max_forks_repo_name": "sungmino/film-store", "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": 18.3426294821, "max_line_length": 72, "alphanum_fraction": 0.6166377063}
const { checkString } = require('../utils/checkString');
const { checkPositiveNumber } = require('../utils/checkPositiveNumber');
const { Err } = require('../config/constant');

const checkGetAllFilmsInput = input => {
  const { page, records } = input;

  // Check pagination
  return checkPagination(page, records);
}

const checkAddFilmInput = input => {
  let error = new Err();

  // Required fields
  const fields = [
    'title',
    'category',
    'episodeNumber',
    'directors',
    'characters',
    'description',
    'image',
    'thumb',
    'type',
    'links',
    'tags'
  ];

  fields.forEach(field => {
    if (input[field] === '' || !input[field]) {
      error.isError = true;
      error.errorMessage[field] = field + ' must not be empty';

      return error;
    }
  });

  return error;
}

const checkUpdateFilmInput = input => {
  const { query, body } = input;
  let error = new Err();
  const { id } = query;

  error = checkId(id);
  if (error.isError) {
    return error;
  }

  if (!Object.keys(body).length) {
    error.isError = true;
    error.errorMessage.body = errorMessage;

    return error;
  }

  // Required fields
  const fields = [
    'title',
    'supportedResolution',
    'dateReleased',
    'dateUpdated',
    'category',
    'country',
    'time',
    'episodeNumber',
    'imdb',
    'scripts',
    'directors',
    'characters',
    'description',
    'trailer',
    'image',
    'thumb',
    'type',
    'tags',
    'content'
  ];

  fields.forEach(field => {
    if (body[field]) {
      if (body[field] === '' || body[field].length === 0) {
        error.isError = true;
        error.errorMessage[field] = field + ' must not be empty';

        return error;
      }
    }
  });

  return error;
}

const checkGetFilmByIdInput = input => {
  const { id } = input;

  return checkId(id);
}

const checkFilterFilmsInput = input => {
  const { page, records } = input;

  return checkPagination(page, records);
}

const checkSearchFilmByFieldInput = input => {
  const { page, records } = input;

  // Check pagination
  let error = checkPagination(page, records);
  if (error.isError) {
    return error;
  }

  // Check input
  error = checkSearchFilmByField(input);
  if (error.isError) {
    return error;
  }

  return error;
}

const checkSearchFilmByField = input => {
  const { page, records } = input;

  // Check pagination
  let error = checkPagination(page, records);
  if (error.isError) {
    return error;
  }

  // Required fields
  const fields = [
    'field',
    'value'
  ];

  fields.forEach(field => {
    if (input[field] === '' || !input[field]) {
      error.isError = true;
      error.errorMessage[field] = field + ' must not be empty';

      return error;
    }
  });

  return error;
}

const checkSearchFilmInput = input => {
  const { value, page, records } = input;

  // Check pagination
  let error = checkPagination(page, records);
  if (error.isError) {
    return error;
  }

  // Check input
  const err = checkString(value);

  if (err.isError) {
    error.isError = true;
    error.errorMessage.value = 'value ' + err.message;

    return error;
  }

  return error;
}

const checkDeleteFilmInput = input => {
  const { id } = input;

  return checkId(id);
}

const checkGetRelatedFilmsInput = input => {
  const { id, page, records } = input;

  let error = checkId(id);
  if (error.isError) {
    return error;
  }

  // Check pagination
  error = checkPagination(page, records);
  if (error.isError) {
    return error;
  }

  return error;
}

const checkId = id => {
  let error = new Err();

  const err = checkString(id);

  if (err.isError) {
    error.isError = true;
    error.errorMessage.id = 'id ' + err.message;

    return error;
  }

  return error;
}

const checkPagination = (page, records) => {
  let error = new Err();

  // Comment this to not alow page and records undefined
  if (page === undefined && records === undefined) {
    return error;
  }

  let check = checkPositiveNumber(page);
  if (check.isError) {
    error.isError = true;
    error.errorMessage.page = 'page ' + check.message;

    return error;
  }

  check = checkPositiveNumber(records);
  if (check.isError) {
    error.isError = true;
    error.errorMessage.records = 'records ' + check.message;

    return error;
  }

  return error;
}

module.exports = {
  checkGetAllFilmsInput,
  checkAddFilmInput,
  checkUpdateFilmInput,
  checkGetFilmByIdInput,
  checkFilterFilmsInput,
  checkSearchFilmByFieldInput,
  checkSearchFilmByField,
  checkSearchFilmInput,
  checkDeleteFilmInput,
  checkGetRelatedFilmsInput
}