All files / build/helpers parsers.js

99.17% Statements 119/120
88.89% Branches 40/45
100% Functions 28/28
99.07% Lines 106/107
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268    30x   2x 2x 2x 2x                   2x 2x   4x 12x 14x   4x 4x             4x 4x 12x 24x   12x 6x         6x         12x     4x 8x   2x 2x     2x 6x       2x       4x           2x 4x     2x                                   5x 5x 5x 5x   5x 5x 5x 4x   1x   5x 4x   1x   5x 4x   1x   5x 4x   1x   5x 1x   4x     2x                         2x   5x 13x 16x   5x 5x   5x 4x 4x 4x   1x             1x     4x 3x 3x     1x     3x 3x 3x       2x 2x 2x 2x       1x 1x       5x           2x 5x                           2x   4x 14x 18x   4x 4x 4x 4x     4x   4x 4x 4x           4x 4x 4x     2x   2x 6x   2x 6x   2x       4x           2x 4x           2x      
'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"); }); }; }
 
var _require = require('./validators'),
    validateScopeNames = _require.validateScopeNames,
    validateCharacteristicNames = _require.validateCharacteristicNames,
    areCharacteristicsInSameTree = _require.areCharacteristicsInSameTree;
 
/**
 * Given an array of scopeIds create an array with the referenceIds and scopeId with the same order.
 * @param {*} scopeIds Array of scope ids. The array has to be ordered based on priority.
 * @param {*} referenceIds Object that contains keys with same name that scopes.
 * Each key will have a comma separated string with all the referencedIds.
 */
 
 
