All files / src/app/core/services/withdrawal withdrawal.service.ts

100% Statements 24/24
80% Branches 8/10
100% Functions 7/7
100% Lines 21/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 923x 3x 3x 3x     3x   3x             3x       10x         10x                   5x         5x 2x     3x         3x 3x                           4x         4x 1x     3x                       3x 3x            
import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { pick } from 'lodash-es';
import { Observable, map, tap, throwError } from 'rxjs';
 
import { WithdrawalData } from 'ish-core/models/withdrawal/withdrawal.interface';
import { WithdrawalMapper } from 'ish-core/models/withdrawal/withdrawal.mapper';
import { Withdrawal } from 'ish-core/models/withdrawal/withdrawal.model';
import { ApiService, AvailableOptions } from 'ish-core/services/api/api.service';
 
/**
 * Service for handling withdrawal (right of withdrawal) requests.
 * Provides methods to create a new withdrawal entry and to submit the full withdrawal request.
 */
@Injectable({ providedIn: 'root' })
export class WithdrawalService {
  /**
   * http header for Withdrawal API v1
   */
  private withdrawalV1Headers = new HttpHeaders({
    'content-type': 'application/json',
    Accept: 'application/vnd.intershop.withdrawal.v1+json',
  });
 
  constructor(private apiService: ApiService) {}
 
  /**
   * Creates a new withdrawal entry based on the provided order data.
   * Sends the withdrawal data as POST body to the withdrawals endpoint.
   *
   * @param withdrawal - The withdrawal data containing `orderDocumentNumber` and `orderEmail` (both required).
   * @returns An observable emitting the created {@link Withdrawal} with status `INITIAL`.
   */
  createWithdrawal(withdrawal: Withdrawal): Observable<Withdrawal> {
    const options: AvailableOptions = {
      skipApiErrorHandling: true,
      captcha: pick(withdrawal, ['captcha', 'captchaAction']),
    };
 
    if (!withdrawal?.orderDocumentNumber || !withdrawal?.orderEmail) {
      return throwError(() => new Error('createWithdrawal() called with missing required data'));
    }
 
    return this.apiService
      .post<WithdrawalData>(`withdrawals`, withdrawal, { headers: this.withdrawalV1Headers, ...options })
      .pipe(
        map(WithdrawalMapper.fromData),
        tap(data => {
          Eif (!SSR) {
            sessionStorage.setItem('withdrawal', JSON.stringify(data));
          }
        })
      );
  }
 
  /**
   * Submits the full withdrawal request for an existing withdrawal entry.
   * Updates the withdrawal with the customer's confirmation details (name, confirmation email).
   *
   * @param withdrawal - The withdrawal data including the `id` of the existing entry and additional customer details.
   * @returns An observable emitting the updated {@link Withdrawal} with status `CREATED`.
   */
  sendWithdrawalRequest(withdrawal: Withdrawal): Observable<Withdrawal> {
    const options: AvailableOptions = {
      skipApiErrorHandling: true,
      captcha: pick(withdrawal, ['captcha', 'captchaAction']),
    };
 
    if (!withdrawal?.id) {
      return throwError(() => new Error('sendWithdrawalRequest() called without required withdrawal id'));
    }
 
    return this.apiService
      .patch<WithdrawalData>(
        `withdrawals/${this.apiService.encodeResourceId(withdrawal.id)}`,
        { ...withdrawal, status: 'CREATED' },
        {
          headers: this.withdrawalV1Headers,
          ...options,
        }
      )
      .pipe(
        map(WithdrawalMapper.fromData),
        tap(data => {
          Eif (!SSR) {
            sessionStorage.setItem('withdrawal', JSON.stringify(data));
          }
        })
      );
  }
}