From 6e1195ed05cc3338eac94e47f439f10b7b0e6e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Dias?= <37903776+antoniolodias@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:22:59 +0100 Subject: [PATCH] fix: filter out unchanged taxrates from changed list (#1914) * 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 --- .changeset/honest-hats-push.md | 5 ++++ .../src/tax-categories-actions.js | 30 +++++++++++++++---- .../sync-actions/src/utils/remove-typename.js | 4 +++ .../test/tax-categories-sync.spec.js | 8 +++++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 .changeset/honest-hats-push.md create mode 100644 packages/sync-actions/src/utils/remove-typename.js diff --git a/.changeset/honest-hats-push.md b/.changeset/honest-hats-push.md new file mode 100644 index 000000000..e35b79a6a --- /dev/null +++ b/.changeset/honest-hats-push.md @@ -0,0 +1,5 @@ +--- +"@commercetools/sync-actions": patch +--- + +fix: filter out unchanged taxrates from changed list diff --git a/packages/sync-actions/src/tax-categories-actions.js b/packages/sync-actions/src/tax-categories-actions.js index 776a76a57..24f4956f4 100644 --- a/packages/sync-actions/src/tax-categories-actions.js +++ b/packages/sync-actions/src/tax-categories-actions.js @@ -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' }, @@ -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) diff --git a/packages/sync-actions/src/utils/remove-typename.js b/packages/sync-actions/src/utils/remove-typename.js new file mode 100644 index 000000000..9ad80bbe6 --- /dev/null +++ b/packages/sync-actions/src/utils/remove-typename.js @@ -0,0 +1,4 @@ +export default function removeTypename(obj) { + const { __typename, ...objWithoutTypename } = obj + return objWithoutTypename +} diff --git a/packages/sync-actions/test/tax-categories-sync.spec.js b/packages/sync-actions/test/tax-categories-sync.spec.js index 9a1afda97..3cfd3cd39 100644 --- a/packages/sync-actions/test/tax-categories-sync.spec.js +++ b/packages/sync-actions/test/tax-categories-sync.spec.js @@ -64,6 +64,12 @@ 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: [ { @@ -71,6 +77,7 @@ describe('Actions', () => { name: '5% US', amount: '0.05', }, + unchangedTaxRate, ], } const now = { @@ -80,6 +87,7 @@ describe('Actions', () => { name: '11% US', amount: '0.11', }, + unchangedTaxRate, ], }