All files / src/app/pages/withdrawal-request/withdrawal-request-form withdrawal-request-form.component.ts

100% Statements 26/26
91.66% Branches 11/12
100% Functions 7/7
100% Lines 25/25

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 1832x 2x   2x   2x                                                               2x   10x 10x   10x   10x     10x     10x       3x 1x     2x 1x                 1x                 10x 10x       10x 4x             6x     10x       10x         10x                                                                                                                                                         4x      
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { Observable, combineLatest, map } from 'rxjs';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { Withdrawal } from 'ish-core/models/withdrawal/withdrawal.model';
 
/**
 * Dual-mode form component for handling withdrawal (right of withdrawal) requests.
 *
 * The component operates in two modes based on the presence of the `withdrawal` input:
 *
 * **Search Mode** (no withdrawal input):
 * - Displays fields for order number and order email
 * - Emits `createWithdrawal` event to initiate a withdrawal entry
 *
 * **Request Mode** (withdrawal input provided):
 * - Displays fields for customer name and confirmation email
 * - Pre-fills order data from the existing withdrawal
 * - Emits `submitWithdrawal` event to complete the withdrawal request
 *
 * Both modes support optional CAPTCHA validation based on server configuration.
 *
 * @example
 * <!-- Search mode -->
 * <ish-withdrawal-form (createWithdrawal)="onCreateWithdrawal($event)" />
 *
 * @example
 * <!-- Request mode -->
 * <ish-withdrawal-form [withdrawal]="withdrawal" (submitWithdrawal)="onSubmitWithdrawal($event)" />
 */
@Component({
  selector: 'ish-withdrawal-request-form',
  templateUrl: './withdrawal-request-form.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class WithdrawalRequestFormComponent implements OnInit {
  @Input() withdrawal: Withdrawal;
  @Output() readonly createWithdrawal = new EventEmitter<{ orderDocumentNumber: string; orderEmail: string }>();
  @Output() readonly submitWithdrawal = new EventEmitter<Withdrawal>();
 
  withdrawalForm = new FormGroup({});
  fields$: Observable<FormlyFieldConfig[]>;
  model: Partial<Withdrawal> = {};
  submitButtonLabel: string;
 
  constructor(private appFacade: AppFacade) {}
 
  ngOnInit() {
    this.getFormConfiguration();
  }
 
  submitForm() {
    if (this.withdrawalForm.invalid) {
      return;
    }
 
    if (this.isRequestMode()) {
      this.submitWithdrawal.emit({
        ...this.withdrawalForm.value,
        orderDocumentNumber: this.withdrawal.orderDocumentNumber,
        name: this.withdrawalForm.get('name').value,
        orderEmail: this.withdrawal.orderEmail,
        confirmationEmail: this.withdrawalForm.get('confirmationEmail').value,
        id: this.withdrawal.id,
      });
    } else {
      this.createWithdrawal.emit({
        ...this.withdrawalForm.value,
        orderDocumentNumber: this.withdrawalForm.get('orderDocumentNumber').value,
        orderEmail: this.withdrawalForm.get('orderEmail').value,
      });
    }
  }
 
  private getFormConfiguration() {
    const isRequestMode = !!this.withdrawal;
    this.submitButtonLabel = isRequestMode
      ? 'account.withdrawal.form.request.button.label'
      : 'account.withdrawal.form.search.button.label';
 
    if (isRequestMode) {
      this.model = {
        orderDocumentNumber: this.withdrawal.orderDocumentNumber,
        orderEmail: this.withdrawal.orderEmail,
        name: undefined,
        confirmationEmail: this.withdrawal.orderEmail,
      };
    } else {
      this.model = {};
    }
 
    this.fields$ = combineLatest([
      this.appFacade.serverSetting$<boolean>('services.ReCaptchaV2ServiceDefinition.runnable'),
      this.appFacade.serverSetting$<boolean>('captcha.withdrawal'),
    ]).pipe(
      map(([isCaptchaV2, isCaptchaTopicEnabled]) => this.buildFields(isRequestMode, isCaptchaTopicEnabled, isCaptchaV2))
    );
  }
 
  private buildFields(isRequestMode: boolean, isCaptchaEnabled: boolean, isCaptchaV2: boolean): FormlyFieldConfig[] {
    return [
      {
        key: 'orderDocumentNumber',
        type: 'ish-text-input-field',
        props: {
          label: 'account.withdrawal.form.search.order_number.label',
          required: true,
          disabled: isRequestMode,
          hideRequiredMarker: true,
        },
        validation: {
          messages: {
            required: 'account.withdrawal.form.search.order_number.error',
          },
        },
      },
      ...(isRequestMode
        ? [
            {
              key: 'name',
              type: 'ish-text-input-field',
              props: {
                label: 'account.withdrawal.form.request.order_username.label',
                required: true,
                hideRequiredMarker: true,
              },
              validation: {
                messages: {
                  required: 'account.withdrawal.form.request.order_username.error',
                },
              },
            },
            {
              key: 'confirmationEmail',
              type: 'ish-email-field',
              props: {
                label: 'account.withdrawal.form.request.confirmation_email.label',
                required: true,
                hideRequiredMarker: true,
                postWrappers: [{ wrapper: 'description', index: -1 }],
                customDescription: {
                  key: 'account.withdrawal.form.request.confirmation_email.extrainfo.message',
                },
              },
            },
          ]
        : [
            {
              key: 'orderEmail',
              type: 'ish-email-field',
              props: {
                label: 'account.withdrawal.form.search.order_email.label',
                required: true,
                hideRequiredMarker: true,
                postWrappers: [{ wrapper: 'description', index: -1 }],
                customDescription: {
                  key: 'account.withdrawal.form.search.order_email.extrainfo.message',
                },
              },
            },
          ]),
      ...(isCaptchaEnabled
        ? [
            {
              type: 'ish-captcha-field',
              props: {
                topic: 'withdrawal',
                required: isCaptchaV2,
                fieldClass: 'offset-md-4 col-md-8',
              },
            },
          ]
        : []),
    ];
  }
 
  private isRequestMode(): boolean {
    return !!this.withdrawal;
  }
}