From db29954d881f9fcbce21361e5772016cda5fc177 Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Mon, 20 Aug 2018 14:34:11 +0200 Subject: [PATCH] feat(productImport): remove transpiled files (#116) --- .gitignore | 1 + dist/common-utils.js | 92 --- dist/ensure-default-attributes.js | 82 --- dist/enum-validator.js | 243 -------- dist/index.js | 10 - dist/price-import.js | 297 --------- dist/product-discount-import.js | 143 ----- dist/product-export.js | 34 -- dist/product-import.js | 962 ------------------------------ dist/unknown-attributes-filter.js | 89 --- 10 files changed, 1 insertion(+), 1952 deletions(-) delete mode 100644 dist/common-utils.js delete mode 100644 dist/ensure-default-attributes.js delete mode 100644 dist/enum-validator.js delete mode 100644 dist/index.js delete mode 100644 dist/price-import.js delete mode 100644 dist/product-discount-import.js delete mode 100644 dist/product-export.js delete mode 100644 dist/product-import.js delete mode 100644 dist/unknown-attributes-filter.js diff --git a/.gitignore b/.gitignore index 8b61e2f..5d555bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store node_modules *.log +dist dist-test tmp config.js diff --git a/dist/common-utils.js b/dist/common-utils.js deleted file mode 100644 index b5312f3..0000000 --- a/dist/common-utils.js +++ /dev/null @@ -1,92 +0,0 @@ -var CommonUtils, _, debug, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - -debug = require('debug')('sphere-product-import-common-utils'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -CommonUtils = (function() { - function CommonUtils(logger) { - this.logger = logger; - this.uniqueObjectFilter = bind(this.uniqueObjectFilter, this); - debug("Enum Validator initialized."); - } - - CommonUtils.prototype.uniqueObjectFilter = function(objCollection) { - var uniques; - uniques = []; - _.each(objCollection, (function(_this) { - return function(obj) { - if (!_this.isObjectPresentInArray(uniques, obj)) { - return uniques.push(obj); - } - }; - })(this)); - return uniques; - }; - - CommonUtils.prototype.isObjectPresentInArray = function(array, object) { - return _.find(array, function(element) { - return _.isEqual(element, object); - }); - }; - - - /** - * takes an array of sku chunks and returns an array of sku chunks - * where each chunk fits inside the query - */ - - CommonUtils.prototype._separateSkusChunksIntoSmallerChunks = function(skus, queryLimit) { - var availableSkuBytes, chunks, fixBytes, getBytesOfChunk, whereQuery; - whereQuery = "masterVariant(sku in ()) or variants(sku in ())"; - fixBytes = Buffer.byteLength(encodeURIComponent(whereQuery), 'utf-8'); - availableSkuBytes = queryLimit - fixBytes; - getBytesOfChunk = function(chunk) { - var skuString; - skuString = encodeURIComponent("\"" + (chunk.join('","')) + "\"\"" + (chunk.join('","')) + "\""); - return Buffer.byteLength(skuString, 'utf-8'); - }; - chunks = _.reduce(skus, function(chunks, sku) { - var lastChunk; - lastChunk = _.clone(_.last(chunks)); - lastChunk.push(sku); - if (getBytesOfChunk(lastChunk) < availableSkuBytes) { - chunks.pop(); - chunks.push(lastChunk); - } else { - chunks.push([sku]); - } - return chunks; - }, [[]]); - return chunks; - }; - - CommonUtils.prototype.canBePublished = function(product, publishingStrategy) { - if (publishingStrategy === 'always') { - return true; - } else if (publishingStrategy === 'stagedAndPublishedOnly') { - if (product.hasStagedChanges === true && product.published === true) { - return true; - } else { - return false; - } - } else if (publishingStrategy === 'notStagedAndPublishedOnly') { - if (product.hasStagedChanges === false && product.published === true) { - return true; - } else { - return false; - } - } else { - this.logger.warn('unknown publishing strategy ' + publishingStrategy); - return false; - } - }; - - return CommonUtils; - -})(); - -module.exports = CommonUtils; diff --git a/dist/ensure-default-attributes.js b/dist/ensure-default-attributes.js deleted file mode 100644 index 94351b7..0000000 --- a/dist/ensure-default-attributes.js +++ /dev/null @@ -1,82 +0,0 @@ -var EnsureDefaultAttributes, Promise, _, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -EnsureDefaultAttributes = (function() { - function EnsureDefaultAttributes(logger, defaultAttributes1) { - this.logger = logger; - this.defaultAttributes = defaultAttributes1; - this._ensureInVariant = bind(this._ensureInVariant, this); - this.ensureDefaultAttributesInProduct = bind(this.ensureDefaultAttributesInProduct, this); - this.logger.debug('Ensuring default attributes'); - } - - EnsureDefaultAttributes.prototype.ensureDefaultAttributesInProduct = function(product, productFromServer) { - var masterVariant, updatedProduct, updatedVariants; - updatedProduct = _.deepClone(product); - if (productFromServer) { - masterVariant = productFromServer.masterVariant; - } - updatedProduct.masterVariant = this._ensureInVariant(product.masterVariant, masterVariant); - updatedVariants = _.map(product.variants, (function(_this) { - return function(variant) { - var serverVariant; - if (productFromServer) { - serverVariant = productFromServer.variants.filter(function(v) { - return v.sku === variant.sku; - })[0]; - } - return _this._ensureInVariant(variant, serverVariant); - }; - })(this)); - updatedProduct.variants = updatedVariants; - return Promise.resolve(updatedProduct); - }; - - EnsureDefaultAttributes.prototype._ensureInVariant = function(variant, serverVariant) { - var defaultAttribute, defaultAttributes, extendedAttributes, i, len, serverAttributes; - defaultAttributes = _.deepClone(this.defaultAttributes); - if (!variant.attributes) { - return variant; - } - extendedAttributes = _.deepClone(variant.attributes); - if (serverVariant) { - serverAttributes = serverVariant.attributes; - } - for (i = 0, len = defaultAttributes.length; i < len; i++) { - defaultAttribute = defaultAttributes[i]; - if (!this._isAttributeExisting(defaultAttribute, variant.attributes)) { - this._updateAttribute(serverAttributes, defaultAttribute, extendedAttributes); - } - } - variant.attributes = extendedAttributes; - return variant; - }; - - EnsureDefaultAttributes.prototype._updateAttribute = function(serverAttributes, defaultAttribute, extendedAttributes) { - var serverAttribute; - if (serverAttributes) { - serverAttribute = this._isAttributeExisting(defaultAttribute, serverAttributes); - if (serverAttribute) { - defaultAttribute.value = serverAttribute.value; - } - } - return extendedAttributes.push(defaultAttribute); - }; - - EnsureDefaultAttributes.prototype._isAttributeExisting = function(defaultAttribute, attributeList) { - return _.findWhere(attributeList, { - name: "" + defaultAttribute.name - }); - }; - - return EnsureDefaultAttributes; - -})(); - -module.exports = EnsureDefaultAttributes; diff --git a/dist/enum-validator.js b/dist/enum-validator.js deleted file mode 100644 index 3101f98..0000000 --- a/dist/enum-validator.js +++ /dev/null @@ -1,243 +0,0 @@ -var EnumValidator, Promise, SphereClient, _, debug, slugify, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; - -debug = require('debug')('sphere-product-import:enum-validator'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -slugify = require('underscore.string/slugify'); - -SphereClient = require('sphere-node-sdk').SphereClient; - -EnumValidator = (function() { - function EnumValidator(logger) { - this.logger = logger; - this._extractEnumAttributesFromProductType = bind(this._extractEnumAttributesFromProductType, this); - this._fetchEnumAttributeNamesOfProductType = bind(this._fetchEnumAttributeNamesOfProductType, this); - this._fetchEnumAttributesOfProductType = bind(this._fetchEnumAttributesOfProductType, this); - this._fetchEnumAttributesFromVariant = bind(this._fetchEnumAttributesFromVariant, this); - this._fetchEnumAttributesFromProduct = bind(this._fetchEnumAttributesFromProduct, this); - this._generateEnumSetUpdateAction = bind(this._generateEnumSetUpdateAction, this); - this._generateMultipleValueEnumSetUpdateAction = bind(this._generateMultipleValueEnumSetUpdateAction, this); - this._generateEnumSetUpdateActionByValueType = bind(this._generateEnumSetUpdateActionByValueType, this); - this._generateUpdateAction = bind(this._generateUpdateAction, this); - this._isEnumGenerated = bind(this._isEnumGenerated, this); - this._handleNewEnumAttributeUpdate = bind(this._handleNewEnumAttributeUpdate, this); - this._validateEnums = bind(this._validateEnums, this); - this.validateProduct = bind(this.validateProduct, this); - this._resetCache(); - debug("Enum Validator initialized."); - } - - EnumValidator.prototype._resetCache = function() { - return this._cache = { - productTypeEnumMap: {}, - generatedEnums: {} - }; - }; - - EnumValidator.prototype.validateProduct = function(product, resolvedProductType) { - var enumAttributes, update, updateActions; - enumAttributes = this._fetchEnumAttributesFromProduct(product, resolvedProductType); - updateActions = this._validateEnums(enumAttributes, resolvedProductType); - update = { - productTypeId: resolvedProductType.id, - actions: updateActions - }; - return update; - }; - - EnumValidator.prototype._validateEnums = function(enumAttributes, productType) { - var ea, i, len, referenceEnums, updateActions; - updateActions = []; - referenceEnums = this._fetchEnumAttributesOfProductType(productType); - for (i = 0, len = enumAttributes.length; i < len; i++) { - ea = enumAttributes[i]; - if (!this._isEnumGenerated(ea)) { - this._handleNewEnumAttributeUpdate(ea, referenceEnums, updateActions, productType); - } else { - debug("Skipping " + ea.name + " update action generation as already exists."); - } - } - return updateActions; - }; - - EnumValidator.prototype._handleNewEnumAttributeUpdate = function(ea, referenceEnums, updateActions, productType) { - var refEnum; - refEnum = _.findWhere(referenceEnums, { - name: "" + ea.name - }); - if (refEnum && !this._isEnumKeyPresent(ea, refEnum)) { - return updateActions.push(this._generateUpdateAction(ea, refEnum)); - } else { - return debug("enum attribute name: " + ea.name + " not found in Product Type: " + productType.name); - } - }; - - EnumValidator.prototype._isEnumGenerated = function(ea) { - return this._cache.generatedEnums[ea.name + "-" + (slugify(ea.value))]; - }; - - EnumValidator.prototype._generateUpdateAction = function(enumAttribute, refEnum) { - switch (refEnum.type.name) { - case 'enum': - return this._generateEnumUpdateAction(enumAttribute, refEnum); - case 'lenum': - return this._generateLenumUpdateAction(enumAttribute, refEnum); - case 'set': - return this._generateEnumSetUpdateActionByValueType(enumAttribute, refEnum); - default: - throw err("Invalid enum type: " + refEnum.type.name); - } - }; - - EnumValidator.prototype._generateEnumSetUpdateActionByValueType = function(enumAttribute, refEnum) { - if (_.isArray(enumAttribute.value)) { - return this._generateMultipleValueEnumSetUpdateAction(enumAttribute, refEnum); - } else { - return this._generateEnumSetUpdateAction(enumAttribute, refEnum); - } - }; - - EnumValidator.prototype._generateMultipleValueEnumSetUpdateAction = function(enumAttribute, refEnum) { - return _.map(enumAttribute.value, (function(_this) { - return function(attributeValue) { - var ea; - ea = { - name: enumAttribute.name, - value: attributeValue - }; - return _this._generateEnumSetUpdateAction(ea, refEnum); - }; - })(this)); - }; - - EnumValidator.prototype._generateEnumSetUpdateAction = function(enumAttribute, refEnum) { - switch (refEnum.type.elementType.name) { - case 'enum': - return this._generateEnumUpdateAction(enumAttribute, refEnum); - case 'lenum': - return this._generateLenumUpdateAction(enumAttribute, refEnum); - default: - throw err("Invalid set enum type: " + refEnum.type.elementType.name); - } - }; - - EnumValidator.prototype._generateEnumUpdateAction = function(ea, refEnum) { - var updateAction; - updateAction = { - action: 'addPlainEnumValue', - attributeName: refEnum.name, - value: { - key: slugify(ea.value), - label: ea.value - } - }; - return updateAction; - }; - - EnumValidator.prototype._generateLenumUpdateAction = function(ea, refEnum) { - var updateAction; - updateAction = { - action: 'addLocalizedEnumValue', - attributeName: refEnum.name, - value: { - key: slugify(ea.value), - label: { - en: ea.value, - de: ea.value, - fr: ea.value, - it: ea.value, - es: ea.value - } - } - }; - return updateAction; - }; - - EnumValidator.prototype._isEnumKeyPresent = function(enumAttribute, refEnum) { - if (refEnum.type.name === 'set') { - return _.findWhere(refEnum.type.elementType.values, { - key: slugify(enumAttribute.value) - }); - } else { - return _.findWhere(refEnum.type.values, { - key: slugify(enumAttribute.value) - }); - } - }; - - EnumValidator.prototype._fetchEnumAttributesFromProduct = function(product, resolvedProductType) { - var enumAttributes, i, len, ref, variant; - enumAttributes = this._fetchEnumAttributesFromVariant(product.masterVariant, resolvedProductType); - if (product.variants && !_.isEmpty(product.variants)) { - ref = product.variants; - for (i = 0, len = ref.length; i < len; i++) { - variant = ref[i]; - enumAttributes = enumAttributes.concat(this._fetchEnumAttributesFromVariant(variant, resolvedProductType)); - } - } - return enumAttributes; - }; - - EnumValidator.prototype._fetchEnumAttributesFromVariant = function(variant, productType) { - var attribute, enums, i, len, productTypeEnumNames, ref; - enums = []; - productTypeEnumNames = this._fetchEnumAttributeNamesOfProductType(productType); - ref = variant.attributes; - for (i = 0, len = ref.length; i < len; i++) { - attribute = ref[i]; - if (this._isEnumVariantAttribute(attribute, productTypeEnumNames)) { - enums.push(attribute); - } - } - return enums; - }; - - EnumValidator.prototype._isEnumVariantAttribute = function(attribute, productTypeEnums) { - var ref; - return ref = attribute.name, indexOf.call(productTypeEnums, ref) >= 0; - }; - - EnumValidator.prototype._fetchEnumAttributesOfProductType = function(productType) { - return this._extractEnumAttributesFromProductType(productType); - }; - - EnumValidator.prototype._fetchEnumAttributeNamesOfProductType = function(productType) { - var enums, names; - if (this._cache.productTypeEnumMap[productType.id + "_names"]) { - return this._cache.productTypeEnumMap[productType.id + "_names"]; - } else { - enums = this._fetchEnumAttributesOfProductType(productType); - names = _.pluck(enums, 'name'); - this._cache.productTypeEnumMap[productType.id + "_names"] = names; - return names; - } - }; - - EnumValidator.prototype._extractEnumAttributesFromProductType = function(productType) { - return _.filter(productType.attributes, this._enumLenumFilterPredicate).concat(_.filter(productType.attributes, this._enumSetFilterPredicate)).concat(_.filter(productType.attributes, this._lenumSetFilterPredicate)); - }; - - EnumValidator.prototype._enumLenumFilterPredicate = function(attribute) { - return attribute.type.name === 'enum' || attribute.type.name === 'lenum'; - }; - - EnumValidator.prototype._enumSetFilterPredicate = function(attribute) { - return attribute.type.name === 'set' && attribute.type.elementType.name === 'enum'; - }; - - EnumValidator.prototype._lenumSetFilterPredicate = function(attribute) { - return attribute.type.name === 'set' && attribute.type.elementType.name === 'lenum'; - }; - - return EnumValidator; - -})(); - -module.exports = EnumValidator; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 1ac65a1..0000000 --- a/dist/index.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - ProductExport: require('./product-export'), - ProductImport: require('./product-import'), - PriceImport: require('./price-import'), - ProductDiscountImport: require('./product-discount-import'), - EnumValidator: require('./enum-validator'), - UnknownAttributesFilter: require('./unknown-attributes-filter'), - CommonUtils: require('./common-utils'), - EnsureDefaultAttributes: require('./ensure-default-attributes') -}; diff --git a/dist/price-import.js b/dist/price-import.js deleted file mode 100644 index 2d383d6..0000000 --- a/dist/price-import.js +++ /dev/null @@ -1,297 +0,0 @@ -var PriceImport, ProductImport, ProductSync, Promise, Repeater, SphereClient, _, debug, ref, slugify, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, - hasProp = {}.hasOwnProperty; - -debug = require('debug')('sphere-price-import'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -slugify = require('underscore.string/slugify'); - -ref = require('sphere-node-sdk'), SphereClient = ref.SphereClient, ProductSync = ref.ProductSync; - -Repeater = require('sphere-node-utils').Repeater; - -ProductImport = require('./product-import'); - -PriceImport = (function(superClass) { - extend(PriceImport, superClass); - - function PriceImport(logger, options) { - this.logger = logger; - if (options == null) { - options = {}; - } - this._resolvePriceReferences = bind(this._resolvePriceReferences, this); - this._preparePrice = bind(this._preparePrice, this); - this._preparePrices = bind(this._preparePrices, this); - this._handleFulfilledResponse = bind(this._handleFulfilledResponse, this); - this._handleProcessResponse = bind(this._handleProcessResponse, this); - PriceImport.__super__.constructor.call(this, this.logger, options); - this.batchSize = options.batchSize || 30; - this.sync.config([ - { - type: 'prices', - group: 'white' - } - ].concat(['base', 'references', 'attributes', 'images', 'variants', 'metaAttributes'].map(function(type) { - return { - type: type, - group: 'black' - }; - }))); - this.repeater = new Repeater; - this.preventRemoveActions = options.preventRemoveActions || false; - } - - PriceImport.prototype._resetSummary = function() { - return this._summary = { - unknownSKUCount: 0, - duplicatedSKUs: 0, - variantWithoutPriceUpdates: 0, - updated: 0, - failed: 0 - }; - }; - - PriceImport.prototype.summaryReport = function() { - return ("Summary: there were " + this._summary.updated + " price update(s). ") + ("(unknown skus: " + this._summary.unknownSKUCount + ", duplicate skus: " + this._summary.duplicatedSKUs + ", variants without price updates: " + this._summary.variantWithoutPriceUpdates + ")"); - }; - - PriceImport.prototype._processBatches = function(prices) { - var batchedList; - batchedList = _.batchList(prices, this.batchSize); - return Promise.map(batchedList, (function(_this) { - return function(pricesToProcess) { - var predicate, skus; - skus = _.map(pricesToProcess, function(p) { - return p.sku; - }); - predicate = _this._createProductFetchBySkuQueryPredicate(skus); - return _this.client.productProjections.where(predicate).staged(true).all().fetch().then(function(results) { - var queriedEntries; - queriedEntries = results.body.results; - return _this._preparePrices(pricesToProcess).then(function(preparedPrices) { - var wrappedProducts; - wrappedProducts = _this._wrapPricesIntoProducts(preparedPrices, queriedEntries); - if (_this.logger) { - _this.logger.info("Wrapped " + (_.size(preparedPrices)) + " price(s) into " + (_.size(wrappedProducts)) + " existing product(s)."); - } - return _this._createOrUpdate(wrappedProducts, queriedEntries).then(function(results) { - _.each(results, function(r) { - return _this._handleProcessResponse(r); - }); - return Promise.resolve(_this._summary); - }); - }); - }); - }; - })(this), { - concurrency: 1 - }); - }; - - PriceImport.prototype._handleProcessResponse = function(res) { - var error, errorFile; - if (res.isFulfilled()) { - return this._handleFulfilledResponse(res); - } else if (res.isRejected()) { - error = serializeError(res.reason()); - this._summary.failed++; - if (this.errorDir) { - errorFile = path.join(this.errorDir, "error-" + this._summary.failed + ".json"); - fs.outputJsonSync(errorFile, error, { - spaces: 2 - }); - } - if (_.isFunction(this.errorCallback)) { - return this.errorCallback(error, this.logger); - } else { - return this.logger.error("Error callback has to be a function!"); - } - } - }; - - PriceImport.prototype._handleFulfilledResponse = function(r) { - switch (r.value().statusCode) { - case 201: - return this._summary.created++; - case 200: - return this._summary.updated++; - case 404: - return this._summary.unknownSKUCount++; - case 304: - return this._summary.variantWithoutPriceUpdates++; - } - }; - - PriceImport.prototype._preparePrices = function(pricesToProcess) { - return Promise.map(pricesToProcess, (function(_this) { - return function(priceToProcess) { - return _this._preparePrice(priceToProcess); - }; - })(this), { - concurrency: 1 - }); - }; - - PriceImport.prototype._preparePrice = function(priceToProcess) { - var resolvedPrices; - resolvedPrices = []; - return Promise.map(priceToProcess.prices, (function(_this) { - return function(price) { - return _this._resolvePriceReferences(price).then(function(resolved) { - return resolvedPrices.push(resolved); - }); - }; - })(this), { - concurrency: 1 - }).then(function() { - priceToProcess.prices = resolvedPrices; - return Promise.resolve(priceToProcess); - }); - }; - - PriceImport.prototype._resolvePriceReferences = function(price) { - var ref1, ref2; - return Promise.all([this._resolveReference(this.client.customerGroups, 'customerGroup', price.customerGroup, "name=\"" + ((ref1 = price.customerGroup) != null ? ref1.id : void 0) + "\""), this._resolveReference(this.client.channels, 'channel', price.channel, "key=\"" + ((ref2 = price.channel) != null ? ref2.id : void 0) + "\"")]).spread(function(customerGroupId, channelId) { - if (customerGroupId) { - price.customerGroup = { - id: customerGroupId, - typeId: 'customer-group' - }; - } - if (channelId) { - price.channel = { - id: channelId, - typeId: 'channel' - }; - } - return Promise.resolve(price); - }); - }; - - PriceImport.prototype._createOrUpdate = function(productsToProcess, existingProducts) { - var posts; - debug('Products to process: %j', { - toProcess: productsToProcess, - existing: existingProducts - }); - posts = _.map(productsToProcess, (function(_this) { - return function(prodToProcess) { - var existingProduct, synced, updateTask; - existingProduct = _this._isExistingEntry(prodToProcess, existingProducts); - if (existingProduct != null) { - synced = _this.sync.buildActions(prodToProcess, existingProduct); - if (synced.shouldUpdate()) { - updateTask = function(payload) { - return _this.client.products.byId(synced.getUpdateId()).update(payload); - }; - return _this.repeater.execute(function() { - var payload; - payload = synced.getUpdatePayload(); - if (_this.preventRemoveActions) { - payload.actions = _this._filterPriceActions(payload.actions); - } - if (_this.publishingStrategy && _this.commonUtils.canBePublished(existingProduct, _this.publishingStrategy)) { - payload.actions.push({ - action: 'publish' - }); - } - return updateTask(payload); - }, function(e) { - var newTask; - if (e.statusCode === 409) { - debug('retrying to update %s because of 409', synced.getUpdateId()); - newTask = function() { - return _this.client.productProjections.staged(true).byId(synced.getUpdateId()).fetch().then(function(result) { - var newPayload; - newPayload = _.extend({}, synced.getUpdatePayload(), { - version: result.body.version - }); - return updateTask(newPayload); - }); - }; - return Promise.resolve(newTask); - } else { - return Promise.reject(e); - } - }); - } else { - return Promise.resolve({ - statusCode: 304 - }); - } - } else { - _this._summary.unknownSKUCount++; - return Promise.resolve({ - statusCode: 404 - }); - } - }; - })(this)); - debug('About to send %s requests', _.size(posts)); - return Promise.settle(posts); - }; - - - /** - * filters out remove actions - * so no prices get deleted - */ - - PriceImport.prototype._filterPriceActions = function(actions) { - return _.filter(actions, function(action) { - return action.action !== "removePrice"; - }); - }; - - PriceImport.prototype._wrapPricesIntoProducts = function(prices, products) { - var productsWithPrices, sku2index; - sku2index = {}; - _.each(prices, (function(_this) { - return function(p, index) { - if (!_.has(sku2index, p.sku)) { - return sku2index[p.sku] = index; - } else { - _this.logger.warn("Duplicate SKU found - '" + p.sku + "' - ignoring!"); - return _this._summary.duplicatedSKUs++; - } - }; - })(this)); - productsWithPrices = _.map(products, (function(_this) { - return function(p) { - var product; - product = _.deepClone(p); - _this._wrapPricesIntoVariant(product.masterVariant, prices, sku2index); - _.each(product.variants, function(v) { - return _this._wrapPricesIntoVariant(v, prices, sku2index); - }); - return product; - }; - })(this)); - this._summary.unknownSKUCount += Object.keys(sku2index).length; - return productsWithPrices; - }; - - PriceImport.prototype._wrapPricesIntoVariant = function(variant, prices, sku2index) { - var index; - if (_.has(sku2index, variant.sku)) { - index = sku2index[variant.sku]; - variant.prices = _.deepClone(prices[index].prices); - return delete sku2index[variant.sku]; - } else { - return this._summary.variantWithoutPriceUpdates++; - } - }; - - return PriceImport; - -})(ProductImport); - -module.exports = PriceImport; diff --git a/dist/product-discount-import.js b/dist/product-discount-import.js deleted file mode 100644 index a8d1551..0000000 --- a/dist/product-discount-import.js +++ /dev/null @@ -1,143 +0,0 @@ -var ProductDiscountImport, Promise, Repeater, SphereClient, _, debug, slugify; - -debug = require('debug')('sphere-discount-import'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -slugify = require('underscore.string/slugify'); - -SphereClient = require('sphere-node-sdk').SphereClient; - -Repeater = require('sphere-node-utils').Repeater; - -ProductDiscountImport = (function() { - function ProductDiscountImport(logger, options) { - this.logger = logger; - if (options == null) { - options = {}; - } - this.client = new SphereClient(options.clientConfig); - this.language = 'en'; - this._resetSummary(); - } - - ProductDiscountImport.prototype._resetSummary = function() { - return this._summary = { - created: 0, - updated: 0, - unChanged: 0 - }; - }; - - ProductDiscountImport.prototype.summaryReport = function() { - var message; - if (this._summary.updated === 0) { - message = 'Summary: nothing to update'; - } else { - message = "Summary: there were " + this._summary.updated + " update(s) and " + this._summary.created + " creation(s) of product discount."; - } - return message; - }; - - ProductDiscountImport.prototype.performStream = function(chunk, cb) { - return this._processBatches(chunk).then(function() { - return cb(); - })["catch"](function(err) { - return cb(err.body); - }); - }; - - ProductDiscountImport.prototype._createProductDiscountFetchByNamePredicate = function(discounts) { - var names; - names = _.map(discounts, (function(_this) { - return function(d) { - return "\"" + d.name[_this.language] + "\""; - }; - })(this)); - return "name(" + this.language + " in (" + (names.join(', ')) + "))"; - }; - - ProductDiscountImport.prototype._processBatches = function(discounts) { - var batchedList; - batchedList = _.batchList(discounts, 30); - return Promise.map(batchedList, (function(_this) { - return function(discountsToProcess) { - var predicate; - predicate = _this._createProductDiscountFetchByNamePredicate(discountsToProcess); - return _this.client.productDiscounts.where(predicate).all().fetch().then(function(results) { - var queriedEntries; - debug("Fetched product discounts: %j", results); - queriedEntries = results.body.results; - return _this._createOrUpdate(discountsToProcess, queriedEntries).then(function(results) { - _.each(results, function(r) { - switch (r.statusCode) { - case 200: - return _this._summary.updated++; - case 201: - return _this._summary.created++; - case 304: - return _this._summary.unChanged++; - } - }); - return Promise.resolve(_this._summary); - }); - }); - }; - })(this), { - concurrency: 1 - }); - }; - - ProductDiscountImport.prototype._findMatch = function(discount, existingDiscounts) { - return _.find(existingDiscounts, (function(_this) { - return function(d) { - return _.isString(d.name[_this.language]) && d.name[_this.language] === discount.name[_this.language]; - }; - })(this)); - }; - - ProductDiscountImport.prototype._createOrUpdate = function(discountsToProcess, existingDiscounts) { - var posts; - debug('Product discounts to process: %j', { - toProcess: discountsToProcess, - existing: existingDiscounts - }); - posts = _.map(discountsToProcess, (function(_this) { - return function(discount) { - var existingDiscount, payload; - existingDiscount = _this._findMatch(discount, existingDiscounts); - if (existingDiscount != null) { - if (discount.predicate === existingDiscount.predicate) { - return Promise.resolve({ - statusCode: 304 - }); - } else { - payload = { - version: existingDiscount.version, - actions: [ - { - action: 'changePredicate', - predicate: discount.predicate - } - ] - }; - return _this.client.productDiscounts.byId(existingDiscount.id).update(payload); - } - } else { - return _this.client.productDiscounts.create(discount); - } - }; - })(this)); - debug('About to send %s requests', _.size(posts)); - return Promise.all(posts); - }; - - return ProductDiscountImport; - -})(); - -module.exports = ProductDiscountImport; diff --git a/dist/product-export.js b/dist/product-export.js deleted file mode 100644 index dc667f5..0000000 --- a/dist/product-export.js +++ /dev/null @@ -1,34 +0,0 @@ -var ProductExport, Promise, SphereClient, _, debug, slugify; - -debug = require('debug')('sphere-product-export'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -slugify = require('underscore.string/slugify'); - -SphereClient = require('sphere-node-sdk').SphereClient; - -ProductExport = (function() { - function ProductExport(logger, options) { - this.logger = logger; - if (options == null) { - options = {}; - } - this.client = new SphereClient(options); - } - - ProductExport.prototype.processStream = function(cb) { - return this.client.productProjections.staged(true).process(cb, { - accumulate: false - }); - }; - - return ProductExport; - -})(); - -module.exports = ProductExport; diff --git a/dist/product-import.js b/dist/product-import.js deleted file mode 100644 index 38b6b87..0000000 --- a/dist/product-import.js +++ /dev/null @@ -1,962 +0,0 @@ -var CommonUtils, EnsureDefaultAttributes, EnumValidator, ProductImport, ProductSync, Promise, Reassignment, Repeater, SphereClient, UnknownAttributesFilter, _, debug, fs, path, ref1, serializeError, slugify, util, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; - -debug = require('debug')('sphere-product-import'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -slugify = require('underscore.string/slugify'); - -ref1 = require('sphere-node-sdk'), SphereClient = ref1.SphereClient, ProductSync = ref1.ProductSync; - -Repeater = require('sphere-node-utils').Repeater; - -fs = require('fs-extra'); - -path = require('path'); - -serializeError = require('serialize-error'); - -EnumValidator = require('./enum-validator'); - -UnknownAttributesFilter = require('./unknown-attributes-filter'); - -CommonUtils = require('./common-utils'); - -EnsureDefaultAttributes = require('./ensure-default-attributes'); - -util = require('util'); - -Reassignment = require('commercetools-node-variant-reassignment')["default"]; - -ProductImport = (function() { - function ProductImport(logger1, options) { - this.logger = logger1; - if (options == null) { - options = {}; - } - this._fetchAndResolveCustomReferences = bind(this._fetchAndResolveCustomReferences, this); - this._updateProductSlug = bind(this._updateProductSlug, this); - this._ensureDefaults = bind(this._ensureDefaults, this); - this._fetchSameForAllAttributesOfProductType = bind(this._fetchSameForAllAttributesOfProductType, this); - this._updateProductType = bind(this._updateProductType, this); - this._validateEnums = bind(this._validateEnums, this); - this._ensureProductTypeInMemory = bind(this._ensureProductTypeInMemory, this); - this._ensureDefaultAttributesInProducts = bind(this._ensureDefaultAttributesInProducts, this); - this._ensureProductTypesInMemory = bind(this._ensureProductTypesInMemory, this); - this._filterUniqueUpdateActions = bind(this._filterUniqueUpdateActions, this); - this._filterAttributes = bind(this._filterAttributes, this); - this._handleFulfilledResponse = bind(this._handleFulfilledResponse, this); - this._handleErrorResponse = bind(this._handleErrorResponse, this); - this._errorLogger = bind(this._errorLogger, this); - this._getExistingProductsForSkus = bind(this._getExistingProductsForSkus, this); - this._configErrorHandling = bind(this._configErrorHandling, this); - this._configureSync = bind(this._configureSync, this); - this.sync = new ProductSync; - if (options.blackList && ProductSync.actionGroups) { - this.sync.config(this._configureSync(options.blackList)); - } - this.errorCallback = options.errorCallback || this._errorLogger; - this.ensureEnums = options.ensureEnums || false; - this.filterUnknownAttributes = options.filterUnknownAttributes || false; - this.ignoreSlugUpdates = options.ignoreSlugUpdates || false; - this.batchSize = options.batchSize || 30; - this.failOnDuplicateAttr = options.failOnDuplicateAttr || false; - this.logOnDuplicateAttr = options.logOnDuplicateAttr != null ? options.logOnDuplicateAttr : true; - this.client = new SphereClient(options.clientConfig); - this.enumValidator = new EnumValidator(this.logger); - this.unknownAttributesFilter = new UnknownAttributesFilter(this.logger); - this.commonUtils = new CommonUtils(this.logger); - this.filterActions = _.isFunction(options.filterActions) ? options.filterActions : _.isArray(options.filterActions) ? function(action) { - return !_.contains(options.filterActions, action.action); - } : function(action) { - return true; - }; - this.urlLimit = 8192; - if (options.defaultAttributes) { - this.defaultAttributesService = new EnsureDefaultAttributes(this.logger, options.defaultAttributes); - } - this.publishingStrategy = options.publishingStrategy || false; - this.variantReassignmentOptions = options.variantReassignmentOptions || {}; - this.reassignmentService = new Reassignment(this.client, this.logger, (function(_this) { - return function(error) { - return _this._handleErrorResponse(error); - }; - })(this), this.variantReassignmentOptions.retainExistingData); - this._configErrorHandling(options); - this._resetCache(); - this._resetSummary(); - debug("Product Importer initialized with config -> errorDir: " + this.errorDir + ", errorLimit: " + this.errorLimit + ", blacklist actions: " + options.blackList + ", ensureEnums: " + this.ensureEnums); - } - - ProductImport.prototype._configureSync = function(blackList) { - this._validateSyncConfig(blackList); - debug("Product sync config validated"); - return _.difference(ProductSync.actionGroups, blackList).map(function(type) { - return { - type: type, - group: 'white' - }; - }).concat(blackList.map(function(type) { - return { - type: type, - group: 'black' - }; - })); - }; - - ProductImport.prototype._validateSyncConfig = function(blackList) { - var actionGroup, i, len, results1; - results1 = []; - for (i = 0, len = blackList.length; i < len; i++) { - actionGroup = blackList[i]; - if (!_.contains(ProductSync.actionGroups, actionGroup)) { - throw "invalid product sync action group: " + actionGroup; - } else { - results1.push(void 0); - } - } - return results1; - }; - - ProductImport.prototype._configErrorHandling = function(options) { - if (options.errorDir) { - this.errorDir = options.errorDir; - } else { - this.errorDir = path.join(__dirname, '../errors'); - } - fs.emptyDirSync(this.errorDir); - if (options.errorLimit) { - return this.errorLimit = options.errorLimit; - } else { - return this.errorLimit = 30; - } - }; - - ProductImport.prototype._resetCache = function() { - return this._cache = { - productType: {}, - categories: {}, - taxCategory: {} - }; - }; - - ProductImport.prototype._resetSummary = function() { - this._summary = { - productsWithMissingSKU: 0, - created: 0, - updated: 0, - failed: 0, - productTypeUpdated: 0, - errorDir: this.errorDir - }; - if (this.filterUnknownAttributes) { - this._summary.unknownAttributeNames = []; - } - if (this.variantReassignmentOptions.enabled) { - this._summary.variantReassignment = null; - return this.reassignmentService._resetStats(); - } - }; - - ProductImport.prototype.summaryReport = function(filename) { - var message, report; - message = ("Summary: there were " + (this._summary.created + this._summary.updated) + " imported products ") + ("(" + this._summary.created + " were new and " + this._summary.updated + " were updates)."); - if (this._summary.productsWithMissingSKU > 0) { - message += "\nFound " + this._summary.productsWithMissingSKU + " product(s) which do not have SKU and won't be imported."; - if (filename) { - message += " '" + filename + "'"; - } - } - if (this._summary.failed > 0) { - message += "\n " + this._summary.failed + " product imports failed. Error reports stored at: " + this.errorDir; - } - report = { - reportMessage: message, - detailedSummary: this._summary - }; - return report; - }; - - ProductImport.prototype._filterOutProductsBySkus = function(products, blacklistSkus) { - return products.filter((function(_this) { - return function(product) { - var isProductBlacklisted, variantSkus, variants; - variants = product.variants.concat(product.masterVariant); - variantSkus = variants.map(function(v) { - return v.sku; - }); - isProductBlacklisted = variantSkus.find(function(sku) { - return blacklistSkus.indexOf(sku) >= 0; - }); - return !isProductBlacklisted; - }; - })(this)); - }; - - ProductImport.prototype.performStream = function(chunk, cb) { - return this._processBatches(chunk).then(function() { - return cb(); - }); - }; - - ProductImport.prototype._processBatches = function(products) { - var batchedList; - batchedList = _.batchList(products, this.batchSize); - return Promise.map(batchedList, (function(_this) { - return function(productsToProcess) { - debug('Ensuring existence of product type in memory.'); - return _this._ensureProductTypesInMemory(productsToProcess).then(function() { - var enumUpdateActions, uniqueEnumUpdateActions; - if (_this.ensureEnums) { - debug('Ensuring existence of enum keys in product type.'); - enumUpdateActions = _this._validateEnums(productsToProcess); - uniqueEnumUpdateActions = _this._filterUniqueUpdateActions(enumUpdateActions); - return _this._updateProductType(uniqueEnumUpdateActions); - } - }).then(function() { - var filteredProductsLength, originalLength; - originalLength = productsToProcess.length; - productsToProcess = productsToProcess.filter(_this._doesProductHaveSkus); - filteredProductsLength = originalLength - productsToProcess.length; - if (filteredProductsLength) { - _this.logger.warn("Filtering out " + filteredProductsLength + " product(s) which do not have SKU"); - _this._summary.productsWithMissingSKU += filteredProductsLength; - } - if (_this.variantReassignmentOptions.enabled) { - _this.logger.debug('execute reassignment process'); - return _this.reassignmentService.execute(productsToProcess, _this._cache.productType).then(function(res) { - if (res.failedSkus.length) { - _this.logger.warn("Removing " + res.failedSkus + " skus from processing due to a reassignment error"); - return productsToProcess = _this._filterOutProductsBySkus(productsToProcess, res.failedSkus); - } - }); - } - }).then(function() { - return _this._getExistingProductsForSkus(_this._extractUniqueSkus(productsToProcess)); - }).then(function(queriedEntries) { - if (_this.defaultAttributesService) { - debug('Ensuring default attributes'); - return _this._ensureDefaultAttributesInProducts(productsToProcess, queriedEntries).then(function() { - return Promise.resolve(queriedEntries); - }); - } else { - return Promise.resolve(queriedEntries); - } - }).then(function(queriedEntries) { - return _this._createOrUpdate(productsToProcess, queriedEntries); - }).then(function(results) { - _.each(results, function(r) { - if (r.isFulfilled()) { - return _this._handleFulfilledResponse(r); - } else if (r.isRejected()) { - return _this._handleErrorResponse(r.reason()); - } - }); - return Promise.resolve(_this._summary); - }); - }; - })(this), { - concurrency: 1 - }).then((function(_this) { - return function() { - if (_this.variantReassignmentOptions.enabled) { - _this._summary.variantReassignment = _this.reassignmentService.statistics; - } - return console.log("TEMP_SUMMARY:", _this._summary); - }; - })(this)); - }; - - ProductImport.prototype._getWhereQueryLimit = function() { - var client, url; - client = this.client.productProjections.where('a').staged(true); - url = _.clone(this.client.productProjections._rest._options.uri); - url = url.replace(/.*?:\/\//g, ""); - url += this.client.productProjections._currentEndpoint; - url += "?" + this.client.productProjections._queryString(); - this.client.productProjections._setDefaults(); - return this.urlLimit - Buffer.byteLength(url, 'utf-8') - 1; - }; - - ProductImport.prototype._getExistingProductsForSkus = function(skus) { - return new Promise((function(_this) { - return function(resolve, reject) { - var skuChunks; - if (skus.length === 0) { - return resolve([]); - } - skuChunks = _this.commonUtils._separateSkusChunksIntoSmallerChunks(skus, _this._getWhereQueryLimit()); - return Promise.map(skuChunks, function(skus) { - var predicate; - predicate = _this._createProductFetchBySkuQueryPredicate(skus); - return _this.client.productProjections.where(predicate).staged(true).perPage(200).all().fetch().then(function(res) { - return res.body.results; - }); - }, { - concurrency: 30 - }).then(function(results) { - debug('Fetched products: %j', results); - return resolve(_.flatten(results)); - })["catch"](function(err) { - return reject(err); - }); - }; - })(this)); - }; - - ProductImport.prototype._errorLogger = function(res, logger) { - if (this._summary.failed < this.errorLimit || this.errorLimit === 0) { - return logger.error(res, "Skipping product due to an error"); - } else { - return logger.warn("Error not logged as error limit of " + this.errorLimit + " has reached."); - } - }; - - ProductImport.prototype._handleErrorResponse = function(error) { - var errorFile; - error = serializeError(error); - this._summary.failed++; - if (this.errorDir) { - errorFile = path.join(this.errorDir, "error-" + this._summary.failed + ".json"); - fs.outputJsonSync(errorFile, error, { - spaces: 2 - }); - } - if (_.isFunction(this.errorCallback)) { - return this.errorCallback(error, this.logger); - } else { - return this.logger.error("Error callback has to be a function!"); - } - }; - - ProductImport.prototype._handleFulfilledResponse = function(res) { - switch (res.value().statusCode) { - case 201: - return this._summary.created++; - case 200: - return this._summary.updated++; - } - }; - - ProductImport.prototype._createProductFetchBySkuQueryPredicate = function(skus) { - var skuString; - skuString = "sku in (" + (skus.map(function(val) { - return JSON.stringify(val); - })) + ")"; - return "masterVariant(" + skuString + ") or variants(" + skuString + ")"; - }; - - ProductImport.prototype._doesProductHaveSkus = function(product) { - var i, len, ref2, ref3, variant; - if (product.masterVariant && !product.masterVariant.sku) { - return false; - } - if ((ref2 = product.variants) != null ? ref2.length : void 0) { - ref3 = product.variants; - for (i = 0, len = ref3.length; i < len; i++) { - variant = ref3[i]; - if (!variant.sku) { - return false; - } - } - } - return true; - }; - - ProductImport.prototype._extractUniqueSkus = function(products) { - var i, j, len, len1, product, ref2, ref3, ref4, skus, variant; - skus = []; - for (i = 0, len = products.length; i < len; i++) { - product = products[i]; - if ((ref2 = product.masterVariant) != null ? ref2.sku : void 0) { - skus.push(product.masterVariant.sku); - } - if ((ref3 = product.variants) != null ? ref3.length : void 0) { - ref4 = product.variants; - for (j = 0, len1 = ref4.length; j < len1; j++) { - variant = ref4[j]; - if (variant.sku) { - skus.push(variant.sku); - } - } - } - } - return _.uniq(skus, false); - }; - - ProductImport.prototype._isExistingEntry = function(prodToProcess, existingProducts) { - var prodToProcessSkus; - prodToProcessSkus = this._extractUniqueSkus([prodToProcess]); - return _.find(existingProducts, (function(_this) { - return function(existingEntry) { - var existingProductSkus, matchingSkus; - existingProductSkus = _this._extractUniqueSkus([existingEntry]); - matchingSkus = _.intersection(prodToProcessSkus, existingProductSkus); - if (matchingSkus.length > 0) { - return true; - } else { - return false; - } - }; - })(this)); - }; - - ProductImport.prototype._updateProductRepeater = function(prodToProcess, existingProduct) { - var repeater; - repeater = new Repeater({ - attempts: 5 - }); - return repeater.execute((function(_this) { - return function() { - return _this._updateProduct(prodToProcess, existingProduct); - }; - })(this), (function(_this) { - return function(e) { - if (e.statusCode !== 409) { - return Promise.reject(e); - } - _this.logger.warn("Recovering from 409 concurrentModification error on product '" + existingProduct.id + "'"); - return Promise.resolve(function() { - return _this.client.productProjections.staged(true).byId(existingProduct.id).fetch().then(function(result) { - return _this._updateProduct(prodToProcess, result.body, true); - }); - }); - }; - })(this)); - }; - - ProductImport.prototype._updateProduct = function(prodToProcess, existingProduct, productIsPrepared) { - return this._fetchSameForAllAttributesOfProductType(prodToProcess.productType).then((function(_this) { - return function(sameForAllAttributes) { - var productPromise; - productPromise = Promise.resolve(prodToProcess); - if (!productIsPrepared) { - productPromise = _this._prepareUpdateProduct(prodToProcess, existingProduct); - } - return productPromise.then(function(preparedProduct) { - var synced; - synced = _this.sync.buildActions(preparedProduct, existingProduct, sameForAllAttributes).filterActions(function(action) { - return _this.filterActions(action, existingProduct, preparedProduct); - }); - if (synced.shouldUpdate()) { - return _this._updateInBatches(synced.getUpdateId(), synced.getUpdatePayload()); - } else { - return Promise.resolve({ - statusCode: 304 - }); - } - }); - }; - })(this)); - }; - - ProductImport.prototype._updateInBatches = function(id, updateRequest) { - var batchedActions, latestVersion; - latestVersion = updateRequest.version; - batchedActions = _.batchList(updateRequest.actions, 500); - return Promise.mapSeries(batchedActions, (function(_this) { - return function(actions) { - var request; - request = { - version: latestVersion, - actions: actions - }; - return _this.client.products.byId(id).update(request).tap(function(res) { - return latestVersion = res.body.version; - }); - }; - })(this)).then(_.last); - }; - - ProductImport.prototype._cleanVariantAttributes = function(variant) { - var attributeMap; - attributeMap = []; - if (_.isArray(variant.attributes)) { - return variant.attributes = variant.attributes.filter((function(_this) { - return function(attribute) { - var isDuplicate, msg; - isDuplicate = attributeMap.indexOf(attribute.name) >= 0; - attributeMap.push(attribute.name); - if (isDuplicate) { - msg = "Variant with SKU '" + variant.sku + "' has duplicate attributes with name '" + attribute.name + "'."; - if (_this.failOnDuplicateAttr) { - throw new Error(msg); - } else if (_this.logOnDuplicateAttr) { - _this.logger.warn(msg); - } - } - return !isDuplicate; - }; - })(this)); - } - }; - - ProductImport.prototype._cleanDuplicateAttributes = function(prodToProcess) { - prodToProcess.variants = prodToProcess.variants || []; - this._cleanVariantAttributes(prodToProcess.masterVariant); - return prodToProcess.variants.forEach((function(_this) { - return function(variant) { - return _this._cleanVariantAttributes(variant); - }; - })(this)); - }; - - ProductImport.prototype._createOrUpdate = function(productsToProcess, existingProducts) { - var posts; - debug('Products to process: %j', { - toProcess: productsToProcess, - existing: existingProducts - }); - posts = _.map(productsToProcess, (function(_this) { - return function(product) { - return _this._filterAttributes(product).then(function(prodToProcess) { - var existingProduct; - _this._cleanDuplicateAttributes(prodToProcess); - existingProduct = _this._isExistingEntry(prodToProcess, existingProducts); - if (existingProduct != null) { - return _this._updateProductRepeater(prodToProcess, existingProduct); - } else { - return _this._prepareNewProduct(prodToProcess).then(function(product) { - return _this.client.products.create(product); - }); - } - }); - }; - })(this)); - debug('About to send %s requests', _.size(posts)); - return Promise.settle(posts); - }; - - ProductImport.prototype._filterAttributes = function(product) { - return new Promise((function(_this) { - return function(resolve) { - if (_this.filterUnknownAttributes) { - return _this.unknownAttributesFilter.filter(_this._cache.productType[product.productType.id], product, _this._summary.unknownAttributeNames).then(function(filteredProduct) { - return resolve(filteredProduct); - }); - } else { - return resolve(product); - } - }; - })(this)); - }; - - ProductImport.prototype._filterUniqueUpdateActions = function(updateActions) { - return _.reduce(_.keys(updateActions), (function(_this) { - return function(acc, productTypeId) { - var actions, uniqueActions; - actions = updateActions[productTypeId]; - uniqueActions = _this.commonUtils.uniqueObjectFilter(actions); - acc[productTypeId] = uniqueActions; - return acc; - }; - })(this), {}); - }; - - ProductImport.prototype._ensureProductTypesInMemory = function(products) { - return Promise.map(products, (function(_this) { - return function(product) { - return _this._ensureProductTypeInMemory(product.productType.id); - }; - })(this), { - concurrency: 1 - }); - }; - - ProductImport.prototype._ensureDefaultAttributesInProducts = function(products, queriedEntries) { - if (queriedEntries) { - queriedEntries = _.compact(queriedEntries); - } - return Promise.map(products, (function(_this) { - return function(product) { - var productFromServer, uniqueSkus; - if ((queriedEntries != null ? queriedEntries.length : void 0) > 0) { - uniqueSkus = _this._extractUniqueSkus([product]); - productFromServer = _.find(queriedEntries, function(entry) { - var intersection, serverUniqueSkus; - serverUniqueSkus = _this._extractUniqueSkus([entry]); - intersection = _.intersection(uniqueSkus, serverUniqueSkus); - return _.compact(intersection).length > 0; - }); - } - return _this.defaultAttributesService.ensureDefaultAttributesInProduct(product, productFromServer); - }; - })(this), { - concurrency: 1 - }); - }; - - ProductImport.prototype._ensureProductTypeInMemory = function(productTypeId) { - var productType; - if (this._cache.productType[productTypeId]) { - return Promise.resolve(); - } else { - productType = { - id: productTypeId - }; - return this._resolveReference(this.client.productTypes, 'productType', productType, "name=\"" + (productType != null ? productType.id : void 0) + "\""); - } - }; - - ProductImport.prototype._validateEnums = function(products) { - var enumUpdateActions; - enumUpdateActions = {}; - _.each(products, (function(_this) { - return function(product) { - var updateActions; - updateActions = _this.enumValidator.validateProduct(product, _this._cache.productType[product.productType.id]); - if (updateActions && _.size(updateActions.actions) > 0) { - return _this._updateEnumUpdateActions(enumUpdateActions, updateActions); - } - }; - })(this)); - return enumUpdateActions; - }; - - ProductImport.prototype._updateProductType = function(enumUpdateActions) { - if (_.isEmpty(enumUpdateActions)) { - return Promise.resolve(); - } else { - debug("Updating product type(s): " + (_.keys(enumUpdateActions))); - return Promise.map(_.keys(enumUpdateActions), (function(_this) { - return function(productTypeId) { - var updateRequest; - updateRequest = { - version: _this._cache.productType[productTypeId].version, - actions: enumUpdateActions[productTypeId] - }; - return _this.client.productTypes.byId(_this._cache.productType[productTypeId].id).update(updateRequest).then(function(updatedProductType) { - _this._cache.productType[productTypeId] = updatedProductType.body; - return _this._summary.productTypeUpdated++; - }); - }; - })(this)); - } - }; - - ProductImport.prototype._updateEnumUpdateActions = function(enumUpdateActions, updateActions) { - if (enumUpdateActions[updateActions.productTypeId]) { - return enumUpdateActions[updateActions.productTypeId] = enumUpdateActions[updateActions.productTypeId].concat(updateActions.actions); - } else { - return enumUpdateActions[updateActions.productTypeId] = updateActions.actions; - } - }; - - ProductImport.prototype._fetchSameForAllAttributesOfProductType = function(productType) { - if (this._cache.productType[productType.id + "_sameForAllAttributes"]) { - return Promise.resolve(this._cache.productType[productType.id + "_sameForAllAttributes"]); - } else { - return this._resolveReference(this.client.productTypes, 'productType', productType, "name=\"" + (productType != null ? productType.id : void 0) + "\"").then((function(_this) { - return function() { - var sameValueAttributeNames, sameValueAttributes; - sameValueAttributes = _.where(_this._cache.productType[productType.id].attributes, { - attributeConstraint: "SameForAll" - }); - sameValueAttributeNames = _.pluck(sameValueAttributes, 'name'); - _this._cache.productType[productType.id + "_sameForAllAttributes"] = sameValueAttributeNames; - return Promise.resolve(sameValueAttributeNames); - }; - })(this)); - } - }; - - ProductImport.prototype._ensureVariantDefaults = function(variant) { - var variantDefaults; - if (variant == null) { - variant = {}; - } - variantDefaults = { - attributes: [], - prices: [], - images: [] - }; - return _.defaults(variant, variantDefaults); - }; - - ProductImport.prototype._ensureDefaults = function(product) { - debug('ensuring default fields in variants.'); - _.defaults(product, { - masterVariant: this._ensureVariantDefaults(product.masterVariant), - variants: _.map(product.variants, (function(_this) { - return function(variant) { - return _this._ensureVariantDefaults(variant); - }; - })(this)) - }); - return product; - }; - - ProductImport.prototype._prepareUpdateProduct = function(productToProcess, existingProduct) { - var ref2; - productToProcess = this._ensureDefaults(productToProcess); - return Promise.all([this._resolveProductCategories(productToProcess.categories), this._resolveReference(this.client.taxCategories, 'taxCategory', productToProcess.taxCategory, "name=\"" + ((ref2 = productToProcess.taxCategory) != null ? ref2.id : void 0) + "\""), this._fetchAndResolveCustomReferences(productToProcess)]).spread((function(_this) { - return function(prodCatsIds, taxCatId) { - if (taxCatId) { - productToProcess.taxCategory = { - id: taxCatId, - typeId: 'tax-category' - }; - } - if (prodCatsIds) { - productToProcess.categories = _.map(prodCatsIds, function(catId) { - return { - id: catId, - typeId: 'category' - }; - }); - } - productToProcess.slug = _this._updateProductSlug(productToProcess, existingProduct); - return Promise.resolve(productToProcess); - }; - })(this)); - }; - - ProductImport.prototype._updateProductSlug = function(productToProcess, existingProduct) { - var slug; - if (this.ignoreSlugUpdates) { - slug = existingProduct.slug; - } else if (!productToProcess.slug) { - debug('slug missing in product to process, assigning same as existing product: %s', existingProduct.slug); - slug = existingProduct.slug; - } else { - slug = productToProcess.slug; - } - return slug; - }; - - ProductImport.prototype._prepareNewProduct = function(product) { - var ref2, ref3; - product = this._ensureDefaults(product); - return Promise.all([this._resolveReference(this.client.productTypes, 'productType', product.productType, "name=\"" + ((ref2 = product.productType) != null ? ref2.id : void 0) + "\""), this._resolveProductCategories(product.categories), this._resolveReference(this.client.taxCategories, 'taxCategory', product.taxCategory, "name=\"" + ((ref3 = product.taxCategory) != null ? ref3.id : void 0) + "\""), this._fetchAndResolveCustomReferences(product)]).spread((function(_this) { - return function(prodTypeId, prodCatsIds, taxCatId) { - if (prodTypeId) { - product.productType = { - id: prodTypeId, - typeId: 'product-type' - }; - } - if (taxCatId) { - product.taxCategory = { - id: taxCatId, - typeId: 'tax-category' - }; - } - if (prodCatsIds) { - product.categories = _.map(prodCatsIds, function(catId) { - return { - id: catId, - typeId: 'category' - }; - }); - } - if (!product.slug) { - if (product.name) { - product.slug = _this._generateSlug(product.name); - } - } - return Promise.resolve(product); - }; - })(this)); - }; - - ProductImport.prototype._generateSlug = function(name) { - var slugs; - slugs = _.mapObject(name, (function(_this) { - return function(val) { - var uniqueToken; - uniqueToken = _this._generateUniqueToken(); - return slugify(val).concat("-" + uniqueToken).substring(0, 256); - }; - })(this)); - return slugs; - }; - - ProductImport.prototype._generateUniqueToken = function() { - return _.uniqueId("" + (new Date().getTime())); - }; - - ProductImport.prototype._fetchAndResolveCustomReferences = function(product) { - return Promise.all([ - this._fetchAndResolveCustomReferencesByVariant(product.masterVariant), Promise.map(product.variants, (function(_this) { - return function(variant) { - return _this._fetchAndResolveCustomReferencesByVariant(variant); - }; - })(this), { - concurrency: 5 - }) - ]).spread(function(masterVariant, variants) { - return Promise.resolve(_.extend(product, { - masterVariant: masterVariant, - variants: variants - })); - }); - }; - - ProductImport.prototype._fetchAndResolveCustomAttributeReferences = function(variant) { - if (variant.attributes && !_.isEmpty(variant.attributes)) { - return Promise.map(variant.attributes, (function(_this) { - return function(attribute) { - if (attribute && _.isArray(attribute.value)) { - if (_.every(attribute.value, _this._isReferenceTypeAttribute)) { - return _this._resolveCustomReferenceSet(attribute.value).then(function(result) { - attribute.value = result; - return Promise.resolve(attribute); - }); - } else { - return Promise.resolve(attribute); - } - } else { - if (attribute && _this._isReferenceTypeAttribute(attribute)) { - return _this._resolveCustomReference(attribute).then(function(refId) { - return Promise.resolve({ - name: attribute.name, - value: { - id: refId, - typeId: attribute.type.referenceTypeId - } - }); - }); - } else { - return Promise.resolve(attribute); - } - } - }; - })(this)).then(function(attributes) { - return Promise.resolve(_.extend(variant, { - attributes: attributes - })); - }); - } else { - return Promise.resolve(variant); - } - }; - - ProductImport.prototype._fetchAndResolveCustomPriceReferences = function(variant) { - if (variant.prices && !_.isEmpty(variant.prices)) { - return Promise.map(variant.prices, (function(_this) { - return function(price) { - var ref, service; - if (price && price.custom && price.custom.type && price.custom.type.id) { - service = _this.client.types; - ref = { - id: price.custom.type.id - }; - return _this._resolveReference(service, "types", ref, "key=\"" + ref.id + "\"").then(function(refId) { - price.custom.type.id = refId; - return Promise.resolve(price); - }); - } else { - return Promise.resolve(price); - } - }; - })(this)).then(function(prices) { - return Promise.resolve(_.extend(variant, { - prices: prices - })); - }); - } else { - return Promise.resolve(variant); - } - }; - - ProductImport.prototype._fetchAndResolveCustomReferencesByVariant = function(variant) { - return this._fetchAndResolveCustomAttributeReferences(variant).then((function(_this) { - return function(variant) { - return _this._fetchAndResolveCustomPriceReferences(variant); - }; - })(this)); - }; - - ProductImport.prototype._resolveCustomReferenceSet = function(attributeValue) { - return Promise.map(attributeValue, (function(_this) { - return function(referenceObject) { - return _this._resolveCustomReference(referenceObject); - }; - })(this)); - }; - - ProductImport.prototype._isReferenceTypeAttribute = function(attribute) { - return _.has(attribute, 'type') && attribute.type.name === 'reference'; - }; - - ProductImport.prototype._resolveCustomReference = function(referenceObject) { - var predicate, ref, refKey, service; - service = (function() { - switch (referenceObject.type.referenceTypeId) { - case 'product': - return this.client.productProjections; - } - }).call(this); - refKey = referenceObject.type.referenceTypeId; - ref = _.deepClone(referenceObject); - ref.id = referenceObject.value; - predicate = referenceObject._custom.predicate; - return this._resolveReference(service, refKey, ref, predicate); - }; - - ProductImport.prototype._resolveProductCategories = function(cats) { - return new Promise((function(_this) { - return function(resolve, reject) { - if (_.isEmpty(cats)) { - return resolve(); - } else { - return Promise.all(cats.map(function(cat) { - return _this._resolveReference(_this.client.categories, 'categories', cat, "externalId=\"" + cat.id + "\""); - })).then(function(result) { - return resolve(result.filter(function(c) { - return c; - })); - })["catch"](function(err) { - return reject(err); - }); - } - }; - })(this)); - }; - - ProductImport.prototype._resolveReference = function(service, refKey, ref, predicate) { - return new Promise((function(_this) { - return function(resolve, reject) { - var request; - if (!ref) { - resolve(); - } - if (!_this._cache[refKey]) { - _this._cache[refKey] = {}; - } - if (_this._cache[refKey][ref.id]) { - return resolve(_this._cache[refKey][ref.id].id); - } else { - request = service.where(predicate); - if (refKey === 'product') { - request.staged(true); - } - return request.fetch().then(function(result) { - if (result.body.count === 0) { - return reject("Didn't find any match while resolving " + refKey + " (" + predicate + ")"); - } else { - if (_.size(result.body.results) > 1) { - _this.logger.warn("Found more than 1 " + refKey + " for " + ref.id); - } - _this._cache[refKey][ref.id] = result.body.results[0]; - if (refKey === 'productType') { - _this._cache[refKey][result.body.results[0].id] = result.body.results[0]; - } - return resolve(result.body.results[0].id); - } - }); - } - }; - })(this)); - }; - - return ProductImport; - -})(); - -module.exports = ProductImport; diff --git a/dist/unknown-attributes-filter.js b/dist/unknown-attributes-filter.js deleted file mode 100644 index fcb7541..0000000 --- a/dist/unknown-attributes-filter.js +++ /dev/null @@ -1,89 +0,0 @@ -var Promise, UnknownAttributesFilter, _, debug, - bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; - -debug = require('debug')('sphere-product-import:unknown-attributes-filter'); - -_ = require('underscore'); - -_.mixin(require('underscore-mixins')); - -Promise = require('bluebird'); - -UnknownAttributesFilter = (function() { - function UnknownAttributesFilter(logger) { - this.logger = logger; - this._unknownAttributeNameCollector = bind(this._unknownAttributeNameCollector, this); - this._filterAttributes = bind(this._filterAttributes, this); - this._filterVariantAttributes = bind(this._filterVariantAttributes, this); - this.filter = bind(this.filter, this); - debug("Unknown Attributes Filter initialized."); - } - - UnknownAttributesFilter.prototype.filter = function(productType, product, collectedUnknownAttributeNames) { - var attrNameList; - this.collectedUnknownAttributeNames = collectedUnknownAttributeNames; - if (productType.attributes) { - attrNameList = _.pluck(productType.attributes, 'name'); - return Promise.all([ - this._filterVariantAttributes(product.masterVariant, attrNameList), Promise.map(product.variants, (function(_this) { - return function(variant) { - return _this._filterVariantAttributes(variant, attrNameList); - }; - })(this), { - concurrency: 5 - }) - ]).spread(function(masterVariant, variants) { - return Promise.resolve(_.extend(product, { - masterVariant: masterVariant, - variants: variants - })); - }); - } else { - debug('product type received without attributes, aborting attribute filter.'); - return Promise.resolve(); - } - }; - - UnknownAttributesFilter.prototype._filterVariantAttributes = function(variant, attrNameList) { - if (variant.attributes) { - return this._filterAttributes(attrNameList, variant.attributes).then(function(filteredAttributes) { - variant.attributes = filteredAttributes; - return Promise.resolve(variant); - }); - } else { - debug("skipping variant filter: as variant without attributes: " + variant.sku); - return Promise.resolve(variant); - } - }; - - UnknownAttributesFilter.prototype._filterAttributes = function(attrNameList, attributes) { - var attribute, filteredAttributes, i, len; - filteredAttributes = []; - for (i = 0, len = attributes.length; i < len; i++) { - attribute = attributes[i]; - if (this._isKnownAttribute(attribute, attrNameList)) { - filteredAttributes.push(attribute); - } else if (this.collectedUnknownAttributeNames) { - this._unknownAttributeNameCollector(attribute.name); - } - } - return Promise.resolve(filteredAttributes); - }; - - UnknownAttributesFilter.prototype._isKnownAttribute = function(attribute, attrNameList) { - var ref; - return ref = attribute.name, indexOf.call(attrNameList, ref) >= 0; - }; - - UnknownAttributesFilter.prototype._unknownAttributeNameCollector = function(attributeName) { - if (!_.contains(this.collectedUnknownAttributeNames, attributeName)) { - return this.collectedUnknownAttributeNames.push(attributeName); - } - }; - - return UnknownAttributesFilter; - -})(); - -module.exports = UnknownAttributesFilter;