All files / build/helpers validators.js

98.61% Statements 71/72
88.89% Branches 16/18
95.45% Functions 21/22
100% Lines 59/59
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144    18x             3x 6x 6x 14x   14x 7x 7x   14x   6x               3x 3x   5x 15x 18x   5x 5x     5x 5x             5x   5x 6x 6x   5x 7x   2x     5x 3x 3x     2x     3x       5x           3x 5x               3x 3x   4x 12x 14x   4x 4x     4x 4x             4x   4x 5x 5x   4x 6x   2x     4x 2x 2x     2x     2x       4x           3x 4x     3x        
"use strict";
 
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
 
/**
 * 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
 */
var areCharacteristicsInSameTree = function areCharacteristicsInSameTree(characteristics) {
  var areInSameTree = false;
  areInSameTree = characteristics.reduce(function (prev, _ref) {
    var curr = _ref.dataValues.tree_id;
 
    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.
 */
var validateScopeNames = function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(scopeNames, db) {
    var errors, result, scopes;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            errors = [];
            result = {};
            // Determines if the specified scope is valid.
 
            _context.next = 4;
            return db.scopes.findAll({
              where: {
                name: scopeNames
              }
            });
 
          case 4:
            scopes = _context.sent;
 
            result = scopes.reduce(function (keys, scope) {
              keys[scope.dataValues.name] = scope.dataValues.id;
              return keys;
            }, {});
            errors = errors.concat(scopeNames.filter(function (scopeName) {
              return result[scopeName] === undefined;
            }).map(function (scopeName) {
              return "Invalid Scope: " + scopeName;
            }));
 
            if (!(errors.length > 0)) {
              _context.next = 9;
              break;
            }
 
            throw errors;
 
          case 9:
            return _context.abrupt("return", result);
 
          case 10:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));
 
  return function validateScopeNames(_x, _x2) {
    return _ref2.apply(this, arguments);
  };
}();
/**
 * 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.
 */
var validateCharacteristicNames = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(characteristicNames, db) {
    var errors, result, characteristics;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            errors = [];
            result = {};
            // Determines if the specified scope is valid.
 
            _context2.next = 4;
            return db.characteristics.findAll({
              where: {
                name: characteristicNames
              }
            });
 
          case 4:
            characteristics = _context2.sent;
 
            result = characteristics.reduce(function (keys, scope) {
              keys[scope.dataValues.name] = scope.dataValues.id;
              return keys;
            }, {});
            errors = errors.concat(characteristicNames.filter(function (characteristicName) {
              return result[characteristicName] === undefined;
            }).map(function (characteristicName) {
              return "Invalid Characteristic: " + characteristicName;
            }));
 
            if (!(errors.length > 0)) {
              _context2.next = 9;
              break;
            }
 
            throw errors;
 
          case 9:
            return _context2.abrupt("return", result);
 
          case 10:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, undefined);
  }));
 
  return function validateCharacteristicNames(_x3, _x4) {
    return _ref3.apply(this, arguments);
  };
}();
module.exports = {
  areCharacteristicsInSameTree: areCharacteristicsInSameTree,
  validateScopeNames: validateScopeNames,
  validateCharacteristicNames: validateCharacteristicNames
};