All files / src/app/shared/forms/utils forms.service.ts

94.28% Statements 33/35
83.33% Branches 10/12
85.71% Functions 12/14
93.33% Lines 28/30

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 11312x 12x 12x 12x 12x       12x 12x             12x 26x       12x 9x                 43x     2x                       20x   120x                           6x   12x                                                 6x   6x   1x 1x     1x 1x     1x 1x     1x 1x     6x      
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { Observable, OperatorFunction, forkJoin } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
 
import { Address } from 'ish-core/models/address/address.model';
import { SelectOption } from 'ish-core/models/select-option/select-option.model';
import { getCurrentLocale } from 'ish-core/store/core/configuration';
import { whenTruthy } from 'ish-core/utils/operators';
 
/**
 * FormsService.getAddressOptions as a pipeable operator
 *
 * @returns the input addresses, mapped to select options
 */
export function mapToAddressOptions(): OperatorFunction<Address[], SelectOption[]> {
  return (source$: Observable<Address[]>) => FormsService.getAddressOptions(source$);
}
 
@Injectable({ providedIn: 'root' })
export class FormsService {
  constructor(private translate: TranslateService, private store: Store) {}
 
  /**
   * Get address select options for addresses in order to render them in an address select box.
   *
   * @param addresses
   * @returns address select options observable
   */
  static getAddressOptions(addresses$: Observable<Address[]>): Observable<SelectOption[]> {
    return addresses$.pipe(
      whenTruthy(),
      map(addresses =>
        addresses.map(a => ({
          label: `${a.firstName} ${a.lastName}, ${a.addressLine1}, ${a.city}`,
          value: a.id,
        }))
      )
    );
  }
 
  /**
   * Gets budget period select options for cost center budgets.
   */
  static getCostCenterBudgetPeriodOptions() {
    const periods = ['fixed', 'weekly', 'monthly', 'quarterly', 'half-yearly', 'yearly'];
 
    return periods.map(period => ({
      value: period,
      // keep-localization-pattern: ^account\.costcenter\.budget\.period\.value.*
      label: `account.costcenter.budget.period.value.${period}`,
    }));
  }
 
  /**
   * Gets all possible salutation options for a certain country.
   *
   * @param countryCode country code of the country for which the salutations should be determined.
   * @returns salutation select options
   */
  getSalutationOptionsForCountryCode(countryCode: string): Observable<SelectOption[]> {
    return forkJoin<SelectOption[]>(
      this.determineSalutations(countryCode).map(title =>
        this.translate.get(title).pipe(map(translation => ({ value: translation, label: title })))
      )
    );
  }
 
  /**
   * Gets all possible salutation options for the current locale.
   *
   * @returns salutation select options
   */
  getSalutationOptions(): Observable<SelectOption[]> {
    return this.store.pipe(select(getCurrentLocale)).pipe(
      whenTruthy(),
      switchMap(locale => this.getSalutationOptionsForCountryCode(locale?.substring(3)))
    );
  }
 
  /**
   * Gets all possible salutations for a certain country.
   *
   * @param countryCode country code of the country for which the salutations should be determined.
   * @returns translation keys of the salutations
   */
  private determineSalutations(countryCode: string): string[] {
    // TODO: should come from configuration?
    let salutationLabels: string[] = [];
 
    switch (countryCode) {
      case 'DE': {
        salutationLabels = ['account.salutation.ms.text', 'account.salutation.mr.text', 'account.salutation.dr.text'];
        break;
      }
      case 'FR': {
        salutationLabels = ['account.salutation.ms.text', 'account.salutation.mr.text', 'account.salutation.dr.text'];
        break;
      }
      case 'US': {
        salutationLabels = ['account.salutation.ms.text', 'account.salutation.mr.text', 'account.salutation.dr.text'];
        break;
      }
      case 'GB': {
        salutationLabels = ['account.salutation.ms.text', 'account.salutation.mr.text', 'account.salutation.dr.text'];
        break;
      }
    }
    return salutationLabels;
  }
}