All files / src/app/core/models/product-variation variation-attribute.mapper.ts

46.15% Statements 6/13
45.45% Branches 5/11
42.85% Functions 3/7
50% Lines 6/12

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  27x   27x                 27x 49x     1x                       2x                                                    
/* eslint-disable ish-custom-rules/project-structure */
import { Injectable } from '@angular/core';
 
import { ImageMapper } from 'ish-core/models/image/image.mapper';
 
import { VariationAttributeData, VariationAttributeMetaData } from './variation-attribute.interface';
import { VariationAttribute, VariationAttributeType } from './variation-attribute.model';
 
/**
 * Maps variation attributes data of HTTP requests to client side model instance.
 */
@Injectable({ providedIn: 'root' })
export class VariationAttributeMapper {
  constructor(private imageMapper: ImageMapper) {}
 
  fromData(data: VariationAttributeData[]): VariationAttribute[] {
    return (
      data?.map(varAttr => ({
        variationAttributeId: varAttr.variationAttributeId,
        name: varAttr.name,
        value: varAttr.value.value,
        attributeType: varAttr.attributeType,
        metaData: this.mapMetaData(varAttr.attributeType, varAttr.metadata),
      })) ?? []
    );
  }
 
  fromMasterData(variationAttributes: VariationAttributeData[]): VariationAttribute[] {
    return variationAttributes
      ?.map(varAttr =>
        varAttr.values.map(value => ({
          variationAttributeId: varAttr.variationAttributeId,
          name: varAttr.name,
          value: value.value.value,
          attributeType: varAttr.attributeType,
          metaData: this.mapMetaData(varAttr.attributeType, value.metadata),
        }))
      )
      .flat();
  }
 
  private mapMetaData(attributeType: VariationAttributeType, metaData: VariationAttributeMetaData): string {
    switch (attributeType) {
      case 'colorCode':
      case 'defaultAndColorCode':
        return metaData?.colorCode;
      case 'swatchImage':
      case 'defaultAndSwatchImage':
        return this.imageMapper.fromEffectiveUrl(metaData?.imagePath);
      default:
        return;
    }
  }
}