async _getChannelsById(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = ids.filter((id: string) => !this.channelsCache[id]) // fetch unknown channels from API if (notCachedIds.length) { const predicate = `id in ("${notCachedIds.join('", "')}")` const channelService = this._createService('channels') const uri = channelService.where(predicate).build() const results = ProductJsonToCsv._pickAndConcatResults( await this.fetchReferences(uri) ) results.forEach((result: Channel) => { // we should keep old channels in the cache because we can resolve // multiple products in parallel and we don't want to fetch same channels // multiple times this.channelsCache[result.id] = result }) } // pick only requested channels from cache return pick(this.channelsCache, ids) }
// This method decides if to get the categories from cache or API _getCategories(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = [] const cachedCategories = [] ids.forEach((id: string) => { if (this.categoriesCache[id]) cachedCategories.push(this.categoriesCache[id]) else notCachedIds.push(id) }) if (!notCachedIds.length) return Promise.resolve(cachedCategories) const predicate = `id in ("${notCachedIds.join('", "')}")` const categoriesService = this._createService('categories') const uri = categoriesService.where(predicate).build() return this.fetchReferences(uri).then( (res: SuccessResult[]): Array<Category> => { const results = ProductJsonToCsv._pickAndConcatResults(res) results.forEach((result: Category) => { cachedCategories.push(result) this.categoriesCache[result.id] = result }) return cachedCategories } ) }
async _getCustomerGroupsById(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = ids.filter( (id: string) => !this.customerGroupsCache[id] ) // fetch unknown customerGroups from API if (notCachedIds.length) { const predicate = `id in ("${notCachedIds.join('", "')}")` const customerGroupService = this._createService('customerGroups') const uri = customerGroupService.where(predicate).build() const results = ProductJsonToCsv._pickAndConcatResults( await this.fetchReferences(uri) ) results.forEach((result: CustomerGroup) => { // we should keep old customerGroups in the cache because we can resolve // multiple products in parallel and we don't want to fetch same customerGroups // multiple times this.customerGroupsCache[result.id] = result }) } // pick only requested customerGroups from cache return pick(this.customerGroupsCache, ids) }
async _getChannelsById(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = ids.filter((id: string) => !this.channelsCache[id]) // fetch unknown channels from API if (notCachedIds.length) { const predicate = `id in ("${notCachedIds.join('", "')}")` const channelService = this._createService('channels') const uri = channelService.where(predicate).build() const results = ProductJsonToCsv._pickAndConcatResults( await this.fetchReferences(uri) ) results.forEach((result: Channel) => { // we should keep old channels in the cache because we can resolve // multiple products in parallel and we don't want to fetch same channels // multiple times this.channelsCache[result.id] = result }) } // pick only requested channels from cache return pick(this.channelsCache, ids) }
// This method decides if to get the categories from cache or API _getCategories(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = [] const cachedCategories = [] ids.forEach((id: string) => { if (this.categoriesCache[id]) cachedCategories.push(this.categoriesCache[id]) else notCachedIds.push(id) }) if (!notCachedIds.length) return Promise.resolve(cachedCategories) const predicate = `id in ("${notCachedIds.join('", "')}")` const categoriesService = this._createService('categories') const uri = categoriesService.where(predicate).build() return this.fetchReferences(uri).then( (res: SuccessResult[]): Array<Category> => { const results = ProductJsonToCsv._pickAndConcatResults(res) results.forEach((result: Category) => { cachedCategories.push(result) this.categoriesCache[result.id] = result }) return cachedCategories } ) }
async _getCustomerGroupsById(ids: Array<string>): Promise<Array<Object>> { const notCachedIds = ids.filter( (id: string) => !this.customerGroupsCache[id] ) // fetch unknown customerGroups from API if (notCachedIds.length) { const predicate = `id in ("${notCachedIds.join('", "')}")` const customerGroupService = this._createService('customerGroups') const uri = customerGroupService.where(predicate).build() const results = ProductJsonToCsv._pickAndConcatResults( await this.fetchReferences(uri) ) results.forEach((result: CustomerGroup) => { // we should keep old customerGroups in the cache because we can resolve // multiple products in parallel and we don't want to fetch same customerGroups // multiple times this.customerGroupsCache[result.id] = result }) } // pick only requested customerGroups from cache return pick(this.customerGroupsCache, ids) }