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

91.66% Statements 22/24
81.25% Branches 13/16
100% Functions 7/7
90% Lines 18/20

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 854x   4x               4x   2x 2x 2x               4x 3x                 1x       9x   8x 1x                           7x   8x                   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 { Link } from 'ish-core/models/link/link.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;
    }
  }
 
  fromListData(wishlistsData: Link[]): Wishlist[] {
    if (wishlistsData) {
      return wishlistsData.map(wishlistData => ({
        id: wishlistData.itemId,
        title: wishlistData.title,
        itemsCount: AttributeHelper.getAttributeValueByAttributeName<number>(wishlistData.attributes, 'itemsCount'),
        preferred: AttributeHelper.getAttributeValueByAttributeName<boolean>(wishlistData.attributes, 'preferred'),
        public: AttributeHelper.getAttributeValueByAttributeName<boolean>(wishlistData.attributes, 'public'),
        shared: AttributeHelper.getAttributeValueByAttributeName<boolean>(wishlistData.attributes, 'shared'),
      }));
    }
    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,
        shared: wishlistData.shared,
        items,
      };
    } else {
      throw new Error(`wishlistData is required`);
    }
  }
 
  fromDataToId(wishlistData: WishlistData): string {
    return wishlistData ? WishlistMapper.parseIdFromURI(wishlistData.uri) : undefined;
  }
 
  fromUpdate(wishlist: Wishlist, id: string): Wishlist {
    Eif (wishlist && id) {
      return {
        id,
        title: wishlist.title,
        preferred: wishlist.preferred,
        public: wishlist.public,
      };
    }
  }
}