Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/graphql / dist / endpoints / catalog / stream / transform.js
Size: Mime:
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const exotic_1 = require("exotic");
// import { autofixSafe as autofix } from 'exotic-core'
const identifier_1 = require("@skava/modules/___dist/identifier");
const composition_1 = require("@skava/modules/___dist/composition");
// import {reshape} from 'deps'
// import {schema, remap} from 'server/endpoints/catalog'
// const camelKeys = ['categoryIds', 'additionalInfo', 'productFilter', 'selectedFilters']
// const transform = data => {
//   return reshape
//     .setSchema(schema)
//     .setRemap(remap)
//     .remapKeys(camelKeys)
//     .transformn(data)
// }
// @curry2
const clientSideSorting = {
    isProductSorted: false,
    sortedValue: '',
};
const toAdditional = response => {
    const { array } = composition_1.getTyped(response);
    // random stupid Type
    const info = array('properties.iteminfo.additionalinfo');
    // specifications
    const additionalproperties = array('properties.iteminfo.additionalproperties');
    const stateAdditionalProperties = array('properties.state.additionalproperties');
    return [...info, ...additionalproperties, ...stateAdditionalProperties];
};
const fromValueToAlwaysUniq = value => {
    value.identifier = value.identifier + identifier_1.uuid();
    return value;
};
const fromLabel = name => {
    let label = name;
    if (name === 'Friendly Color') {
        label = 'Color';
    }
    return label;
};
const appendCurrencyCode = value => {
    value.name = '$' + value.name;
    return value;
};
const fromFacetToAlwaysUniq = facet => {
    let values = '';
    // should be value
    const oldvalues = facet.values.map(fromValueToAlwaysUniq);
    // const identifier = facet.identifier || name + uuid()
    if (facet.name === 'salePrice') {
        values = oldvalues.map(appendCurrencyCode);
    }
    else {
        values = oldvalues;
    }
    if (facet.name === 'Color') {
        return undefined;
    }
    const name = fromLabel(facet.name);
    // @todo careful to ensure selected identifier isn't the same as the name
    // if (name === 'Friendly Color') {
    //   name = 'Color'
    // }
    return { name, values };
};
const isValidProduct = (product = {}) => {
    const { image, name, identifier } = product;
    const isValid = exotic_1.isSafe(image && name && identifier);
    return isValid;
};
const getPriceValue = properties => {
    const { array } = composition_1.getTyped(properties);
    const prices = array('properties.buyinfo.pricing.prices');
    const sortPrice = prices.find(item => {
        if (item.type.toLowerCase() === 'sale') {
            return item;
        }
        return exotic_1.toNumber(sortPrice.value);
    });
};
const sortSkus = skus => {
    if (clientSideSorting.isProductSorted) {
        const isAscendingSort = clientSideSorting.sortedValue.includes('Price|asc');
        skus.sort(function (item1, item2) {
            const item1Price = getPriceValue(item1);
            const item2Price = getPriceValue(item2);
            if (isAscendingSort) {
                return parseFloat(item1Price) - item2Price;
            }
            else {
                return parseFloat(item2Price) - item1Price;
            }
        });
        return skus;
    }
    else {
        return skus;
    }
};
const toProductChildren = (children = {}) => {
    const { array } = composition_1.getTyped(children);
    const { skus } = children, remainingData = __rest(children, ["skus"]);
    const validSkus = array('skus').filter(isValidProduct);
    const sortedSkus = sortSkus(validSkus);
    return Object.assign({ skus: sortedSkus }, remainingData);
};
const toProduct = product => {
    const validProduct = isValidProduct(product) ? product : {};
    const { array } = composition_1.getTyped(validProduct);
    const { children } = validProduct, remainingData = __rest(validProduct, ["children"]);
    return Object.assign({}, remainingData, { children: toProductChildren(children), specifications: array('properties.iteminfo.specifications'), additional: toAdditional(product) });
};
exports.toProduct = toProduct;
exports.transformProduct = toProduct;
const transformCatalogResponse = response => {
    const { array, string, number } = composition_1.getTyped(response);
    // TODO - remove this once price  is rendered in call
    const facetValue = array('facets')
        .map(fromFacetToAlwaysUniq)
        .filter(exotic_1.isSafe);
    // facetValue.push({
    //   name: 'Price',
    //   values: [
    //     {
    //       identifier: '0-50',
    //       name: '0-50',
    //       count: '15',
    //     },
    //     {
    //       identifier: '50-100',
    //       name: '50-100',
    //       count: '15',
    //     },
    //     {
    //       identifier: '100-150',
    //       name: '100-150',
    //       count: '4',
    //     },
    //     {
    //       identifier: '200-350',
    //       name: '200-250',
    //       count: '15',
    //     },
    //   ],
    // })
    const catalogResponse = {
        identifier: string('identifier'),
        // navtype: string('navtype'),
        image: string('image'),
        name: string('name'),
        link: string('link'),
        additional: toAdditional(response),
        // useless
        // additionalInfo: getArray('properties.iteminfo.additionalinfo'),
        categoryIds: array('properties.iteminfo.categoryids').reverse(),
        /**
         * @todo - could take refine out of the response here
         */
        productFilter: {
            facets: facetValue,
            selectedFilters: array('properties.state.selectedFacets'),
        },
        productSort: {
            options: array('properties.state.sorting.0.options'),
            sortedBy: string('properties.state.sorting.0.selectedname') || 'Relevance',
        },
        // .map(remapProduct), @see /products
        products: array('children.products').map(toProduct),
        productCount: number('properties.state.productcount'),
        searchterm: string('properties.state.searchterm'),
    };
    return catalogResponse;
};
// @todo hack till API fixes sort SKREACT-4968
const checkIsProductSorted = dynamicParams => {
    const { sort } = dynamicParams;
    const sortValue = sort;
    if (exotic_1.isSafe(sort) && !exotic_1.isEmpty(sort)) {
        clientSideSorting.isProductSorted = true;
        clientSideSorting.sortedValue = sortValue;
    }
};
const transformCatalog = (response, dynamicParams) => {
    checkIsProductSorted(dynamicParams);
    const data = transformCatalogResponse(response);
    const isSortedItem = false;
    // return autofix(data)
    return data;
};
exports.transformCatalog = transformCatalog;
const toTabProductItem = product => {
    const url = `/product/${product.identifier}`;
    return Object.assign({}, product, { url });
};
/**
 * basically reformatted plp
 * with {meta, values} as {title url viewall products}
 */
const transformCatalogTab = (response, id, tabName, attribute) => {
    // THIS IS DONE ON GRAPHQL (FACEPALM)
    // const transformed = transformCatalogResponse(response)
    // console.log('transformCatalogTab', { response, transformed })
    const { children: { products }, name, link, image, } = response;
    const url = '/category/' + id;
    const meta = {
        title: tabName || name || 'unknown',
        url,
        image,
        // @todo this would be parent categories?
        viewAll: url,
        qa: attribute,
    };
    const oneThing = Object.assign({}, meta, { meta, values: products.map(toTabProductItem) });
    return oneThing;
};
exports.transformCatalogTab = transformCatalogTab;
exports.default = transformCatalog;
//# sourceMappingURL=transform.js.map