All files / src/app/extensions/order-templates/services/order-template order-template.service.ts

100% Statements 45/45
95% Branches 19/20
100% Functions 21/21
100% Lines 38/38

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 1463x 3x 3x 3x     3x     3x       3x   16x 16x                 2x         2x 1x         1x 2x   1x                       8x 1x   7x   7x                   1x   1x                   2x 1x   1x                   1x   1x                       4x 1x   3x 1x   2x               2x                     3x 1x   2x 1x   1x           1x      
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, forkJoin, of, throwError } from 'rxjs';
import { concatMap, map, switchMap } from 'rxjs/operators';
 
import { Link } from 'ish-core/models/link/link.model';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
 
import { OrderTemplateData } from '../../models/order-template/order-template.interface';
import { OrderTemplateMapper } from '../../models/order-template/order-template.mapper';
import { OrderTemplate, OrderTemplateHeader } from '../../models/order-template/order-template.model';
 
@Injectable({ providedIn: 'root' })
export class OrderTemplateService {
  constructor(
    private apiService: ApiService,
    private orderTemplateMapper: OrderTemplateMapper
  ) {}
 
  /**
   * Gets a list of order template for the current user.
   *
   * @returns           The customer's order templates.
   */
  getOrderTemplates(): Observable<OrderTemplate[]> {
    return this.apiService.get(`customers/-/users/-/wishlists`).pipe(
      unpackEnvelope<Link | OrderTemplateData>(),
      switchMap(orderTemplateData => {
        // ICM 14.2.0+ returns the list attributes directly, so only the list data is fetched after login
        // and item details are loaded on demand by the consuming components/pages
        if (orderTemplateData.length === 0 || 'attributes' in orderTemplateData[0]) {
          return of(this.orderTemplateMapper.fromListData(orderTemplateData as Link[]));
        }
        // legacy data format (ICM < 14.2.0) with uri only in the list response
        // -> get each order template separately, i.e. all data is still fetched after login
        // TODO: remove once ICM versions < 14.2.0 are no longer supported
        const obsArray = orderTemplateData.map((d: OrderTemplateData) =>
          this.getOrderTemplate(this.orderTemplateMapper.fromDataToId(d))
        );
        return obsArray.length ? forkJoin(obsArray) : of([]);
      })
    );
  }
 
  /**
   * Gets a order template of the given id for the current user.
   *
   * @param orderTemplateId  The order template id.
   * @returns           The order template.
   */
  getOrderTemplate(orderTemplateId: string): Observable<OrderTemplate> {
    if (!orderTemplateId) {
      return throwError(() => new Error('getOrderTemplate() called without orderTemplateId'));
    }
    return this.apiService
      .get<OrderTemplateData>(`customers/-/users/-/wishlists/${this.apiService.encodeResourceId(orderTemplateId)}`)
      .pipe(map(orderTemplateData => this.orderTemplateMapper.fromData(orderTemplateData, orderTemplateId)));
  }
 
  /**
   * Creates a order template for the current user.
   *
   * @param OrderTemplateDetails   The order template data.
   * @returns                 The created order template.
   */
  createOrderTemplate(orderTemplateData: OrderTemplateHeader): Observable<OrderTemplate> {
    return this.apiService
      .post('customers/-/users/-/wishlists', orderTemplateData)
      .pipe(concatMap((response: OrderTemplateData) => this.getOrderTemplate(response.title)));
  }
 
  /**
   * Deletes a order template of the given id.
   *
   * @param orderTemplateId   The order template id.
   * @returns            The order template.
   */
  deleteOrderTemplate(orderTemplateId: string): Observable<void> {
    if (!orderTemplateId) {
      return throwError(() => new Error('deleteOrderTemplate() called without orderTemplateId'));
    }
    return this.apiService.delete(`customers/-/users/-/wishlists/${this.apiService.encodeResourceId(orderTemplateId)}`);
  }
 
  /**
   * Updates a order template of the given id.
   *
   * @param orderTemplate   The order template to be updated.
   * @returns          The updated order template.
   */
  updateOrderTemplate(orderTemplate: OrderTemplate): Observable<OrderTemplate> {
    return this.apiService
      .put(`customers/-/users/-/wishlists/${this.apiService.encodeResourceId(orderTemplate.id)}`, orderTemplate)
      .pipe(map((response: OrderTemplate) => this.orderTemplateMapper.fromUpdate(response, orderTemplate.id)));
  }
 
  /**
   * Adds a product to the order template with the given id and reloads the order template.
   *
   * @param orderTemplate Id   The order template id.
   * @param sku           The product sku.
   * @param quantity      The product quantity (default = 1).
   * @returns             The changed order template.
   */
  addProductToOrderTemplate(orderTemplateId: string, sku: string, quantity: number): Observable<OrderTemplate> {
    if (!orderTemplateId) {
      return throwError(() => new Error('addProductToOrderTemplate() called without orderTemplateId'));
    }
    if (!sku) {
      return throwError(() => new Error('addProductToOrderTemplate() called without sku'));
    }
    return this.apiService
      .post(
        `customers/-/users/-/wishlists/${this.apiService.encodeResourceId(
          orderTemplateId
        )}/products/${this.apiService.encodeResourceId(sku)}`,
        {},
        { params: new HttpParams().set('quantity', quantity || 1) }
      )
      .pipe(concatMap(() => this.getOrderTemplate(orderTemplateId)));
  }
 
  /**
   * Removes a product from the order template with the given id. Returns an error observable if parameters are falsy.
   *
   * @param wishlist Id   The order template id.
   * @param sku           The product sku.
   * @returns             The changed order template.
   */
  removeProductFromOrderTemplate(orderTemplateId: string, sku: string): Observable<OrderTemplate> {
    if (!orderTemplateId) {
      return throwError(() => new Error('removeProductFromOrderTemplate() called without orderTemplateId'));
    }
    if (!sku) {
      return throwError(() => new Error('removeProductFromOrderTemplate() called without sku'));
    }
    return this.apiService
      .delete(
        `customers/-/users/-/wishlists/${this.apiService.encodeResourceId(
          orderTemplateId
        )}/products/${this.apiService.encodeResourceId(sku)}`
      )
      .pipe(concatMap(() => this.getOrderTemplate(orderTemplateId)));
  }
}