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

95.45% Statements 21/22
68.42% Branches 13/19
83.33% Functions 5/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 871x 1x   1x     1x   1x               1x       3x           3x   3x     3x 3x       3x               2x 1x 1x 1x     1x   1x                                                     1x       7x      
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
import { v4 as uuid } from 'uuid';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { markAsDirtyRecursive } from 'ish-shared/forms/utils/form-utils';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
import { B2bUser } from '../../models/b2b-user/b2b-user.model';
 
@Component({
  selector: 'ish-user-create-page',
  templateUrl: './user-create-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserCreatePageComponent implements OnInit {
  loading$: Observable<boolean>;
  userError$: Observable<HttpError>;
 
  form: FormGroup = this.fb.group({
    profile: this.fb.group({}),
    roleIDs: this.fb.control([]),
    userBudget: this.fb.group({}),
  });
 
  private submitted = false;
 
  constructor(private fb: FormBuilder, private organizationManagementFacade: OrganizationManagementFacade) {}
 
  ngOnInit() {
    this.loading$ = this.organizationManagementFacade.usersLoading$;
    this.userError$ = this.organizationManagementFacade.usersError$;
  }
 
  get profile(): FormGroup {
    return this.form.get('profile') as FormGroup;
  }
 
  get budgetForm() {
    return this.form.get('userBudget') as FormGroup;
  }
 
  submitForm() {
    if (this.form.invalid) {
      this.submitted = true;
      markAsDirtyRecursive(this.form);
      return;
    }
 
    const formValue = this.form.value;
 
    const user: B2bUser = {
      title: formValue.profile.title,
      firstName: formValue.profile.firstName,
      lastName: formValue.profile.lastName,
      email: formValue.profile.email,
      active: formValue.profile.active,
      phoneHome: formValue.profile.phoneHome,
      birthday: formValue.profile.birthday === '' ? undefined : formValue.birthday,
      businessPartnerNo: `U${uuid()}`,
      roleIDs: formValue.roleIDs,
      userBudget: formValue.userBudget.currency
        ? {
            budget: formValue.userBudget.budgetValue
              ? { value: formValue.userBudget.budgetValue, currency: formValue.userBudget.currency, type: 'Money' }
              : undefined,
            budgetPeriod: formValue.userBudget.budgetPeriod,
            orderSpentLimit: formValue.userBudget.orderSpentLimitValue
              ? {
                  value: formValue.userBudget.orderSpentLimitValue,
                  currency: formValue.userBudget.currency,
                  type: 'Money',
                }
              : undefined,
          }
        : undefined,
    };
 
    this.organizationManagementFacade.addUser(user);
  }
 
  get formDisabled() {
    return this.form.invalid && this.submitted;
  }
}