Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync actions #563

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c2b8524
feat(migrate): make compile
phofmann Nov 7, 2023
23ffa13
feat(migrate): run tests
phofmann Nov 7, 2023
26d555f
feat(migrate): remove @flow
phofmann Nov 7, 2023
b61881d
feat(migrate): remove lodash.flatten
phofmann Nov 7, 2023
dd3ffc4
feat(migrate): remove lodash.forEach
phofmann Nov 7, 2023
54b9856
feat(migrate): replace * imports
phofmann Nov 7, 2023
63d7600
feat(migrate): type results
phofmann Nov 7, 2023
324a662
feat(migrate): replace import * as with proper imports
phofmann Nov 7, 2023
f0a5b32
feat(migrate): replace lodash.isequal
phofmann Nov 7, 2023
a378620
feat(migrate): replace lodash.isnil
phofmann Nov 7, 2023
cddcda4
feat(migrate): replace lodash.without
phofmann Nov 7, 2023
cf7d4c1
feat(migrate): replace lodash.intersection
phofmann Nov 7, 2023
eff813f
feat(migrate): replace lodash.shuffle
phofmann Nov 7, 2023
83e9c16
feat(migrate): replace lodash.uniqwith
phofmann Nov 7, 2023
1e20f61
feat(migrate): replace lodash.sortby
phofmann Nov 7, 2023
e8321e1
feat(migrate): type product-type sync
phofmann Nov 8, 2023
e0c47d2
feat(migrate): type tests
phofmann Nov 8, 2023
fe4f143
feat(migrate): types united
phofmann Nov 8, 2023
a9c8834
feat(migrate): imports
phofmann Nov 8, 2023
1c1f7be
feat(migrate): imports
phofmann Nov 8, 2023
b588f4e
feat(migrate): adding types to create-map-action-group.ts
phofmann Nov 9, 2023
afc7774
feat(migrate): more types
phofmann Nov 9, 2023
1ea94c8
feat(migrate): latest version of jsondiffpatch
phofmann Nov 9, 2023
53afc44
chore: latest yarn.lock after rebase
phofmann Aug 23, 2024
eb6438f
feat(migrate): incorporate latest changes
phofmann Aug 23, 2024
86080ed
feat(migrate): incorporate latest changes and added types
phofmann Aug 29, 2024
c18fab6
feat(migrate): types for action
phofmann Aug 29, 2024
121e1a5
feat(fix): project action
phofmann Aug 30, 2024
26cf7d2
feat(fix): introducing types
phofmann Aug 30, 2024
1d0a9a1
feat(fix): introducing types
phofmann Aug 30, 2024
daca08c
feat(fix): introducing types
phofmann Aug 30, 2024
27c17da
feat(chore): removed unused
phofmann Aug 30, 2024
b27864a
feat(fix): proper exports
phofmann Aug 30, 2024
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
155 changes: 155 additions & 0 deletions packages/sync-actions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# @commercetools/sync-actions

## 5.15.0

### Minor Changes

