All files / src/app/core/services/prices prices.service.ts

92.85% Statements 26/28
76.92% Branches 10/13
85.71% Functions 6/7
95.83% Lines 23/24

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 5419x 19x 19x 19x 19x     19x   19x 19x 19x     19x 2x       2x   2x                 1x       1x     1x 2x   1x 1x   1x 1x   1x   2x          
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, throwError } from 'rxjs';
import { map, switchMap, take, withLatestFrom } from 'rxjs/operators';
 
import { ProductPriceDetailsData } from 'ish-core/models/product-prices/product-prices.interface';
import { ProductPricesMapper } from 'ish-core/models/product-prices/product-prices.mapper';
import { ProductPriceDetails } from 'ish-core/models/product-prices/product-prices.model';
import { ApiService } from 'ish-core/services/api/api.service';
import { getCurrentBasket } from 'ish-core/store/customer/basket';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
 
@Injectable({ providedIn: 'root' })
export class PricesService {
  private priceHeaders = new HttpHeaders({
    Accept: 'application/vnd.intershop.pricing.v1+json',
  });
 
  constructor(private apiService: ApiService, private store: Store) {}
 
  private currentCustomer$ = this.store.pipe(select(getLoggedInCustomer), take(1));
 
  /**
   * Gets the prices for an array of products. Prices might be customer specific or depend on the user location (basket ship to address).
   *
   * @param           Array of product skus.
   * @returns         Product Prices.
   */
  getProductPrices(skus: string[]): Observable<ProductPriceDetails[]> {
    Iif (!skus || skus.length === 0) {
      return throwError(() => new Error('getProductPrices() called without skus'));
    }
 
    return this.currentCustomer$.pipe(
      withLatestFrom(this.store.pipe(select(getCurrentBasket))),
      switchMap(([customer, basket]) => {
        let params = new HttpParams();
        skus.map(sku => (params = params.append('sku', sku)));
 
        if (customer?.customerNo) {
          params = params.set('customerID', customer.customerNo);
        }
        if (basket?.commonShipToAddress) {
          params = params.set('shipToAddress', basket.commonShipToAddress.urn);
        }
        return this.apiService
          .get<{ data: ProductPriceDetailsData[] }>(`productprices`, { headers: this.priceHeaders, params })
          .pipe(map(element => element?.data?.map(prices => ProductPricesMapper.fromData(prices))));
      })
    );
  }
}