All files / web/bundles/pimui/js/manager attribute-manager.js

100% Statements 32/32
90% Branches 18/20
100% Functions 10/10
100% Lines 32/32

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135    470x                   470x                   6036x   6036x 577x 5459x 5075x 5075x     384x     6036x                           7440x 7440x   7440x                         81x 81x   81x                                     81x 81x 81x             81x 81x 81x     81x 7x         81x                       7x 7x 14x   14x 14x     14x     7x          
'use strict';
 
define(
    [
        'jquery',
        'underscore',
        'pim/fetcher-registry'
    ], function (
        $,
        _,
        FetcherRegistry
    ) {
        return {
            /**
             * Check if an attribute is optional
             *
             * @param {Object} attribute
             * @param {Object} product
             *
             * @return {Promise}
             */
            isOptional: function (attribute, product) {
                var promise = new $.Deferred();
 
                if ('pim_catalog_identifier' === attribute.type) {
                    promise.resolve(false);
                } else if (undefined !== product.family && null !== product.family) {
                    promise = FetcherRegistry.getFetcher('family').fetch(product.family).then(function (family) {
                        return !_.contains(_.pluck(family.attributes, 'code'), attribute.code);
                    });
                } else {
                    promise.resolve(true);
                }
 
                return promise;
            },
 
            /**
             * Get the value in the given collection for the given locale and scope
             *
             * @param {Array}  values
             * @param {Object} attribute
             * @param {string} locale
             * @param {string} scope
             *
             * @return {Object}
             */
            getValue: function (values, attribute, locale, scope) {
                locale = attribute.localizable ? locale : null;
                scope  = attribute.scopable ? scope : null;
 
                return _.findWhere(values, { scope: scope, locale: locale });
            },
 
            /**
             * Generate a single value for the given attribute, scope and locale
             *
             * @param {Object} attribute
             * @param {string} locale
             * @param {string} scope
             *
             * @return {Object}
             */
            generateValue: function (attribute, locale, scope) {
                locale = attribute.localizable ? locale : null;
                scope  = attribute.scopable ? scope : null;
 
                return {
                    'locale': locale,
                    'scope':  scope,
                    'data':   attribute.empty_value
                };
            },
 
            /**
             * Generate all missing values for an attribute
             *
             * @param {Array}  values
             * @param {Object} attribute
             * @param {Array}  locales
             * @param {Array}  channels
             * @param {Array}  currencies
             *
             * @return {Array}
             */
            generateMissingValues: function (values, attribute, locales, channels, currencies) {
                _.each(locales, function (locale) {
                    _.each(channels, function (channel) {
                        var newValue = this.getValue(
                            values,
                            attribute,
                            locale.code,
                            channel.code
                        );
 
                        Eif (!newValue) {
                            newValue = this.generateValue(attribute, locale.code, channel.code);
                            values.push(newValue);
                        }
 
                        if ('pim_catalog_price_collection' === attribute.type) {
                            newValue.data = this.generateMissingPrices(newValue.data, currencies);
                        }
                    }.bind(this));
                }.bind(this));
 
                return values;
            },
 
            /**
             * Generate missing prices in the given collection for the given currencies
             *
             * @param {Array} prices
             * @param {Array} currencies
             *
             * @return {Array}
             */
            generateMissingPrices: function (prices, currencies) {
                var generatedPrices = [];
                _.each(currencies, function (currency) {
                    var price = _.findWhere(prices, { currency: currency.code });
 
                    Eif (!price) {
                        price = { amount: null, currency: currency.code };
                    }
 
                    generatedPrices.push(price);
                });
 
                return _.sortBy(generatedPrices, 'currency');
            }
        };
    }
);