Skip to content

Commit

Permalink
feat(migrate): more types
Browse files Browse the repository at this point in the history
  • Loading branch information
phofmann committed Nov 9, 2023
1 parent 8bebbbd commit be1f6e0
Show file tree
Hide file tree
Showing 61 changed files with 851 additions and 840 deletions.
41 changes: 0 additions & 41 deletions packages/sync-actions/src/assets-actions.ts

This file was deleted.

22 changes: 11 additions & 11 deletions packages/sync-actions/src/attribute-groups-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ import createBuildArrayActions, {
CHANGE_ACTIONS,
REMOVE_ACTIONS,
} from './utils/create-build-array-actions'
import { UpdateAction } from '@commercetools/sdk-client-v2'
import { ActionMapBase } from './utils/create-map-action-group'
import { AttributeReference } from '@commercetools/platform-sdk/src'

const hasAttribute = (attributes, newValue) =>
attributes.some((attribute) => attribute.key === newValue.key)
const hasAttribute = (
attributes: Array<AttributeReference>,
newValue: AttributeReference
) => attributes.some((attribute) => attribute.key === newValue.key)

export const baseActionsList = [
export const baseActionsList: Array<UpdateAction> = [
{ action: 'changeName', key: 'name' },
{ action: 'setKey', key: 'key' },
{ action: 'setDescription', key: 'description' },
]

export function actionsMapBase(
diff,
oldObj,
newObj,
config: { shouldOmitEmptyString?: boolean } = {}
) {
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config.shouldOmitEmptyString,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}

export function actionsMapAttributes(diff, oldObj, newObj) {
export function actionsMapAttributes(diff: any, oldObj: any, newObj: any) {
const handler = createBuildArrayActions('attributes', {
[ADD_ACTIONS]: (newAttribute) => ({
action: 'addAttribute',
Expand Down
31 changes: 12 additions & 19 deletions packages/sync-actions/src/attribute-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,26 @@ import {
} from './attribute-groups-actions'
import { SyncAction } from './types/update-actions'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import createMapActionGroup, {
MapActionGroup,
MapActionResult,
} from './utils/create-map-action-group'
import { diff } from './utils/diffpatcher'

function createAttributeGroupsMapActions(
mapActionGroup: (
type: string,
fn: () => Array<UpdateAction>
) => Array<UpdateAction>,
mapActionGroup: MapActionGroup,
syncActionConfig?: SyncActionConfig
): (diff: any, newObj: any, oldObj: any) => Array<UpdateAction> {
return function doMapActions(
diff: any,
newObj: any,
oldObj: any
): Array<UpdateAction> {
const allActions = []
): MapActionResult {
return function doMapActions(diff, newObj, oldObj) {
const allActions: Array<Array<UpdateAction>> = []
allActions.push(
mapActionGroup(
'base',
(): Array<UpdateAction> =>
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
mapActionGroup('base', () =>
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
)
)
allActions.push(
mapActionGroup(
'attributes',
(): Array<UpdateAction> => actionsMapAttributes(diff, oldObj, newObj)
mapActionGroup('attributes', () =>
actionsMapAttributes(diff, oldObj, newObj)
).flat()
)
return allActions.flat()
Expand Down
13 changes: 5 additions & 8 deletions packages/sync-actions/src/cart-discounts-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { buildBaseAttributesActions } from './utils/common-actions'
import { ActionMapBase } from './utils/create-map-action-group'
import { UpdateAction } from '@commercetools/sdk-client-v2'

export const baseActionsList = [
export const baseActionsList: Array<UpdateAction> = [
{ action: 'changeIsActive', key: 'isActive' },
{ action: 'changeName', key: 'name' },
{ action: 'changeCartPredicate', key: 'cartPredicate' },
Expand All @@ -15,17 +17,12 @@ export const baseActionsList = [
{ action: 'setKey', key: 'key' },
]

export function actionsMapBase(
diff,
oldObj,
newObj,
config: { shouldOmitEmptyString?: boolean } = {}
) {
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config.shouldOmitEmptyString,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}
18 changes: 14 additions & 4 deletions packages/sync-actions/src/cart-discounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ import {
CartDiscount,
CartDiscountUpdateAction,
} from '@commercetools/platform-sdk'
import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2'
import {
ActionGroup,
SyncActionConfig,
UpdateAction,
} from '@commercetools/sdk-client-v2'
import { actionsMapBase } from './cart-discounts-actions'
import { SyncAction } from './types/update-actions'
import actionsMapCustom from './utils/action-map-custom'
import combineValidityActions from './utils/combine-validity-actions'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import createMapActionGroup, {
MapActionGroup,
MapActionResult,
} from './utils/create-map-action-group'
import { diff } from './utils/diffpatcher'

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

function createCartDiscountsMapActions(mapActionGroup, syncActionConfig) {
function createCartDiscountsMapActions(
mapActionGroup: MapActionGroup,
syncActionConfig?: SyncActionConfig
): MapActionResult {
return function doMapActions(diff, newObj, oldObj) {
const allActions = []
const allActions: Array<Array<UpdateAction>> = []

allActions.push(
mapActionGroup('base', () =>
Expand Down
45 changes: 16 additions & 29 deletions packages/sync-actions/src/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
SyncActionConfig,
UpdateAction,
} from '@commercetools/sdk-client-v2'
import actionsMapAssets from './assets-actions'
import actionsMapAssets from './category-assets-actions'
import {
actionsMapBase,
actionsMapMeta,
Expand All @@ -14,56 +14,43 @@ import { SyncAction } from './types/update-actions'
import actionsMapCustom from './utils/action-map-custom'
import copyEmptyArrayProps from './utils/copy-empty-array-props'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import createMapActionGroup, {
MapActionGroup,
MapActionResult,
} from './utils/create-map-action-group'
import { diff } from './utils/diffpatcher'

export const actionGroups = ['base', 'references', 'meta', 'custom', 'assets']

function createCategoryMapActions(
mapActionGroup: Function,
mapActionGroup: MapActionGroup,
syncActionConfig?: SyncActionConfig
): (diff: any, newObj: any, oldObj: any) => Array<UpdateAction> {
return function doMapActions(
diff: any,
newObj: any,
oldObj: any /* , options */
): Array<UpdateAction> {
const allActions = []
): MapActionResult {
return function doMapActions(diff, newObj, oldObj) {
const allActions: Array<Array<UpdateAction>> = []

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

allActions.push(
mapActionGroup(
'references',
(): Array<UpdateAction> => actionsMapReferences(diff, oldObj, newObj)
mapActionGroup('references', () =>
actionsMapReferences(diff, oldObj, newObj)
)
)

allActions.push(
mapActionGroup(
'meta',
(): Array<UpdateAction> => actionsMapMeta(diff, oldObj, newObj)
)
mapActionGroup('meta', () => actionsMapMeta(diff, oldObj, newObj))
)

allActions.push(
mapActionGroup(
'custom',
(): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj)
)
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
)

allActions.push(
mapActionGroup(
'assets',
(): Array<UpdateAction> => actionsMapAssets(diff, oldObj, newObj)
)
mapActionGroup('assets', () => actionsMapAssets(diff, oldObj, newObj))
)

return allActions.flat()
Expand Down
19 changes: 8 additions & 11 deletions packages/sync-actions/src/category-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
buildBaseAttributesActions,
buildReferenceActions,
} from './utils/common-actions'
import { ActionMapBase } from './utils/create-map-action-group'
import { UpdateAction } from '@commercetools/sdk-client-v2'

export const baseActionsList = [
export const baseActionsList: Array<UpdateAction> = [
{ action: 'changeName', key: 'name' },
{ action: 'changeSlug', key: 'slug' },
{ action: 'setDescription', key: 'description' },
Expand All @@ -12,7 +14,7 @@ export const baseActionsList = [
{ action: 'setKey', key: 'key' },
]

export const metaActionsList = [
export const metaActionsList: Array<UpdateAction> = [
{ action: 'setMetaTitle', key: 'metaTitle' },
{ action: 'setMetaKeywords', key: 'metaKeywords' },
{ action: 'setMetaDescription', key: 'metaDescription' },
Expand All @@ -24,22 +26,17 @@ export const referenceActionsList = [{ action: 'changeParent', key: 'parent' }]
* SYNC FUNCTIONS
*/

export function actionsMapBase(
diff,
oldObj,
newObj,
config: { shouldOmitEmptyString?: boolean } = {}
) {
export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config.shouldOmitEmptyString,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}

export function actionsMapReferences(diff, oldObj, newObj) {
export function actionsMapReferences(diff: any, oldObj: any, newObj: any) {
return buildReferenceActions({
actions: referenceActionsList,
diff,
Expand All @@ -48,7 +45,7 @@ export function actionsMapReferences(diff, oldObj, newObj) {
})
}

export function actionsMapMeta(diff, oldObj, newObj) {
export function actionsMapMeta(diff: any, oldObj: any, newObj: any) {
return buildBaseAttributesActions({
actions: metaActionsList,
diff,
Expand Down
45 changes: 45 additions & 0 deletions packages/sync-actions/src/category-assets-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import createBuildArrayActions, {
ADD_ACTIONS,
CHANGE_ACTIONS,
REMOVE_ACTIONS,
} from './utils/create-build-array-actions'
import {
CategoryAddAssetAction,
CategoryRemoveAssetAction,
} from '@commercetools/platform-sdk'
import { UpdateAction } from '@commercetools/sdk-client-v2'

function toAssetIdentifier(asset: { id?: string; key?: string }) {
return asset.id ? { assetId: asset.id } : { assetKey: asset.key }
}

export default function actionsMapAssets(diff: any, oldObj: any, newObj: any) {
const handler = createBuildArrayActions('assets', {
[ADD_ACTIONS]: (newAsset): CategoryAddAssetAction => ({
action: 'addAsset',
asset: newAsset,
}),
[REMOVE_ACTIONS]: (oldAsset): CategoryRemoveAssetAction => ({
action: 'removeAsset',
...toAssetIdentifier(oldAsset),
}),
[CHANGE_ACTIONS]: (oldAsset, newAsset): Array<UpdateAction> =>
// here we could use more atomic update actions (e.g. changeAssetName)
// but for now we use the simpler approach to first remove and then
// re-add the asset - which reduces the code complexity
{
return [
{
action: 'removeAsset',
...toAssetIdentifier(oldAsset),
},
{
action: 'addAsset',
asset: newAsset,
},
]
},
})

return handler(diff, oldObj, newObj)
}
Loading

0 comments on commit be1f6e0

Please sign in to comment.