All files / src/app/extensions/order-templates/facades order-templates.facade.ts

18.51% Statements 5/27
37.5% Branches 3/8
0% Functions 0/15
20% Lines 5/25

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 10610x 10x   10x             10x                                 10x                                                                                                                                                            
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { map, startWith, withLatestFrom } from 'rxjs/operators';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { LineItem } from 'ish-core/models/line-item/line-item.model';
import { SelectOption } from 'ish-core/models/select-option/select-option.model';
 
import { OrderTemplate, OrderTemplateHeader } from '../models/order-template/order-template.model';
import {
  addProductToNewOrderTemplate,
  addProductToOrderTemplate,
  createOrderTemplate,
  deleteOrderTemplate,
  getAllOrderTemplates,
  getOrderTemplateError,
  getOrderTemplateLoading,
  getSelectedOrderTemplateDetails,
  getSelectedOrderTemplateOutOfStockItems,
  moveItemToOrderTemplate,
  orderTemplatesActions,
  removeItemFromOrderTemplate,
  updateOrderTemplate,
} from '../store/order-template';
 
@Injectable({ providedIn: 'root' })
export class OrderTemplatesFacade {
  constructor(private store: Store) {}
 
  orderTemplates$: Observable<OrderTemplate[]> = this.store.pipe(select(getAllOrderTemplates));
  currentOrderTemplate$: Observable<OrderTemplate> = this.store.pipe(select(getSelectedOrderTemplateDetails));
  currentOrderTemplateOutOfStockItems$: Observable<string[]> = this.store.pipe(
    select(getSelectedOrderTemplateOutOfStockItems)
  );
  orderTemplateLoading$: Observable<boolean> = this.store.pipe(select(getOrderTemplateLoading));
  orderTemplateError$: Observable<HttpError> = this.store.pipe(select(getOrderTemplateError));
 
  orderTemplatesSelectOptions$(filterCurrent = true): Observable<SelectOption[]> {
    return this.orderTemplates$.pipe(
      startWith([] as OrderTemplate[]),
      map(orderTemplates =>
        orderTemplates.map(orderTemplate => ({
          value: orderTemplate.id,
          label: orderTemplate.title,
        }))
      ),
      withLatestFrom(this.currentOrderTemplate$),
      map(([orderTemplateOptions, currentOrderTemplate]) => {
        Iif (filterCurrent && currentOrderTemplate) {
          return orderTemplateOptions.filter(option => option.value !== currentOrderTemplate.id);
        }
        return orderTemplateOptions;
      })
    );
  }
 
  addOrderTemplate(orderTemplate: OrderTemplateHeader): void | HttpError {
    this.store.dispatch(createOrderTemplate({ orderTemplate }));
  }
 
  createOrderTemplateFromLineItems(orderTemplate: OrderTemplateHeader, lineItems: LineItem[]): void | HttpError {
    this.store.dispatch(orderTemplatesActions.createOrderTemplateFromLineItems({ orderTemplate, lineItems }));
  }
 
  deleteOrderTemplate(id: string): void {
    this.store.dispatch(deleteOrderTemplate({ orderTemplateId: id }));
  }
 
  updateOrderTemplate(orderTemplate: OrderTemplate): void {
    this.store.dispatch(updateOrderTemplate({ orderTemplate }));
  }
 
  addProductToNewOrderTemplate(title: string, sku: string, quantity?: number): void {
    this.store.dispatch(addProductToNewOrderTemplate({ title, sku, quantity }));
  }
 
  addProductToOrderTemplate(orderTemplateId: string, sku: string, quantity?: number): void {
    this.store.dispatch(addProductToOrderTemplate({ orderTemplateId, sku, quantity }));
  }
 
  moveItemToOrderTemplate(
    sourceOrderTemplateId: string,
    targetOrderTemplateId: string,
    sku: string,
    quantity: number
  ): void {
    this.store.dispatch(
      moveItemToOrderTemplate({
        source: { id: sourceOrderTemplateId },
        target: { id: targetOrderTemplateId, sku, quantity },
      })
    );
  }
 
  moveItemToNewOrderTemplate(sourceOrderTemplateId: string, title: string, sku: string, quantity: number): void {
    this.store.dispatch(
      moveItemToOrderTemplate({ source: { id: sourceOrderTemplateId }, target: { title, sku, quantity } })
    );
  }
 
  removeProductFromOrderTemplate(orderTemplateId: string, sku: string): void {
    this.store.dispatch(removeItemFromOrderTemplate({ orderTemplateId, sku }));
  }
}