Skip to content

Commit 41fdb40

Browse files
committed
feat(migrate): type product-type sync
1 parent bb14465 commit 41fdb40

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
buildBaseAttributesActions,
44
createIsEmptyValue,
55
} from './utils/common-actions'
6+
import { LocalizedString } from '@commercetools/platform-sdk'
67

78
export const baseActionsList = [
89
{ action: 'changeName', key: 'name' },
@@ -112,8 +113,14 @@ export const generateBaseFieldsUpdateActions = (
112113
)
113114
}
114115

116+
type AttributeDefinition = {
117+
previous?: { name: string } | undefined
118+
next?: { name: string } | undefined
119+
hint?: { attributeName: string; isLocalized: boolean }
120+
}
121+
115122
const generateUpdateActionsForAttributeDefinitions = (
116-
attributeDefinitions = []
123+
attributeDefinitions: Array<AttributeDefinition> = []
117124
) => {
118125
const removedAttributeDefinitions = attributeDefinitions.filter(
119126
(attributeDefinition) =>
@@ -170,8 +177,22 @@ const generateUpdateActionsForAttributeDefinitions = (
170177
})),
171178
]
172179
}
180+
181+
export type AttributeEnumValues = {
182+
previous?: {
183+
key: string
184+
label: LocalizedString | string | undefined
185+
[key: string]: unknown
186+
}
187+
next?: {
188+
key: string
189+
label: LocalizedString | string | undefined
190+
[key: string]: unknown
191+
}
192+
hint?: { attributeName: string; isLocalized: boolean }
193+
}
173194
const generateUpdateActionsForAttributeEnumValues = (
174-
attributeEnumValues = []
195+
attributeEnumValues: Array<AttributeEnumValues> = []
175196
) => {
176197
const removedAttributeEnumValues = attributeEnumValues.filter(
177198
(attributeEnumValue) =>
@@ -295,20 +316,21 @@ const generateChangeAttributeOrderAction = (
295316
return null
296317
}
297318

319+
export type NestedValues = {
320+
attributeDefinitions?: Array<AttributeDefinition>
321+
attributeEnumValues?: Array<AttributeEnumValues>
322+
}
298323
export const actionsMapForHints = (
299-
nestedValuesChanges: {
300-
attributeDefinitions: Array<any>
301-
attributeEnumValues: Array<any>
302-
} = { attributeDefinitions: [], attributeEnumValues: [] },
324+
nestedValuesChanges: NestedValues,
303325
ptOld,
304326
ptNew
305327
) => {
306328
const updateActions = [
307329
...generateUpdateActionsForAttributeDefinitions(
308-
nestedValuesChanges.attributeDefinitions
330+
nestedValuesChanges?.attributeDefinitions
309331
),
310332
...generateUpdateActionsForAttributeEnumValues(
311-
nestedValuesChanges.attributeEnumValues
333+
nestedValuesChanges?.attributeEnumValues
312334
),
313335
]
314336

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SyncAction } from './types/update-actions'
99
import createBuildActions from './utils/create-build-actions'
1010
import createMapActionGroup from './utils/create-map-action-group'
1111
import { diff } from './utils/diffpatcher'
12+
import { NestedValues } from './product-types-actions'
1213

1314
type SyncActionConfig = { withHints?: boolean } & BaseSyncActionConfig
1415

@@ -49,10 +50,14 @@ function createProductTypeMapActions(
4950
}
5051
}
5152

53+
export type ProductTypeConfig = {
54+
nestedValuesChanges: NestedValues
55+
}
56+
5257
export default (
5358
actionGroupList?: Array<ActionGroup>,
5459
syncActionConfig?: SyncActionConfig
55-
): SyncAction<ProductTypeUpdateAction> => {
60+
): SyncAction<ProductTypeUpdateAction, ProductTypeConfig> => {
5661
const mapActionGroup = createMapActionGroup(actionGroupList)
5762
const doMapActions = createProductTypeMapActions(
5863
mapActionGroup,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { UpdateAction } from '@commercetools/sdk-client-v2'
22

3-
export type SyncAction<T extends UpdateAction> = {
4-
buildActions: (now: Object, before: Object) => Array<T>
3+
export type SyncAction<T extends UpdateAction, S = {}> = {
4+
buildActions: (now: any, before: any, config: S) => Array<T>
55
}

packages/sync-actions/test/product-types-sync-attribute-hints.spec.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import createSyncProductTypes from '../src/product-types'
1+
import createSyncProductTypes, { ProductTypeConfig } from '../src/product-types'
2+
import { SyncAction } from '../src/types/update-actions'
3+
import { ProductTypeUpdateAction } from '@commercetools/platform-sdk/src'
4+
import { AttributeEnumValues } from '../src/product-types-actions'
25

3-
const createAttributeDefinitionDraftItem = (custom?) => ({
6+
const createAttributeDefinitionDraftItem = (custom?): AttributeEnumValues => ({
47
previous: {
58
type: { name: 'text' },
69
name: 'attribute-name',
@@ -24,7 +27,9 @@ const createAttributeDefinitionDraftItem = (custom?) => ({
2427
...custom,
2528
})
2629

27-
const createAttributeEnumDraftItem = (custom?) => ({
30+
const createAttributeEnumDraftItem = (
31+
custom?: AttributeEnumValues
32+
): AttributeEnumValues => ({
2833
previous: {
2934
key: 'enum-key',
3035
label: 'enum-label',
@@ -41,13 +46,13 @@ const createAttributeEnumDraftItem = (custom?) => ({
4146
})
4247

4348
describe('product type hints', () => {
44-
let updateActions
45-
let sync
49+
let updateActions: Array<ProductTypeUpdateAction>
50+
let sync: SyncAction<ProductTypeUpdateAction, ProductTypeConfig>
4651
beforeEach(() => {
4752
sync = createSyncProductTypes([])
4853
})
4954
describe('attribute enum values', () => {
50-
let attributeEnumDraftItem
55+
let attributeEnumDraftItem: AttributeEnumValues
5156
describe('with previous', () => {
5257
describe('with no changes', () => {
5358
beforeEach(() => {
@@ -312,6 +317,40 @@ describe('product type hints', () => {
312317
])
313318
})
314319
})
320+
describe('when is truly localized', () => {
321+
beforeEach(() => {
322+
attributeEnumDraftItem = createAttributeEnumDraftItem({
323+
previous: { label: { 'en-GB': 'uk-label' }, key: 'enum-key' },
324+
next: { label: { 'en-GB': 'uk-label-new' }, key: 'enum-key' },
325+
hint: {
326+
// this hint value is used as `attributeName` for enum update actions
327+
attributeName: 'attribute-name',
328+
isLocalized: true,
329+
},
330+
})
331+
updateActions = sync.buildActions(
332+
{},
333+
{},
334+
{
335+
nestedValuesChanges: {
336+
attributeEnumValues: [attributeEnumDraftItem],
337+
},
338+
}
339+
)
340+
})
341+
it('should match snapshot', () => {
342+
expect(updateActions).toMatchSnapshot()
343+
})
344+
it('should generate `addLocalizedEnumValue`', () => {
345+
expect(updateActions).toEqual([
346+
{
347+
action: 'changeLocalizedEnumValueLabel',
348+
attributeName: attributeEnumDraftItem.hint.attributeName,
349+
newValue: attributeEnumDraftItem.next,
350+
},
351+
])
352+
})
353+
})
315354
})
316355
})
317356
describe('attribute hints', () => {

0 commit comments

Comments
 (0)