Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plain-crews-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/sync-actions': minor
---

handle product level attributes when money value is undefined
7 changes: 4 additions & 3 deletions packages/sync-actions/src/product-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ function _buildAttributeValue(
newAttributeValue
) {
let value

if (Array.isArray(diffedValue))
value = diffpatcher.getDeltaValue(diffedValue, oldAttributeValue)
else if (typeof diffedValue === 'string')
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this package already super confusing, please feel free to add some comment or ask AI to add it :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here 😄 -> 9a4c036


let action = {
action: 'setAttribute',
Expand Down Expand Up @@ -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',
Expand Down
31 changes: 31 additions & 0 deletions packages/sync-actions/test/product-sync-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})