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

100% Statements 14/14
80% Branches 8/10
100% Functions 5/5
100% Lines 13/13

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 5520x 20x 20x       20x 20x               20x 59x               13x 13x   13x 13x       13x   13x                                        
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, map, take } from 'rxjs';
 
import { Product } from 'ish-core/models/product/product.model';
import { Recommendations, RecommendationsContext } from 'ish-core/models/recommendations/recommendations.model';
import { SparqueRecommendationsService } from 'ish-core/services/sparque-recommendations/sparque-recommendations.service';
import { getSparqueConfig } from 'ish-core/store/core/configuration';
 
/**
 * Service provider that dynamically selects the appropriate recommendation service implementation
 * based on feature toggles and configuration. Currently supports Sparque AI-powered recommendations
 * or returns undefined when recommendations are not available.
 */
@Injectable({ providedIn: 'root' })
export class RecommendationsServiceProvider {
  constructor(private sparqueRecommendationsService: SparqueRecommendationsService, private store: Store) {}
 
  /**
   * Gets the appropriate recommendations service implementation based on current configuration.
   *
   * @returns The Sparque recommendations service if enabled, otherwise undefined.
   */
  get(): RecommendationsServiceInterface {
    let enabled = false;
    this.isSparqueRecommendationsEnabled()
      .pipe(take(1))
      .subscribe(sparqueRecommendationsEnabled => (enabled = sparqueRecommendationsEnabled));
    return enabled ? this.sparqueRecommendationsService : undefined;
  }
 
  private isSparqueRecommendationsEnabled(): Observable<boolean> {
    return this.store.pipe(
      select(getSparqueConfig),
      map(sparqueConfig => sparqueConfig?.features?.includes('recommendations'))
    );
  }
}
 
/**
 * Abstract service interface that defines methods for product recommendations.
 * Implementations of this service should define the behavior for these recommendation functionalities.
 */
export interface RecommendationsServiceInterface {
  /**
   * Gets product recommendations based on the provided recommendation configuration.
   *
   * @param recommendationsContext - The recommendation context containing strategy and parameters.
   * @returns An observable that emits the recommendation results with products.
   */
  getRecommendations(
    recommendationsContext: RecommendationsContext
  ): Observable<{ recommendations: Recommendations; products: Partial<Product>[] }>;
}