All files / src/app/core/service-provider products.service-provider.ts

100% Statements 16/16
82.35% Branches 14/17
100% Functions 5/5
100% Lines 16/16

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 8620x 20x 20x     20x 20x 20x                                 20x   14x 14x 14x                     10x 10x   10x 10x       10x   10x                                                                        
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, map, take } from 'rxjs';
 
import { SearchParameter, SearchResponse } from 'ish-core/models/search/search.model';
import { ProductsService } from 'ish-core/services/products/products.service';
import { SparqueProductsService } from 'ish-core/services/sparque-products/sparque-products.service';
import { getSparqueConfig } from 'ish-core/store/core/configuration';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
/**
 * Service provider that determines which products service implementation to use
 * based on Sparque configuration and feature toggles.
 *
 * This provider acts as a factory that returns either the standard ProductsService
 * or the SparqueProductsService based on:
 * - Sparque configuration availability
 * - Feature toggle states (sparque_search)
 * - skipSparque parameter override
 *
 * The provider implements a legacy mode where any valid Sparque configuration
 * will enable Sparque functionality even without explicit feature toggles.
 */
@Injectable({ providedIn: 'root' })
export class ProductsServiceProvider {
  constructor(
    private productsService: ProductsService,
    private sparqueProductsService: SparqueProductsService,
    private store: Store
  ) {}
 
  /**
   * Gets the appropriate products service implementation based on configuration and parameters.
   *
   * @param skipSparque - Optional flag to force use of standard ProductsService, defaults to false.
   * @returns The SparqueProductsService if enabled and not skipped, otherwise the standard ProductsService.
   */
  // TODO: (Sparque handling) remove 'skipSparque' parameter once the category navigation will be handled by Sparque
  get(skipSparque: boolean = false): ProductsServiceInterface {
    let enabled = false;
    this.isSparqueSearchEnabled()
      .pipe(take(1))
      .subscribe(sparqueSearchEnabled => (enabled = sparqueSearchEnabled));
    return enabled && !skipSparque ? this.sparqueProductsService : this.productsService;
  }
 
  isSparqueSearchEnabled(): Observable<boolean> {
    return this.store.pipe(
      select(getSparqueConfig),
      map(sparqueConfig => sparqueConfig?.features?.includes('search'))
    );
  }
}
 
/**
 * Service for handling search-related operations.
 * This abstract class provides methods for searching suggestions, products,
 * and filtered products. Implementations of this service should define the
 * behavior for these search functionalities.
 */
export interface ProductsServiceInterface {
  /**
   * Searches for products based on the provided search parameters.
   *
   * @param searchParams - The parameters to filter and search for products.
   * @returns An observable that emits the search response containing the products.
   */
  searchProducts(searchParams: SearchParameter): Observable<SearchResponse>;
 
  /**
   * Retrieves filtered products based on the provided search parameters.
   *
   * @param searchParameter - The URL-formatted parameters for filtering the products.
   * @param amount - The number of products to retrieve.
   * @param sortKey - (Optional) The key to sort the products by.
   * @param offset - (Optional) The offset for pagination.
   * @returns An observable that emits the filtered search response.
   */
  getFilteredProducts(
    searchParameter: URLFormParams,
    amount: number,
    sortKey?: string,
    offset?: number
  ): Observable<SearchResponse>;
}