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

100% Statements 18/18
85.71% Branches 6/7
100% Functions 6/6
100% Lines 15/15

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 4122x 22x 22x 22x     22x   22x           22x 5x       5x                 4x 2x     2x 3x   2x   2x      
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { ProductInventoryData } from 'ish-core/models/product-inventory/product-inventory.interface';
import { ProductInventoryMapper } from 'ish-core/models/product-inventory/product-inventory.mapper';
import { ProductInventory } from 'ish-core/models/product-inventory/product-inventory.model';
import { ApiService } from 'ish-core/services/api/api.service';
 
/**
 * The Inventory Service handles the interaction with the 'inventories' REST API.
 */
@Injectable({ providedIn: 'root' })
export class InventoryService {
  private readonly inventoryHeaders = new HttpHeaders({
    Accept: 'application/vnd.intershop.inventory.v1+json',
  });
 
  constructor(private apiService: ApiService) {}
 
  /**
   * Gets the inventory information for an array of products.
   *
   * @param skus           Array of product skus.
   * @returns              Product Inventory details.
   */
  getProductInventory(skus: string[]): Observable<ProductInventory[]> {
    if (!skus || skus.length === 0) {
      return throwError(() => new Error('getProductInventory() called without skus'));
    }
 
    let params = new HttpParams();
    params = skus.reduce((p, sku) => p.append('sku', sku), params);
 
    return this.apiService
      .get<{ data: ProductInventoryData[] }>(`inventories`, { headers: this.inventoryHeaders, params })
      .pipe(map(response => response?.data?.map(inventory => ProductInventoryMapper.fromData(inventory))));
  }
}