|
| 1 | + |
| 2 | +import TagCache from 'redis-tag-cache' |
| 3 | +import get from 'lodash/get'; |
| 4 | +import cache from '../../lib/cache-instance' |
| 5 | +import { adjustQuery, getClient as getElasticClient } from './../../lib/elastic' |
| 6 | +import bodybuilder from 'bodybuilder' |
| 7 | + |
| 8 | +export interface AttributeListParam { |
| 9 | + [key: string]: number[] |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Transforms ES aggregates into valid format for AttributeService - {[attribute_code]: [bucketId1, bucketId2]} |
| 14 | + * @param body - products response body |
| 15 | + * @param config - global config |
| 16 | + * @param indexName - current indexName |
| 17 | + */ |
| 18 | +function transformAggsToAttributeListParam (aggregations): AttributeListParam { |
| 19 | + const attributeListParam: AttributeListParam = Object.keys(aggregations) |
| 20 | + .filter(key => aggregations[key].buckets.length) // leave only buckets with values |
| 21 | + .reduce((acc, key) => { |
| 22 | + const attributeCode = key.replace(/^(agg_terms_|agg_range_)|(_options)$/g, '') |
| 23 | + const bucketsIds = aggregations[key].buckets.map(bucket => bucket.key) |
| 24 | + |
| 25 | + if (!acc[attributeCode]) { |
| 26 | + acc[attributeCode] = [] |
| 27 | + } |
| 28 | + |
| 29 | + // there can be more then one attributes for example 'agg_terms_color' and 'agg_terms_color_options' |
| 30 | + // we need to get buckets from both |
| 31 | + acc[attributeCode] = [...new Set([...acc[attributeCode], ...bucketsIds])] |
| 32 | + |
| 33 | + return acc |
| 34 | + }, {}) |
| 35 | + |
| 36 | + return attributeListParam |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Returns attributes from cache |
| 41 | + */ |
| 42 | +async function getAttributeFromCache (attributeCode: string, config) { |
| 43 | + if (config.server.useOutputCache && cache) { |
| 44 | + try { |
| 45 | + const res = await (cache as TagCache).get( |
| 46 | + 'api:attribute-list' + attributeCode |
| 47 | + ) |
| 48 | + return res |
| 49 | + } catch (err) { |
| 50 | + console.error(err) |
| 51 | + return null |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Save attributes in cache |
| 58 | + */ |
| 59 | +async function setAttributeInCache (attributeList, config) { |
| 60 | + if (config.server.useOutputCache && cache) { |
| 61 | + try { |
| 62 | + await Promise.all( |
| 63 | + attributeList.map(attribute => (cache as TagCache).set( |
| 64 | + 'api:attribute-list' + attribute.attribute_code, |
| 65 | + attribute |
| 66 | + )) |
| 67 | + ) |
| 68 | + } catch (err) { |
| 69 | + console.error(err) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Returns attribute with only needed options |
| 76 | + * @param attribute - attribute object |
| 77 | + * @param optionsIds - list of only needed options ids |
| 78 | + */ |
| 79 | +function clearAttributeOpitons (attribute, optionsIds: number[]) { |
| 80 | + const stringOptionsIds = optionsIds.map(String) |
| 81 | + return { |
| 82 | + ...attribute, |
| 83 | + options: (attribute.options || []).filter(option => stringOptionsIds.includes(String(option.value))) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +async function list (attributesParam: AttributeListParam, config, indexName: string): Promise<any[]> { |
| 88 | + // we start with all attributeCodes that are requested |
| 89 | + let attributeCodes = Object.keys(attributesParam) |
| 90 | + |
| 91 | + // here we check if some of attribute are in cache |
| 92 | + const rawCachedAttributeList = await Promise.all( |
| 93 | + attributeCodes.map(attributeCode => getAttributeFromCache(attributeCode, config)) |
| 94 | + ) |
| 95 | + |
| 96 | + const cachedAttributeList = rawCachedAttributeList |
| 97 | + .map((cachedAttribute, index) => { |
| 98 | + if (cachedAttribute) { |
| 99 | + const attributeOptionsIds = attributesParam[cachedAttribute.attribute_code] |
| 100 | + |
| 101 | + // side effect - we want to reduce starting 'attributeCodes' if some of them are in cache |
| 102 | + attributeCodes.splice(index, 1) |
| 103 | + |
| 104 | + // clear unused options |
| 105 | + return clearAttributeOpitons(cachedAttribute, attributeOptionsIds) |
| 106 | + } |
| 107 | + }) |
| 108 | + // remove empty results from cache.get |
| 109 | + // this needs to be after .map because we want to have same indexes as are in attributeCodes |
| 110 | + .filter(Boolean) |
| 111 | + |
| 112 | + // if all requested attributes are in cache then we can return here |
| 113 | + if (!attributeCodes.length) { |
| 114 | + return cachedAttributeList |
| 115 | + } |
| 116 | + |
| 117 | + // fetch attributes for rest attributeCodes |
| 118 | + try { |
| 119 | + const query = adjustQuery({ |
| 120 | + index: indexName, |
| 121 | + type: 'attribute', |
| 122 | + body: bodybuilder().filter('terms', 'attribute_code', attributeCodes).build() |
| 123 | + }, 'attribute', config) |
| 124 | + const response = await getElasticClient(config).search(query) |
| 125 | + const fetchedAttributeList = get(response.body, 'hits.hits', []).map(hit => hit._source) |
| 126 | + |
| 127 | + // save atrributes in cache |
| 128 | + await setAttributeInCache(fetchedAttributeList, config) |
| 129 | + |
| 130 | + // cached and fetched attributes |
| 131 | + const allAttributes = [ |
| 132 | + ...cachedAttributeList, |
| 133 | + ...fetchedAttributeList.map(fetchedAttribute => { |
| 134 | + const attributeOptionsIds = attributesParam[fetchedAttribute.attribute_code] |
| 135 | + |
| 136 | + // clear unused options |
| 137 | + return clearAttributeOpitons(fetchedAttribute, attributeOptionsIds) |
| 138 | + }) |
| 139 | + ] |
| 140 | + |
| 141 | + return allAttributes |
| 142 | + } catch (err) { |
| 143 | + console.error(err) |
| 144 | + return [] |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Returns only needed data for filters in vsf |
| 150 | + */ |
| 151 | +function transformToMetadata ({ |
| 152 | + is_visible_on_front, |
| 153 | + is_visible, |
| 154 | + default_frontend_label, |
| 155 | + attribute_id, |
| 156 | + entity_type_id, |
| 157 | + id, |
| 158 | + is_user_defined, |
| 159 | + is_comparable, |
| 160 | + attribute_code, |
| 161 | + slug, |
| 162 | + options = [] |
| 163 | +}) { |
| 164 | + return { |
| 165 | + is_visible_on_front, |
| 166 | + is_visible, |
| 167 | + default_frontend_label, |
| 168 | + attribute_id, |
| 169 | + entity_type_id, |
| 170 | + id, |
| 171 | + is_user_defined, |
| 172 | + is_comparable, |
| 173 | + attribute_code, |
| 174 | + slug, |
| 175 | + options |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +export default { |
| 180 | + list, |
| 181 | + transformToMetadata, |
| 182 | + transformAggsToAttributeListParam |
| 183 | +} |
0 commit comments