All files / src/app/extensions/wishlists/models/wishlist wishlist.mapper.ts

89.47% Statements 17/19
84.61% Branches 11/13
100% Functions 5/5
88.23% Lines 15/17

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 724x   4x             4x   2x 2x 2x               7x   6x 1x                           5x   6x                 1x         2x 2x                         2x      
import { Injectable } from '@angular/core';
 
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Attribute } from 'ish-core/models/attribute/attribute.model';
 
import { WishlistData } from './wishlist.interface';
import { Wishlist, WishlistItem } from './wishlist.model';
 
@Injectable({ providedIn: 'root' })
export class WishlistMapper {
  private static parseIdFromURI(uri: string): string {
    const match = /wishlists[^\/]*\/([^\?]*)/.exec(uri);
    if (match) {
      return match[1];
    } else E{
      console.warn(`could not find id in uri '${uri}'`);
      return;
    }
  }
 
  fromData(wishlistData: WishlistData, wishlistId: string): Wishlist {
    if (wishlistData) {
      let items: WishlistItem[];
      if (wishlistData.items?.length) {
        items = wishlistData.items.map(item => ({
          sku: AttributeHelper.getAttributeValueByAttributeName(item.attributes, 'sku'),
          id: AttributeHelper.getAttributeValueByAttributeName(item.attributes, 'id'),
          creationDate: Number(AttributeHelper.getAttributeValueByAttributeName(item.attributes, 'creationDate')),
          desiredQuantity: {
            value: AttributeHelper.getAttributeValueByAttributeName<Attribute<number>>(
              item.attributes,
              'desiredQuantity'
            ).value,
            // TBD: is the unit necessary?
            // unit: item.desiredQuantity.unit,
          },
        }));
      } else {
        items = [];
      }
      return {
        id: wishlistId,
        title: wishlistData.title,
        itemsCount: wishlistData.itemsCount || 0,
        preferred: wishlistData.preferred,
        public: wishlistData.public,
        items,
      };
    } else {
      throw new Error(`wishlistData is required`);
    }
  }
 
  fromUpdate(wishlist: Wishlist, id: string): Wishlist {
    if (wishlist && id) {
      return {
        id,
        title: wishlist.title,
        preferred: wishlist.preferred,
        public: wishlist.public,
      };
    }
  }
 
  /**
   * extract ID from URI
   */
  fromDataToId(wishlistData: WishlistData): string {
    return wishlistData ? WishlistMapper.parseIdFromURI(wishlistData.uri) : undefined;
  }
}