Skip to content

Commit

Permalink
fix: filter out unchanged taxrates from changed list (#1914)
Browse files Browse the repository at this point in the history
* fix: filter out unchanged taxrates from changed list

* fix: remove typename from subrates as well

* refactor: extract funtion to utils

* Create honest-hats-push.md

* test: unchanged taxrates on actions
  • Loading branch information
antoniolodias authored Sep 19, 2024
1 parent 1947ded commit 6e1195e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-hats-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@commercetools/sync-actions": patch
---

fix: filter out unchanged taxrates from changed list
30 changes: 24 additions & 6 deletions packages/sync-actions/src/tax-categories-actions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { deepEqual } from 'fast-equals'
import { buildBaseAttributesActions } from './utils/common-actions'
import createBuildArrayActions, {
ADD_ACTIONS,
REMOVE_ACTIONS,
CHANGE_ACTIONS,
} from './utils/create-build-array-actions'
import removeTypename from './utils/remove-typename'

export const baseActionsList = [
{ action: 'changeName', key: 'name' },
Expand Down Expand Up @@ -31,12 +33,28 @@ export function actionsMapRates(diff, oldObj, newObj) {
action: 'removeTaxRate',
taxRateId: objectToRemove.id,
}),
[CHANGE_ACTIONS]: (oldObject, updatedObject) => ({
action: 'replaceTaxRate',
taxRateId:
oldObject.id === updatedObject.id ? oldObject.id : updatedObject.id,
taxRate: updatedObject,
}),
[CHANGE_ACTIONS]: (oldObject, updatedObject) => {
// filter out taxRates that were not changed
// so the API doesn't return it with a different id
// we need to remove __typename from the object to compare them
const taxCategoryWithoutTypeName = removeTypename(oldObject)
const oldObjectSubRatesWithoutTypename =
oldObject.subRates?.map(removeTypename)

const oldObjectWithoutTypename = {
...taxCategoryWithoutTypeName,
subRates: oldObjectSubRatesWithoutTypename,
}

if (deepEqual(oldObjectWithoutTypename, updatedObject)) return null

return {
action: 'replaceTaxRate',
taxRateId:
oldObject.id === updatedObject.id ? oldObject.id : updatedObject.id,
taxRate: updatedObject,
}
},
})

return handler(diff, oldObj, newObj)
Expand Down
4 changes: 4 additions & 0 deletions packages/sync-actions/src/utils/remove-typename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function removeTypename(obj) {
const { __typename, ...objWithoutTypename } = obj
return objWithoutTypename
}
8 changes: 8 additions & 0 deletions packages/sync-actions/test/tax-categories-sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ describe('Actions', () => {
})

test('should build `replaceTaxRate` action', () => {
// unchanged tax rates should not be included in the actions
const unchangedTaxRate = {
id: 'taxRate-2',
name: '1% DE',
amount: '0.01',
}
const before = {
rates: [
{
id: 'taxRate-1',
name: '5% US',
amount: '0.05',
},
unchangedTaxRate,
],
}
const now = {
Expand All @@ -80,6 +87,7 @@ describe('Actions', () => {
name: '11% US',
amount: '0.11',
},
unchangedTaxRate,
],
}

Expand Down

0 comments on commit 6e1195e

Please sign in to comment.