All files / projects/organization-management/src/app/components/cost-center-form cost-center-form.component.ts

95.65% Statements 22/23
76.92% Branches 10/13
100% Functions 6/6
95.23% Lines 20/21

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 1873x 3x   3x 3x   3x 3x   3x 3x 3x   3x             3x                 4x     4x       4x         4x 4x     4x   4x       4x                                 4x                                                                                                                                                                                                                                            
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { CostCenter, CostCenterBase } from 'ish-core/models/cost-center/cost-center.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { whenTruthy } from 'ish-core/utils/operators';
import { FormsService } from 'ish-shared/forms/utils/forms.service';
import { SpecialValidators } from 'ish-shared/forms/validators/special-validators';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
 
@Component({
  selector: 'ish-cost-center-form',
  templateUrl: './cost-center-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CostCenterFormComponent implements OnInit {
  @Input({ required: true }) form: FormGroup;
  @Input() costCenter: CostCenter;
 
  fields$: Observable<FormlyFieldConfig[]>;
  model$: Observable<{ budgetValue?: number; currency: string; costCenterManager?: string } & Partial<CostCenterBase>>;
 
  error$: Observable<HttpError>;
 
  constructor(private organizationManagementFacade: OrganizationManagementFacade, private appFacade: AppFacade) {}
 
  ngOnInit() {
    Iif (!this.form) {
      throw new Error('required input parameter <form> is missing for CostCenterFormComponent');
    }
 
    const currencyAndManagerOptions$ = combineLatest([
      this.appFacade.currentCurrency$.pipe(whenTruthy()),
      this.organizationManagementFacade.costCenterManagerSelectOptions$(),
    ]);
 
    this.model$ = currencyAndManagerOptions$.pipe(
      map(([currency, options]) => this.getModel(currency, options[0].value))
    );
 
    this.fields$ = currencyAndManagerOptions$.pipe(map(([currency, options]) => this.getFields(currency, options)));
 
    this.error$ = this.organizationManagementFacade.costCentersError$;
  }
 
  private getModel(currentCurrency: string, defaultManagerLogin: string) {
    return this.costCenter
      ? {
          ...this.costCenter,
          currency: this.costCenter.budget?.currency,
          budgetValue: this.costCenter.budget.value,
          budgetPeriod: this.costCenter.budgetPeriod,
          costCenterManager: this.costCenter?.costCenterOwner?.login,
        }
      : {
          costCenterManager: defaultManagerLogin,
          currency: currentCurrency,
          budgetPeriod: 'fixed',
          active: true,
        };
  }
 
  private getFields(currentCurrency: string, managerOptions: { label: string; value: string }[]) {
    return [
      {
        type: 'ish-fieldset-field',
        fieldGroup: [
          {
            key: 'currency',
            type: 'ish-text-input-field',
            props: {
              fieldClass: 'd-none',
              required: true,
            },
          },
          {
            key: 'costCenterId',
            type: 'ish-text-input-field',
            props: {
              label: 'account.costcenter.id.label',
              required: true,
              hideRequiredMarker: true,
              readonly: !!(this.costCenter?.pendingOrders + this.costCenter?.approvedOrders),
            },
            validators: {
              validation: [SpecialValidators.noSpecialChars],
            },
            validation: {
              messages: {
                required: 'account.costcenter.id.error.required',
                noSpecialChars: 'account.name.error.forbidden.chars',
              },
            },
          },
          {
            key: 'name',
            type: 'ish-text-input-field',
            props: {
              label: 'account.costcenter.name.label',
              required: true,
              hideRequiredMarker: true,
            },
            validation: {
              messages: {
                required: 'account.costcenter.name.error.required',
              },
            },
          },
          {
            fieldGroupClassName: 'row',
            fieldGroup: [
              {
                className: 'col-6 col-md-8',
                key: 'budgetValue',
                type: 'ish-text-input-field',
                props: {
                  postWrappers: [{ wrapper: 'input-addon', index: -1 }],
                  labelClass: 'col-md-6',
                  fieldClass: 'col-md-6 pr-0',
                  label: 'account.costcenter.budget.label',
                  required: true,
                  hideRequiredMarker: true,
                  addonLeft: {
                    text: this.appFacade.currencySymbol$(currentCurrency),
                  },
                  mask: 'separator.2',
                },
                validation: {
                  messages: {
                    required: 'account.costcenter.budget.error.required',
                  },
                },
              },
              {
                className: 'col-6 col-md-4',
                type: 'ish-select-field',
                key: 'budgetPeriod',
                props: {
                  fieldClass: 'col-12 label-empty',
                  options: FormsService.getCostCenterBudgetPeriodOptions(),
                },
              },
            ],
          },
          {
            key: 'costCenterManager',
            type: 'ish-select-field',
            props: {
              label: 'account.costcenter.manager.label',
              required: true,
              hideRequiredMarker: true,
              options: managerOptions,
            },
            validation: {
              messages: {
                required: 'account.costcenter.manager.error.required',
              },
            },
          },
        ],
      },
      {
        type: 'ish-fieldset-field',
        props: {
          // hide active flag for new cost centers
          fieldsetClass: !this.costCenter ? 'd-none' : undefined,
        },
        fieldGroup: [
          {
            key: 'active',
            type: 'ish-checkbox-field',
            props: {
              label: 'account.costCenter.active.label',
              title: 'account.costCenter.active.title',
            },
          },
        ],
      },
    ];
  }
}