All files / src/app/extensions/order-templates/models/order-template order-template.mapper.ts

91.66% Statements 22/24
87.5% Branches 14/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 85 86 874x   4x               4x   3x 3x 3x               9x   8x 1x                           7x     8x               1x         4x 1x   3x                             4x       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 { OrderTemplateData } from './order-template.interface';
import { OrderTemplate, OrderTemplateItem } from './order-template.model';
 
@Injectable({ providedIn: 'root' })
export class OrderTemplateMapper {
  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(orderTemplateData: OrderTemplateData, orderTemplateId: string): OrderTemplate {
    if (orderTemplateData) {
      let items: OrderTemplateItem[];
      if (orderTemplateData.items?.length) {
        items = orderTemplateData.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: orderTemplateId,
        title: orderTemplateData.title,
        itemsCount: orderTemplateData.itemsCount || 0,
        creationDate: orderTemplateData.creationDate,
        items,
      };
    } else {
      throw new Error(`orderTemplateData is required`);
    }
  }
 
  fromListData(orderTemplateLinks: Link[]): OrderTemplate[] {
    if (!orderTemplateLinks) {
      return [];
    }
    return orderTemplateLinks.map(orderTemplateLink => ({
      id: orderTemplateLink.itemId,
      title: orderTemplateLink.title,
      itemsCount: AttributeHelper.getAttributeValueByAttributeName<number>(orderTemplateLink.attributes, 'itemsCount'),
      creationDate: AttributeHelper.getAttributeValueByAttributeName<number>(
        orderTemplateLink.attributes,
        'creationDate'
      ),
    }));
  }
 
  /**
   * extract ID from URI
   */
  fromDataToId(orderTemplateData: OrderTemplateData): string {
    return orderTemplateData ? OrderTemplateMapper.parseIdFromURI(orderTemplateData.uri) : undefined;
  }
 
  fromUpdate(orderTemplate: OrderTemplate, id: string): OrderTemplate {
    Eif (orderTemplate && id) {
      return {
        id,
        title: orderTemplate.title,
        creationDate: orderTemplate.creationDate,
      };
    }
  }
}