@@ -4,14 +4,20 @@ import TagCache from 'redis-tag-cache'
44import get from 'lodash/get' ;
55import cache from '../../lib/cache-instance'
66
7- interface AttributeListParam {
7+ export interface AttributeListParam {
88 [ key : string ] : number [ ]
99}
1010
11- function getUri ( config , indexName ) {
11+ /**
12+ * Build ES uri fro attributes
13+ */
14+ function getUri ( config , indexName : string ) : string {
1215 return `${ config . elasticsearch . protocol } ://${ config . elasticsearch . host } :${ config . elasticsearch . port } /${ indexName } /attribute/_search`
1316}
1417
18+ /**
19+ * Returns attributes from cache
20+ */
1521async function getAttributeFromCache ( attributeCode : string , config ) {
1622 if ( config . server . useOutputCache && cache ) {
1723 try {
@@ -26,6 +32,9 @@ async function getAttributeFromCache (attributeCode: string, config) {
2632 }
2733}
2834
35+ /**
36+ * Save attributes in cache
37+ */
2938async function setAttributeInCache ( attributeList , config ) {
3039 if ( config . server . useOutputCache && cache ) {
3140 try {
@@ -41,36 +50,49 @@ async function setAttributeInCache (attributeList, config) {
4150 }
4251}
4352
53+ /**
54+ * Returns attribute with only needed options
55+ * @param attribute - attribute object
56+ * @param optionsIds - list of only needed options ids
57+ */
4458function clearAttributeOpitons ( attribute , optionsIds : number [ ] ) {
4559 const stringOptionsIds = optionsIds . map ( String )
4660 return {
4761 ...attribute ,
48- options : ( attribute . options || [ ] ) . filter ( option => stringOptionsIds . includes ( option . value ) )
62+ options : ( attribute . options || [ ] ) . filter ( option => stringOptionsIds . includes ( String ( option . value ) ) )
4963 }
5064}
5165
52- function list ( attributesParam : AttributeListParam , config , indexName ) {
66+ function list ( attributesParam : AttributeListParam , config , indexName : string ) : Promise < any [ ] > {
5367 return new Promise ( async ( resolve , reject ) => {
68+ // we start with all attributeCodes that are requested
5469 let attributeCodes = Object . keys ( attributesParam )
5570
71+ // here we check if some of attribute are in cache
5672 const rawCachedAttributeList = await Promise . all (
5773 attributeCodes . map ( attributeCode => getAttributeFromCache ( attributeCode , config ) )
5874 )
5975
6076 const cachedAttributeList = rawCachedAttributeList
77+ . filter ( Boolean ) // remove empty results from cache.get
6178 . map ( ( cachedAttribute , index ) => {
6279 if ( cachedAttribute ) {
6380 const attributeOptionsIds = attributesParam [ cachedAttribute . attribute_code ]
64- attributeCodes . splice ( index , 1 ) // side effect - reduce elements in needed attribute list
81+
82+ // side effect - we want to reduce starting 'attributeCodes' because some of them are in cache
83+ attributeCodes . splice ( index , 1 )
84+
85+ // clear unused options
6586 return clearAttributeOpitons ( cachedAttribute , attributeOptionsIds )
6687 }
6788 } )
68- . filter ( Boolean )
6989
90+ // if all requested attributes are in cache then we can return here
7091 if ( ! attributeCodes . length ) {
71- return cachedAttributeList
92+ return resolve ( cachedAttributeList )
7293 }
7394
95+ // fetch attributes for rest attributeCodes
7496 request ( {
7597 uri : getUri ( config , indexName ) ,
7698 method : 'POST' ,
@@ -81,17 +103,29 @@ function list (attributesParam: AttributeListParam, config, indexName) {
81103 reject ( err )
82104 }
83105 const fetchedAttributeList = get ( body , 'hits.hits' , [ ] ) . map ( hit => hit . _source )
106+
107+ // save atrributes in cache
84108 await setAttributeInCache ( fetchedAttributeList , config )
85- resolve ( cachedAttributeList . concat (
86- fetchedAttributeList . map ( fetchedAttribute => {
109+
110+ // cached and fetched attributes
111+ const allAttributes = [
112+ ...cachedAttributeList ,
113+ ...fetchedAttributeList . map ( fetchedAttribute => {
87114 const attributeOptionsIds = attributesParam [ fetchedAttribute . attribute_code ]
115+
116+ // clear unused options
88117 return clearAttributeOpitons ( fetchedAttribute , attributeOptionsIds )
89- } ) )
90- )
118+ } )
119+ ]
120+
121+ return resolve ( allAttributes )
91122 } )
92123 } )
93124}
94125
126+ /**
127+ * Returns only needed data for filters in vsf
128+ */
95129function transformToMetadata ( {
96130 is_visible_on_front,
97131 is_visible,
@@ -103,7 +137,8 @@ function transformToMetadata ({
103137 is_comparable,
104138 attribute_code,
105139 slug,
106- options
140+ options = [ ] ,
141+ buckets = [ ]
107142} ) {
108143 return {
109144 is_visible_on_front,
@@ -116,7 +151,8 @@ function transformToMetadata ({
116151 is_comparable,
117152 attribute_code,
118153 slug,
119- options
154+ options,
155+ buckets
120156 }
121157}
122158
0 commit comments