import { SocketsService, BuyingOptionModel, ApiSocketEmitAction, PaginationParamsModel } from '@arrow/bom/core'; import { IBuyingOptionSaveRequest, IBuyingOptionInformation, IArrowReelOptions, PackagingFilterOption } from './buying-options.interfaces'; import { Subject } from 'rxjs'; import { Injectable } from '@angular/core'; import { takeUntil } from 'rxjs/operators'; import { ConfigService } from '@arrow/bom/config'; @Injectable({ providedIn: "root" }) export class BuyingOptionService { private _killSig$$: Subject = new Subject(); constructor( private _socketService: SocketsService, private _configService: ConfigService ) { this._socketService.observeApiErrors().pipe(takeUntil(this._killSig$$)); } public isTargetArrowcom(): boolean { return this._configService.isArrowcom(); } public getUserSelectedCurrency(): string { return this._configService.userSelectedCurrency ? this._configService.userSelectedCurrency : this._configService.defaultCurrency; } public saveBuyingOptions( buyingOptions: Array, bomId: string, rowId: string, hasMultiplier: boolean, paginationParams: PaginationParamsModel ) { const payload = this.getBuyingOptionSavePayload( buyingOptions, bomId, rowId, hasMultiplier, paginationParams ); this._socketService .emitAction(ApiSocketEmitAction.ADD_SBO, payload) .pipe(takeUntil(this._killSig$$)) .subscribe(); } public getRequestQuantityByWarehouse(warehouse: BuyingOptionModel): number { let quantityRequested = +warehouse.input ? +warehouse.input : 0; if ( warehouse.packagingTypeId == PackagingFilterOption.arrowReel && warehouse.reelOptions ) { warehouse.reelOptions.reelCount = this._configService.isArrowcom() ? 1 : warehouse.reelOptions.reelCount; quantityRequested = warehouse.reelOptions.reelLength * warehouse.reelOptions.reelCount; } return quantityRequested; } // something similar in BuyingOptionModel called selectedSource. It seems to messy to call from there. Needs double check // keep it here for readability, for now public getWarehouseById( sourcePartId: string, buyingOptions: Array ): BuyingOptionModel { return buyingOptions.find(x => x.sourcePartId === sourcePartId); } public updateReelsTotalQuantity(buyingOption: BuyingOptionModel): void { const totalRequested = this.getRequestQuantityByWarehouse( buyingOption ); buyingOption.input = totalRequested; buyingOption.reelTotalQuantity = totalRequested; } public getWarehouseReelOptions( sourcePartId: string, buyingOptions: Array ): IArrowReelOptions { const warehouse = this.getWarehouseById(sourcePartId, buyingOptions); return warehouse.reelOptions ? warehouse.reelOptions : { reelCount: 0, reelLength: 0 }; } public isValidArrowReel(warehouse: BuyingOptionModel):boolean { return +warehouse.reelOptions.reelLength > 0 && (this._configService.isArrowcom() || +warehouse.reelOptions.reelCount > 0); } private getBuyingOptionSavePayload( buyingOptions: Array, bomId: string, rowId: string, hasMultiplier: boolean, paginationParams: PaginationParamsModel ): IBuyingOptionSaveRequest { let payload: IBuyingOptionSaveRequest; const buyingOptionsData = buyingOptions .map(bo => { let result: IBuyingOptionInformation; let quantityRequested = this.getRequestQuantityByWarehouse(bo); result = { sourcePartId: bo.sourcePartId, quantityRequested: quantityRequested, validBuyingOption: bo.isValidBuyingOption(), reelOptions: this._validateReelOptions(bo.reelOptions) }; return result; }) .filter((option: IBuyingOptionInformation) => option); return (payload = { bomId: bomId, rowId: rowId, original: buyingOptionsData, multiplied: hasMultiplier ? buyingOptionsData : null, pageNum: paginationParams.currentPage, perPage: paginationParams.itemsPerPage, filter: paginationParams.filter, sorting: paginationParams.sorting }); } private _validateReelOptions( reelOptions: IArrowReelOptions ): IArrowReelOptions { if ( this._configService.isArrowcom() && reelOptions && reelOptions.reelLength >= 1 ) { reelOptions.reelCount = 1; } return reelOptions; } }