| 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 |
2x
2x
4x
4x
24x
12x
6x
6x
12x
8x
6x
2x
2x
5x
5x
5x
4x
1x
5x
4x
1x
5x
4x
1x
5x
4x
1x
5x
1x
4x
2x
5x
5x
5x
4x
4x
4x
1x
1x
4x
1x
3x
3x
2x
2x
1x
4x
4x
4x
4x
4x
4x
6x
6x
2x
2x
| const {
validateScopeNames,
validateCharacteristicNames,
areCharacteristicsInSameTree,
} = require('./validators');
/**
* 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.
*/
const extractScopesFromRequestOrderByPriority = async (scopeIds, referenceIds, db) => {
const scopes = await db.scopes.findAll({
where: {
id: scopeIds,
},
});
const orderedReferenceIds = scopeIds.reduce((prev, scopeId) => {
const scopeName = scopes.find(scope => 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(x => x.ids.length > 0)) {
throw new Error(`The hierachy for this/those characteristic(s) need(s) the following queryParameters: ${scopes.map(scope => scope.dataValues.name).join(', ')}`);
}
return orderedReferenceIds;
};
const 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: ({
scope, characteristic, referenceId, value,
}) => {
const result = {};
const 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;
},
};
const 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: async (newValues, db) => {
const result = {};
const errors = [];
if (Array.isArray(newValues)) {
result.newValues = newValues.map((characteristicScopeValue, idx) => {
try {
return CharacteristicScopeValue.parse(characteristicScopeValue);
} catch (error) {
throw new Error({
idx,
errors: error,
});
}
});
} else {
errors.push('Invalid newValues');
}
if (errors.length > 0) {
throw errors;
}
result.scopeIdByName = await validateScopeNames(
result.newValues.map(characteristicScopeValue => characteristicScopeValue.scope),
db,
);
result.characteristicIdByName = await validateCharacteristicNames(
result.newValues.map(x => x.characteristic), db,
);
return result;
},
},
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: async (queryRequest, db) => {
const result = {};
const temp = queryRequest || {};
result.persistedCharacteristics = await db.characteristics
.findAllByName(temp.characteristics);
Iif (!areCharacteristicsInSameTree(result.persistedCharacteristics)) {
throw new Error('Sorry, this characteristics seem to be in different trees, we can“t handle that');
}
result.priorityScopeIds = result.persistedCharacteristics[0].dataValues
.tree.getScopeIdsOrderByPriority();
result.referenceIdsArray = await (
extractScopesFromRequestOrderByPriority(result.priorityScopeIds, temp, db)
);
result.scopeNamesArray = result.referenceIdsArray.map(x => x.name);
result.referenceIdsArray = result.referenceIdsArray.map(x => x.ids);
return result;
},
},
};
module.exports = {
CharacterRequest,
CharacteristicScopeValue,
};
|