|
1 | 1 |
|
2 | | -import request from 'request'; |
3 | 2 | import TagCache from 'redis-tag-cache' |
4 | 3 | import get from 'lodash/get'; |
5 | 4 | import cache from '../../lib/cache-instance' |
| 5 | +import { adjustQuery, getClient as getElasticClient } from './../../lib/elastic' |
| 6 | +import bodybuilder from 'bodybuilder' |
6 | 7 |
|
7 | 8 | export interface AttributeListParam { |
8 | 9 | [key: string]: number[] |
@@ -63,64 +64,63 @@ function clearAttributeOpitons (attribute, optionsIds: number[]) { |
63 | 64 | } |
64 | 65 | } |
65 | 66 |
|
66 | | -function list (attributesParam: AttributeListParam, config, indexName: string): Promise<any[]> { |
67 | | - return new Promise(async (resolve, reject) => { |
68 | | - // we start with all attributeCodes that are requested |
69 | | - let attributeCodes = Object.keys(attributesParam) |
| 67 | +async function list (attributesParam: AttributeListParam, config, indexName: string): Promise<any[]> { |
| 68 | + // we start with all attributeCodes that are requested |
| 69 | + let attributeCodes = Object.keys(attributesParam) |
70 | 70 |
|
71 | | - // here we check if some of attribute are in cache |
72 | | - const rawCachedAttributeList = await Promise.all( |
73 | | - attributeCodes.map(attributeCode => getAttributeFromCache(attributeCode, config)) |
74 | | - ) |
| 71 | + // here we check if some of attribute are in cache |
| 72 | + const rawCachedAttributeList = await Promise.all( |
| 73 | + attributeCodes.map(attributeCode => getAttributeFromCache(attributeCode, config)) |
| 74 | + ) |
75 | 75 |
|
76 | | - const cachedAttributeList = rawCachedAttributeList |
77 | | - .filter(Boolean) // remove empty results from cache.get |
78 | | - .map((cachedAttribute, index) => { |
79 | | - if (cachedAttribute) { |
80 | | - const attributeOptionsIds = attributesParam[cachedAttribute.attribute_code] |
| 76 | + const cachedAttributeList = rawCachedAttributeList |
| 77 | + .filter(Boolean) // remove empty results from cache.get |
| 78 | + .map((cachedAttribute, index) => { |
| 79 | + if (cachedAttribute) { |
| 80 | + const attributeOptionsIds = attributesParam[cachedAttribute.attribute_code] |
81 | 81 |
|
82 | | - // side effect - we want to reduce starting 'attributeCodes' because some of them are in cache |
83 | | - attributeCodes.splice(index, 1) |
| 82 | + // side effect - we want to reduce starting 'attributeCodes' because some of them are in cache |
| 83 | + attributeCodes.splice(index, 1) |
84 | 84 |
|
85 | | - // clear unused options |
86 | | - return clearAttributeOpitons(cachedAttribute, attributeOptionsIds) |
87 | | - } |
88 | | - }) |
89 | | - |
90 | | - // if all requested attributes are in cache then we can return here |
91 | | - if (!attributeCodes.length) { |
92 | | - return resolve(cachedAttributeList) |
93 | | - } |
94 | | - |
95 | | - // fetch attributes for rest attributeCodes |
96 | | - request({ |
97 | | - uri: getUri(config, indexName), |
98 | | - method: 'POST', |
99 | | - body: {'query': {'bool': {'filter': {'bool': {'must': [{'terms': {'attribute_code': attributeCodes}}]}}}}}, |
100 | | - json: true |
101 | | - }, async (err, res, body) => { |
102 | | - if (err) { |
103 | | - reject(err) |
| 85 | + // clear unused options |
| 86 | + return clearAttributeOpitons(cachedAttribute, attributeOptionsIds) |
104 | 87 | } |
105 | | - const fetchedAttributeList = get(body, 'hits.hits', []).map(hit => hit._source) |
106 | | - |
107 | | - // save atrributes in cache |
108 | | - await setAttributeInCache(fetchedAttributeList, config) |
| 88 | + }) |
109 | 89 |
|
110 | | - // cached and fetched attributes |
111 | | - const allAttributes = [ |
112 | | - ...cachedAttributeList, |
113 | | - ...fetchedAttributeList.map(fetchedAttribute => { |
114 | | - const attributeOptionsIds = attributesParam[fetchedAttribute.attribute_code] |
| 90 | + // if all requested attributes are in cache then we can return here |
| 91 | + if (!attributeCodes.length) { |
| 92 | + return cachedAttributeList |
| 93 | + } |
115 | 94 |
|
116 | | - // clear unused options |
117 | | - return clearAttributeOpitons(fetchedAttribute, attributeOptionsIds) |
118 | | - }) |
119 | | - ] |
| 95 | + // fetch attributes for rest attributeCodes |
| 96 | + try { |
| 97 | + const query = adjustQuery({ |
| 98 | + index: indexName, |
| 99 | + type: 'attribute', |
| 100 | + body: bodybuilder().filter('terms', 'attribute_code', attributeCodes).build() |
| 101 | + }, 'attribute', config) |
| 102 | + const response = await getElasticClient(config).search(query) |
| 103 | + const fetchedAttributeList = get(response.body, 'hits.hits', []).map(hit => hit._source) |
| 104 | + |
| 105 | + // save atrributes in cache |
| 106 | + await setAttributeInCache(fetchedAttributeList, config) |
| 107 | + |
| 108 | + // cached and fetched attributes |
| 109 | + const allAttributes = [ |
| 110 | + ...cachedAttributeList, |
| 111 | + ...fetchedAttributeList.map(fetchedAttribute => { |
| 112 | + const attributeOptionsIds = attributesParam[fetchedAttribute.attribute_code] |
| 113 | + |
| 114 | + // clear unused options |
| 115 | + return clearAttributeOpitons(fetchedAttribute, attributeOptionsIds) |
| 116 | + }) |
| 117 | + ] |
120 | 118 |
|
121 | | - return resolve(allAttributes) |
122 | | - }) |
123 | | - }) |
| 119 | + return allAttributes |
| 120 | + } catch (err) { |
| 121 | + console.error(err) |
| 122 | + return [] |
| 123 | + } |
124 | 124 | } |
125 | 125 |
|
126 | 126 | /** |
|
0 commit comments