-
Notifications
You must be signed in to change notification settings - Fork 70
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
/* eslint-disable max-len */ | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
@@ -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) | ||
|
@@ -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 = [] | ||
|
||
|
@@ -478,7 +507,12 @@ function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) { | |
} | ||
}) | ||
|
||
return assetActions | ||
const changedAssetOrderAction = _buildVariantChangeAssetOrderAction( | ||
diffAssets, | ||
oldVariant, | ||
newVariant | ||
) | ||
return [...changedAssetOrderAction, ...assetActions] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that brilliant 👍 |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -29,7 +26,7 @@ function createQuoteRequestsMapActions( | |
return function doMapActions( | ||
diff: Object, | ||
newObj: Object, | ||
oldObj: Object, | ||
oldObj: Object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did we remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
||
|
@@ -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 } | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This File