Skip to content

Commit 4bb8f97

Browse files
authored
BMU-401 feat(sync-actions): type entity, support new actions (#1853)
* feat(sync-actions): type entity, support , , actions * ci(sync-actions): add changeset
1 parent 3f986fe commit 4bb8f97

4 files changed

Lines changed: 391 additions & 9 deletions

File tree

.changeset/rich-taxis-shake.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+
types sync-actions: support the following actions `changeInputHint`, `changeEnumValueLabel`, `changeLocalizedEnumValueLabel`.

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import forEach from 'lodash.foreach'
22
import flatten from 'lodash.flatten'
33
import sortBy from 'lodash.sortby'
44

5+
import isEqual from 'lodash.isequal'
56
import createBuildArrayActions, {
67
ADD_ACTIONS,
78
CHANGE_ACTIONS,
@@ -40,6 +41,10 @@ function actionsMapEnums(
4041
) {
4142
const addEnumActionName =
4243
attributeType === 'Enum' ? 'addEnumValue' : 'addLocalizedEnumValue'
44+
const changeEnumValueLabelActionName =
45+
attributeType === 'Enum'
46+
? 'changeEnumValueLabel'
47+
: 'changeLocalizedEnumValueLabel'
4348
const changeEnumOrderActionName =
4449
attributeType === 'Enum'
4550
? 'changeEnumValueOrder'
@@ -61,13 +66,31 @@ function actionsMapEnums(
6166
// to the client.
6267
const changeActions = []
6368
if (oldEnumInNext) {
64-
// todo check if action is relevant, there is no action with this structure
65-
// https://docs.commercetools.com/api/projects/types#update-actions
66-
changeActions.push({
67-
fieldName,
68-
action: changeEnumOrderActionName,
69-
value: newEnum,
70-
})
69+
// If the enum value is changed, we need to change the order first
70+
const isKeyChanged = oldEnum.key !== newEnum.key
71+
72+
// check if the label is changed
73+
const foundPreviousEnum = previous.values.find(
74+
(previousEnum) => previousEnum.key === newEnum.key
75+
)
76+
const isLabelEqual = isEqual(foundPreviousEnum.label, newEnum.label)
77+
78+
if (isKeyChanged) {
79+
// these actions is then flatten in the end
80+
changeActions.push({
81+
fieldName,
82+
action: changeEnumOrderActionName,
83+
value: newEnum,
84+
})
85+
}
86+
87+
if (!isLabelEqual) {
88+
changeActions.push({
89+
fieldName,
90+
action: changeEnumValueLabelActionName,
91+
value: newEnum,
92+
})
93+
}
7194
} else {
7295
changeActions.push({
7396
fieldName,
@@ -82,12 +105,12 @@ function actionsMapEnums(
82105
const actions = []
83106
// following lists are necessary to ensure that when we change the
84107
// order of enumValues, we generate one updateAction instead of one at a time.
85-
const newEnumValuesOrder = []
108+
let newEnumValuesOrder = []
86109

87110
flatten(buildArrayActions(attributeDiff, previous, next)).forEach(
88111
(updateAction) => {
89112
if (updateAction.action === changeEnumOrderActionName) {
90-
newEnumValuesOrder.push(updateAction.value.key)
113+
newEnumValuesOrder = next.values.map((enumValue) => enumValue.key)
91114
} else actions.push(updateAction)
92115
}
93116
)
@@ -136,6 +159,12 @@ export function actionsMapFieldDefinitions(
136159
label: extractedPairs.newObj.label,
137160
fieldName: extractedPairs.oldObj.name,
138161
})
162+
} else if (diffValue.inputHint) {
163+
actions.push({
164+
action: 'changeInputHint',
165+
inputHint: extractedPairs.newObj.inputHint,
166+
fieldName: extractedPairs.oldObj.name,
167+
})
139168
} else if (diffValue?.type?.values) {
140169
actions.push(
141170
...actionsMapEnums(

0 commit comments

Comments
 (0)