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

61.9% Statements 26/42
38.88% Branches 7/18
63.63% Functions 14/22
70.58% Lines 24/34

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 1323x 3x 3x 3x   3x     3x       3x 7x               1x     1x   1x                     4x     4x   4x                   1x   1x                   1x     1x                   1x   1x                                                                         1x     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 { 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<OrderTemplateData>(),
      map(orderTemplateData =>
        orderTemplateData.map(data => this.getOrderTemplate(this.orderTemplateMapper.fromDataToId(data)))
      ),
      switchMap(obsArray => (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> {
    Iif (!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> {
    Iif (!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> {
    Iif (!orderTemplateId) {
      return throwError(() => new Error('addProductToOrderTemplate() called without orderTemplateId'));
    }
    Iif (!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> {
    Iif (!orderTemplateId) {
      return throwError(() => new Error('removeProductFromOrderTemplate() called without orderTemplateId'));
    }
    Iif (!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)));
  }
}