All files / src/app/extensions/wishlists/services/wishlist wishlist.service.ts

78.94% Statements 45/57
52.38% Branches 11/21
83.33% Functions 30/36
87.5% Lines 42/48

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 2093x 3x 3x   3x 3x       3x       3x 11x               2x     2x   2x 2x                         4x     4x     4x   4x                       1x     1x   1x                       1x     1x     1x                       1x     1x   1x                           1x     1x     1x     1x             1x                         1x     1x     1x     1x           1x                         1x     1x   1x                     1x   1x                         1x   1x      
import { Injectable } from '@angular/core';
import { Observable, forkJoin, of, throwError } from 'rxjs';
import { concatMap, first, map, switchMap } from 'rxjs/operators';
 
import { AppFacade } from 'ish-core/facades/app.facade';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
 
import { WishlistSharing, WishlistSharingResponse } from '../../models/wishlist-sharing/wishlist-sharing.model';
import { WishlistData } from '../../models/wishlist/wishlist.interface';
import { WishlistMapper } from '../../models/wishlist/wishlist.mapper';
import { Wishlist, WishlistHeader } from '../../models/wishlist/wishlist.model';
 
@Injectable({ providedIn: 'root' })
export class WishlistService {
  constructor(private apiService: ApiService, private wishlistMapper: WishlistMapper, private appFacade: AppFacade) {}
 
  /**
   * Gets a list of wishlists for the current user.
   *
   * @returns           The customer's wishlists.
   */
  getWishlists(): Observable<Wishlist[]> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService.get(`${restResource}/-/wishlists`).pipe(
          unpackEnvelope<WishlistData>(),
          map(wishlistData => wishlistData.map(data => this.getWishlist(this.wishlistMapper.fromDataToId(data)))),
          switchMap(obsArray => (obsArray.length ? forkJoin(obsArray) : of([])))
        )
      )
    );
  }
 
  /**
   * Gets a wishlist of the given id for the current user.
   *
   * @param wishlistId  The wishlist id.
   * @returns           The wishlist.
   */
  private getWishlist(wishlistId: string): Observable<Wishlist> {
    Iif (!wishlistId) {
      return throwError(() => new Error('getWishlist() called without wishlistId'));
    }
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .get<WishlistData>(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlistId)}`)
          .pipe(map(wishlistData => this.wishlistMapper.fromData(wishlistData, wishlistId)))
      )
    );
  }
 
  /**
   * Creates a wishlists for the current user.
   *
   * @param wishlistDetails   The wishlist data.
   * @returns                 The created wishlist.
   */
  createWishlist(wishlistData: WishlistHeader): Observable<Wishlist> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .post(`${restResource}/-/wishlists`, wishlistData)
          .pipe(map((response: WishlistData) => this.wishlistMapper.fromData(wishlistData, response.title)))
      )
    );
  }
 
  /**
   * Deletes a wishlist of the given id.
   *
   * @param wishlistId   The wishlist id.
   * @returns            The wishlist.
   */
  deleteWishlist(wishlistId: string): Observable<void> {
    Iif (!wishlistId) {
      return throwError(() => new Error('deleteWishlist() called without wishlistId'));
    }
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService.delete<void>(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlistId)}`)
      )
    );
  }
 
  /**
   * Updates a wishlist of the given id.
   *
   * @param wishlist   The wishlist to be updated.
   * @returns          The updated wishlist.
   */
  updateWishlist(wishlist: Wishlist): Observable<Wishlist> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .put(`${restResource}/-/wishlists/${this.apiService.encodeResourceId(wishlist.id)}`, wishlist)
          .pipe(map((response: Wishlist) => this.wishlistMapper.fromUpdate(response, wishlist.id)))
      )
    );
  }
 
  /**
   * Adds a product to the wishlist with the given id and reloads the wishlist.
   *
   * @param wishlist Id   The wishlist id.
   * @param sku           The product sku.
   * @param quantity      The product quantity (default = 1).
   * @returns             The changed wishlist.
   */
  addProductToWishlist(wishlistId: string, sku: string, quantity = 1): Observable<Wishlist> {
    Iif (!wishlistId) {
      return throwError(() => new Error('addProductToWishlist() called without wishlistId'));
    }
    Iif (!sku) {
      return throwError(() => new Error('addProductToWishlist() called without sku'));
    }
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .post(
            `${restResource}/-/wishlists/${this.apiService.encodeResourceId(
              wishlistId
            )}/products/${this.apiService.encodeResourceId(sku)}`,
            { quantity }
          )
          .pipe(concatMap(() => this.getWishlist(wishlistId)))
      )
    );
  }
 
  /**
   * Removes a product from the wishlist with the given id. Returns an error observable if parameters are falsy.
   *
   * @param wishlist Id   The wishlist id.
   * @param sku           The product sku.
   * @returns             The changed wishlist.
   */
  removeProductFromWishlist(wishlistId: string, sku: string): Observable<Wishlist> {
    Iif (!wishlistId) {
      return throwError(() => new Error('removeProductFromWishlist() called without wishlistId'));
    }
    Iif (!sku) {
      return throwError(() => new Error('removeProductFromWishlist() called without sku'));
    }
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .delete(
            `${restResource}/-/wishlists/${this.apiService.encodeResourceId(
              wishlistId
            )}/products/${this.apiService.encodeResourceId(sku)}`
          )
          .pipe(concatMap(() => this.getWishlist(wishlistId)))
      )
    );
  }
 
  /**
   * Shares the wishlist with other users (a comma separated list of email addresses)
   *
   * @param wishlistId       The wishlist id.
   * @param wishlistSharing  The wishlist sharing data.
   * @returns                The wishlist sharing response data with the secureCode which should be used to access the shared wishlist.
   */
  shareWishlist(wishlistId: string, wishlistSharing: WishlistSharing): Observable<WishlistSharingResponse> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource =>
        this.apiService
          .post(`${restResource}/-/wishlists/${wishlistId}/share`, wishlistSharing)
          .pipe(map((response: WishlistSharingResponse) => response))
      )
    );
  }
 
  /**
   * Unshare the wishlist and invalidate all generated secureCodes.
   *
   * @param wishlistId  The wishlist id.
   */
  unshareWishlist(wishlistId: string): Observable<void> {
    return this.appFacade.customerRestResource$.pipe(
      first(),
      concatMap(restResource => this.apiService.delete<void>(`${restResource}/-/wishlists/${wishlistId}/share`))
    );
  }
 
  /**
   * Gets the shared wishlist using the secureCode to check access.
   *
   * @param wishlistId  The wishlist id.
   * @param owner       The wishlist owner.
   * @param secureCode  The secureCode.
   * @returns           The wishlist.
   */
  getSharedWishlist(wishlistId: string, owner: string, secureCode: string): Observable<Wishlist> {
    return this.apiService
      .get<WishlistData>(`wishlists/${wishlistId};owner=${owner};secureCode=${secureCode}`)
      .pipe(map(wishlist => this.wishlistMapper.fromData(wishlist, wishlistId)));
  }
}