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

feat(sync-actions): adding support for changeAssetOrder in ProductVariants #1885

Merged
merged 2 commits into from
Sep 26, 2023
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/tender-wasps-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/sync-actions': minor
---

Add support for 'changeAssetOrder' in (ProductVariants)[https://docs.commercetools.com/api/projects/products#change-asset-order].
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,3 @@ We'd love to have your helping hand on this ecosystem! Please see [CONTRIBUTING.
[personal-data-erasure-icon]: https://img.shields.io/npm/v/@commercetools/personal-data-erasure.svg?style=flat-square
[personal-data-erasure-dependencies]: https://david-dm.org/commercetools/nodejs?path=packages/personal-data-erasure
[personal-data-erasure-dependencies-icon]: https://img.shields.io/david/commercetools/nodejs.svg?path=packages/personal-data-erasure&style=flat-square

9 changes: 2 additions & 7 deletions docs/sdk/api/sdkMiddlewareHttp.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The HTTP middleware can run in either a browser or Node.js environment. For Node
13. `fetch` _(Function)_: A `fetch` implementation which can be e.g. `node-fetch` or `unfetch` but also the native browser `fetch` function
14. `timeout` _(Number)_: Request/response timeout in ms. Must be globally available or passed in `AbortController`
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.
16. `retryConfig` _(Object)_: Field required in the object listed below
16. `retryConfig` _(Object)_: Field required in the object listed below

#### Retrying requests

Expand Down Expand Up @@ -73,12 +73,7 @@ const client = createClient({
retryDelay: 300, //milliseconds
maxDelay: 5000, //milliseconds
retryOnAbort: false,
retryCodes: [
504,
'ETIMEDOUT',
'ECONNREFUSED',
503
]
retryCodes: [504, 'ETIMEDOUT', 'ECONNREFUSED', 503],
},

// Optional if not globally available
Expand Down
4 changes: 3 additions & 1 deletion packages/sync-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"lodash.isnil": "^4.0.0",
"lodash.shuffle": "^4.2.0",
"lodash.sortby": "^4.7.0",
"lodash.uniqwith": "^4.5.0"
"lodash.uniqwith": "^4.5.0",
"lodash.intersection": "^4.4.0",
"lodash.without": "^4.4.0"
}
}
36 changes: 35 additions & 1 deletion packages/sync-actions/src/product-actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable max-len */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This File

import forEach from 'lodash.foreach'
import uniqWith from 'lodash.uniqwith'
import intersection from 'lodash.intersection'
import without from 'lodash.without'
Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

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

would avoid lodash, yep its clean but it had performance cost, there were old PR removed some lodash cause it were impacting performance, but tbh that would do the job for now 😉

import * as diffpatcher from './utils/diffpatcher'
import extractMatchingPairs from './utils/extract-matching-pairs'
import actionsMapCustom from './utils/action-map-custom'
Expand Down Expand Up @@ -58,6 +60,9 @@ const getIsUpdateAction = (key, resource) =>
const getIsRemoveAction = (key, resource) =>
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0

const getIsItemMovedAction = (key, resource) =>
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 3

function _buildSkuActions(variantDiff, oldVariant) {
if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
const newValue = diffpatcher.getDeltaValue(variantDiff.sku)
Expand Down Expand Up @@ -401,6 +406,30 @@ function toVariantIdentifier(variant) {
return id ? { variantId: id } : { sku }
}

function _buildVariantChangeAssetOrderAction(
diffAssets,
oldVariant,
newVariant
) {
const isAssetOrderChanged = Object.entries(diffAssets).find((entry) =>
getIsItemMovedAction(entry[0], entry[1])
)
if (!isAssetOrderChanged) {
return []
}
const assetIdsBefore = oldVariant.assets.map((_) => _.id)
const assetIdsCurrent = newVariant.assets
.map((_) => _.id)
.filter((_) => _ !== undefined)
const assetIdsToKeep = intersection(assetIdsCurrent, assetIdsBefore)
const assetIdsToRemove = without(assetIdsBefore, ...assetIdsToKeep)
const changeAssetOrderAction = {
action: 'changeAssetOrder',
assetOrder: assetIdsToKeep.concat(assetIdsToRemove),
...toVariantIdentifier(oldVariant),
}
return [changeAssetOrderAction]
}
function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
const assetActions = []

Expand Down Expand Up @@ -478,7 +507,12 @@ function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
}
})

