All files / web/bundles/pimanalytics/js patch-fetcher.js

100% Statements 18/18
100% Branches 6/6
100% Functions 4/4
100% Lines 18/18

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 441110x         1110x         1132x 1132x 1132x 22x 22x   22x     1110x 1110x 1110x 1110x 1110x   1110x           1107x 1107x 1107x   1107x                
define(
    ['jquery', 'underscore', 'pim/data-collector'],
    function ($, _, DataCollector) {
        'use strict';
 
        return {
            /**
             * @return {Promise}
             */
            fetch: function (updateServerUrl) {
                var storageEnabled = typeof Storage !== 'undefined' && sessionStorage;
                var lastPatchKey = 'last-patch-available';
                if (storageEnabled && null !== sessionStorage.getItem(lastPatchKey)) {
                    var deferred = $.Deferred();
                    deferred.resolve(sessionStorage.getItem(lastPatchKey));
 
                    return deferred.promise();
 
                } else {
                    return DataCollector.collect('pim_analytics_data_collect').then(function (collectedData) {
                        var version = collectedData.pim_version;
                        var minorVersion = _.first(version.match(/^\d+.\d+/g));
                        var page = collectedData.pim_edition + '-' + minorVersion + '.json';
                        var lastPatchUrl = updateServerUrl + '/' + page;
 
                        return $.ajax({
                            dataType: 'json',
                            url: lastPatchUrl,
                            data: collectedData,
                            timeout: 10000
                        }).then(function (patchData) {
                            var patch = patchData.last_patch.name;
                            var cleanedPatch = patch.replace(/^v/g, '');
                            sessionStorage.setItem(lastPatchKey, cleanedPatch);
 
                            return cleanedPatch;
                        });
                    });
                }
            }
        };
    }
);