@@ -10,37 +10,46 @@ import { UtilService } from '@/services/UtilService'
1010
1111const actions : ActionTree < ProductState , RootState > = {
1212
13- async fetchProducts ( { commit, state } , { productIds , identificationTypeId } ) {
13+ async fetchProducts ( { commit, state } , { productIdentificationIds , identificationTypeId } ) {
1414
15- // TODO Add try-catch block
15+ const productIdentificationFilter = productIdentificationIds . reduce ( ( filter : Array < any > , productIdentificationId : any ) => {
16+ if ( ! productIdentificationId ) return filter ;
17+
18+ // Check if this productIdentificationId exists in any cached product's identifications
19+ const productExistsInCache = Object . values ( state . cached ) . some ( ( cachedProduct : any ) => {
20+ return cachedProduct . identifications ?. some ( ( identification : any ) =>
21+ identification . productIdTypeEnumId . toLowerCase ( ) === identificationTypeId . toLowerCase ( ) && identification . idValue === productIdentificationId ) ;
22+ } ) ;
1623
17- const cachedProductIds = Object . keys ( state . cached ) ;
18- const productIdFilter = productIds . reduce ( ( filter : Array < any > , productId : any ) => {
1924 // If product does not exist in cached products then add the id
20- if ( ! cachedProductIds . includes ( productId ) && productId ) {
21- filter . push ( productId ) ;
22- }
25+ if ( ! productExistsInCache ) filter . push ( productIdentificationId ) ;
2326 return filter ;
2427 } , [ ] ) ;
2528
26- // If there are no product ids to search skip the API call
27- if ( productIdFilter . length == 0 ) return state . cached ;
29+ // If there are no product to search skip the API call
30+ if ( productIdentificationFilter . length == 0 ) return state . cached ;
2831
29- const modifiedProductIdFilters = productIdFilter . map ( ( productId : string ) => identificationTypeId + '/' + productId ) ;
30- const resp = await fetchProducts ( {
31- filters : { 'goodIdentifications' : { 'value' : modifiedProductIdFilters } } ,
32- viewSize : productIdFilter . length ,
33- viewIndex : 0
34- } )
35-
36- if ( ! isError ( resp ) ) {
37- const products = resp . products ;
38- // Handled empty response in case of failed query
39- if ( resp . total ) commit ( types . PRODUCT_ADD_TO_CACHED_MULTIPLE , { products } ) ;
40- } else {
41- logger . error ( resp . serverResponse )
32+ const modifiedProductIdentificationFilters = productIdentificationFilter . map ( ( productIdentificationId : string ) => identificationTypeId + '/' + productIdentificationId ) ;
33+ let products = [ ] as any , viewIndex = 0 , resp ;
34+ try {
35+ do {
36+ resp = await fetchProducts ( {
37+ filters : { 'goodIdentifications' : { 'value' : modifiedProductIdentificationFilters } } ,
38+ viewSize : productIdentificationFilter . length ,
39+ viewIndex
40+ } )
41+
42+ if ( ! isError ( resp ) && resp . total > 0 ) {
43+ products = products . concat ( resp . products ) ;
44+ commit ( types . PRODUCT_ADD_TO_CACHED_MULTIPLE , { products } ) ;
45+ viewIndex += resp . products . length ;
46+ } else {
47+ logger . error ( resp . serverResponse )
48+ }
49+ } while ( resp . total > products . length ) ;
50+ } catch ( error ) {
51+ logger . error ( error ) ;
4252 }
43- // TODO Handle specific error
4453 return state . cached ;
4554 } ,
4655 async findProduct ( { commit, state } , payload ) {
0 commit comments