All files / projects/organization-management/src/app/pages/cost-centers cost-centers-page.component.ts

63.15% Statements 24/38
55.55% Branches 5/9
43.75% Functions 7/16
64.86% Lines 24/37

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 1221x 1x 1x             1x                   1x           6x   6x 6x 6x 6x   6x                         6x     6x 6x 6x 6x 6x                 6x   6x 6x 6x 15x                                                                                               15x       1x      
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { BehaviorSubject, Observable, combineLatest, map, of, take } from 'rxjs';
 
import { CostCenter } from 'ish-core/models/cost-center/cost-center.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { PagingInfo } from 'ish-core/models/paging-info/paging-info.model';
import { ModalDialogComponent } from 'ish-shared/components/common/modal-dialog/modal-dialog.component';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
import { CostCenterQuery } from '../../models/cost-center-query/cost-center-query.model';
 
type CostCenterColumnsType = 'costCenterId' | 'costCenterName' | 'costCenterManager' | 'costCenterBudget' | 'actions';
 
@Component({
  selector: 'ish-cost-centers-page',
  templateUrl: './cost-centers-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CostCentersPageComponent implements OnInit {
  costCenters$: Observable<CostCenter[]>;
  costCentersError$: Observable<HttpError>;
  costCentersLoading$: Observable<boolean>;
  costCentersForPage$: Observable<CostCenter[]>;
  pagingInfo$: Observable<PagingInfo>;
  pageSize = 25;
 
  private activeFilters: Partial<CostCenterQuery> = {};
  private destroyRef = inject(DestroyRef);
  private pageNumberSubject = new BehaviorSubject<number>(1);
  pageNumber$ = this.pageNumberSubject.asObservable();
 
  columnsToDisplay: CostCenterColumnsType[] = [
    'costCenterId',
    'costCenterName',
    'costCenterManager',
    'costCenterBudget',
    'actions',
  ];
 
  /**
   * keep cost center for usage in confirmation dialogs (delete/deactivate)
   */
  selectedCostCenter: CostCenter;
 
  constructor(private organizationManagementFacade: OrganizationManagementFacade) {}
 
  ngOnInit() {
    this.costCenters$ = this.organizationManagementFacade.costCenters$;
    this.costCentersError$ = this.organizationManagementFacade.costCentersError$;
    this.costCentersLoading$ = this.organizationManagementFacade.costCentersLoading$;
    this.pagingInfo$ = this.organizationManagementFacade.costCentersPagingInfo$;
    this.getCostCentersForPage();
  }
 
  openConfirmationDialog(costCenter: CostCenter, modal: ModalDialogComponent<string>) {
    this.selectedCostCenter = costCenter;
    modal.show();
  }
 
  private getCostCentersForPage() {
    this.costCentersForPage$ = combineLatest([this.costCenters$, this.pageNumber$]).pipe(
      map(([costCenters, pageNumber]) => {
        const start = (pageNumber - 1) * this.pageSize;
        const end = start + this.pageSize;
        return costCenters.filter(
          costCenter => costCenter.paginationPosition >= start && costCenter.paginationPosition < end
        );
      })
    );
  }
 
  loadMoreCostCenters(pageNumber: number): void {
    this.pageNumberSubject.next(pageNumber);
 
    this.costCenters$.pipe(take(1), takeUntilDestroyed(this.destroyRef)).subscribe(costCenters => {
      Iif (!costCenters.find(costCenter => costCenter.paginationPosition === (pageNumber - 1) * this.pageSize)) {
        this.organizationManagementFacade.loadCostCenters({
          offset: (pageNumber - 1) * this.pageSize,
          limit: this.pageSize,
          costCenterNameId: this.activeFilters.costCenterNameId ? this.activeFilters.costCenterNameId : undefined,
        });
      }
    });
  }
 
  getTotalPages(totalCostCenters: number) {
    return Math.ceil(totalCostCenters / this.pageSize);
  }
 
  loadFilteredCostCenters(filters: Partial<CostCenterQuery>) {
    this.activeFilters = filters;
    this.organizationManagementFacade.loadCostCenters({
      offset: 0,
      limit: this.pageSize,
      costCenterNameId: filters.costCenterNameId,
    });
    this.pageNumberSubject.next(1);
  }
 
  /** Deletes the cost center */
  delete() {
    this.organizationManagementFacade.deleteCostCenter(this.selectedCostCenter.id);
  }
 
  deactivate() {
    this.organizationManagementFacade.updateCostCenter({ ...this.selectedCostCenter, active: false });
  }
 
  activate(costCenter: CostCenter) {
    this.organizationManagementFacade.updateCostCenter({ ...costCenter, active: true });
  }
 
  isDeletable(costCenter: CostCenter): Observable<boolean> {
    return this.organizationManagementFacade.isCostCenterEditable(of(costCenter));
  }
 
  get filtersActive(): boolean {
    return Object.keys(this.activeFilters).length > 0;
  }
}