Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #84 from commercetools/82-no-matching-products
Browse files Browse the repository at this point in the history
No matching products results in an error.
  • Loading branch information
junajan authored Feb 25, 2019
2 parents ffbd6da + c1675f7 commit 24dee09
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 21 deletions.
19 changes: 17 additions & 2 deletions lib/runner/variant-reassignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ export default class VariantReassignment {
async _processUnfinishedTransactions (transactions, retryCount = 0) {
try {
if (!transactions) {
log('Loading unfinished transactions')
this.logger.info('Loading unfinished transactions')
transactions = await this.transactionService.getTransactions()
}

for (const transactionObject of transactions) {
const { key, value: transaction } = transactionObject

this.logger.debug(`Processing unfinished transaction with key ${key}`)
this.logger.info(`Processing unfinished transaction with key ${key}`)
await this._createAndExecuteActions(
transaction.newProductDraft,
transaction.backupProductDraft,
Expand All @@ -175,6 +175,10 @@ export default class VariantReassignment {
await this.transactionService.deleteTransaction(key)
}
} catch (e) {
this.logger.warn(
'VariantReassignment Error occurred while processing an unfinished transaction',
util.inspect(e, false, null)
)
if (retryCount < constants.MAX_RETRIES && !this._isUnrecoverableError(e))
await this._handleUnfinishedTransactionsError(retryCount)
else
Expand Down Expand Up @@ -216,6 +220,11 @@ export default class VariantReassignment {
ctpProductToUpdate, transaction, matchingProducts)
await this.transactionService.deleteTransaction(transaction.key)
} catch (e) {
this.logger.error(
'VariantReassignment Error occurred while processing product draft',
util.inspect(productDraft, false, null), util.inspect(e, false, null)
)

if (retryCount < constants.MAX_RETRIES && !this._isUnrecoverableError(e))
await this._retryProcessingOfProductDraft(productDraft, products, retryCount)
else
Expand Down Expand Up @@ -244,6 +253,12 @@ export default class VariantReassignment {
ctpProductToUpdate = await this._createNewProduct(
ctpProductToUpdate, productDraft.productType.id
)

if (!ctpProductToUpdate)
// there are no matching products and no ctpProductToUpdate
// eg it was deleted or slug/sku was changed
// see: https://github.com/commercetools/commercetools-node-variant-reassignment/issues/82
return
}

// check if product types are the same for productDraft and CTP product to update
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "commercetools-node-variant-reassignment",
"version": "1.1.2",
"version": "1.1.3",
"description": "Node.js utility which complements product sync/import process by automatic conflicts resolution and reassignment of variants from one product to another",
"license": "MIT",
"homepage": "https://github.com/commercetools/commercetools-node-variant-reassignment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
* +---------------------------+---------------------------------------------+--------------------+---------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - moving variant to another product', () => {
let ctpClient
let product1
const logger = utils.createLogger(__filename)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
* +--------------------------------------------+-------------------------------------------------------------------------+--------------------+--------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - handling sameForAll attributes', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
*/
/* eslint-enable max-len */
// todo: https://github.com/commercetools/commercetools-node-variant-reassignment/issues/43
describe('Variant reassignment', () => {
describe('Variant reassignment - sameForAll and missing attribute', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
*/
/* eslint-enable max-len */
// todo: https://github.com/commercetools/commercetools-node-variant-reassignment/issues/43
describe.skip('Variant reassignment', () => {
describe.skip('Variant reassignment - invalid sameForAll', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const productTypeDraft2 = _.cloneDeep(require('../../resources/productType.json'
* +---------------------------+----------------------------+--------------------+----------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - changing productType', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
+---------+----------------------------+---------------------------------------+--------------------+----------------------------+-------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - different staged and current', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
+---------+----------------------------+----------------------------+----------------------------+--------------------+----------------------------+-------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - different product and variant info on staged and current', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
+---------+----------------------------+----------------------------+--------------------+----------------------------+------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - different variant info on staged and current', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
* +------------------------------------+--------------------------------------+--------------------+------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - product anonymization', () => {
let ctpClient
let product1
let product2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
* +------------------------------------+--------------------------------------------------+--------------------+--------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - changing SKU', () => {
let ctpClient
let product1
const logger = utils.createLogger(__filename)
Expand Down
79 changes: 79 additions & 0 deletions spec/integration/runner/variant-reassignment-example-18.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { expect } from 'chai'
import sinon from 'sinon'
import * as utils from '../../utils/helper'
import VariantReassignment from '../../../lib/runner/variant-reassignment'

/* eslint-disable max-len */
/**
* +------------------------------------+--------------------------------------------------+--------------------+--------------------------------------------------------------+
* | Product draft | CTP product | After reassignment | CTP product |
* +------------------------------------+--------------------------------------------------+ +--------------------------------------------------------------+
* | Product: | Product: | product SLUG was | Product: |
* | slug: { en: "bike" } | id: 1 | changed during | id: 1 |
* | masterVariant: { sku: "red-bike" } | slug: { en: "bike" } | reassignment | slug: { en: "ball" } |
* | variants: [] | masterVariant: { sku: "red-tshirt" } | | masterVariant: { sku: "blue-tshirt" } |
* | | variants: [] | | variants: [] |
* +------------------------------------+--------------------------------------------------+--------------------+--------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment - concurrent modification on ctpProductToUpdate', () => {
let ctpClient
let product
const logger = utils.createLogger(__filename)
const productSku = 'red-tshirt'
const productSlug = { en: 'bike' }
const productSlug2 = { en: 'tshirt' }

before(async () => {
ctpClient = await utils.createClient()
const products = await utils.createCtpProducts([[productSku]],
ctpClient,
(pD) => {
pD.slug = productSlug
})
product = products[0]
})

after(() =>
utils.deleteResourcesAll(ctpClient, logger)
)

it('handle no matching products', async () => {
const reassignment = new VariantReassignment(ctpClient, logger)
const productDraftSku = 'red-bike'

// before executing actions update CTP product
const executeActionsStub = sinon.stub(reassignment, '_createAndExecuteActions')
.callsFake(async (...args) => {
await ctpClient.products
.byId(product.id)
.update({
version: product.version,
actions: [{
action: 'changeSlug',
slug: productSlug2
}]
})

return executeActionsStub.wrappedMethod.call(reassignment, ...args)
})

const { statistics } = await reassignment.execute([{
productType: {
id: product.productType.id
},
name: product.name,
slug: product.slug,
masterVariant: { sku: productDraftSku }
}])

utils.expectStatistics(statistics, 0, 0, 1, 1, 1, 0) // 1 processed, 1 success, 1 retries

const { body: { results } } = await utils.getProductsBySkus([productSku, productDraftSku],
ctpClient)

expect(results).to.have.lengthOf(1)
expect(results[0].masterVariant.sku).to.equal(productSku) // red-tshirt
expect(results[0].slug).to.deep.equal(productSlug2) // tshirt
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const productDraftProductType = require('../../resources/productType.json')
* +---------------------------+---------------------------------------------+--------------------+---------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - moving + removing variants, changing productType', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
* +-----------------------------------------+-------------------------+--------------------+-------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - removing variants', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const productTypeDraft2 = _.cloneDeep(require('../../resources/productType.json'
* +---------------------------------------------+---------------------------+--------------------+---------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - removing + moving variants, deleting product', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let productType2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
+-------------------------------------------------+---------------------------+--------------------+---------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - creating new product, promoting masterVariant', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const productTypeDraft2 = _.cloneDeep(require('../../resources/productType.json'
* +-----------------------------------------+--------------------------+--------------------+----------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - merging products with duplicate slugs', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { PRODUCT_ANONYMIZE_SLUG_KEY } from '../../../lib/constants'
* +-----------------------------------------+--------------------------+--------------------+--------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - merging + removing variants', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
* +-------------------------+---------------------------------------------------------------+--------------------+---------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - changing backup variant', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import VariantReassignment from '../../../lib/runner/variant-reassignment'
* +------------------------------------------------------------+-------------------------------------------------------------------------+--------------------+------------------------------------------------------------+
*/
/* eslint-enable max-len */
describe('Variant reassignment', () => {
describe('Variant reassignment - handling sameForAll attributes with blacklist', () => {
const logger = utils.createLogger(__filename)
let ctpClient
let product1
Expand Down

0 comments on commit 24dee09

Please sign in to comment.