Skip to content

Commit a550762

Browse files
fix(products): handle product level attributes when value is undefined (#1975)
* fix(products): handle product level attributes when type is not passed * chore(products): add changeset for handle product level attributes when type is not passed * fix(products): fix undefined value being passed in product level attribute * chore(products): fix changeset * chore(products): add comments
1 parent ddbbfb9 commit a550762

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.changeset/plain-crews-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/sync-actions': minor
3+
---
4+
5+
handle product level attributes when money value is undefined

packages/sync-actions/src/product-actions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ function _buildAttributeValue(
9797
newAttributeValue
9898
) {
9999
let value
100-
101100
if (Array.isArray(diffedValue))
102101
value = diffpatcher.getDeltaValue(diffedValue, oldAttributeValue)
103102
else if (typeof diffedValue === 'string')
@@ -175,7 +174,8 @@ function _buildSetAttributeAction(
175174
attribute,
176175
sameForAllAttributeNames
177176
) {
178-
if (!attribute) return undefined
177+
// in the case of diffedValue being null or undefined, _buildAttributeValue will fail.
178+
if (!attribute || !diffedValue) return undefined
179179

180180
let action = {
181181
action: 'setAttribute',
@@ -219,7 +219,8 @@ function _buildSetProductAttributeAction(
219219
oldProductData,
220220
newAttribute
221221
) {
222-
if (!newAttribute) return undefined
222+
// in the case of diffedValue being null or undefined, _buildAttributeValue will fail.
223+
if (!newAttribute || !diffedValue) return undefined
223224

224225
const action = {
225226
action: 'setProductAttribute',

packages/sync-actions/test/product-sync-base.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,35 @@ describe('Actions', () => {
837837
{ action: 'setProductAttribute', name: 'indexedAttr', value: 'newValue' },
838838
])
839839
})
840+
841+
test('should not fail when type is not passed', () => {
842+
const before = {
843+
attributes: [
844+
{
845+
name: 'price',
846+
value: { centAmount: 100, currencyCode: 'USD' },
847+
},
848+
],
849+
}
850+
851+
const now = {
852+
attributes: [
853+
{
854+
name: 'price',
855+
value: undefined,
856+
},
857+
],
858+
}
859+
const actions = productsSync.buildActions(now, before)
860+
861+
const expected = [
862+
{
863+
action: 'setProductAttribute',
864+
name: 'price',
865+
value: now.attributes[0].value,
866+
},
867+
]
868+
869+
expect(actions).toEqual(expected)
870+
})
840871
})

0 commit comments

Comments
 (0)