Skip to content

Commit 00fe639

Browse files
committed
feat(sync-actions): asset actions, improve code
1 parent 33472aa commit 00fe639

File tree

1 file changed

+111
-96
lines changed

1 file changed

+111
-96
lines changed

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

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export const referenceActionsList = [
4848
* HELPER FUNCTIONS
4949
*/
5050

51+
const addAction = (key, resource) =>
52+
REGEX_NUMBER.test(key) && Array.isArray(resource) && resource.length
53+
54+
const updateAction = (key, resource) =>
55+
REGEX_NUMBER.test(key) && Object.keys(resource).length
56+
57+
const removeAction = (key, resource) =>
58+
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0
59+
5160
function _buildSkuActions(variantDiff, oldVariant) {
5261
if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
5362
const newValue = diffpatcher.getDeltaValue(variantDiff.sku)
@@ -263,45 +272,48 @@ function _buildVariantPricesAction(
263272
oldVariant.prices,
264273
newVariant.prices
265274
)
266-
if (REGEX_NUMBER.test(key)) {
267-
if (Array.isArray(price) && price.length) {
268-
// Remove read-only fields
269-
const patchedPrice = price.map((p) => {
270-
const shallowClone = { ...p }
271-
if (enableDiscounted !== true) delete shallowClone.discounted
272-
return shallowClone
273-
})
275+
if (addAction(key, price)) {
276+
// Remove read-only fields
277+
const patchedPrice = price.map((p) => {
278+
const shallowClone = { ...p }
279+
if (enableDiscounted !== true) delete shallowClone.discounted
280+
return shallowClone
281+
})
282+
283+
addPriceActions.push({
284+
action: 'addPrice',
285+
variantId: oldVariant.id,
286+
price: diffpatcher.getDeltaValue(patchedPrice),
287+
})
288+
return
289+
}
274290

275-
addPriceActions.push({
276-
action: 'addPrice',
277-
variantId: oldVariant.id,
278-
price: diffpatcher.getDeltaValue(patchedPrice),
279-
})
280-
} else if (Object.keys(price).length) {
281-
// Remove the discounted field and make sure that the price
282-
// still has other values, otherwise simply return
283-
const filteredPrice = { ...price }
284-
if (enableDiscounted !== true) delete filteredPrice.discounted
285-
if (Object.keys(filteredPrice).length) {
286-
// At this point price should have changed, simply pick the new one
287-
const newPrice = { ...newObj }
288-
if (enableDiscounted !== true) delete newPrice.discounted
289-
290-
changePriceActions.push({
291-
action: 'changePrice',
292-
priceId: oldObj.id,
293-
price: newPrice,
294-
})
295-
}
296-
}
297-
} else if (REGEX_UNDERSCORE_NUMBER.test(key))
298-
if (Number(price[2]) === 0) {
299-
// price removed
300-
removePriceActions.push({
301-
action: 'removePrice',
291+
if (updateAction(key, price)) {
292+
// Remove the discounted field and make sure that the price
293+
// still has other values, otherwise simply return
294+
const filteredPrice = { ...price }
295+
if (enableDiscounted !== true) delete filteredPrice.discounted
296+
if (Object.keys(filteredPrice).length) {
297+
// At this point price should have changed, simply pick the new one
298+
const newPrice = { ...newObj }
299+
if (enableDiscounted !== true) delete newPrice.discounted
300+
301+
changePriceActions.push({
302+
action: 'changePrice',
302303
priceId: oldObj.id,
304+
price: newPrice,
303305
})
304306
}
307+
return
308+
}
309+
310+
if (removeAction(key, price)) {
311+
// price removed
312+
removePriceActions.push({
313+
action: 'removePrice',
314+
priceId: oldObj.id,
315+
})
316+
}
305317
})
306318

307319
return [addPriceActions, changePriceActions, removePriceActions]
@@ -389,11 +401,9 @@ function toVariantIdentifier(variant) {
389401
}
390402

391403
function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
392-
const addAssetActions = []
393-
let changeAssetActions = []
394-
const removeAssetActions = []
404+
const assetActions = []
395405

396-
// generate a hashMap to be able to reference the right image from both ends
406+
// generate a hashMap to be able to reference the right asset from both ends
397407
const matchingAssetPairs = findMatchingPairs(
398408
diffAssets,
399409
oldVariant.assets,
@@ -407,62 +417,67 @@ function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
407417
oldVariant.assets,
408418
newVariant.assets
409419
)
410-
if (REGEX_NUMBER.test(key)) {
411-
if (Array.isArray(asset) && asset.length) {
412-
addAssetActions.push({
413-
action: 'addAsset',
414-
asset: diffpatcher.getDeltaValue(asset),
415-
...toVariantIdentifier(newVariant),
416-
position: Number(key),
417-
})
418-
} else if (Object.keys(asset).length) {
419-
// todo add changeAssetOrder
420-
const basicActions = buildBaseAttributesActions({
421-
actions: baseAssetActionsList,
422-
diff: asset,
423-
oldObj: oldAsset,
424-
newObj: newAsset,
425-
}).map((action) => {
426-
if (action.action === 'setAssetKey') {
427-
return {
428-
...action,
429-
...toVariantIdentifier(oldVariant),
430-
assetId: oldAsset.id,
431-
}
432-
}
433420

421+
if (addAction(key, asset)) {
422+
assetActions.push({
423+
action: 'addAsset',
424+
asset: diffpatcher.getDeltaValue(asset),
425+
...toVariantIdentifier(newVariant),
426+
position: Number(key),
427+
})
428+
return
429+
}
430+
431+
if (updateAction(key, asset)) {
432+
// todo add changeAssetOrder
433+
const basicActions = buildBaseAttributesActions({
434+
actions: baseAssetActionsList,
435+
diff: asset,
436+
oldObj: oldAsset,
437+
newObj: newAsset,
438+
}).map((action) => {
439+
// in case of 'setAssetKey' then the identifier will be only 'assetId'
440+
if (action.action === 'setAssetKey') {
434441
return {
435442
...action,
436443
...toVariantIdentifier(oldVariant),
437-
...toAssetIdentifier(oldAsset),
444+
assetId: oldAsset.id,
438445
}
439-
})
440-
changeAssetActions = changeAssetActions.concat(basicActions)
441-
442-
if (asset.custom) {
443-
const customActions = actionsMapCustom(asset, newAsset, oldAsset, {
444-
actions: {
445-
setCustomType: 'setAssetCustomType',
446-
setCustomField: 'setAssetCustomField',
447-
},
448-
...toVariantIdentifier(oldVariant),
449-
...toAssetIdentifier(oldAsset),
450-
})
451-
changeAssetActions = changeAssetActions.concat(customActions)
452446
}
453-
}
454-
} else if (REGEX_UNDERSCORE_NUMBER.test(key))
455-
if (Number(asset[2]) === 0) {
456-
// asset removed
457-
removeAssetActions.push({
458-
action: 'removeAsset',
447+
448+
return {
449+
...action,
450+
...toVariantIdentifier(oldVariant),
459451
...toAssetIdentifier(oldAsset),
452+
}
453+
})
454+
assetActions.push(...basicActions)
455+
456+
if (asset.custom) {
457+
const customActions = actionsMapCustom(asset, newAsset, oldAsset, {
458+
actions: {
459+
setCustomType: 'setAssetCustomType',
460+
setCustomField: 'setAssetCustomField',
461+
},
460462
...toVariantIdentifier(oldVariant),
463+
...toAssetIdentifier(oldAsset),
461464
})
465+
assetActions.push(...customActions)
462466
}
467+
468+
return
469+
}
470+
471+
if (removeAction(key, asset)) {
472+
assetActions.push({
473+
action: 'removeAsset',
474+
...toAssetIdentifier(oldAsset),
475+
...toVariantIdentifier(oldVariant),
476+
})
477+
}
463478
})
464479

465-
return [addAssetActions, changeAssetActions, removeAssetActions]
480+
return assetActions
466481
}
467482

468483
/**
@@ -572,9 +587,7 @@ export function actionsMapCategoryOrderHints(diff) {
572587
}
573588

574589
export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
575-
let addAssetActions = []
576-
let changeAssetActions = []
577-
let removeAssetActions = []
590+
let allAssetsActions = []
578591

579592
const { variants } = diff
580593

@@ -590,20 +603,17 @@ export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
590603
variant.assets &&
591604
(REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key))
592605
) {
593-
const [a, c, r] = _buildVariantAssetsActions(
606+
const assetActions = _buildVariantAssetsActions(
594607
variant.assets,
595608
oldVariant,
596609
newVariant
597610
)
598611

599-
// add if (assetActions)
600-
addAssetActions = addAssetActions.concat(a)
601-
changeAssetActions = changeAssetActions.concat(c)
602-
removeAssetActions = removeAssetActions.concat(r)
612+
allAssetsActions = allAssetsActions.concat(assetActions)
603613
}
604614
})
605615

606-
return changeAssetActions.concat(removeAssetActions).concat(addAssetActions)
616+
return allAssetsActions
607617
}
608618

609619
export function actionsMapAttributes(
@@ -697,19 +707,24 @@ export function actionsMapPrices(
697707
newObj.variants
698708
)
699709
if (REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key)) {
700-
const [a, c, r] = _buildVariantPricesAction(
710+
const [
711+
addPriceAction,
712+
changePriceAction,
713+
removePriceAction,
714+
] = _buildVariantPricesAction(
701715
variant.prices,
702716
oldVariant,
703717
newVariant,
704718
enableDiscounted
705719
)
706720

707-
addPriceActions = addPriceActions.concat(a)
708-
changePriceActions = changePriceActions.concat(c)
709-
removePriceActions = removePriceActions.concat(r)
721+
addPriceActions = addPriceActions.concat(addPriceAction)
722+
changePriceActions = changePriceActions.concat(changePriceAction)
723+
removePriceActions = removePriceActions.concat(removePriceAction)
710724
}
711725
})
712726

727+
// price actions need to be in this below order
713728
return changePriceActions.concat(removePriceActions).concat(addPriceActions)
714729
}
715730

0 commit comments

Comments
 (0)