Skip to content

Commit d6cb274

Browse files
kafismarkus-azer
andauthored
feat(sync-actions): adding support for changeAssetOrder in ProductVariants (#1885)
* feat(sync-actions): adding support for changeAssetOrder in ProductVariants * test(sync-actions): extend test for changeAssetOrder --------- Co-authored-by: Markus Azer <[email protected]>
1 parent d79eb04 commit d6cb274

20 files changed

+256
-141
lines changed

.changeset/tender-wasps-judge.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+
Add support for 'changeAssetOrder' in (ProductVariants)[https://docs.commercetools.com/api/projects/products#change-asset-order].

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,3 @@ We'd love to have your helping hand on this ecosystem! Please see [CONTRIBUTING.
188188
[personal-data-erasure-icon]: https://img.shields.io/npm/v/@commercetools/personal-data-erasure.svg?style=flat-square
189189
[personal-data-erasure-dependencies]: https://david-dm.org/commercetools/nodejs?path=packages/personal-data-erasure
190190
[personal-data-erasure-dependencies-icon]: https://img.shields.io/david/commercetools/nodejs.svg?path=packages/personal-data-erasure&style=flat-square
191-

docs/sdk/api/sdkMiddlewareHttp.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The HTTP middleware can run in either a browser or Node.js environment. For Node
4242
13. `fetch` _(Function)_: A `fetch` implementation which can be e.g. `node-fetch` or `unfetch` but also the native browser `fetch` function
4343
14. `timeout` _(Number)_: Request/response timeout in ms. Must be globally available or passed in `AbortController`
4444
15. `abortController` or `getAbortController` depending on what you chose to handle the timeout (_abortController_): This property accepts the `AbortController` instance. Could be [abort-controller](https://www.npmjs.com/package/abort-controller) or a globally available one.
45-
16. `retryConfig` _(Object)_: Field required in the object listed below
45+
16. `retryConfig` _(Object)_: Field required in the object listed below
4646

4747
#### Retrying requests
4848

@@ -73,12 +73,7 @@ const client = createClient({
7373
retryDelay: 300, //milliseconds
7474
maxDelay: 5000, //milliseconds
7575
retryOnAbort: false,
76-
retryCodes: [
77-
504,
78-
'ETIMEDOUT',
79-
'ECONNREFUSED',
80-
503
81-
]
76+
retryCodes: [504, 'ETIMEDOUT', 'ECONNREFUSED', 503],
8277
},
8378

8479
// Optional if not globally available

packages/sync-actions/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"lodash.isnil": "^4.0.0",
4444
"lodash.shuffle": "^4.2.0",
4545
"lodash.sortby": "^4.7.0",
46-
"lodash.uniqwith": "^4.5.0"
46+
"lodash.uniqwith": "^4.5.0",
47+
"lodash.intersection": "^4.4.0",
48+
"lodash.without": "^4.4.0"
4749
}
4850
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable max-len */
22
import forEach from 'lodash.foreach'
33
import uniqWith from 'lodash.uniqwith'
4+
import intersection from 'lodash.intersection'
5+
import without from 'lodash.without'
46
import * as diffpatcher from './utils/diffpatcher'
57
import extractMatchingPairs from './utils/extract-matching-pairs'
68
import actionsMapCustom from './utils/action-map-custom'
@@ -58,6 +60,9 @@ const getIsUpdateAction = (key, resource) =>
5860
const getIsRemoveAction = (key, resource) =>
5961
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0
6062

63+
const getIsItemMovedAction = (key, resource) =>
64+
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 3
65+
6166
function _buildSkuActions(variantDiff, oldVariant) {
6267
if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
6368
const newValue = diffpatcher.getDeltaValue(variantDiff.sku)
@@ -401,6 +406,30 @@ function toVariantIdentifier(variant) {
401406
return id ? { variantId: id } : { sku }
402407
}
403408

409+
function _buildVariantChangeAssetOrderAction(
410+
diffAssets,
411+
oldVariant,
412+
newVariant
413+
) {
414+
const isAssetOrderChanged = Object.entries(diffAssets).find((entry) =>
415+
getIsItemMovedAction(entry[0], entry[1])
416+
)
417+
if (!isAssetOrderChanged) {
418+
return []
419+
}
420+
const assetIdsBefore = oldVariant.assets.map((_) => _.id)
421+
const assetIdsCurrent = newVariant.assets
422+
.map((_) => _.id)
423+
.filter((_) => _ !== undefined)
424+
const assetIdsToKeep = intersection(assetIdsCurrent, assetIdsBefore)
425+
const assetIdsToRemove = without(assetIdsBefore, ...assetIdsToKeep)
426+
const changeAssetOrderAction = {
427+
action: 'changeAssetOrder',
428+
assetOrder: assetIdsToKeep.concat(assetIdsToRemove),
429+
...toVariantIdentifier(oldVariant),
430+
}
431+
return [changeAssetOrderAction]
432+
}
404433
function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
405434
const assetActions = []
406435

@@ -478,7 +507,12 @@ function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
478507
}
479508
})
480509

481-
return assetActions
510+
const changedAssetOrderAction = _buildVariantChangeAssetOrderAction(
511+
diffAssets,
512+
oldVariant,
513+
newVariant
514+
)
515+
return [...changedAssetOrderAction, ...assetActions]
482516
}
483517

