All files / src/app/core/models/sparque-category sparque-category.mapper.ts

100% Statements 40/40
83.33% Branches 10/12
100% Functions 5/5
100% Lines 40/40

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10320x   20x 20x   20x 20x         20x 2x     2x 2x   2x 1x 1x 1x 1x       2x       1x 1x 1x   1x         1x 1x 1x 1x 1x     1x                               1x               2x 2x 2x 2x   2x 2x   2x   1x 1x 1x 1x     2x                     2x   2x      
import { Injectable } from '@angular/core';
 
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { CategoryTreeHelper } from 'ish-core/models/category-tree/category-tree.helper';
import { CategoryTree } from 'ish-core/models/category-tree/category-tree.model';
import { Category, CategoryHelper } from 'ish-core/models/category/category.model';
import { SparqueImageMapper } from 'ish-core/models/sparque-image/sparque-image.mapper';
 
import { SparqueCategory, SparqueParentCategory } from './sparque-category.interface';
 
@Injectable({ providedIn: 'root' })
export class SparqueCategoryMapper {
  constructor(private sparqueImageMapper: SparqueImageMapper) {}
 
  fromSuggestionsData(categoriesData: SparqueCategory[]): { categoryIds: string[]; categoryTree: CategoryTree } {
    const categoryIds: string[] = [];
    let categoryTree = CategoryTreeHelper.empty();
 
    if (categoriesData?.length) {
      categoriesData.forEach(category => {
        const result = this.fromData(category);
        categoryTree = CategoryTreeHelper.merge(categoryTree, result.categoryTree);
        categoryIds.push(result.category.uniqueId);
      });
    }
 
    return { categoryIds, categoryTree };
  }
 
  private fromData(data: SparqueCategory): { category: Category; categoryTree: CategoryTree } {
    let parentCategory: Category = undefined;
    let categoryTree = CategoryTreeHelper.empty();
    let uniqueId = data.categoryID;
 
    const parentsData = AttributeHelper.getAttributeValueByAttributeName<SparqueParentCategory[]>(
      data?.attributes,
      'hasParent'
    );
 
    if (parentsData) {
      const result = this.mapHierarchyFromParentsData(parentsData?.[0]);
      parentCategory = result.parentCategory;
      categoryTree = result.categoryTree;
      uniqueId = parentCategory.uniqueId + CategoryHelper.uniqueIdSeparator + data.categoryID;
    }
 
    const category: Category = {
      uniqueId,
      name: data.categoryName,
      categoryRef: undefined, // categoryRef not available in Sparque data
      categoryPath: parentCategory ? [...parentCategory.categoryPath, uniqueId] : [uniqueId],
      description: AttributeHelper.getAttributeValueByAttributeName<string>(data.attributes, 'description'),
      images: [
        this.sparqueImageMapper.fromImageUrl(
          AttributeHelper.getAttributeValueByAttributeName(data.attributes, 'image')
        ),
      ],
      hasOnlineProducts: !!data.totalCount,
      productCount: data.totalCount,
      completenessLevel: 0,
    };
 
    return { category, categoryTree: CategoryTreeHelper.add(categoryTree, category) };
  }
 
  // recursively map the hierarchy of categories from the parent data
  private mapHierarchyFromParentsData(data: SparqueParentCategory): {
    parentCategory: Category;
    categoryTree: CategoryTree;
  } {
    let category: Category = undefined;
    let parentCategory: Category = undefined;
    let categoryTree = CategoryTreeHelper.empty();
    let uniqueId = data.identifier;
 
    if (data) {
      const parentsData = data.hasParent?.[0];
 
      if (parentsData) {
        // recursively map the parent categories
        const result = this.mapHierarchyFromParentsData(parentsData);
        parentCategory = result.parentCategory;
        categoryTree = result.categoryTree;
        uniqueId = parentCategory.uniqueId + CategoryHelper.uniqueIdSeparator + data.identifier;
      }
 
      category = {
        uniqueId,
        name: data.name['en-US'], // TODO: implement locale specific value access
        categoryRef: undefined, // categoryRef not available in Sparque data
        categoryPath: parentCategory ? [...parentCategory.categoryPath, uniqueId] : [uniqueId],
        description: undefined, // TODO: do we need a description for parent categories?
        images: [this.sparqueImageMapper.fromImageUrl(data.image)],
        hasOnlineProducts: true,
        completenessLevel: 0,
      };
 
      categoryTree = CategoryTreeHelper.add(categoryTree, category);
    }
    return { parentCategory: category, categoryTree };
  }
}