All files / src/app/extensions/order-templates/models/order-template order-template.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 704x   4x             4x   1x 1x 1x             6x   5x 1x                           4x     5x               1x         2x 2x                       1x      
import { Injectable } from '@angular/core';
 
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { Attribute } from 'ish-core/models/attribute/attribute.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`);
    }
  }
 
  fromUpdate(orderTemplate: OrderTemplate, id: string): OrderTemplate {
    if (orderTemplate && id) {
      return {
        id,
        title: orderTemplate.title,
        creationDate: orderTemplate.creationDate,
      };
    }
  }
 
  /**
   * extract ID from URI
   */
  fromDataToId(orderTemplateData: OrderTemplateData): string {
    return orderTemplateData ? OrderTemplateMapper.parseIdFromURI(orderTemplateData.uri) : undefined;
  }
}