-
Notifications
You must be signed in to change notification settings - Fork 26
/
indexes.js
executable file
·481 lines (398 loc) · 15.3 KB
/
indexes.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* Copyright (c) 2019 CoNWeT Lab., Universidad Politécnica de Madrid
*
* This file belongs to the business-ecosystem-logic-proxy of the
* Business API Ecosystem
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require("babel-polyfill");
var config = require("../config"),
deepcopy = require("deepcopy"),
itertools = require("aureooms-js-itertools"),
md5 = require("blueimp-md5"),
leftPad = require("left-pad"),
request = require("request"),
utils = require("./utils"),
Promise = require("promiz"),
logger = require('./logger').logger.getLogger('TMF'),
localIndexes = require('./local_indexes'),
elasticIndexes = require('./elastic_indexes');
// Check the indexes engine to be used by the system
let indexes;
if (config.indexes.engine == 'local') {
indexes = localIndexes;
} else {
indexes = elasticIndexes;
}
exports.siTables = indexes.siTables;
exports.setClient = indexes.setClient;
exports.init = indexes.init;
exports.close = indexes.close;
exports.removeIndex = indexes.removeIndex;
var query = function query(path, query, isCount) {
// logger.info(" ---- query (" + JSON.stringify(query) + ") ---");
var method = isCount ? indexes.count : indexes.search;
return method(path, query);
};
exports.searchOfferings = query.bind(null, 'offerings');
exports.searchProducts = query.bind(null, 'products');
exports.searchCatalogs = query.bind(null, 'catalogs');
exports.searchInventory = query.bind(null, 'inventory');
exports.searchOrders = query.bind(null, 'orders');
var searchId = function searchId(f, id) {
return f({ AND: { sortedId: [leftPad(id, 12, 0)] } });
};
exports.searchOfferingId = searchId.bind(null, exports.searchOfferings);
exports.searchProductId = searchId.bind(null, exports.searchProducts);
var getUserFromOffer = function getUserFromOffer(id, def) {
if (def) {
return Promise.resolve(def);
}
id = typeof id === 'string' ? id.replace(/^0+/, '') : id;
return exports.searchProductId(id).then((results) => {
if (results.length > 0 && results[0].document.relatedParty.length > 0) {
return Promise.resolve({ id: results[0].document.relatedParty[0] });
}
return Promise.reject('The specified product id is not indexed');
});
};
exports.fixUserId = function fixUserId(id) {
return md5(id);
};
var indexCreateData = function indexCreateData(keys, data, newData) {
keys.forEach(key => {
if (typeof data[key] !== "undefined" && data[key] !== null) {
newData[key] = data[key];
}
});
return newData;
};
var convertInventoryData = function convertInventoryData(data) {
var lastUpdate;
if (data.startDate) {
lastUpdate = new Date(data.startDate).getTime();
} else {
lastUpdate = new Date().getTime();
}
var initialData = {
id: 'inventory:' + data.id,
originalId: data.id,
body: data.searchable,
sortedId: leftPad(data.id, 12, 0),
lastUpdate: lastUpdate,
productOffering: data.productOffering.id,
relatedPartyHash: data.relatedParty
.filter((user) => user.role.toLowerCase() === 'customer')
.map((x) => exports.fixUserId(x.id)),
relatedParty: data.relatedParty.map((x) => x.id)
};
return indexCreateData(['href', 'name', 'status', 'startDate', 'orderDate', 'terminationDate'], data, initialData);
};
var addOfferInformationToInventory = function addOfferInformationToInventory(data) {
return exports.searchOfferingId(data.productOffering.id).then((results) => {
if (results.length > 0) {
data.searchable = results[0].document.searchable;
return Promise.resolve(data);
}
return Promise.reject('The offering specified in the product is not indexed');
});
};
exports.saveIndexInventory = function saveIndexInventory(data) {
// Adds the searchable properties to data
var opts = {
fieldOptions: {
status: {
preserveCase: false
},
body: {
preserveCase: false
},
lastUpdate: {
sortable: true
}
}
};
var promise = Promise.resolve();
data.forEach(function(invData) {
promise = promise.then(() => {
return addOfferInformationToInventory(invData)
.then((offerData) => indexes.saveIndex('inventory', [convertInventoryData(offerData)], opts));
});
});
return promise;
};
var catalog_ops = {
fieldOptions: {
lastUpdate: {
sortable: true
},
name: {
sortable: true
},
lifecycleStatus: {
preserveCase: false
},
body: {
preserveCase: false
}
}
};
var convertCatalogData = function convertCatalogData(data) {
var description = typeof data.description !== 'undefined' ? data.description.toLowerCase() : '';
var searchable = [data.name.toLowerCase(), description];
var initialData = {
id: 'catalog:' + data.id,
originalId: data.id,
body: searchable,
sortedId: leftPad(data.id, 12, 0),
lastUpdate: new Date(data.lastUpdate).getTime(),
relatedPartyHash: data.relatedParty.map((x) => exports.fixUserId(x.id)),
relatedParty: data.relatedParty.map((x) => x.id)
};
return indexCreateData(['href', 'name', 'lifecycleStatus'], data, initialData);
};
exports.saveIndexCatalog = function saveIndexCatalog(data) {
var newData = data.map(convertCatalogData);
return indexes.saveIndex('catalogs', newData, catalog_ops);
};
var convertProductData = function convertProductData(data) {
var description = typeof data.description !== 'undefined' ? data.description.toLowerCase() : '';
var searchable = [data.name.toLowerCase(), data.brand.toLowerCase(), description];
var initialData = {
id: 'product:' + data.id,
originalId: data.id,
sortedId: leftPad(data.id, 12, 0),
body: searchable,
lastUpdate: new Date(data.lastUpdate).getTime(),
relatedPartyHash: data.relatedParty.map((x) => exports.fixUserId(x.id)),
relatedParty: data.relatedParty.map((x) => x.id)
};
return indexCreateData(['name', 'href', 'lifecycleStatus', 'isBundle', 'productNumber'], data, initialData);
};
exports.saveIndexProduct = function saveIndexProduct(data) {
var newData = data.map(convertProductData);
return indexes.saveIndex('products', newData, catalog_ops);
};
var blockPromises = function blockPromises(data, f) {
var p = Promise.resolve([]);
data.forEach((x) => (p = p.then((arr) => f(x).then((newD) => arr.concat([newD])))));
return p;
};
var createUrl = function createUrl(api, extra) {
return utils.getAPIProtocol(api) + "://" + utils.getAPIHost(api) + ":" +
utils.getAPIPort(api) + '/' + api + extra;
};
var convertOffer = function convertOffer(data, user) {
data.productSpecification = data.productSpecification || {};
var searchable = [data.name, data.description];
var initialData = {
id: 'offering:' + data.id,
originalId: data.id,
sortedId: leftPad(data.id, 12, 0),
catalog: leftPad(data.catalog, 12, 0),
body: searchable,
userId: exports.fixUserId(user.id),
lastUpdate: new Date(data.lastUpdate).getTime(),
isBundle: data.isBundle ? 'T' : 'F',
productSpecification:
typeof data.productSpecification.id !== 'undefined'
? leftPad(data.productSpecification.id, 12, 0)
: undefined
};
if (!!data.category && data.category.length > 0) {
return blockPromises(data.category, (cat) => {
var p = new Promise();
var url = createUrl(config.endpoints.catalog.path, "/api/catalogManagement/v2/category/" + cat.id);
request(url, function(err, response, body) {
if (err || response.statusCode >= 400) {
p.reject('One of the categories could not be indexed');
} else {
var data = JSON.parse(body);
p.resolve({ id: data.id, name: data.name });
}
});
return p;
}).then((categories) => {
initialData.categoriesId = categories.map((x) => leftPad(x.id, 12, 0));
initialData.categoriesName = categories.map((x) => md5(x.name.toLowerCase()));
return indexCreateData(['href', 'name', 'lifecycleStatus'], data, initialData);
});
}
var newData = indexCreateData(['href', 'name', 'lifecycleStatus'], data, initialData);
return Promise.resolve(newData);
};
var convertNotBundle = function convertNotBundle(data, user) {
return getUserFromOffer(data.productSpecification.id, user).then((user) => convertOffer(data, user));
};
var convertBundle = function convertBundle(data, user) {
if (user) {
return convertOffer(data, user);
} else {
return exports
.searchOfferingId(data.bundledProductOffering[0].id)
.then((offer) => getUserFromOffer(offer[0].document.productSpecification, user))
.then((user) => convertOffer(data, user));
}
};
var convertOfferingData = function convertOfferingData(data, user) {
if (!data.isBundle) {
return convertNotBundle(data, user);
} else {
return convertBundle(data, user);
}
};
var blockConvert = function blockConvert(data, user) {
return blockPromises(data, (x) => convertOfferingData(x, user));
};
exports.saveIndexOffering = function saveIndexOffering(data, user) {
var notBundle = data.filter((x) => !x.isBundle);
var bundle = data.filter((x) => x.isBundle);
return blockConvert(notBundle, user)
.then(newData => indexes.saveIndex('offerings', newData, catalog_ops))
.then(() => blockConvert(bundle, user))
.then(newData => indexes.saveIndex('offerings', newData, catalog_ops));
};
var convertOrderData = function convertOrderData(data) {
var initialData = {
id: 'order:' + data.id,
originalId: data.id,
sortedId: leftPad(data.id, 12, 0),
lastUpdate: new Date(data.orderDate).getTime(),
relatedPartyHash: data.relatedParty
.filter((user) => user.role.toLowerCase() === 'customer')
.map((x) => exports.fixUserId(x.id)),
sellerHash: data.relatedParty
.filter((user) => user.role.toLowerCase() === 'seller')
.map((x) => exports.fixUserId(x.id)),
relatedParty: data.relatedParty.map((x) => x.id)
};
return indexCreateData(['href', 'priority', 'category', 'state', 'notificationContact', 'note'], data, initialData);
};
exports.saveIndexOrder = function saveIndexOrder(data) {
var newData = data.map(convertOrderData);
var opts = {
fieldOptions: {
lastUpdate: {
sortable: true
},
status: {
preserveCase: false
}
}
};
return indexes.saveIndex('orders', newData, opts);
};
// Request helpers!
exports.getMiddleware = function getMiddleware(reg, createOfferF, queryF, req) {
var queryPromise = Promise.resolve();
if (req.method == "GET" && reg.test(req.apiUrl) && (typeof req.query.id === "undefined")) {
var q = createOfferF(req);
var handler, isCount;
// If query is empty or none of the queries have AND and OR, means that there wasn't any filtering parameter, so query for all
if (q.query.length <= 0 || (q.query.length === 1 && Object.keys(q.query[0].AND).length <= 0)) {
q.query = [{ AND: { '*': ['*'] } }];
}
if (q.query.length === 1) q.query = q.query[0];
if ('action' in req.query && req.query.action === 'count') {
isCount = true;
handler = (count) => {
req.apiUrl = '/' + config.endpoints.management.path + '/count/' + count;
};
} else {
isCount = false;
handler = (results) => {
var newUrl = req._parsedUrl.pathname + '?id=' + results.map((r) => r.document.originalId).join(',');
for (var key in req.query) {
if (['depth', 'fields'].indexOf(key) >= 0) {
newUrl = newUrl + '&' + key + '=' + req.query[key];
}
}
req.apiUrl = newUrl;
};
}
queryPromise = queryF(q, isCount).then(handler);
}
return queryPromise;
};
var createInitialQuery = function createInitialQuery(req) {
var valid_sorts = {
date: {
field: 'lastUpdate',
direction: 'desc'
},
name: {
field: 'name',
direction: 'asc'
}
};
var result = {};
var sorting = 'date';
if (typeof req.query.sort !== 'undefined' && req.query.sort in valid_sorts) {
sorting = req.query.sort;
}
result.sort = valid_sorts[sorting];
if (typeof req.query.offset !== 'undefined' && typeof req.query.size !== 'undefined') {
result.offset = req.query.offset;
result.pageSize = req.query.size;
}
return result;
};
exports.addOrCondition = function addOrCondition(query, name, conds) {
if (typeof query.OR === 'undefined') {
query.OR = [];
}
query.OR.push(conds.map((c) => ({ [name]: [c] })));
};
exports.addAndCondition = function addAndCondition(query, cond) {
if (typeof query.AND === 'undefined') {
query.AND = [];
}
query.AND.push(cond);
};
// Do the product of OR possibilities and create an AND condition with every combination
var transformQuery = function transformQuery(query) {
var q = [];
var qand = { AND: {} };
query.AND.forEach((a) => (qand.AND = Object.assign(qand.AND, deepcopy(a))));
// TODO: Check whether the product is actually working
for (var qs of itertools.product(query.OR)) {
var orq = deepcopy(qand);
qs.forEach((qsi) => (orq.AND = Object.assign(orq.AND, deepcopy(qsi))));
q.push(orq);
}
return q;
};
exports.genericCreateQuery = function genericCreateQuery(extras, name, f, req) {
var q = createInitialQuery(req);
var query = { AND: [], OR: [] };
f = f || function() {};
f(req, query);
extras.forEach((key) => {
if (typeof req.query[key] !== 'undefined') {
let newq = {};
let value = req.query[key]
if (key == 'isBundle') {
value = req.query[key] == 'true' ? 'T' : 'F';
}
newq[key] = [value];
if (typeof newq[key][0] === 'string') {
newq[key][0] = newq[key][0].toLowerCase();
}
exports.addAndCondition(query, newq);
}
});
q.query = transformQuery(query);
return q;
};
exports.getDataStores = indexes.getDataStores;