484518
/**

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,14 @@ const generateUpdateActionsForAttributeEnumValues = (
186186
...Object.values(
187187
removedAttributeEnumValues.reduce(
188188
(nextEnumUpdateActions, removedAttributeEnumValue) => {
189-
const removedAttributeEnumValueOfSameAttributeName = nextEnumUpdateActions[
190-
removedAttributeEnumValue.hint.attributeName
191-
] || {
192-
keys: [],
193-
attributeName: removedAttributeEnumValue.hint.attributeName,
194-
action: 'removeEnumValues',
195-
}
189+
const removedAttributeEnumValueOfSameAttributeName =
190+
nextEnumUpdateActions[
191+
removedAttributeEnumValue.hint.attributeName
192+
] || {
193+
keys: [],
194+
attributeName: removedAttributeEnumValue.hint.attributeName,
195+
action: 'removeEnumValues',
196+
}
196197
return {
197198
...nextEnumUpdateActions,
198199
[removedAttributeEnumValue.hint.attributeName]: {

packages/sync-actions/src/quote-requests-actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { buildBaseAttributesActions } from './utils/common-actions'
22

33
export const baseActionsList = [
44
{ action: 'changeQuoteRequestState', key: 'quoteRequestState' },
5-
{ action: 'transitionState', key: 'state'},
5+
{ action: 'transitionState', key: 'state' },
66
]
77

88
export function actionsMapBase(diff, oldObj, newObj, config = {}) {

packages/sync-actions/src/quote-requests.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
1212
import * as QuoteRequestsActions from './quote-requests-actions'
1313
import * as diffpatcher from './utils/diffpatcher'
1414

15-
const actionGroups = [
16-
'base',
17-
'custom',
18-
]
15+
const actionGroups = ['base', 'custom']
1916

2017
function createQuoteRequestsMapActions(
2118
mapActionGroup: Function,
@@ -29,7 +26,7 @@ function createQuoteRequestsMapActions(
2926
return function doMapActions(
3027
diff: Object,
3128
newObj: Object,
32-
oldObj: Object,
29+
oldObj: Object
3330
): Array<UpdateAction> {
3431
const allActions = []
3532

@@ -59,13 +56,13 @@ export default (
5956
syncActionConfig: SyncActionConfig
6057
): SyncAction => {
6158
const mapActionGroup = createMapActionGroup(actionGroupList)
62-
const doMapActions = createQuoteRequestsMapActions(mapActionGroup, syncActionConfig)
63-
64-
const buildActions = createBuildActions(
65-
diffpatcher.diff,
66-
doMapActions,
59+
const doMapActions = createQuoteRequestsMapActions(
60+
mapActionGroup,
61+
syncActionConfig
6762
)
6863

64+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
65+
6966
return { buildActions }
7067
}
7168

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { buildBaseAttributesActions } from './utils/common-actions'
33
export const baseActionsList = [
44
{ action: 'changeQuoteState', key: 'quoteState' },
55
{ action: 'requestQuoteRenegotiation', key: 'buyerComment' },
6-
{ action: 'transitionState', key: 'state'},
6+
{ action: 'transitionState', key: 'state' },
77
]
88

99
export function actionsMapBase(diff, oldObj, newObj, config = {}) {

packages/sync-actions/src/quotes.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
1212
import * as QuotesActions from './quotes-actions'
1313
import * as diffpatcher from './utils/diffpatcher'
1414

15-
const actionGroups = [
16-
'base',
17-
'custom',
18-
]
15+
const actionGroups = ['base', 'custom']
1916

2017
function createQuotesMapActions(
2118
mapActionGroup: Function,
@@ -29,18 +26,13 @@ function createQuotesMapActions(
2926
return function doMapActions(
3027
diff: Object,
3128
newObj: Object,
32-
oldObj: Object,
29+
oldObj: Object
3330
): Array<UpdateAction> {
3431
const allActions = []
3532

3633
allActions.push(
3734
mapActionGroup('base', (): Array<UpdateAction> =>
38-
QuotesActions.actionsMapBase(
39-
diff,
40-
oldObj,
41-
newObj,
42-
syncActionConfig
43-
)
35+
QuotesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig)
4436
)
4537
)
4638

@@ -61,10 +53,7 @@ export default (
6153
const mapActionGroup = createMapActionGroup(actionGroupList)
6254
const doMapActions = createQuotesMapActions(mapActionGroup, syncActionConfig)
6355

64-
const buildActions = createBuildActions(
65-
diffpatcher.diff,
66-
doMapActions,
67-
)
56+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
6857

6958
return { buildActions }
7059
}

0 commit comments

Comments
 (0)