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

100% Statements 16/16
78.57% Branches 11/14
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 6120x 20x 20x         20x 20x 20x               20x   59x 59x 59x                 14x 14x   14x 14x       14x   14x                                        
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, map, take } from 'rxjs';
 
import { CategoryTree } from 'ish-core/models/category-tree/category-tree.model';
import { Product } from 'ish-core/models/product/product.model';
import { Suggestions } from 'ish-core/models/suggestions/suggestions.model';
import { SparqueSuggestionsService } from 'ish-core/services/sparque-suggestions/sparque-suggestions.service';
import { SuggestService } from 'ish-core/services/suggest/suggest.service';
import { getSparqueConfig } from 'ish-core/store/core/configuration';
 
/**
 * Service provider that dynamically selects between different suggestion service implementations
 * based on feature toggles and configuration. This allows switching between the default ICM
 * suggestion service and the Sparque AI-powered suggestion service.
 */
@Injectable({ providedIn: 'root' })
export class SuggestionsServiceProvider {
  constructor(
    private suggestService: SuggestService,
    private sparqueSuggestionsService: SparqueSuggestionsService,
    private store: Store
  ) {}
 
  /**
   * Gets the appropriate suggestions service implementation based on current configuration.
   *
   * @returns The Sparque suggestions service if enabled, otherwise the default ICM suggest service.
   */
  get(): SuggestionsServiceInterface {
    let enabled = false;
    this.isSparqueSuggestionsEnabled()
      .pipe(take(1))
      .subscribe(sparqueSuggestionsEnabled => (enabled = sparqueSuggestionsEnabled));
    return enabled ? this.sparqueSuggestionsService : this.suggestService;
  }
 
  private isSparqueSuggestionsEnabled(): Observable<boolean> {
    return this.store.pipe(
      select(getSparqueConfig),
      map(sparqueConfig => sparqueConfig?.features?.includes('suggestions'))
    );
  }
}
 
/**
 * Abstract service class that defines methods for search suggestions.
 * Implementations of this service should define the behavior for these suggestions functionalities.
 */
export interface SuggestionsServiceInterface {
  /**
   * Searches for suggestions based on the provided search term.
   *
   * @param searchTerm - The term to search for suggestions.
   * @returns An observable that emits the search suggestions.
   */
  searchSuggestions(
    searchTerm: string
  ): Observable<{ suggestions: Suggestions; categories?: CategoryTree; products?: Partial<Product>[] }>;
}