All files validators.js

100% Statements 35/35
100% Branches 6/6
100% Functions 10/10
100% Lines 35/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83          3x 6x 6x   14x 7x 7x   14x     6x               3x 5x 5x   5x         5x   6x 6x   5x   7x 2x   5x 2x   3x             3x 4x 4x   4x         4x   5x 5x   4x   6x 2x   4x 2x   2x   3x          
/**
 * Given an array of characteristics database objects then determine if they are in the same tree;
 * @param {*} characteristics[] Database object that represents a characteristic item.
 * @returns {boolean} True if all the characteristics are of the same tree
 */
const areCharacteristicsInSameTree = (characteristics) => {
  let areInSameTree = false;
  areInSameTree = characteristics
    .reduce((prev, { dataValues: { tree_id: curr } }) => {
      if (!prev.distinctTrees[curr.toString()]) {
        prev.distinctTrees[curr.toString()] = 1;
        prev.countDistinctTrees += 1;
      }
      return prev;
    },
    { distinctTrees: [], countDistinctTrees: 0 }).countDistinctTrees === 1;
  return areInSameTree;
};
/**
 * Verify if the names that are specified are scopes that are valid(exists).
 * @param {string[]} scopeNames
 * @param {*} db Object that will have access to current database.
 * @returns {*} Return an object that contains as keys the scopeNames and as value their id.
 */
const validateScopeNames = async (scopeNames, db) => {
  let errors = [];
  let result = {};
  // Determines if the specified scope is valid.
  const scopes = await db.scopes.findAll({
    where: {
      name: scopeNames,
    },
  });
  result = scopes
    .reduce((keys, scope) => {
      keys[scope.dataValues.name] = scope.dataValues.id;
      return keys;
    }, {});
  errors = errors.concat(
    scopeNames
      .filter(scopeName => result[scopeName] === undefined)
      .map(scopeName => `Invalid Scope: ${scopeName}`),
  );
  if (errors.length > 0) {
    throw errors;
  }
  return result;
};
/**
 * Verify if the names that are specified are existing characteristics.
 * @param {string[]} characteristicNames
 * @returns {*} Return an object that contains as keys the characetristcNames and as value their id.
 */
const validateCharacteristicNames = async (characteristicNames, db) => {
  let errors = [];
  let result = {};
  // Determines if the specified scope is valid.
  const characteristics = await db.characteristics.findAll({
    where: {
      name: characteristicNames,
    },
  });
  result = characteristics
    .reduce((keys, scope) => {
      keys[scope.dataValues.name] = scope.dataValues.id;
      return keys;
    }, {});
  errors = errors.concat(
    characteristicNames
      .filter(characteristicName => result[characteristicName] === undefined)
      .map(characteristicName => `Invalid Characteristic: ${characteristicName}`),
  );
  if (errors.length > 0) {
    throw errors;
  }
  return result;
};
module.exports = {
  areCharacteristicsInSameTree,
  validateScopeNames,
  validateCharacteristicNames,
};