All files / src/app/core/models/price price.pipe.ts

96.15% Statements 25/26
73.68% Branches 14/19
100% Functions 4/4
96.15% Lines 25/26

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 5331x 31x 31x 31x   31x 31x         31x 93x 93x       31x     80x     80x 80x 80x       104x 13x     91x       91x   48x 15x   33x 33x 33x   33x   43x        
import { formatCurrency, getCurrencySymbol } from '@angular/common';
import { ChangeDetectorRef, DestroyRef, Pipe, PipeTransform, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { TranslateService } from '@ngx-translate/core';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { PriceItemHelper } from 'ish-core/models/price-item/price-item.helper';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
 
import { Price } from './price.model';
 
export function formatPrice(price: Price, lang: string): string {
  const symbol = getCurrencySymbol(price.currency, 'narrow', lang);
  return symbol ? formatCurrency(price.value, lang, symbol) : price.value?.toString();
}
 
@Pipe({ name: 'ishPrice', pure: false })
export class PricePipe implements PipeTransform {
  displayText: string;
 
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private translateService: TranslateService,
    private cdRef: ChangeDetectorRef,
    private accountFacade: AccountFacade
  ) {}
 
  transform(data: Price | PriceItem, priceType?: 'gross' | 'net'): string {
    if (!data) {
      return this.translateService.instant('product.price.na.text');
    }
 
    Iif (!this.translateService.currentLang) {
      return 'N/A';
    }
 
    switch (data.type) {
      case 'PriceItem':
        if (priceType) {
          return formatPrice(PriceItemHelper.selectType(data, priceType), this.translateService.currentLang);
        }
        this.accountFacade.userPriceDisplayType$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(type => {
          this.displayText = formatPrice(PriceItemHelper.selectType(data, type), this.translateService.currentLang);
          this.cdRef.markForCheck();
        });
        return this.displayText;
      default:
        return formatPrice(data as Price, this.translateService.currentLang);
    }
  }
}