All files / src/app/core/services/sparque-products sparque-products.service.ts

96.42% Statements 27/28
66.66% Branches 12/18
100% Functions 7/7
96.29% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 8718x 18x 18x       18x   18x 18x               18x   4x   4x   4x                 2x       2x     2x 2x     2x   1x                                   2x         2x 2x   2x   2x   1x       2x 2x 2x   2x      
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, map } from 'rxjs';
 
import { SearchParameter, SearchResponse } from 'ish-core/models/search/search.model';
import { SparqueSearch } from 'ish-core/models/sparque-search/sparque-search.interface';
import { SparqueSearchMapper } from 'ish-core/models/sparque-search/sparque-search.mapper';
import { ProductsServiceInterface } from 'ish-core/service-provider/products.service-provider';
import { SparqueApiService } from 'ish-core/services/sparque-api/sparque-api.service';
import { omit } from 'ish-core/utils/functions';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
/**
 * Service for interacting with the Sparque API to perform search-related operations.
 * Extends the base `SearchService` to provide specific implementations for Sparque.
 */
@Injectable({ providedIn: 'root' })
export class SparqueProductsService implements ProductsServiceInterface {
  // API version for Sparque API.
  private readonly apiVersion = 'v2';
  // Maximum number of facet options to request from the Sparque API.
  private readonly facetOptionsCount = '10';
 
  constructor(private sparqueApiService: SparqueApiService, private sparqueSearchMapper: SparqueSearchMapper) {}
 
  /**
   * Searches for products based on the provided search parameters.
   *
   * @param searchParams - The parameters for the product search, including term, amount, offset, and sorting.
   * @returns An observable emitting the mapped search response.
   */
  searchProducts(searchParams: SearchParameter): Observable<SearchResponse> {
    let params = new HttpParams()
      .set('count', searchParams.amount.toString())
      .set('offset', searchParams.offset.toString())
      .set('facetOptionsCount', this.facetOptionsCount);
    Iif (searchParams.sorting) {
      params = params.set('sorting', searchParams.sorting);
    }
    if (!searchParams.searchParameter && searchParams.searchTerm) {
      params = params.set('keyword', searchParams.searchTerm);
    }
 
    return this.sparqueApiService
      .get<SparqueSearch>(`search`, this.apiVersion, { params, skipApiErrorHandling: true })
      .pipe(map(result => this.sparqueSearchMapper.fromData(result, { searchTerm: [searchParams.searchTerm] })));
  }
 
  /**
   * Retrieves filtered products based on the provided search parameters.
   *
   * @param searchParameter - The search parameters, including facets and search term.
   * @param amount - The number of products to retrieve.
   * @param sortKey - The key to sort the results by (optional).
   * @param offset - The offset for pagination (default is 0).
   * @returns An observable emitting the mapped search response.
   */
  getFilteredProducts(
    searchParameter: URLFormParams,
    amount: number,
    sortKey?: string,
    offset = 0
  ): Observable<SearchResponse> {
    let params = new HttpParams()
      .set('count', amount ? amount.toString() : '')
      .set('offset', offset.toString())
      .set('facetOptionsCount', this.facetOptionsCount)
      .set('keyword', searchParameter.searchTerm ? searchParameter.searchTerm[0] : '');
    if (sortKey) {
      params = params.set('sorting', sortKey);
    }
    params = params.append('selectedFacets', this.selectedFacets(omit(searchParameter, 'searchTerm')));
 
    return this.sparqueApiService
      .get<SparqueSearch>(`search`, this.apiVersion, { params, skipApiErrorHandling: true })
      .pipe(map(result => this.sparqueSearchMapper.fromData(result, searchParameter)));
  }
 
  private selectedFacets(object: URLFormParams): string {
    let params = '';
    Object.entries(object).forEach(([key, val]) => {
      params = params + key.concat('|').concat(val[0]).concat(',');
    });
    return params.substring(0, params.length - 1);
  }
}