| 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 |
3x
1x
1x
1x
5x
6x
6x
6x
6x
5x
1x
1x
1x
1x
1x
1x
4x
4x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
2x
2x
2x
3x
2x
2x
2x
1x
1x
1x
1x
1x
1x
1x
| '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('./parsers'),
CharacterRequest = _require.CharacterRequest;
/**
* This method creates an object with all the specified keys, and inside of each value of those
* keys will be an object with characteristic names.
* @param {*} keys
* @param {*} characteristicNames
* @param {*} initialValue
*/
var buildKeyCharacteristicTree = function buildKeyCharacteristicTree(keys, characteristicNames, initialValue) {
var result = (keys || []).reduce(function (prevRes, refId) {
prevRes[refId.toString()] = (characteristicNames || []).reduce(function (previousValue, currentValue) {
previousValue[currentValue] = JSON.parse(JSON.stringify(initialValue));
return previousValue;
}, {});
return prevRes;
}, {});
return result;
};
/**
* Makes all the tasks that need to be done to store(upsert)
* the value of a characteristic into database.
* @param {*} db Database object that will handle the connection.
* @param {*[]} newValues Values to be stored
* @param {String} newValues[].scope Name of the scope object that is going to be used.
* @param {String} newValues[].characteristic Name of the characteristic that wants to be setted.
* @param {String} newValues[].referenceId Id of the object that is going to be described
* @param {String} newValues[].value Value as a string.
*/
var setCharacteristicForObject = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(db, newValues) {
var data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return CharacterRequest.Post.parse(newValues, db);
case 2:
data = _context.sent;
Promise.all(data.newValues.map(function (newValue) {
return db.characteristic_scope_values.upsertValueBy(newValue.value, {
characteristicId: data.characteristicIdByName[newValue.characteristic],
referenceId: newValue.referenceId,
scopeId: data.scopeIdByName[newValue.scope]
});
}));
return _context.abrupt('return', data);
case 5:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function setCharacteristicForObject(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
/**
* Get the value of a set of characteristics considering a hierachial query.
* @param {*} db Database object that will handle the connection.
* @param {*} referenceIdsByScopeName This object contains a set of keys that are the
* scopeName and the ids of the referenceIds that want to be used to calculate the
* value of a characteristic.
* @param {String[]} characteristics Array of Strings indicating the names of the
* characteristics to be queried.
*/
var getCharacteristicForObject = function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(db, referenceIdsByScopeName, characteristics) {
var result, parserParameters, data, keys, higherPriorityIdx, characteristicScopeValues, lastIdx, characteristicNamesById;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
result = {};
parserParameters = JSON.parse(JSON.stringify(referenceIdsByScopeName));
parserParameters.characteristics = characteristics;
_context2.next = 5;
return CharacterRequest.Get.parse(parserParameters, db);
case 5:
data = _context2.sent;
// Build default response.
keys = data.referenceIdsArray.find(function (x) {
return x.length > 0;
});
higherPriorityIdx = data.referenceIdsArray.findIndex(function (x) {
return x.length > 0;
});
// Get all the values that we can reference to give a value to the characteristic
_context2.next = 10;
return db.characteristic_scope_values.findAllHierachyNodes(data.priorityScopeIds, data.referenceIdsArray, data.persistedCharacteristics.map(function (charact) {
return charact.dataValues.id;
}));
case 10:
characteristicScopeValues = _context2.sent;
result = buildKeyCharacteristicTree(keys, data.persistedCharacteristics.map(function (charact) {
return charact.dataValues.name;
}), {
value: '',
valueFrom: null
});
lastIdx = buildKeyCharacteristicTree(keys, data.persistedCharacteristics.map(function (charact) {
return charact.dataValues.name;
}), data.priorityScopeIds.length + 1);
// populate the result with founded data
Eif (characteristicScopeValues !== null && characteristicScopeValues.length > 0) {
characteristicNamesById = data.persistedCharacteristics.reduce(function (prev, characteristic) {
prev[characteristic.dataValues.id] = characteristic.dataValues.name;
return prev;
}, {});
characteristicScopeValues.forEach(function (characteristicScopeValue) {
var refId = characteristicScopeValue.dataValues.reference_id.toString();
var charactName = characteristicNamesById[characteristicScopeValue.dataValues.characteristic_id];
var priority = data.priorityScopeIds.findIndex(function (x) {
return x === characteristicScopeValue.dataValues.scope_id;
});
keys.filter(function (x) {
return priority === higherPriorityIdx && x === refId || priority > higherPriorityIdx;
}).forEach(function (key) {
if (priority < lastIdx[key][charactName]) {
lastIdx[key][charactName] = priority;
result[key][charactName] = {
value: characteristicScopeValue.dataValues.value,
valueFrom: {
scope: data.scopeNamesArray[priority],
id: refId
}
};
}
});
});
}
return _context2.abrupt('return', result);
case 15:
case 'end':
return _context2.stop();
}
}
}, _callee2, undefined);
}));
return function getCharacteristicForObject(_x3, _x4, _x5) {
return _ref2.apply(this, arguments);
};
}();
module.exports = {
buildKeyCharacteristicTree: buildKeyCharacteristicTree,
getCharacteristicForObject: getCharacteristicForObject,
setCharacteristicForObject: setCharacteristicForObject
}; |