- [#1885](https://github.com/commercetools/nodejs/pull/1885) [`d6cb2740`](https://github.com/commercetools/nodejs/commit/d6cb27401279cb42a49366f32802f8ca8c7f01a3) Thanks [@kafis](https://github.com/kafis)! - Add support for 'changeAssetOrder' in (ProductVariants)[https://docs.commercetools.com/api/projects/products#change-asset-order].

## 5.14.0

### Minor Changes

- [#1876](https://github.com/commercetools/nodejs/pull/1876) [`27f0d2b6`](https://github.com/commercetools/nodejs/commit/27f0d2b66fefbe082b6a27e7fa940b09e7e6088c) Thanks [@jaikumar-tj](https://github.com/jaikumar-tj)! - Add support for attribute groups `changeName`, `setKey`, `setDescription`, `addAttribute` and `removeAttribute` actions.

## 5.13.0

### Minor Changes

- [#1874](https://github.com/commercetools/nodejs/pull/1874) [`69f4501d`](https://github.com/commercetools/nodejs/commit/69f4501dc5401ab2b44f4d3096a978094e402c9f) Thanks [@taylor-knapp](https://github.com/taylor-knapp)! - Handle long text values performantly

## 5.12.2

### Patch Changes

- [#1871](https://github.com/commercetools/nodejs/pull/1871) [`4f8ea39b`](https://github.com/commercetools/nodejs/commit/4f8ea39b66ddd5014ac8f923ed980584bd96290c) Thanks [@ARRIOLALEO](https://github.com/ARRIOLALEO)! - rollback setPriceTiers name change

## 5.12.1

### Patch Changes

- [#1869](https://github.com/commercetools/nodejs/pull/1869) [`7285a9fb`](https://github.com/commercetools/nodejs/commit/7285a9fbcbcfca6a9460e36ba7b58bb30f34fac6) Thanks [@ARRIOLALEO](https://github.com/ARRIOLALEO)! - Add support for StandalonePrice `setPriceTier`

## 5.12.0

### Minor Changes

- [#1863](https://github.com/commercetools/nodejs/pull/1863) [`7ed7a663`](https://github.com/commercetools/nodejs/commit/7ed7a663c1cb3aa87bfb4b4c2c008949a66a62e0) Thanks [@ragafus](https://github.com/ragafus)! - Add support for StandalonePrice `setKey`, `setValidFrom`, `setValidUntil`, `setValidFromAndUntil` and `changeActive` actions.

## 5.11.0

### Minor Changes

- [#1864](https://github.com/commercetools/nodejs/pull/1864) [`91f6b617`](https://github.com/commercetools/nodejs/commit/91f6b61794e7d66766097965e452e14c85e40f14) Thanks [@ARRIOLALEO](https://github.com/ARRIOLALEO)! - Add support for StandalonePrice `setPriceTiers`

## 5.10.0

### Minor Changes

- [#1856](https://github.com/commercetools/nodejs/pull/1856) [`9a3e3711`](https://github.com/commercetools/nodejs/commit/9a3e3711bf6594deafb5d54a9ce9e32450f9c4d6) Thanks [@qmateub](https://github.com/qmateub)! - orders sync-actions: support action on delivery items `setDeliveryItems`

## 5.9.0

### Minor Changes

- [#1853](https://github.com/commercetools/nodejs/pull/1853) [`4bb8f979`](https://github.com/commercetools/nodejs/commit/4bb8f979c317bbce1654ca0f1abc9b4717fdda0b) Thanks [@markus-azer](https://github.com/markus-azer)! - types sync-actions: support the following actions `changeInputHint`, `changeEnumValueLabel`, `changeLocalizedEnumValueLabel`.

## 5.8.0

### Minor Changes

- [#1852](https://github.com/commercetools/nodejs/pull/1852) [`94a376c8`](https://github.com/commercetools/nodejs/commit/94a376c89525b7cee58b710154ddf7cb146cd16c) Thanks [@markus-azer](https://github.com/markus-azer)! - types sync-actions: fix action structure for changeFieldDefinitionOrder
fix internal type sync error by adding optional chaining

## 5.7.0

### Minor Changes

- [#1850](https://github.com/commercetools/nodejs/pull/1850) [`330cd9a9`](https://github.com/commercetools/nodejs/commit/330cd9a9b4fca045d479d2d220d2a2a2b966b1f4) Thanks [@markus-azer](https://github.com/markus-azer)! - types sync-actions: fix action structure for changeLocalizedEnumValueOrder, changeEnumValueOrder

## 5.6.0

### Minor Changes

- [#1844](https://github.com/commercetools/nodejs/pull/1844) [`23f0529b`](https://github.com/commercetools/nodejs/commit/23f0529bbf359a11500dbf87bdc9e59cb759c89a) Thanks [@markus-azer](https://github.com/markus-azer)! - Add localizedName action to shipping methods

## 5.5.0

### Minor Changes

- [#1841](https://github.com/commercetools/nodejs/pull/1841) [`b90c7238`](https://github.com/commercetools/nodejs/commit/b90c7238f0d3d892e1066fd2883cff062b099e66) Thanks [@Rombelirk](https://github.com/Rombelirk)! - Add Custom Fields to Shipping Methods.

## 5.4.1

### Patch Changes

- [#1839](https://github.com/commercetools/nodejs/pull/1839) [`d6cadcbc`](https://github.com/commercetools/nodejs/commit/d6cadcbc4b850fa6f438b65c3b63b294a32a58ee) Thanks [@tdeekens](https://github.com/tdeekens)! - Fix failing to sync froozen arrays for prices

## 5.4.0

### Minor Changes

- [#1836](https://github.com/commercetools/nodejs/pull/1836) [`ad34d030`](https://github.com/commercetools/nodejs/commit/ad34d03041e7e6b8284da6224dc968fde537a85a) Thanks [@nicolasnieto92](https://github.com/nicolasnieto92)! - Add setAuthenticationMode sync action

## 5.3.1

### Patch Changes

- [#1818](https://github.com/commercetools/nodejs/pull/1818) [`856929e3`](https://github.com/commercetools/nodejs/commit/856929e3bc176021a9b52e1ff9c888e51c83cccd) Thanks [@qmateub](https://github.com/qmateub)! - fix(sync-actions/orders): adjust diff calculation of returnInfo items

## 5.3.0

### Minor Changes

- [#1820](https://github.com/commercetools/nodejs/pull/1820) [`c3964026`](https://github.com/commercetools/nodejs/commit/c3964026b401cb1c8ae8b581a3fcc4ea692ed3b4) Thanks [@danrleyt](https://github.com/danrleyt)! - Adding support to quote requests and staged quotes

## 5.2.0

### Minor Changes

- [`cad54c42`](https://github.com/commercetools/nodejs/commit/cad54c421e18464ae03fb283a30f2ba2f3f6e46a) Thanks [@qmateub](https://github.com/qmateub)! - feat(sync-actions): improve performance for large arrays comparisons"

## 5.1.0

### Minor Changes

- [#1803](https://github.com/commercetools/nodejs/pull/1803) [`823985ae`](https://github.com/commercetools/nodejs/commit/823985ae67465673c26f296b68681f255230d571) Thanks [@nicolasnieto92](https://github.com/nicolasnieto92)! - Add createSyncStandalonePrices export to index for supporting prices sync actions

## 5.0.0

### Major Changes

- [#1775](https://github.com/commercetools/nodejs/pull/1775) [`35669f30`](https://github.com/commercetools/nodejs/commit/35669f30dbc4b24d59ec3df3f38417b1f2a77837) Thanks [@ajimae](https://github.com/ajimae)! - Drop support for Node `v10` and `v12`. Supported versions now are `v14`, `v16` and `v18`.

## 4.13.0

### Minor Changes

- [#1798](https://github.com/commercetools/nodejs/pull/1798) [`850325d0`](https://github.com/commercetools/nodejs/commit/850325d08603764787c387b2341e4009d0c4f788) Thanks [@markus-azer](https://github.com/markus-azer)! - support standalone prices

## 4.12.0

### Minor Changes

- [#1796](https://github.com/commercetools/nodejs/pull/1796) [`7aaf91cd`](https://github.com/commercetools/nodejs/commit/7aaf91cdecb7c844943369fc137a5356becdba36) Thanks [@VineetKumarKushwaha](https://github.com/VineetKumarKushwaha)! - Fix custom types sync actions to detect addEnumValue action correctly

## 4.11.0

### Minor Changes

- [#1788](https://github.com/commercetools/nodejs/pull/1788) [`f1acfb67`](https://github.com/commercetools/nodejs/commit/f1acfb67708d8253f551481fd65097add48c6686) Thanks [@nicolasnieto92](https://github.com/nicolasnieto92)! - Add setPriceMode sync action for commercetools-importer project

## 4.10.1

### Patch Changes

- [#1770](https://github.com/commercetools/nodejs/pull/1770) [`381d1e1f`](https://github.com/commercetools/nodejs/commit/381d1e1f07cc2705962973e3a48934bf7884e309) Thanks [@mohib0306](https://github.com/mohib0306)! - Fix product selection's name update action. `setName` => `changeName`
Expose `createSyncProductSelections` from `sync-actions` package

## 4.10.0

### Minor Changes

- [#1767](https://github.com/commercetools/nodejs/pull/1767) [`1aef3423`](https://github.com/commercetools/nodejs/commit/1aef3423e96da7f5df20fd5f66ec29146cacee83) Thanks [@mohib0306](https://github.com/mohib0306)! - feat(sync-actions/product-selections): add sync action support for product selections

As product selections are available via the API, the sync-actions package is updated to support generating update actions for product selections.
11 changes: 11 additions & 0 deletions packages/sync-actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# commercetools-sync-actions

Construct API update actions, for usage with `@commercetools/sdk-client`.

https://commercetools.github.io/nodejs/sdk/api/syncActions.html

## Install

```bash
npm install --save @commercetools/sync-actions
```
42 changes: 42 additions & 0 deletions packages/sync-actions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@commercetools/sync-actions",
"version": "1.0.0",
"engines": {
"node": ">=14"
},
"description": "Build API update actions for the commercetools platform.",
"keywords": ["commercetools", "sync", "actions"],
"homepage": "https://commercetools.github.io/nodejs/",
"license": "MIT",
"directories": {
"lib": "lib",
"test": "test"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/commercetools/commercetools-sdk-typescript.git"
},
"bugs": {
"url": "https://github.com/commercetools/commercetools-sdk-typescript/issues"
},
"dependencies": {
"fast-equals": "^2.0.0",
"jsondiffpatch": "^0.5.0"
},
"files": ["dist", "CHANGELOG.md"],
"author": "Nicola Molinari <[email protected]> (https://github.com/emmenko)",
"main": "dist/commercetools-sync-actions.cjs.js",
"module": "dist/commercetools-sync-actions.esm.js",
"browser": {
"./dist/commercetools-sync-actions.cjs.js": "./dist/commercetools-sync-actions.browser.cjs.js",
"./dist/commercetools-sync-actions.esm.js": "./dist/commercetools-sync-actions.browser.esm.js"
},
"scripts": {
"organize_imports": "find src -type f -name '*.ts' | xargs organize-imports-cli",
"postbuild": "yarn organize_imports",
"post_process_generate": "yarn organize_imports"
}
}
70 changes: 70 additions & 0 deletions packages/sync-actions/src/attribute-groups-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { buildBaseAttributesActions } from './utils/common-actions'
import createBuildArrayActions, {
ADD_ACTIONS,
CHANGE_ACTIONS,
REMOVE_ACTIONS,
} from './utils/create-build-array-actions'
import { ActionMap, ActionMapBase } from './utils/create-map-action-group'
import { AttributeReference } from '@commercetools/platform-sdk'
import { UpdateAction } from './types/update-actions'

const hasAttribute = (
attributes: Array<AttributeReference>,
newValue: AttributeReference
) => attributes.some((attribute) => attribute.key === newValue.key)

export const baseActionsList: Array<UpdateAction> = [
{ action: 'changeName', key: 'name' },
{ action: 'setKey', key: 'key' },
{ action: 'setDescription', key: 'description' },
]

export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}

export const actionsMapAttributes: ActionMap = (diff, oldObj, newObj) => {
const handler = createBuildArrayActions('attributes', {
[ADD_ACTIONS]: (newAttribute) => ({
action: 'addAttribute',
attribute: newAttribute,
}),
[REMOVE_ACTIONS]: (oldAttribute) => {
// We only add the action if the attribute is not included in the new object.
return !hasAttribute(newObj.attributes, oldAttribute)
? {
action: 'removeAttribute',
attribute: oldAttribute,
}
: null
},
[CHANGE_ACTIONS]: (oldAttribute, newAttribute) => {
const result = []
// We only remove the attribute in case that the oldAttribute is not
// included in the new object
if (!hasAttribute(newObj.attributes, oldAttribute))
result.push({
action: 'removeAttribute',
attribute: oldAttribute,
})

// We only add the attribute in case that the newAttribute was not
// included in the old object
if (!hasAttribute(oldObj.attributes, newAttribute))
result.push({
action: 'addAttribute',
attribute: newAttribute,
})

return result
},
})

return handler(diff, oldObj, newObj)
}
56 changes: 56 additions & 0 deletions packages/sync-actions/src/attribute-groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
AttributeGroup,
AttributeGroupUpdateAction,
} from '@commercetools/platform-sdk'

import {
actionsMapAttributes,
actionsMapBase,
} from './attribute-groups-actions'
import {
ActionGroup,
SyncAction,
SyncActionConfig,
UpdateAction,
} from './types/update-actions'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup, {
MapAction,
} from './utils/create-map-action-group'
import { diff } from './utils/diffpatcher'

const createAttributeGroupsMapActions: MapAction = (
mapActionGroup,
syncActionConfig
) => {
return function doMapActions(diff, newObj, oldObj) {
const allActions: Array<Array<UpdateAction>> = []
allActions.push(
mapActionGroup('base', () =>
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
)
)
allActions.push(
mapActionGroup('attributes', () =>
actionsMapAttributes(diff, oldObj, newObj)
).flat()
)
return allActions.flat()
}
}

export const createSyncAttributeGroups = (
actionGroupList?: Array<ActionGroup>,
syncActionConfig?: SyncActionConfig
): SyncAction<AttributeGroup, AttributeGroupUpdateAction> => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createAttributeGroupsMapActions(
mapActionGroup,
syncActionConfig
)
const buildActions = createBuildActions<
AttributeGroup,
AttributeGroupUpdateAction
>(diff, doMapActions)
return { buildActions }
}
28 changes: 28 additions & 0 deletions packages/sync-actions/src/cart-discounts-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { buildBaseAttributesActions } from './utils/common-actions'
import { ActionMapBase } from './utils/create-map-action-group'
import { UpdateAction } from './types/update-actions'

export const baseActionsList: Array<UpdateAction> = [
{ action: 'changeIsActive', key: 'isActive' },
{ action: 'changeName', key: 'name' },
{ action: 'changeCartPredicate', key: 'cartPredicate' },
{ action: 'changeSortOrder', key: 'sortOrder' },
{ action: 'changeValue', key: 'value' },
{ action: 'changeRequiresDiscountCode', key: 'requiresDiscountCode' },
{ action: 'changeTarget', key: 'target' },
{ action: 'setDescription', key: 'description' },
{ action: 'setValidFrom', key: 'validFrom' },
{ action: 'setValidUntil', key: 'validUntil' },
{ action: 'changeStackingMode', key: 'stackingMode' },
{ action: 'setKey', key: 'key' },
]

export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}
Loading