All files / src/app/shared/components/product/product-inventory product-inventory.component.ts

100% Statements 20/20
88.23% Branches 15/17
100% Functions 7/7
100% Lines 18/18

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 529x 9x   9x     9x 9x 9x 9x                 9x 12x             12x     12x 12x 12x   11x 12x 5x   12x         26x                  
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { Observable, map, shareReplay, startWith } from 'rxjs';
 
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { SupplierStock } from 'ish-core/models/product-inventory/product-inventory.model';
 
enum StockLevel {
  high = 100,
  medium = 50,
  low = 1,
}
 
@Component({
  selector: 'ish-product-inventory',
  templateUrl: './product-inventory.component.html',
  styleUrls: ['./product-inventory.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductInventoryComponent implements OnInit {
  @Input() displayType: 'default' | 'extended' = 'default';
 
  visible$: Observable<boolean>;
  available$: Observable<boolean>;
  availableStock$: Observable<number>;
  supplierStock$: Observable<SupplierStock[]>;
 
  constructor(private context: ProductContextFacade) {}
 
  ngOnInit() {
    this.visible$ = this.context.select('displayProperties', 'inventory');
    this.available$ = this.context.select('inventory', 'inStock').pipe(startWith(true), shareReplay(1));
    this.availableStock$ = this.context
      .select('inventory', 'availableStock')
      .pipe(map(stock => (stock > 0 ? stock : 0)));
    if (this.displayType === 'extended') {
      this.supplierStock$ = this.context
        .select('inventory', 'supplierStock')
        .pipe(map(stocks => [...(stocks ?? [])].sort((a, b) => a.displayName.localeCompare(b.displayName))));
    }
  }
 
  getStockLevel(count?: number): string {
    return count >= StockLevel.high
      ? 'high'
      : count >= StockLevel.medium
      ? 'medium'
      : count >= StockLevel.low
      ? 'low'
      : 'none';
  }
}