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

86.66% Statements 13/15
53.84% Branches 7/13
88.88% Functions 8/9
92.3% Lines 12/13

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 7719x           19x           19x 2x     2x                 2x 2x               2x   2x                         1x                                     2x     2x          
import { Injectable } from '@angular/core';
 
import { Facet } from 'ish-core/models/facet/facet.model';
import { Filter } from 'ish-core/models/filter/filter.model';
import { SortableAttributesType } from 'ish-core/models/product-listing/product-listing.model';
import { SearchResponse } from 'ish-core/models/search/search.model';
import { SparqueProductMapper } from 'ish-core/models/sparque-product/sparque-product.mapper';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
import { SparqueFixedFacetGroup, SparqueSearch, SparqueSortingOption } from './sparque-search.interface';
 
@Injectable({ providedIn: 'root' })
export class SparqueSearchMapper {
  constructor(private sparqueProductMapper: SparqueProductMapper) {}
 
  fromData(search: SparqueSearch, searchParameter: URLFormParams): SearchResponse {
    return {
      products: this.sparqueProductMapper.fromListData(search.products),
      total: search.total,
      sortableAttributes: this.mapSortableAttributes(search.sortings),
      filter: this.mapFilter(search.facets, searchParameter),
    };
  }
 
  private mapSortableAttributes(sortingOptions: SparqueSortingOption[]): SortableAttributesType[] {
    return sortingOptions
      ? sortingOptions.map(sorting => ({
          name: sorting.identifier,
          displayName: sorting.title,
        }))
      : undefined;
  }
 
  private mapFilter(filter: SparqueFixedFacetGroup[], searchParameter: URLFormParams): Filter[] {
    return filter
      ? filter.map(facetGroup => {
          const facets: Facet[] = facetGroup.options.map(facet => ({
            name: facet.id ? facet.id : facet.value,
            displayName: facet.value,
            count: facet.score,
            selected: searchParameter[facetGroup.id] ? searchParameter[facetGroup.id]?.includes(facet.id) : false,
            level: 0,
            searchParameter: calculateSearchParams(
              searchParameter,
              searchParameter[facetGroup.id]?.includes(facet.id),
              facetGroup.id,
              facet.id
            ),
          }));
          return {
            name: facetGroup.title,
            displayType: 'text_clear',
            id: facetGroup.id,
            facets,
            selectionType: 'single',
            limitCount: 5,
          };
        })
      : undefined;
  }
}
 
function calculateSearchParams(
  currentSearchParams: URLFormParams,
  isSelected: boolean,
  facetGroupId: string,
  facetId: string
): URLFormParams {
  Iif (isSelected) {
    return Object.fromEntries(Object.entries(currentSearchParams).filter(([, value]) => !value.includes(facetId)));
  }
  return {
    ...currentSearchParams,
    [facetGroupId]: [facetId],
  };
}