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

61.53% Statements 16/26
42.85% Branches 3/7
50% Functions 3/6
61.53% Lines 16/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 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 931x 1x 1x   1x   1x     1x   1x                     1x 2x     2x         2x     2x 2x 2x   2x                                         2x                                                                  
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { pick } from 'lodash-es';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { Customer } from 'ish-core/models/customer/customer.model';
import { PriceType } from 'ish-core/models/price/price.model';
import { whenTruthy } from 'ish-core/utils/operators';
import { ModalDialogComponent } from 'ish-shared/components/common/modal-dialog/modal-dialog.component';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
/**
 * The Organization Settings Page Component shows the company profile.
 * The account admin can edit it and change the type (gross / net) for the budget calculation
 */
@Component({
  selector: 'ish-organization-settings-page',
  templateUrl: './organization-settings-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OrganizationSettingsPageComponent implements OnInit {
  private destroyRef = inject(DestroyRef);
  private initialBudgetPriceType: PriceType;
 
  budgetTypeForm: FormGroup = new FormGroup({});
  model: Partial<Customer>;
  fields: FormlyFieldConfig[];
  customer: Customer;
 
  constructor(private accountFacade: AccountFacade) {}
 
  ngOnInit() {
    this.accountFacade.customer$.pipe(whenTruthy(), takeUntilDestroyed(this.destroyRef)).subscribe(currentCustomer => {
      this.customer = currentCustomer;
      this.initialBudgetPriceType = currentCustomer.budgetPriceType;
    });
    this.fields = [
      {
        type: 'ish-radio-group-field',
        key: 'budgetPriceType',
        defaultValue: 'gross',
        props: {
          title: 'account.customer.price_type.label',
          customDescription: 'account.organization.org_settings.preferences.budget_price_type.info',
          options: [
            {
              value: 'gross',
              label: 'account.customer.price_type.gross.label',
            },
            {
              value: 'net',
              label: 'account.customer.price_type.net.label',
            },
          ],
        },
      },
    ];
    this.model = pick(this.customer, 'budgetPriceType');
  }
 
  openConfirmationDialog(budgetTypeChangeConfirmationDialog: ModalDialogComponent<string>) {
    Iif (this.model.budgetPriceType !== this.initialBudgetPriceType) {
      budgetTypeChangeConfirmationDialog.show();
    }
  }
 
  resetValue() {
    Iif (this.initialBudgetPriceType !== this.budgetTypeForm.get('budgetPriceType').value) {
      this.budgetTypeForm.get('budgetPriceType').reset({ value: this.initialBudgetPriceType, disabled: false });
    }
  }
 
  /**
   * Submits form and throws update event when form is valid
   */
  submit() {
    Iif (this.budgetTypeForm.invalid) {
      markAsDirtyRecursive(this.budgetTypeForm);
      return;
    }
    const budgetPriceType = this.budgetTypeForm.get('budgetPriceType').value;
 
    this.accountFacade.updateCustomerProfile(
      { ...this.customer, budgetPriceType },
      { message: 'account.profile.update_company_profile.message' }
    );
 
    this.initialBudgetPriceType = this.budgetTypeForm.get('budgetPriceType').value;
  }
}