var extractScopesFromRequestOrderByPriority = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(scopeIds, referenceIds, db) {
    var scopes, orderedReferenceIds;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return db.scopes.findAll({
              where: {
                id: scopeIds
              }
            });
 
          case 2:
            scopes = _context.sent;
            orderedReferenceIds = scopeIds.reduce(function (prev, scopeId) {
              var scopeName = scopes.find(function (scope) {
                return scope.dataValues.id === scopeId;
              }).dataValues.name;
              if (scopeName && Array.isArray(referenceIds[scopeName])) {
                prev.push({
                  ids: referenceIds[scopeName],
                  name: scopeName
                });
              } else {
                prev.push({
                  ids: [],
                  name: scopeName
                });
              }
              return prev;
            }, []);
 
            if (orderedReferenceIds.find(function (x) {
              return x.ids.length > 0;
            })) {
              _context.next = 6;
              break;
            }
 
            throw new Error('The hierachy for this/those characteristic(s) need(s) the following queryParameters: ' + scopes.map(function (scope) {
              return scope.dataValues.name;
            }).join(', '));
 
          case 6:
            return _context.abrupt('return', orderedReferenceIds);
 
          case 7:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));
 
  return function extractScopesFromRequestOrderByPriority(_x, _x2, _x3) {
    return _ref.apply(this, arguments);
  };
}();
var CharacteristicScopeValue = {
  /**
   * This function determines if the object is a valid CharacteristicScopeValue and return it.
   * To be a valid object needs:
   * - scope, characteristic, value must be an string
   * - defined referenceId
   * @param {*} characteristicScopeValue
   * @param {String} characteristicScopeValue.scope
   * @param {String} characteristicScopeValue.characteristic
   * @param {*} characteristicScopeValue.referenceId
   * @param {String} characteristicScopeValue.value
   * @returns {*} result
   * @returns {String} result.scope
   * @returns {String} result.characteristic
   * @returns {String} result.referenceId
   * @returns {String} result.value
   */
  parse: function parse(_ref2) {
    var scope = _ref2.scope,
        characteristic = _ref2.characteristic,
        referenceId = _ref2.referenceId,
        value = _ref2.value;
 
    var result = {};
    var errors = [];
    if (typeof scope === 'string') {
      result.scope = scope;
    } else {
      errors.push('Invalid scope');
    }
    if (typeof characteristic === 'string') {
      result.characteristic = characteristic;
    } else {
      errors.push('Invalid characteristic');
    }
    if (referenceId != null && referenceId !== undefined) {
      result.referenceId = referenceId.toString();
    } else {
      errors.push('Invalid referenceId');
    }
    if (typeof value === 'string') {
      result.value = value;
    } else {
      errors.push('Invalid value');
    }
    if (errors.length > 0) {
      throw errors;
    }
    return result;
  }
};
var CharacterRequest = {
  Post: {
    /** *
     * This function check if the body from the request contains all
     * the data that needs to be included.
     * Data that needs to be included:
     * - newValues needs to be an array
     * - each element needs to be a valid CharacteristicScopeValue.
     * - All the specified scopes are configured in gen-characteristics db.
     * - All the specified characteristics are configured in our db.
     * @param {*} bodyRequest[] Array of values that need to be stored.
     */
    parse: function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(newValues, db) {
        var result, errors;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                result = {};
                errors = [];
 
                if (Array.isArray(newValues)) {
                  result.newValues = newValues.map(function (characteristicScopeValue, idx) {
                    try {
                      return CharacteristicScopeValue.parse(characteristicScopeValue);
                    } catch (error) {
                      throw new Error({
                        idx: idx,
                        errors: error
                      });
                    }
                  });
                } else {
                  errors.push('Invalid newValues');
                }
 
                if (!(errors.length > 0)) {
                  _context2.next = 5;
                  break;
                }
 
                throw errors;
 
              case 5:
                _context2.next = 7;
                return validateScopeNames(result.newValues.map(function (characteristicScopeValue) {
                  return characteristicScopeValue.scope;
                }), db);
 
              case 7:
                result.scopeIdByName = _context2.sent;
                _context2.next = 10;
                return validateCharacteristicNames(result.newValues.map(function (x) {
                  return x.characteristic;
                }), db);
 
              case 10:
                result.characteristicIdByName = _context2.sent;
                return _context2.abrupt('return', result);
 
              case 12:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, undefined);
      }));
 
      return function parse(_x4, _x5) {
        return _ref3.apply(this, arguments);
      };
    }()
  },
  Get: {
    /**
     * This parser creates a sort of data based ond the queryRequest
     * @param queryRequest {*} All the information of the requested information
     * @returns priorityScopeIds {*} Array of the scope id's ordered by priority.
     * @returns referenceIdsArray {*} Array of same length that priorityScopeIds
     * that includes for each position the referenceIds for the scope ith.
     * @returns scopeNamesArray {*} Array of the scope name's ordered by priority.
     */
    parse: function () {
      var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(queryRequest, db) {
        var result, temp;
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                result = {};
                temp = queryRequest || {};
                _context3.next = 4;
                return db.characteristics.findAllByName(temp.characteristics);
 
              case 4:
                result.persistedCharacteristics = _context3.sent;
 
                Eif (areCharacteristicsInSameTree(result.persistedCharacteristics)) {
                  _context3.next = 7;
                  break;
                }
 
                throw new Error('Sorry, this characteristics seem to be in different trees, we can“t handle that');
 
              case 7:
                result.priorityScopeIds = result.persistedCharacteristics[0].dataValues.tree.getScopeIdsOrderByPriority();
                _context3.next = 10;
                return extractScopesFromRequestOrderByPriority(result.priorityScopeIds, temp, db);
 
              case 10:
                result.referenceIdsArray = _context3.sent;
 
                result.scopeNamesArray = result.referenceIdsArray.map(function (x) {
                  return x.name;
                });
                result.referenceIdsArray = result.referenceIdsArray.map(function (x) {
                  return x.ids;
                });
                return _context3.abrupt('return', result);
 
              case 14:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, undefined);
      }));
 
      return function parse(_x6, _x7) {
        return _ref4.apply(this, arguments);
      };
    }()
  }
};
 
module.exports = {
  CharacterRequest: CharacterRequest,
  CharacteristicScopeValue: CharacteristicScopeValue
};