return assetActions
const changedAssetOrderAction = _buildVariantChangeAssetOrderAction(
diffAssets,
oldVariant,
newVariant
)
return [...changedAssetOrderAction, ...assetActions]
Copy link
Member

Choose a reason for hiding this comment

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

that brilliant 👍

}

/**
Expand Down
15 changes: 8 additions & 7 deletions packages/sync-actions/src/product-types-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ const generateUpdateActionsForAttributeEnumValues = (
...Object.values(
removedAttributeEnumValues.reduce(
(nextEnumUpdateActions, removedAttributeEnumValue) => {
const removedAttributeEnumValueOfSameAttributeName = nextEnumUpdateActions[
removedAttributeEnumValue.hint.attributeName
] || {
keys: [],
attributeName: removedAttributeEnumValue.hint.attributeName,
action: 'removeEnumValues',
}
const removedAttributeEnumValueOfSameAttributeName =
nextEnumUpdateActions[
removedAttributeEnumValue.hint.attributeName
] || {
keys: [],
attributeName: removedAttributeEnumValue.hint.attributeName,
action: 'removeEnumValues',
}
return {
...nextEnumUpdateActions,
[removedAttributeEnumValue.hint.attributeName]: {
Expand Down
2 changes: 1 addition & 1 deletion packages/sync-actions/src/quote-requests-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { buildBaseAttributesActions } from './utils/common-actions'

export const baseActionsList = [
{ action: 'changeQuoteRequestState', key: 'quoteRequestState' },
{ action: 'transitionState', key: 'state'},
{ action: 'transitionState', key: 'state' },
]

export function actionsMapBase(diff, oldObj, newObj, config = {}) {
Expand Down
17 changes: 7 additions & 10 deletions packages/sync-actions/src/quote-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
import * as QuoteRequestsActions from './quote-requests-actions'
import * as diffpatcher from './utils/diffpatcher'

const actionGroups = [
'base',
'custom',
]
const actionGroups = ['base', 'custom']

function createQuoteRequestsMapActions(
mapActionGroup: Function,
Expand All @@ -29,7 +26,7 @@ function createQuoteRequestsMapActions(
return function doMapActions(
diff: Object,
newObj: Object,
oldObj: Object,
oldObj: Object
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we remove the , here? Is is some auto-formatting regression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the "put a comma" rule only applies to objects and not function params

): Array<UpdateAction> {
const allActions = []

Expand Down Expand Up @@ -59,13 +56,13 @@ export default (
syncActionConfig: SyncActionConfig
): SyncAction => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createQuoteRequestsMapActions(mapActionGroup, syncActionConfig)

const buildActions = createBuildActions(
diffpatcher.diff,
doMapActions,
const doMapActions = createQuoteRequestsMapActions(
mapActionGroup,
syncActionConfig
)

const buildActions = createBuildActions(diffpatcher.diff, doMapActions)

return { buildActions }
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sync-actions/src/quotes-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildBaseAttributesActions } from './utils/common-actions'
export const baseActionsList = [
{ action: 'changeQuoteState', key: 'quoteState' },
{ action: 'requestQuoteRenegotiation', key: 'buyerComment' },
{ action: 'transitionState', key: 'state'},
{ action: 'transitionState', key: 'state' },
]

export function actionsMapBase(diff, oldObj, newObj, config = {}) {
Expand Down
19 changes: 4 additions & 15 deletions packages/sync-actions/src/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
import * as QuotesActions from './quotes-actions'
import * as diffpatcher from './utils/diffpatcher'

const actionGroups = [
'base',
'custom',
]
const actionGroups = ['base', 'custom']

function createQuotesMapActions(
mapActionGroup: Function,
Expand All @@ -29,18 +26,13 @@ function createQuotesMapActions(
return function doMapActions(
diff: Object,
newObj: Object,
oldObj: Object,
oldObj: Object
): Array<UpdateAction> {
const allActions = []

allActions.push(
mapActionGroup('base', (): Array<UpdateAction> =>
QuotesActions.actionsMapBase(
diff,
oldObj,
newObj,
syncActionConfig
)
QuotesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig)
)
)

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

const buildActions = createBuildActions(
diffpatcher.diff,
doMapActions,
)
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)

return { buildActions }
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sync-actions/src/staged-quotes-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { buildBaseAttributesActions } from './utils/common-actions'
export const baseActionsList = [
{ action: 'changeStagedQuoteState', key: 'stagedQuoteState' },
{ action: 'setSellerComment', key: 'sellerComment' },
{ action: 'setValidTo', key: 'validTo'},
{ action: 'transitionState', key: 'state'},
{ action: 'setValidTo', key: 'validTo' },
{ action: 'transitionState', key: 'state' },
]

export function actionsMapBase(diff, oldObj, newObj, config = {}) {
Expand Down
17 changes: 7 additions & 10 deletions packages/sync-actions/src/staged-quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
import * as StagedQuotesActions from './staged-quotes-actions'
import * as diffpatcher from './utils/diffpatcher'

const actionGroups = [
'base',
'custom',
]
const actionGroups = ['base', 'custom']

function createStagedQuotesMapActions(
mapActionGroup: Function,
Expand All @@ -29,7 +26,7 @@ function createStagedQuotesMapActions(
return function doMapActions(
diff: Object,
newObj: Object,
oldObj: Object,
oldObj: Object
): Array<UpdateAction> {
const allActions = []

Expand Down Expand Up @@ -59,13 +56,13 @@ export default (
syncActionConfig: SyncActionConfig
): SyncAction => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createStagedQuotesMapActions(mapActionGroup, syncActionConfig)

const buildActions = createBuildActions(
diffpatcher.diff,
doMapActions,
const doMapActions = createStagedQuotesMapActions(
mapActionGroup,
syncActionConfig
)

const buildActions = createBuildActions(diffpatcher.diff, doMapActions)

return { buildActions }
}

Expand Down
21 changes: 7 additions & 14 deletions packages/sync-actions/test/product-sync-images.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,14 @@ describe('Actions', () => {
assets: [],
images: [
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
dimensions: {
w: 1456,
h: 1078,
},
},
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
label: 'cactus',
dimensions: {
w: 602,
Expand Down Expand Up @@ -245,17 +243,15 @@ describe('Actions', () => {
sku: '89978FRU',
images: [
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
label: 'cactus',
dimensions: {
w: 602,
h: 600,
},
},
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
dimensions: {
w: 1456,
h: 1078,
Expand Down Expand Up @@ -353,16 +349,14 @@ describe('Actions', () => {
prices: [],
images: [
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/Screen+Shot+2017-04--LOx1OrZZ.png',
dimensions: {
w: 1456,
h: 1078,
},
},
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
label: 'cactus',
dimensions: {
w: 602,
Expand Down Expand Up @@ -402,8 +396,7 @@ describe('Actions', () => {
prices: [],
images: [
{
url:
'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
url: 'https://95bc80c3c245100a18cc-04fc5bec7ec901344d7cbd57f9a2fab3.ssl.cf3.rackcdn.com/cactus-with-surfboar-BmOeVZEZ.jpg',
label: 'cactus',
dimensions: {
w: 602,
Expand Down
Loading