| 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 |
1x
1x
5x
6x
6x
5x
6x
6x
5x
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
| const {
CharacterRequest,
} = require('./parsers');
/**
* 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
*/
const buildKeyCharacteristicTree = (keys, characteristicNames, initialValue) => {
const characteristicLevel = (characteristicNames || [])
.reduce((previousValue, currentValue) => {
previousValue[currentValue] = JSON.parse(JSON.stringify(initialValue));
return previousValue;
}, {});
const result = (keys || []).reduce((prevRes, refId) => {
prevRes[refId.toString()] = JSON.parse(JSON.stringify(characteristicLevel));
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.
*/
const setCharacteristicForObject = async (db, newValues) => {
const data = await CharacterRequest.Post.parse(newValues, db);
Promise.all(
data.newValues.map(newValue => db.characteristic_scope_values
.upsertValueBy(newValue.value, {
characteristicId: data.characteristicIdByName[newValue.characteristic],
referenceId: newValue.referenceId,
scopeId: data.scopeIdByName[newValue.scope],
})),
);
return data;
};
/**
* 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.
*/
const getCharacteristicForObject = async (db, referenceIdsByScopeName, characteristics) => {
let result = {};
const parserParameters = JSON.parse(JSON.stringify(referenceIdsByScopeName));
parserParameters.characteristics = characteristics;
const data = await CharacterRequest.Get.parse(parserParameters, db);
// Build default response.
const keys = data.referenceIdsArray.find(x => x.length > 0);
const higherPriorityIdx = data.referenceIdsArray.findIndex(x => x.length > 0);
// Get all the values that we can reference to give a value to the characteristic
const characteristicScopeValues = await db.characteristic_scope_values
.findAllHierachyNodes(
data.priorityScopeIds,
data.referenceIdsArray,
data.persistedCharacteristics.map(charact => charact.dataValues.id),
);
result = buildKeyCharacteristicTree(
keys,
data.persistedCharacteristics.map(charact => charact.dataValues.name), {
value: '',
valueFrom: null,
},
);
const lastIdx = buildKeyCharacteristicTree(
keys,
data.persistedCharacteristics.map(charact => charact.dataValues.name),
(data.priorityScopeIds.length + 1),
);
// populate the result with founded data
Eif (characteristicScopeValues && characteristicScopeValues.length > 0) {
const characteristicNamesById = data.persistedCharacteristics
.reduce((prev, { dataValues: { id, name } = {} }) => {
prev[id] = name;
return prev;
}, {});
characteristicScopeValues.forEach((characteristicScopeValue) => {
const refId = characteristicScopeValue
.dataValues.reference_id.toString();
const charactName = characteristicNamesById[
characteristicScopeValue.dataValues.characteristic_id
];
const priority = data.priorityScopeIds
.findIndex(x => x === characteristicScopeValue.dataValues.scope_id);
keys
.filter(x => (priority === higherPriorityIdx && x === refId)
|| priority > higherPriorityIdx)
.forEach((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 result;
};
module.exports = {
buildKeyCharacteristicTree,
getCharacteristicForObject,
setCharacteristicForObject,
};
|