diff --git a/.changeset/plain-crews-jog.md b/.changeset/plain-crews-jog.md new file mode 100644 index 000000000..e1414c04e --- /dev/null +++ b/.changeset/plain-crews-jog.md @@ -0,0 +1,5 @@ +--- +'@commercetools/sync-actions': minor +--- + +handle product level attributes when money value is undefined diff --git a/packages/sync-actions/src/product-actions.js b/packages/sync-actions/src/product-actions.js index 2cd6bc99c..f63d61e19 100644 --- a/packages/sync-actions/src/product-actions.js +++ b/packages/sync-actions/src/product-actions.js @@ -97,7 +97,6 @@ function _buildAttributeValue( newAttributeValue ) { let value - if (Array.isArray(diffedValue)) value = diffpatcher.getDeltaValue(diffedValue, oldAttributeValue) else if (typeof diffedValue === 'string') @@ -175,7 +174,8 @@ function _buildSetAttributeAction( attribute, sameForAllAttributeNames ) { - if (!attribute) return undefined + // in the case of diffedValue being null or undefined, _buildAttributeValue will fail. + if (!attribute || !diffedValue) return undefined let action = { action: 'setAttribute', @@ -219,7 +219,8 @@ function _buildSetProductAttributeAction( oldProductData, newAttribute ) { - if (!newAttribute) return undefined + // in the case of diffedValue being null or undefined, _buildAttributeValue will fail. + if (!newAttribute || !diffedValue) return undefined const action = { action: 'setProductAttribute', diff --git a/packages/sync-actions/test/product-sync-base.spec.js b/packages/sync-actions/test/product-sync-base.spec.js index 0e6035d6c..5c64994e2 100644 --- a/packages/sync-actions/test/product-sync-base.spec.js +++ b/packages/sync-actions/test/product-sync-base.spec.js @@ -837,4 +837,35 @@ describe('Actions', () => { { action: 'setProductAttribute', name: 'indexedAttr', value: 'newValue' }, ]) }) + + test('should not fail when type is not passed', () => { + const before = { + attributes: [ + { + name: 'price', + value: { centAmount: 100, currencyCode: 'USD' }, + }, + ], + } + + const now = { + attributes: [ + { + name: 'price', + value: undefined, + }, + ], + } + const actions = productsSync.buildActions(now, before) + + const expected = [ + { + action: 'setProductAttribute', + name: 'price', + value: now.attributes[0].value, + }, + ] + + expect(actions).toEqual(expected) + }) })