"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
}; |