diff --git a/packages/sync-actions/src/assets-actions.ts b/packages/sync-actions/src/assets-actions.ts deleted file mode 100644 index 749a87c9e..000000000 --- a/packages/sync-actions/src/assets-actions.ts +++ /dev/null @@ -1,41 +0,0 @@ -import createBuildArrayActions, { - ADD_ACTIONS, - CHANGE_ACTIONS, - REMOVE_ACTIONS, -} from './utils/create-build-array-actions' - -function toAssetIdentifier(asset) { - const assetIdentifier = asset.id - ? { assetId: asset.id } - : { assetKey: asset.key } - return assetIdentifier -} - -export default function actionsMapAssets(diff, oldObj, newObj) { - const handler = createBuildArrayActions('assets', { - [ADD_ACTIONS]: (newAsset) => ({ - action: 'addAsset', - asset: newAsset, - }), - [REMOVE_ACTIONS]: (oldAsset) => ({ - action: 'removeAsset', - ...toAssetIdentifier(oldAsset), - }), - [CHANGE_ACTIONS]: (oldAsset, newAsset) => - // 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 - [ - { - action: 'removeAsset', - ...toAssetIdentifier(oldAsset), - }, - { - action: 'addAsset', - asset: newAsset, - }, - ], - }) - - return handler(diff, oldObj, newObj) -} diff --git a/packages/sync-actions/src/attribute-groups-actions.ts b/packages/sync-actions/src/attribute-groups-actions.ts index 036a70603..27d324a64 100644 --- a/packages/sync-actions/src/attribute-groups-actions.ts +++ b/packages/sync-actions/src/attribute-groups-actions.ts @@ -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', diff --git a/packages/sync-actions/src/attribute-groups.ts b/packages/sync-actions/src/attribute-groups.ts index 556296a08..20314b85f 100644 --- a/packages/sync-actions/src/attribute-groups.ts +++ b/packages/sync-actions/src/attribute-groups.ts @@ -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() diff --git a/packages/sync-actions/src/cart-discounts-actions.ts b/packages/sync-actions/src/cart-discounts-actions.ts index 7ef4e27be..281ec3481 100644 --- a/packages/sync-actions/src/cart-discounts-actions.ts +++ b/packages/sync-actions/src/cart-discounts-actions.ts @@ -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' }, @@ -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, }) } diff --git a/packages/sync-actions/src/cart-discounts.ts b/packages/sync-actions/src/cart-discounts.ts index 9c769c6d5..f56ea83a9 100644 --- a/packages/sync-actions/src/cart-discounts.ts +++ b/packages/sync-actions/src/cart-discounts.ts @@ -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', () => diff --git a/packages/sync-actions/src/categories.ts b/packages/sync-actions/src/categories.ts index adf36bcce..3ff1d3666 100644 --- a/packages/sync-actions/src/categories.ts +++ b/packages/sync-actions/src/categories.ts @@ -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, @@ -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() diff --git a/packages/sync-actions/src/category-actions.ts b/packages/sync-actions/src/category-actions.ts index 0e23ee5fd..1dc981917 100644 --- a/packages/sync-actions/src/category-actions.ts +++ b/packages/sync-actions/src/category-actions.ts @@ -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' }, @@ -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' }, @@ -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, @@ -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, diff --git a/packages/sync-actions/src/category-assets-actions.ts b/packages/sync-actions/src/category-assets-actions.ts new file mode 100644 index 000000000..e59fd7a9e --- /dev/null +++ b/packages/sync-actions/src/category-assets-actions.ts @@ -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) +} diff --git a/packages/sync-actions/src/channels-actions.ts b/packages/sync-actions/src/channels-actions.ts index 2459c4e02..a45e529e7 100644 --- a/packages/sync-actions/src/channels-actions.ts +++ b/packages/sync-actions/src/channels-actions.ts @@ -1,6 +1,8 @@ import { buildBaseAttributesActions } from './utils/common-actions' +import { UpdateAction } from '@commercetools/sdk-client-v2' +import { ActionMapBase } from './utils/create-map-action-group' -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'changeKey', key: 'key' }, { action: 'changeName', key: 'name' }, { action: 'changeDescription', key: 'description' }, @@ -9,17 +11,12 @@ export const baseActionsList = [ { action: 'setRoles', key: 'roles' }, ] -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, }) } diff --git a/packages/sync-actions/src/channels.ts b/packages/sync-actions/src/channels.ts index 18f90d307..d1d552606 100644 --- a/packages/sync-actions/src/channels.ts +++ b/packages/sync-actions/src/channels.ts @@ -1,17 +1,27 @@ import { Channel, ChannelUpdateAction } from '@commercetools/platform-sdk' -import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2' +import { + ActionGroup, + SyncActionConfig, + UpdateAction, +} from '@commercetools/sdk-client-v2' import { actionsMapBase } from './channels-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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 createChannelsMapActions(mapActionGroup, syncActionConfig) { +function createChannelsMapActions( + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { return function doMapActions(diff, newObj, oldObj) { - const allActions = [] + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => diff --git a/packages/sync-actions/src/customer-actions.ts b/packages/sync-actions/src/customer-actions.ts index a21f7bcbc..4477886a2 100644 --- a/packages/sync-actions/src/customer-actions.ts +++ b/packages/sync-actions/src/customer-actions.ts @@ -10,10 +10,12 @@ import createBuildArrayActions, { REMOVE_ACTIONS, } from './utils/create-build-array-actions' import { patch } from './utils/diffpatcher' +import { ActionMapBase } from './utils/create-map-action-group' +import { UpdateAction } from '@commercetools/sdk-client-v2' const isEmptyValue = createIsEmptyValue([undefined, null, '']) -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'setSalutation', key: 'salutation' }, { action: 'changeEmail', key: 'email' }, { action: 'setFirstName', key: 'firstName' }, @@ -33,7 +35,7 @@ export const baseActionsList = [ { action: 'setKey', key: 'key' }, ] -export const setDefaultBaseActionsList = [ +export const setDefaultBaseActionsList: Array<UpdateAction> = [ { action: 'setDefaultBillingAddress', key: 'defaultBillingAddressId', @@ -62,37 +64,32 @@ export const authenticationModeActionsList = [ * 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 actionsMapSetDefaultBase( +export const actionsMapSetDefaultBase: ActionMapBase = ( diff, oldObj, newObj, - config: { shouldOmitEmptyString?: boolean } = {} -) { + config +) => { return buildBaseAttributesActions({ actions: setDefaultBaseActionsList, 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, @@ -101,7 +98,7 @@ export function actionsMapReferences(diff, oldObj, newObj) { }) } -export function actionsMapAddresses(diff, oldObj, newObj) { +export function actionsMapAddresses(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('addresses', { [ADD_ACTIONS]: (newObject) => ({ action: 'addAddress', @@ -121,7 +118,11 @@ export function actionsMapAddresses(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapBillingAddresses(diff, oldObj, newObj) { +export function actionsMapBillingAddresses( + diff: any, + oldObj: any, + newObj: any +) { const handler = createBuildArrayActions('billingAddressIds', { [ADD_ACTIONS]: (addressId) => ({ action: 'addBillingAddressId', @@ -136,7 +137,11 @@ export function actionsMapBillingAddresses(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapShippingAddresses(diff, oldObj, newObj) { +export function actionsMapShippingAddresses( + diff: any, + oldObj: any, + newObj: any +) { const handler = createBuildArrayActions('shippingAddressIds', { [ADD_ACTIONS]: (addressId) => ({ action: 'addShippingAddressId', @@ -151,7 +156,11 @@ export function actionsMapShippingAddresses(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapAuthenticationModes(diff, oldObj, newObj) { +export function actionsMapAuthenticationModes( + diff: any, + oldObj: any, + newObj: any +) { // eslint-disable-next-line no-use-before-define return buildAuthenticationModeActions({ actions: authenticationModeActionsList, @@ -161,7 +170,17 @@ export function actionsMapAuthenticationModes(diff, oldObj, newObj) { }) } -function buildAuthenticationModeActions({ actions, diff, oldObj, newObj }) { +function buildAuthenticationModeActions({ + actions, + diff, + oldObj, + newObj, +}: { + actions: Array<UpdateAction> + diff: any + oldObj: any + newObj: any +}) { return actions .map((item) => { const key = item.key diff --git a/packages/sync-actions/src/customer-group-actions.ts b/packages/sync-actions/src/customer-group-actions.ts index 7e687e580..5197f4fe0 100644 --- a/packages/sync-actions/src/customer-group-actions.ts +++ b/packages/sync-actions/src/customer-group-actions.ts @@ -1,21 +1,18 @@ 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: 'changeName', key: 'name' }, { 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, }) } diff --git a/packages/sync-actions/src/customer-group.ts b/packages/sync-actions/src/customer-group.ts index ee940e2a7..49173b1f4 100644 --- a/packages/sync-actions/src/customer-group.ts +++ b/packages/sync-actions/src/customer-group.ts @@ -2,19 +2,29 @@ import { CustomerGroup, CustomerGroupUpdateAction, } from '@commercetools/platform-sdk' -import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2' +import { + ActionGroup, + SyncActionConfig, + UpdateAction, +} from '@commercetools/sdk-client-v2' import { actionsMapBase } from './customer-group-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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 createCustomerGroupMapActions(mapActionGroup, syncActionConfig) { +function createCustomerGroupMapActions( + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { return function doMapActions(diff, newObj, oldObj) { - const allActions = [] + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => diff --git a/packages/sync-actions/src/customers.ts b/packages/sync-actions/src/customers.ts index a13807778..f184729f1 100644 --- a/packages/sync-actions/src/customers.ts +++ b/packages/sync-actions/src/customers.ts @@ -17,7 +17,10 @@ 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 = [ @@ -29,74 +32,55 @@ export const actionGroups = [ ] function createCustomerMapActions( - 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( - 'addresses', - (): Array<UpdateAction> => actionsMapAddresses(diff, oldObj, newObj) + mapActionGroup('addresses', () => + actionsMapAddresses(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'base', - (): Array<UpdateAction> => - actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig) + mapActionGroup('base', () => + actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig) ) ) allActions.push( - mapActionGroup( - 'billingAddressIds', - (): Array<UpdateAction> => - actionsMapBillingAddresses(diff, oldObj, newObj) + mapActionGroup('billingAddressIds', () => + actionsMapBillingAddresses(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'shippingAddressIds', - (): Array<UpdateAction> => - actionsMapShippingAddresses(diff, oldObj, newObj) + mapActionGroup('shippingAddressIds', () => + actionsMapShippingAddresses(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) allActions.push( - mapActionGroup( - 'authenticationModes', - (): Array<UpdateAction> => - actionsMapAuthenticationModes(diff, oldObj, newObj) + mapActionGroup('authenticationModes', () => + actionsMapAuthenticationModes(diff, oldObj, newObj) ) ) diff --git a/packages/sync-actions/src/discount-codes-actions.ts b/packages/sync-actions/src/discount-codes-actions.ts index ba39d2565..203d05974 100644 --- a/packages/sync-actions/src/discount-codes-actions.ts +++ b/packages/sync-actions/src/discount-codes-actions.ts @@ -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: 'setName', key: 'name' }, { action: 'setDescription', key: 'description' }, @@ -16,17 +18,12 @@ export const baseActionsList = [ { action: 'changeGroups', key: 'groups' }, ] -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, }) } diff --git a/packages/sync-actions/src/discount-codes.ts b/packages/sync-actions/src/discount-codes.ts index 94f843c18..8870e005f 100644 --- a/packages/sync-actions/src/discount-codes.ts +++ b/packages/sync-actions/src/discount-codes.ts @@ -2,20 +2,30 @@ import { DiscountCode, DiscountCodeUpdateAction, } from '@commercetools/platform-sdk' -import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2' +import { + ActionGroup, + SyncActionConfig, + UpdateAction, +} from '@commercetools/sdk-client-v2' import { actionsMapBase } from './discount-codes-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 createDiscountCodesMapActions(mapActionGroup, syncActionConfig) { +function createDiscountCodesMapActions( + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { return function doMapActions(diff, newObj, oldObj) { - const allActions = [] + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => actionsMapBase(diff, oldObj, newObj, syncActionConfig) diff --git a/packages/sync-actions/src/inventories.ts b/packages/sync-actions/src/inventories.ts index 4514ea411..36a340fbb 100644 --- a/packages/sync-actions/src/inventories.ts +++ b/packages/sync-actions/src/inventories.ts @@ -11,40 +11,33 @@ import { actionsMapBase, actionsMapReferences } from './inventory-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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'] function createInventoryMapActions( - 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( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) return allActions.flat() } diff --git a/packages/sync-actions/src/inventory-actions.ts b/packages/sync-actions/src/inventory-actions.ts index 2c200cf2c..a0e7ff0a2 100644 --- a/packages/sync-actions/src/inventory-actions.ts +++ b/packages/sync-actions/src/inventory-actions.ts @@ -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: 'changeQuantity', key: 'quantityOnStock', actionKey: 'quantity' }, { action: 'setRestockableInDays', key: 'restockableInDays' }, { action: 'setExpectedDelivery', key: 'expectedDelivery' }, @@ -17,22 +19,17 @@ export const referenceActionsList = [ * 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, diff --git a/packages/sync-actions/src/order-actions.ts b/packages/sync-actions/src/order-actions.ts index be0312fdd..f3cabcb30 100644 --- a/packages/sync-actions/src/order-actions.ts +++ b/packages/sync-actions/src/order-actions.ts @@ -6,17 +6,19 @@ import createBuildArrayActions, { import { getDeltaValue } from './utils/diffpatcher' import extractMatchingPairs from './utils/extract-matching-pairs' import findMatchingPairs from './utils/find-matching-pairs' +import { ActionMapBase } from './utils/create-map-action-group' +import { UpdateAction } from '@commercetools/sdk-client-v2' const REGEX_NUMBER = new RegExp(/^\d+$/) const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/) -const isAddAction = (key, resource) => +const isAddAction = (key: string, resource: any) => REGEX_NUMBER.test(key) && Array.isArray(resource) && resource.length -const isRemoveAction = (key, resource) => +const isRemoveAction = (key: string, resource: any) => REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0 -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'changeOrderState', key: 'orderState' }, { action: 'changePaymentState', key: 'paymentState' }, { action: 'changeShipmentState', key: 'shipmentState' }, @@ -26,22 +28,17 @@ export const baseActionsList = [ * 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 actionsMapDeliveries(diff, oldObj, newObj) { +export function actionsMapDeliveries(diff: any, oldObj: any, newObj: any) { const deliveriesDiff = diff.shippingInfo if (!deliveriesDiff) return [] @@ -57,12 +54,12 @@ export function actionsMapDeliveries(diff, oldObj, newObj) { } function _buildDeliveryParcelsAction( - diffedParcels, + diffedParcels: any, oldDelivery: any = {}, newDelivery: any = {} ) { - const addParcelActions = [] - const removeParcelActions = [] + const addParcelActions: Array<UpdateAction> = [] + const removeParcelActions: Array<UpdateAction> = [] // generate a hashMap to be able to reference the right image from both ends const matchingParcelPairs = findMatchingPairs( @@ -99,7 +96,7 @@ function _buildDeliveryParcelsAction( return [addParcelActions, removeParcelActions] } -function _buildDeliveryItemsAction(diffedItems, newDelivery: any = {}) { +function _buildDeliveryItemsAction(diffedItems: any, newDelivery: any = {}) { const setDeliveryItemsAction: Array<any> = [] // If there is a diff it means that there were changes (update, adds or removes) // over the items, which means that `setDeliveryItems` change has happened over @@ -117,10 +114,10 @@ function _buildDeliveryItemsAction(diffedItems, newDelivery: any = {}) { } export function actionsMapParcels( - diff: { shippingInfo: { deliveries: { any } } }, - oldObj, - newObj, - deliveryHashMap + diff: { shippingInfo: { deliveries: { [key: string]: any } } }, + oldObj: any, + newObj: any, + deliveryHashMap: any ) { const shippingInfo = diff.shippingInfo if (!shippingInfo) return [] @@ -128,8 +125,8 @@ export function actionsMapParcels( const deliveries = shippingInfo.deliveries if (!deliveries) return [] - let addParcelActions = [] - let removeParcelActions = [] + let addParcelActions: Array<UpdateAction> = [] + let removeParcelActions: Array<UpdateAction> = [] if (deliveries) Object.entries(deliveries).forEach(([key, delivery]) => { @@ -156,10 +153,10 @@ export function actionsMapParcels( } export function actionsMapDeliveryItems( - diff: { shippingInfo: { deliveries: { any } } }, - oldObj, - newObj, - deliveryHashMap + diff: { shippingInfo: { deliveries: { [key: string]: any } } }, + oldObj: any, + newObj: any, + deliveryHashMap: any ) { const shippingInfo = diff.shippingInfo if (!shippingInfo) return [] @@ -167,7 +164,7 @@ export function actionsMapDeliveryItems( const deliveries = shippingInfo.deliveries if (!deliveries) return [] - let setDeliveryItemsActions = [] + let setDeliveryItemsActions: Array<UpdateAction> = [] Object.entries(deliveries).forEach(([key, delivery]) => { const { newObj: newDelivery } = extractMatchingPairs( @@ -190,7 +187,7 @@ export function actionsMapDeliveryItems( return setDeliveryItemsActions } -export function actionsMapReturnsInfo(diff, oldObj, newObj) { +export function actionsMapReturnsInfo(diff: any, oldObj: any, newObj: any) { const returnInfoDiff = diff.returnInfo if (!returnInfoDiff) return [] diff --git a/packages/sync-actions/src/orders.ts b/packages/sync-actions/src/orders.ts index ca36617b8..6ebd35fc3 100644 --- a/packages/sync-actions/src/orders.ts +++ b/packages/sync-actions/src/orders.ts @@ -20,23 +20,22 @@ import { import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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' import findMatchingPairs from './utils/find-matching-pairs' export const actionGroups = ['base', 'deliveries'] function createOrderMapActions( - 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 = [] - let deliveryHashMap +): MapActionResult { + return function doMapActions(diff, newObj, oldObj) { + const allActions: Array<Array<UpdateAction>> = [] + let deliveryHashMap: any if (diff.shippingInfo && diff.shippingInfo.deliveries) { deliveryHashMap = findMatchingPairs( @@ -47,40 +46,32 @@ function createOrderMapActions( } allActions.push( - mapActionGroup( - 'base', - (): Array<UpdateAction> => - actionsMapBase(diff, oldObj, newObj, syncActionConfig) + mapActionGroup('base', () => + actionsMapBase(diff, oldObj, newObj, syncActionConfig) ) ) allActions.push( - mapActionGroup( - 'deliveries', - (): Array<UpdateAction> => actionsMapDeliveries(diff, oldObj, newObj) + mapActionGroup('deliveries', () => + actionsMapDeliveries(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'parcels', - (): Array<UpdateAction> => - actionsMapParcels(diff, oldObj, newObj, deliveryHashMap) + mapActionGroup('parcels', () => + actionsMapParcels(diff, oldObj, newObj, deliveryHashMap) ) ) allActions.push( - mapActionGroup( - 'items', - (): Array<UpdateAction> => - actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap) + mapActionGroup('items', () => + actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap) ) ) allActions.push( - mapActionGroup( - 'returnInfo', - (): Array<UpdateAction> => actionsMapReturnsInfo(diff, oldObj, newObj) + mapActionGroup('returnInfo', () => + actionsMapReturnsInfo(diff, oldObj, newObj) ).flat() ) diff --git a/packages/sync-actions/src/prices-actions.ts b/packages/sync-actions/src/prices-actions.ts index 5d2b61b38..af1b3c18d 100644 --- a/packages/sync-actions/src/prices-actions.ts +++ b/packages/sync-actions/src/prices-actions.ts @@ -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: 'changeValue', key: 'value' }, { action: 'setDiscountedPrice', key: 'discounted' }, // TODO: Later add more accurate actions `addPriceTier`, `removePriceTier` @@ -11,17 +13,12 @@ export const baseActionsList = [ { action: 'changeActive', key: 'active' }, ] -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, }) } diff --git a/packages/sync-actions/src/prices.ts b/packages/sync-actions/src/prices.ts index 4f6c77372..98e4231e2 100644 --- a/packages/sync-actions/src/prices.ts +++ b/packages/sync-actions/src/prices.ts @@ -8,29 +8,25 @@ import { actionsMapBase } from './prices-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' const actionGroups = ['base', 'custom'] function createPriceMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any, options: any) => Array<UpdateAction> { - return function doMapActions( - diff: any, - newObj: any, - oldObj: any - ): Array<UpdateAction> { - const baseActions = mapActionGroup( - 'base', - (): Array<UpdateAction> => - actionsMapBase(diff, oldObj, newObj, syncActionConfig) +): MapActionResult { + return function doMapActions(diff, newObj, oldObj) { + const baseActions = mapActionGroup('base', () => + actionsMapBase(diff, oldObj, newObj, syncActionConfig) ) - const customActions = mapActionGroup( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) + const customActions = mapActionGroup('custom', () => + actionsMapCustom(diff, newObj, oldObj) ) return combineValidityActions([...baseActions, ...customActions]) diff --git a/packages/sync-actions/src/product-actions.ts b/packages/sync-actions/src/product-actions.ts index caacd7b80..d57a4d25c 100644 --- a/packages/sync-actions/src/product-actions.ts +++ b/packages/sync-actions/src/product-actions.ts @@ -1,4 +1,7 @@ -import { ProductVariant } from '@commercetools/platform-sdk/src' +import { + ProductAddToCategoryAction, + ProductVariant, +} from '@commercetools/platform-sdk/src' import actionsMapCustom from './utils/action-map-custom' import { buildBaseAttributesActions, @@ -11,11 +14,18 @@ import createBuildArrayActions, { import { getDeltaValue } from './utils/diffpatcher' import extractMatchingPairs from './utils/extract-matching-pairs' import findMatchingPairs from './utils/find-matching-pairs' +import { + Asset, + ProductRemoveFromCategoryAction, +} from '@commercetools/platform-sdk' +import { SyncActionConfig } from '@commercetools/sdk-client-v2/src' +import { UpdateAction } from '@commercetools/sdk-client-v2' +import { ActionMapBase } from './utils/create-map-action-group' const REGEX_NUMBER = new RegExp(/^\d+$/) const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/) -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'changeName', key: 'name' }, { action: 'changeSlug', key: 'slug' }, { action: 'setDescription', key: 'description' }, @@ -47,19 +57,19 @@ export const referenceActionsList = [ * HELPER FUNCTIONS */ -const getIsAddAction = (key, resource) => +const getIsAddAction = (key: string, resource: any) => REGEX_NUMBER.test(key) && Array.isArray(resource) && resource.length -const getIsUpdateAction = (key, resource) => +const getIsUpdateAction = (key: string, resource: any) => REGEX_NUMBER.test(key) && Object.keys(resource).length -const getIsRemoveAction = (key, resource) => +const getIsRemoveAction = (key: string, resource: any) => REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0 -const getIsItemMovedAction = (key, resource) => +const getIsItemMovedAction = (key: string, resource: any) => REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 3 -function _buildSkuActions(variantDiff, oldVariant) { +function _buildSkuActions(variantDiff: any, oldVariant: any) { if ({}.hasOwnProperty.call(variantDiff, 'sku')) { const newValue = getDeltaValue(variantDiff.sku) if (!newValue && !oldVariant.sku) return null @@ -73,7 +83,7 @@ function _buildSkuActions(variantDiff, oldVariant) { return null } -function _buildKeyActions(variantDiff, oldVariant) { +function _buildKeyActions(variantDiff: any, oldVariant: any) { if ({}.hasOwnProperty.call(variantDiff, 'key')) { const newValue = getDeltaValue(variantDiff.key) if (!newValue && !oldVariant.key) return null @@ -87,7 +97,11 @@ function _buildKeyActions(variantDiff, oldVariant) { return null } -function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) { +function _buildNewSetAttributeAction( + id: any, + el: any, + sameForAllAttributeNames: Array<any> +) { const attributeName = el && el.name if (!attributeName) return undefined @@ -107,10 +121,10 @@ function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) { } function _buildSetAttributeAction( - diffedValue, - oldVariant, - attribute, - sameForAllAttributeNames + diffedValue: any, + oldVariant: any, + attribute: any, + sameForAllAttributeNames: Array<any> ) { if (!attribute) return undefined @@ -122,7 +136,7 @@ function _buildSetAttributeAction( // Used as original object for patching long diff text const oldAttribute = - oldVariant.attributes.find((a) => a.name === attribute.name) || {} + oldVariant.attributes.find((a: any) => a.name === attribute.name) || {} if (sameForAllAttributeNames.indexOf(attribute.name) !== -1) { action = { ...action, action: 'setAttributeInAllVariants' } @@ -179,7 +193,7 @@ function _buildVariantImagesAction( oldVariant: any = {}, newVariant: any = {} ) { - const actions = [] + const actions: Array<any> = [] // generate a hashMap to be able to reference the right image from both ends const matchingImagePairs = findMatchingPairs( diffedImages, @@ -254,14 +268,14 @@ function _buildVariantImagesAction( } function _buildVariantPricesAction( - diffedPrices, + diffedPrices: any, oldVariant: any = {}, newVariant: any = {}, enableDiscounted = false ) { - const addPriceActions = [] - const changePriceActions = [] - const removePriceActions = [] + const addPriceActions: Array<any> = [] + const changePriceActions: Array<any> = [] + const removePriceActions: Array<any> = [] // generate a hashMap to be able to reference the right image from both ends const matchingPricePairs = findMatchingPairs( @@ -279,7 +293,7 @@ function _buildVariantPricesAction( ) if (getIsAddAction(key, price)) { // Remove read-only fields - const patchedPrice = (price as any).map((p) => { + const patchedPrice = (price as any).map((p: any) => { const shallowClone = { ...p } if (enableDiscounted !== true) delete shallowClone.discounted return shallowClone @@ -325,12 +339,12 @@ function _buildVariantPricesAction( } function _buildVariantAttributesActions( - attributes, - oldVariant, - newVariant, - sameForAllAttributeNames + attributes: any, + oldVariant: any, + newVariant: any, + sameForAllAttributeNames: Array<any> ) { - const actions = [] + const actions: Array<any> = [] if (!attributes) return actions attributes && @@ -393,22 +407,22 @@ function _buildVariantAttributesActions( return actions } -function toAssetIdentifier(asset) { +function toAssetIdentifier(asset: Asset) { const assetIdentifier = asset.id ? { assetId: asset.id } : { assetKey: asset.key } return assetIdentifier } -function toVariantIdentifier(variant) { +function toVariantIdentifier(variant: ProductVariant) { const { id, sku } = variant return id ? { variantId: id } : { sku } } function _buildVariantChangeAssetOrderAction( - diffAssets, - oldVariant, - newVariant + diffAssets: any, + oldVariant: any, + newVariant: any ) { const isAssetOrderChanged = Object.entries(diffAssets).find((entry) => getIsItemMovedAction(entry[0], entry[1]) @@ -416,15 +430,15 @@ function _buildVariantChangeAssetOrderAction( if (!isAssetOrderChanged) { return [] } - const assetIdsBefore = oldVariant.assets.map((_) => _.id) + const assetIdsBefore = oldVariant.assets.map((value: any) => value.id) const assetIdsCurrent = newVariant.assets - .map((_) => _.id) - .filter((_) => _ !== undefined) - const assetIdsToKeep = assetIdsCurrent.filter((value) => + .map((value: any) => value.id) + .filter((value: any) => value !== undefined) + const assetIdsToKeep = assetIdsCurrent.filter((value: any) => assetIdsBefore.includes(value) ) const assetIdsToRemove = assetIdsBefore.filter( - (item) => !assetIdsToKeep.includes(item) + (item: any) => !assetIdsToKeep.includes(item) ) const changeAssetOrderAction = { action: 'changeAssetOrder', @@ -434,8 +448,12 @@ function _buildVariantChangeAssetOrderAction( return [changeAssetOrderAction] } -function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) { - const assetActions = [] +function _buildVariantAssetsActions( + diffAssets: any, + oldVariant: any, + newVariant: any +) { + const assetActions: Array<any> = [] // generate a hashMap to be able to reference the right asset from both ends const matchingAssetPairs = findMatchingPairs( @@ -524,22 +542,17 @@ function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) { * 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 actionsMapMeta(diff, oldObj, newObj) { +export function actionsMapMeta(diff: any, oldObj: any, newObj: any) { return buildBaseAttributesActions({ actions: metaActionsList, diff, @@ -548,9 +561,9 @@ export function actionsMapMeta(diff, oldObj, newObj) { }) } -export function actionsMapAddVariants(diff, oldObj, newObj) { +export function actionsMapAddVariants(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('variants', { - [ADD_ACTIONS]: (newObject) => ({ + [ADD_ACTIONS]: (newObject: any) => ({ ...newObject, action: 'addVariant', }), @@ -558,9 +571,9 @@ export function actionsMapAddVariants(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapRemoveVariants(diff, oldObj, newObj) { +export function actionsMapRemoveVariants(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('variants', { - [REMOVE_ACTIONS]: ({ id }) => ({ + [REMOVE_ACTIONS]: ({ id }: { id: string }) => ({ action: 'removeVariant', id, }), @@ -568,7 +581,7 @@ export function actionsMapRemoveVariants(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapReferences(diff, oldObj, newObj) { +export function actionsMapReferences(diff: any, oldObj: any, newObj: any) { return buildReferenceActions({ actions: referenceActionsList, diff, @@ -577,12 +590,15 @@ export function actionsMapReferences(diff, oldObj, newObj) { }) } -export function actionsMapCategories(diff) { - const actions = [] +export function actionsMapCategories(diff: { + categories?: { [key: string]: any } + [key: string]: any +}): Array<ProductAddToCategoryAction | ProductRemoveFromCategoryAction> { + const actions: Array<any> = [] if (!diff.categories) return actions - const addToCategoryActions = [] - const removeFromCategoryActions = [] + const addToCategoryActions: Array<ProductAddToCategoryAction> = [] + const removeFromCategoryActions: Array<ProductRemoveFromCategoryAction> = [] Object.entries(diff.categories).forEach(([key, category]) => { if (Array.isArray(category)) { @@ -602,10 +618,10 @@ export function actionsMapCategories(diff) { }) // Make sure `removeFromCategory` actions come first - return removeFromCategoryActions.concat(addToCategoryActions) + return [...removeFromCategoryActions, ...addToCategoryActions] } -export function actionsMapCategoryOrderHints(diff) { +export function actionsMapCategoryOrderHints(diff: any) { if (!diff.categoryOrderHints) return [] // Ignore this pattern as its means no changes happened [{},0,0] if (Array.isArray(diff.categoryOrderHints)) return [] @@ -631,8 +647,13 @@ export function actionsMapCategoryOrderHints(diff) { }) } -export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) { - let allAssetsActions = [] +export function actionsMapAssets( + diff: any, + oldObj: any, + newObj: any, + variantHashMap: any +) { + let allAssetsActions: Array<any> = [] const { variants } = diff @@ -664,13 +685,13 @@ export function actionsMapAssets(diff, oldObj, newObj, variantHashMap) { } export function actionsMapAttributes( - diff, - oldObj, - newObj, - sameForAllAttributeNames = [], - variantHashMap + diff: any, + oldObj: any, + newObj: any, + sameForAllAttributeNames: Array<any> = [], + variantHashMap: any ) { - let actions = [] + let actions: Array<any> = [] const { variants } = diff if (variants) @@ -713,8 +734,13 @@ export function actionsMapAttributes( ) } -export function actionsMapImages(diff, oldObj, newObj, variantHashMap) { - let actions = [] +export function actionsMapImages( + diff: any, + oldObj: any, + newObj: any, + variantHashMap: any +) { + let actions: Array<any> = [] const { variants } = diff if (variants) Object.entries(variants as { [key: string]: ProductVariant }).forEach( @@ -740,15 +766,15 @@ export function actionsMapImages(diff, oldObj, newObj, variantHashMap) { } export function actionsMapPrices( - diff, - oldObj, - newObj, - variantHashMap, - enableDiscounted + diff: any, + oldObj: any, + newObj: any, + variantHashMap: any, + enableDiscounted: any ) { - let addPriceActions = [] - let changePriceActions = [] - let removePriceActions = [] + let addPriceActions: Array<any> = [] + let changePriceActions: Array<any> = [] + let removePriceActions: Array<any> = [] const { variants } = diff @@ -781,8 +807,13 @@ export function actionsMapPrices( return changePriceActions.concat(removePriceActions).concat(addPriceActions) } -export function actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) { - let actions = [] +export function actionsMapPricesCustom( + diff: any, + oldObj: any, + newObj: any, + variantHashMap: any +) { + let actions: Array<any> = [] const { variants } = diff @@ -842,12 +873,15 @@ export function actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) { return actions } -export function actionsMapMasterVariant(oldObj, newObj) { - const createChangeMasterVariantAction = (variantId) => ({ +export function actionsMapMasterVariant( + oldObj: any, + newObj: any +): Array<UpdateAction> { + const createChangeMasterVariantAction = (variantId: string) => ({ action: 'changeMasterVariant', variantId, }) - const extractMasterVariantId = (fromObj) => { + const extractMasterVariantId = (fromObj: any) => { const variants = Array.isArray(fromObj.variants) ? fromObj.variants : [] return variants[0] ? variants[0].id : undefined diff --git a/packages/sync-actions/src/product-discounts-actions.ts b/packages/sync-actions/src/product-discounts-actions.ts index 38f7fd72c..1f612e693 100644 --- a/packages/sync-actions/src/product-discounts-actions.ts +++ b/packages/sync-actions/src/product-discounts-actions.ts @@ -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: 'changePredicate', key: 'predicate' }, @@ -12,17 +14,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, }) } diff --git a/packages/sync-actions/src/product-discounts.ts b/packages/sync-actions/src/product-discounts.ts index 989605e61..ac37b21ff 100644 --- a/packages/sync-actions/src/product-discounts.ts +++ b/packages/sync-actions/src/product-discounts.ts @@ -2,19 +2,29 @@ import { ProductDiscount, ProductDiscountUpdateAction, } from '@commercetools/platform-sdk' -import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2' +import { + ActionGroup, + SyncActionConfig, + UpdateAction, +} from '@commercetools/sdk-client-v2' import { actionsMapBase } from './product-discounts-actions' import { SyncAction } from './types/update-actions' 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'] -function createProductDiscountsMapActions(mapActionGroup, syncActionConfig) { +function createProductDiscountsMapActions( + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { return function doMapActions(diff, newObj, oldObj) { - const allActions = [] + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => diff --git a/packages/sync-actions/src/product-selections-actions.ts b/packages/sync-actions/src/product-selections-actions.ts index abeae9464..938c1c2d7 100644 --- a/packages/sync-actions/src/product-selections-actions.ts +++ b/packages/sync-actions/src/product-selections-actions.ts @@ -1,11 +1,13 @@ 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: 'changeName', key: 'name' }, { action: 'setKey', key: 'key' }, ] -export function actionsMapBase(diff, oldObj, newObj) { +export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj) => { return buildBaseAttributesActions({ actions: baseActionsList, diff, diff --git a/packages/sync-actions/src/product-selections.ts b/packages/sync-actions/src/product-selections.ts index b00a3f9a3..ded29b91e 100644 --- a/packages/sync-actions/src/product-selections.ts +++ b/packages/sync-actions/src/product-selections.ts @@ -46,7 +46,7 @@ export default ( ): SyncAction<ProductSelection, ProductSelectionUpdateAction> => { const mapActionGroup = createMapActionGroup(actionGroupList) const doMapActions = createProductSelectionsMapActions(mapActionGroup) - const onBeforeApplyingDiff = null + const onBeforeApplyingDiff: any = null const buildActions = createBuildActions< ProductSelection, ProductSelectionUpdateAction diff --git a/packages/sync-actions/src/product-types-actions.ts b/packages/sync-actions/src/product-types-actions.ts index d0dda3cf1..11ad86e72 100644 --- a/packages/sync-actions/src/product-types-actions.ts +++ b/packages/sync-actions/src/product-types-actions.ts @@ -4,19 +4,16 @@ import { buildBaseAttributesActions, createIsEmptyValue, } 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: 'setKey', key: 'key' }, { action: 'changeDescription', key: 'description' }, ] -export function actionsMapBase( - diff, - previous, - next, - config: { shouldOmitEmptyString?: boolean } = {} -) { +export const actionsMapBase: ActionMapBase = (diff, previous, next, config) => { // when `diff` is undefined, then the underlying `buildActions` has returned any diff // which given in product-types would mean that `buildActions` has run with `nestedValuesChanges` applied // To allow continuation of update-action generation, we let this pass.. @@ -26,7 +23,7 @@ export function actionsMapBase( actions: baseActionsList, oldObj: previous, newObj: next, - shouldOmitEmptyString: config.shouldOmitEmptyString, + shouldOmitEmptyString: config?.shouldOmitEmptyString, }) } @@ -35,8 +32,8 @@ export function actionsMapBase( // which is an object consisting of flags which indicates different operations. // `generateBaseFieldsUpdateActions` only generate based on `previous` and `next`. export const generateBaseFieldsUpdateActions = ( - previous, - next, + previous: any, + next: any, actionDefinition: { [s: string]: { action: string; attributeName?: string } } ) => { const isEmpty = createIsEmptyValue([undefined, null, '']) @@ -284,9 +281,9 @@ const generateUpdateActionsForAttributeEnumValues = ( } const generateChangeAttributeOrderAction = ( - attrsOld = [], - attrsNew = [], - updateActions = [] + attrsOld: Array<any> = [], + attrsNew: Array<any> = [], + updateActions: Array<UpdateAction> = [] ) => { if (!attrsOld.length || !attrsNew.length) return null @@ -322,8 +319,8 @@ export type NestedValues = { } export const actionsMapForHints = ( nestedValuesChanges: NestedValues, - ptOld, - ptNew + ptOld: any, + ptNew: any ) => { const updateActions = [ ...generateUpdateActionsForAttributeDefinitions( diff --git a/packages/sync-actions/src/product-types.ts b/packages/sync-actions/src/product-types.ts index 9a26050a7..da8c76dc8 100644 --- a/packages/sync-actions/src/product-types.ts +++ b/packages/sync-actions/src/product-types.ts @@ -10,7 +10,10 @@ import type { import * as productTypeActions from './product-types-actions' import { NestedValues } from './product-types-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' type SyncActionConfig = { withHints?: boolean } & BaseSyncActionConfig @@ -18,44 +21,30 @@ type SyncActionConfig = { withHints?: boolean } & BaseSyncActionConfig const actionGroups = ['base'] function createProductTypeMapActions( - mapActionGroup: ( - type: string, - fn: () => Array<UpdateAction> - ) => Array<UpdateAction>, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, next: any, previous: any, options: any) => Array<UpdateAction> { - return function doMapActions( - diff: any, - next: any, - previous: any, - options: any - ): Array<UpdateAction> { +): MapActionResult { + return function doMapActions(diff, newObj, oldObj, options) { return [ // we support only base fields for the product type, // for attributes, applying hints would be recommended - mapActionGroup( - 'base', - (): Array<UpdateAction> => - productTypeActions.actionsMapBase( - diff, - previous, - next, - syncActionConfig - ) + mapActionGroup('base', () => + productTypeActions.actionsMapBase( + diff, + oldObj, + newObj, + syncActionConfig + ) ), productTypeActions.actionsMapForHints( options.nestedValuesChanges, - previous, - next + oldObj, + newObj ), ].flat() } } -export type ProductTypeConfig = { - nestedValuesChanges: NestedValues -} - export default ( actionGroupList?: Array<ActionGroup>, syncActionConfig?: SyncActionConfig @@ -65,7 +54,7 @@ export default ( mapActionGroup, syncActionConfig ) - const onBeforeApplyingDiff = null + const onBeforeApplyingDiff: any = null const buildActions = createBuildActions<ProductType, ProductTypeUpdateAction>( diff, doMapActions, diff --git a/packages/sync-actions/src/products.ts b/packages/sync-actions/src/products.ts index f226b1c93..806f69818 100644 --- a/packages/sync-actions/src/products.ts +++ b/packages/sync-actions/src/products.ts @@ -22,7 +22,10 @@ import { import { SyncAction } from './types/update-actions' 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' import findMatchingPairs from './utils/find-matching-pairs' @@ -40,16 +43,11 @@ const actionGroups = [ ] function createProductMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any, options: any) => Array<UpdateAction> { - return function doMapActions( - diff: any, - newObj: any, - oldObj: any, - options: any = {} - ): Array<UpdateAction> { - const allActions = [] +): MapActionResult { + return function doMapActions(diff, newObj, oldObj, options) { + const allActions: Array<Array<UpdateAction>> = [] const { sameForAllAttributeNames, enableDiscounted } = options const { publish, staged } = newObj @@ -60,107 +58,76 @@ function createProductMapActions( ) allActions.push( - mapActionGroup( - 'attributes', - (): Array<UpdateAction> => - actionsMapAttributes( - diff, - oldObj, - newObj, - sameForAllAttributeNames || [], - variantHashMap - ) + mapActionGroup('attributes', () => + actionsMapAttributes( + diff, + oldObj, + newObj, + sameForAllAttributeNames || [], + variantHashMap + ) ) ) allActions.push( - mapActionGroup( - 'variants', - (): Array<UpdateAction> => actionsMapAddVariants(diff, oldObj, newObj) + mapActionGroup('variants', () => + actionsMapAddVariants(diff, oldObj, newObj) ) ) allActions.push(actionsMapMasterVariant(oldObj, newObj)) allActions.push( - mapActionGroup( - 'variants', - (): Array<UpdateAction> => - actionsMapRemoveVariants(diff, oldObj, newObj) + mapActionGroup('variants', () => + actionsMapRemoveVariants(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'base', - (): Array<UpdateAction> => - actionsMapBase(diff, oldObj, newObj, syncActionConfig) + mapActionGroup('base', () => + actionsMapBase(diff, oldObj, newObj, syncActionConfig) ) ) allActions.push( - mapActionGroup( - 'meta', - (): Array<UpdateAction> => actionsMapMeta(diff, oldObj, newObj) - ) + mapActionGroup('meta', () => actionsMapMeta(diff, oldObj, newObj)) ) allActions.push( - mapActionGroup( - 'references', - (): Array<UpdateAction> => actionsMapReferences(diff, oldObj, newObj) + mapActionGroup('references', () => + actionsMapReferences(diff, oldObj, newObj) ) ) allActions.push( - mapActionGroup( - 'images', - (): Array<UpdateAction> => - actionsMapImages(diff, oldObj, newObj, variantHashMap) + mapActionGroup('images', () => + actionsMapImages(diff, oldObj, newObj, variantHashMap) ) ) allActions.push( - mapActionGroup( - 'pricesCustom', - (): Array<UpdateAction> => - actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) + mapActionGroup('pricesCustom', () => + actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) ) ) allActions.push( - mapActionGroup( - 'prices', - (): Array<UpdateAction> => - actionsMapPrices( - diff, - oldObj, - newObj, - variantHashMap, - enableDiscounted - ) + mapActionGroup('prices', () => + actionsMapPrices(diff, oldObj, newObj, variantHashMap, enableDiscounted) ) ) allActions.push( - mapActionGroup( - 'categories', - (): Array<UpdateAction> => actionsMapCategories(diff) - ) + mapActionGroup('categories', () => actionsMapCategories(diff)) ) allActions.push( - mapActionGroup( - 'categories', - (): Array<UpdateAction> => actionsMapCategoryOrderHints(diff) - ) + mapActionGroup('categories', () => actionsMapCategoryOrderHints(diff)) ) allActions.push( - mapActionGroup( - 'assets', - (): Array<UpdateAction> => - actionsMapAssets(diff, oldObj, newObj, variantHashMap) + mapActionGroup('assets', () => + actionsMapAssets(diff, oldObj, newObj, variantHashMap) ) ) diff --git a/packages/sync-actions/src/projects-actions.ts b/packages/sync-actions/src/projects-actions.ts index aa7423351..fd3e22735 100644 --- a/packages/sync-actions/src/projects-actions.ts +++ b/packages/sync-actions/src/projects-actions.ts @@ -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: 'changeName', key: 'name' }, { action: 'changeCurrencies', key: 'currencies' }, { action: 'changeCountries', key: 'countries' }, @@ -9,17 +11,12 @@ export const baseActionsList = [ { action: 'setShippingRateInputType', key: 'shippingRateInputType' }, ] -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, }) } diff --git a/packages/sync-actions/src/projects.ts b/packages/sync-actions/src/projects.ts index 334f4d97e..b571bb702 100644 --- a/packages/sync-actions/src/projects.ts +++ b/packages/sync-actions/src/projects.ts @@ -3,18 +3,28 @@ import { Project, ProjectUpdateAction, } from '@commercetools/platform-sdk' -import { ActionGroup, SyncActionConfig } from '@commercetools/sdk-client-v2' +import { + ActionGroup, + SyncActionConfig, + UpdateAction, +} from '@commercetools/sdk-client-v2' import { actionsMapBase } from './projects-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' export const actionGroups = ['base'] -function createChannelsMapActions(mapActionGroup, syncActionConfig) { +function createChannelsMapActions( + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { return function doMapActions(diff, newObj, oldObj) { - const allActions = [] + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => diff --git a/packages/sync-actions/src/quote-requests-actions.ts b/packages/sync-actions/src/quote-requests-actions.ts index 045bb1d2a..9248317fe 100644 --- a/packages/sync-actions/src/quote-requests-actions.ts +++ b/packages/sync-actions/src/quote-requests-actions.ts @@ -1,21 +1,18 @@ 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: 'changeQuoteRequestState', key: 'quoteRequestState' }, { action: 'transitionState', key: 'state' }, ] -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, }) } diff --git a/packages/sync-actions/src/quote-requests.ts b/packages/sync-actions/src/quote-requests.ts index 087afb27f..bd8e1f4b2 100644 --- a/packages/sync-actions/src/quote-requests.ts +++ b/packages/sync-actions/src/quote-requests.ts @@ -11,35 +11,29 @@ import { actionsMapBase } from './quote-requests-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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' const actionGroups = ['base', 'custom'] function createQuoteRequestsMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any, options: 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( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) return allActions.flat() diff --git a/packages/sync-actions/src/quotes-actions.ts b/packages/sync-actions/src/quotes-actions.ts index a6881cc64..f5e772fa7 100644 --- a/packages/sync-actions/src/quotes-actions.ts +++ b/packages/sync-actions/src/quotes-actions.ts @@ -1,22 +1,19 @@ 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: 'changeQuoteState', key: 'quoteState' }, { action: 'requestQuoteRenegotiation', key: 'buyerComment' }, { action: 'transitionState', key: 'state' }, ] -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, }) } diff --git a/packages/sync-actions/src/quotes.ts b/packages/sync-actions/src/quotes.ts index be7190cd0..e9bdb0d12 100644 --- a/packages/sync-actions/src/quotes.ts +++ b/packages/sync-actions/src/quotes.ts @@ -8,35 +8,29 @@ import { actionsMapBase } from './quotes-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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' const actionGroups = ['base', 'custom'] function createQuotesMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any, options: 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( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) return allActions.flat() diff --git a/packages/sync-actions/src/shipping-methods-actions.ts b/packages/sync-actions/src/shipping-methods-actions.ts index bf299318f..d5b306388 100644 --- a/packages/sync-actions/src/shipping-methods-actions.ts +++ b/packages/sync-actions/src/shipping-methods-actions.ts @@ -4,8 +4,11 @@ import createBuildArrayActions, { CHANGE_ACTIONS, REMOVE_ACTIONS, } from './utils/create-build-array-actions' +import { ActionMapBase } from './utils/create-map-action-group' +import { UpdateAction } from '@commercetools/sdk-client-v2' +import { ZoneRate } from '@commercetools/platform-sdk' -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'setKey', key: 'key' }, { action: 'changeName', key: 'name' }, { action: 'setLocalizedName', key: 'localizedName' }, @@ -16,22 +19,17 @@ export const baseActionsList = [ { action: 'changeTaxCategory', key: 'taxCategory' }, ] -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, }) } -const addShippingRates = (newZoneRate) => +const addShippingRates = (newZoneRate: ZoneRate) => newZoneRate.shippingRates ? newZoneRate.shippingRates.map((shippingRate) => ({ action: 'addShippingRate', @@ -40,7 +38,7 @@ const addShippingRates = (newZoneRate) => })) : [] -function actionsMapZoneRatesShippingRates(diff, oldObj, newObj) { +function actionsMapZoneRatesShippingRates(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('shippingRates', { [ADD_ACTIONS]: (newShippingRate) => ({ action: 'addShippingRate', @@ -69,7 +67,7 @@ function actionsMapZoneRatesShippingRates(diff, oldObj, newObj) { return handler(diff, oldObj, newObj) } -export function actionsMapZoneRates(diff, oldObj, newObj) { +export function actionsMapZoneRates(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('zoneRates', { [ADD_ACTIONS]: (newZoneRate) => [ { diff --git a/packages/sync-actions/src/shipping-methods.ts b/packages/sync-actions/src/shipping-methods.ts index 1859aa440..c9d478edc 100644 --- a/packages/sync-actions/src/shipping-methods.ts +++ b/packages/sync-actions/src/shipping-methods.ts @@ -11,39 +11,32 @@ import { actionsMapBase, actionsMapZoneRates } from './shipping-methods-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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', 'zoneRates', 'custom'] function createShippingMethodsMapActions( - mapActionGroup: Function, + 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( - 'zoneRates', - (): Array<UpdateAction> => actionsMapZoneRates(diff, oldObj, newObj) + mapActionGroup('zoneRates', () => + actionsMapZoneRates(diff, oldObj, newObj) ).flat() ) allActions.push( - mapActionGroup( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) return allActions.flat() } diff --git a/packages/sync-actions/src/staged-quotes-actions.ts b/packages/sync-actions/src/staged-quotes-actions.ts index b454b7708..deca4d90f 100644 --- a/packages/sync-actions/src/staged-quotes-actions.ts +++ b/packages/sync-actions/src/staged-quotes-actions.ts @@ -1,23 +1,20 @@ 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: 'changeStagedQuoteState', key: 'stagedQuoteState' }, { action: 'setSellerComment', key: 'sellerComment' }, { action: 'setValidTo', key: 'validTo' }, { action: 'transitionState', key: 'state' }, ] -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, }) } diff --git a/packages/sync-actions/src/staged-quotes.ts b/packages/sync-actions/src/staged-quotes.ts index 388523b96..eceef819f 100644 --- a/packages/sync-actions/src/staged-quotes.ts +++ b/packages/sync-actions/src/staged-quotes.ts @@ -12,35 +12,29 @@ import { actionsMapBase } from './staged-quotes-actions' import { SyncAction } from './types/update-actions' import actionsMapCustom from './utils/action-map-custom' 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' const actionGroups = ['base', 'custom'] function createStagedQuotesMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any, options: 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( - 'custom', - (): Array<UpdateAction> => actionsMapCustom(diff, newObj, oldObj) - ) + mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj)) ) return allActions.flat() diff --git a/packages/sync-actions/src/state-actions.ts b/packages/sync-actions/src/state-actions.ts index 809f64bf3..6cc6322f9 100644 --- a/packages/sync-actions/src/state-actions.ts +++ b/packages/sync-actions/src/state-actions.ts @@ -3,8 +3,10 @@ import createBuildArrayActions, { ADD_ACTIONS, REMOVE_ACTIONS, } from './utils/create-build-array-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: 'changeKey', key: 'key' }, { action: 'setName', key: 'name' }, { action: 'setDescription', key: 'description' }, @@ -13,22 +15,17 @@ export const baseActionsList = [ { action: 'setTransitions', key: 'transitions' }, ] -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 actionsMapRoles(diff, oldObj, newObj) { +export function actionsMapRoles(diff: any, oldObj: any, newObj: any) { const buildArrayActions = createBuildArrayActions('roles', { [ADD_ACTIONS]: (newRole) => ({ action: 'addRoles', diff --git a/packages/sync-actions/src/states.ts b/packages/sync-actions/src/states.ts index 429d76de1..0ec80be3d 100644 --- a/packages/sync-actions/src/states.ts +++ b/packages/sync-actions/src/states.ts @@ -7,20 +7,20 @@ import type { import { actionsMapBase, actionsMapRoles } from './state-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' -type RoleUpdate = { - action: string - roles: string -} - export const actionGroups = ['base'] // This function groups `addRoles` and `removeRoles` actions to one array -function groupRoleActions([actions]: Array<UpdateAction>): Array<UpdateAction> { - const addActionRoles = [] - const removeActionRoles = [] +function groupRoleActions([actions]: Array< + Array<UpdateAction> +>): Array<UpdateAction> { + const addActionRoles: Array<UpdateAction> = [] + const removeActionRoles: Array<UpdateAction> = [] actions.forEach((action: UpdateAction) => { if (action.action === 'removeRoles') removeActionRoles.push(action.roles) if (action.action === 'addRoles') addActionRoles.push(action.roles) @@ -32,28 +32,19 @@ function groupRoleActions([actions]: Array<UpdateAction>): Array<UpdateAction> { } function createStatesMapActions( - mapActionGroup: Function, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, newObj: any, oldObj: any) => Array<UpdateAction> { - return function doMapActions( - diff: any, - newObj: any, - oldObj: any - ): Array<UpdateAction> { - const baseActions = [] - const roleActions = [] +): MapActionResult { + return function doMapActions(diff, newObj, oldObj) { + const baseActions: Array<Array<UpdateAction>> = [] + const roleActions: Array<Array<UpdateAction>> = [] baseActions.push( - mapActionGroup( - 'base', - (): Array<UpdateAction> => - actionsMapBase(diff, oldObj, newObj, syncActionConfig) + mapActionGroup('base', () => + actionsMapBase(diff, oldObj, newObj, syncActionConfig) ) ) roleActions.push( - mapActionGroup( - 'roles', - (): Array<UpdateAction> => actionsMapRoles(diff, oldObj, newObj) - ) + mapActionGroup('roles', () => actionsMapRoles(diff, oldObj, newObj)) ) return [...baseActions, ...groupRoleActions(roleActions)].flat() } diff --git a/packages/sync-actions/src/stores-actions.ts b/packages/sync-actions/src/stores-actions.ts index 7e1ad8a6a..bec2ee4d0 100644 --- a/packages/sync-actions/src/stores-actions.ts +++ b/packages/sync-actions/src/stores-actions.ts @@ -1,13 +1,15 @@ 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: 'setName', key: 'name' }, { action: 'setLanguages', key: 'languages' }, { action: 'setDistributionChannels', key: 'distributionChannels' }, { action: 'setSupplyChannels', key: 'supplyChannels' }, ] -export function actionsMapBase(diff, oldObj, newObj) { +export const actionsMapBase: ActionMapBase = (diff, oldObj, newObj) => { return buildBaseAttributesActions({ actions: baseActionsList, diff, diff --git a/packages/sync-actions/src/stores.ts b/packages/sync-actions/src/stores.ts index f8d47ae92..f0d5afbca 100644 --- a/packages/sync-actions/src/stores.ts +++ b/packages/sync-actions/src/stores.ts @@ -43,7 +43,7 @@ export default ( ): SyncAction<Store, StoreUpdateAction> => { const mapActionGroup = createMapActionGroup(actionGroupList) const doMapActions = createStoresMapActions(mapActionGroup) - const onBeforeApplyingDiff = null + const onBeforeApplyingDiff: any = null const buildActions = createBuildActions<Store, StoreUpdateAction>( diff, doMapActions, diff --git a/packages/sync-actions/src/tax-categories-actions.ts b/packages/sync-actions/src/tax-categories-actions.ts index 921b2e2f4..d9d12cb9c 100644 --- a/packages/sync-actions/src/tax-categories-actions.ts +++ b/packages/sync-actions/src/tax-categories-actions.ts @@ -4,29 +4,26 @@ import createBuildArrayActions, { CHANGE_ACTIONS, REMOVE_ACTIONS, } from './utils/create-build-array-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: '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 actionsMapRates(diff, oldObj, newObj) { +export function actionsMapRates(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('rates', { [ADD_ACTIONS]: (newObject) => ({ action: 'addTaxRate', diff --git a/packages/sync-actions/src/tax-categories.ts b/packages/sync-actions/src/tax-categories.ts index 91684d0d3..6e97a265d 100644 --- a/packages/sync-actions/src/tax-categories.ts +++ b/packages/sync-actions/src/tax-categories.ts @@ -10,36 +10,27 @@ import type { import { actionsMapBase, actionsMapRates } from './tax-categories-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' export const actionGroups = ['base', 'rates'] function createTaxCategoriesMapActions( - 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( - 'rates', - (): Array<UpdateAction> => actionsMapRates(diff, oldObj, newObj) - ) + mapActionGroup('rates', () => actionsMapRates(diff, oldObj, newObj)) ) return allActions.flat() } diff --git a/packages/sync-actions/src/types-actions.ts b/packages/sync-actions/src/types-actions.ts index 98c927ee7..ffeadd95e 100644 --- a/packages/sync-actions/src/types-actions.ts +++ b/packages/sync-actions/src/types-actions.ts @@ -6,39 +6,36 @@ import createBuildArrayActions, { } from './utils/create-build-array-actions' import { getDeltaValue } from './utils/diffpatcher' import extractMatchingPairs from './utils/extract-matching-pairs' +import { ActionMapBase } from './utils/create-map-action-group' +import { UpdateAction } from '@commercetools/sdk-client-v2' const REGEX_NUMBER = new RegExp(/^\d+$/) const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/) -const getIsChangedOperation = (key) => REGEX_NUMBER.test(key) -const getIsRemovedOperation = (key) => REGEX_UNDERSCORE_NUMBER.test(key) +const getIsChangedOperation = (key: string) => REGEX_NUMBER.test(key) +const getIsRemovedOperation = (key: string) => REGEX_UNDERSCORE_NUMBER.test(key) -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'changeKey', key: 'key' }, { action: 'changeName', key: 'name' }, { 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, }) } function actionsMapEnums( - fieldName, - attributeType, - attributeDiff, - previous, - next + fieldName: any, + attributeType: any, + attributeDiff: any, + previous: any, + next: any ) { const addEnumActionName = attributeType === 'Enum' ? 'addEnumValue' : 'addLocalizedEnumValue' @@ -58,7 +55,7 @@ function actionsMapEnums( }), [CHANGE_ACTIONS]: (oldEnum, newEnum) => { const oldEnumInNext = next.values.find( - (nextEnum) => nextEnum.key === oldEnum.key + (nextEnum: any) => nextEnum.key === oldEnum.key ) // These `changeActions` would impose a nested structure among @@ -72,7 +69,7 @@ function actionsMapEnums( // check if the label is changed const foundPreviousEnum = previous.values.find( - (previousEnum) => previousEnum.key === newEnum.key + (previousEnum: any) => previousEnum.key === newEnum.key ) const isLabelEqual = deepEqual(foundPreviousEnum.label, newEnum.label) @@ -103,16 +100,16 @@ function actionsMapEnums( }, }) - const actions = [] + const actions: Array<UpdateAction> = [] // following lists are necessary to ensure that when we change the // order of enumValues, we generate one updateAction instead of one at a time. - let newEnumValuesOrder = [] + let newEnumValuesOrder: Array<any> = [] buildArrayActions(attributeDiff, previous, next) .flat() .forEach((updateAction) => { if (updateAction.action === changeEnumOrderActionName) { - newEnumValuesOrder = next.values.map((enumValue) => enumValue.key) + newEnumValuesOrder = next.values.map((enumValue: any) => enumValue.key) } else actions.push(updateAction) }) @@ -132,11 +129,11 @@ function actionsMapEnums( export function actionsMapFieldDefinitions( fieldDefinitionsDiff: { [key: string]: any }, - previous, - next, - diffPaths + previous: any, + next: any, + diffPaths: any ) { - const actions = [] + const actions: Array<UpdateAction> = [] fieldDefinitionsDiff && Object.entries(fieldDefinitionsDiff).forEach(([diffKey, diffValue]) => { const extractedPairs = extractMatchingPairs( @@ -183,7 +180,7 @@ export function actionsMapFieldDefinitions( if (diffValue.length === 3 && diffValue[2] === 3) { actions.push({ action: 'changeFieldDefinitionOrder', - fieldNames: next.map((n) => n.name), + fieldNames: next.map((n: any) => n.name), }) } else { const deltaValue = getDeltaValue(diffValue) diff --git a/packages/sync-actions/src/types.ts b/packages/sync-actions/src/types.ts index 240c3daaf..bd59bd0c4 100644 --- a/packages/sync-actions/src/types.ts +++ b/packages/sync-actions/src/types.ts @@ -1,33 +1,36 @@ import { Type, TypeUpdateAction } from '@commercetools/platform-sdk/src' -import { SyncActionConfig } from '@commercetools/sdk-client-v2' +import { SyncActionConfig, UpdateAction } from '@commercetools/sdk-client-v2' import { actionsMapBase, actionsMapFieldDefinitions } from './types-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' import findMatchingPairs from './utils/find-matching-pairs' const actionGroups = ['base', 'fieldDefinitions'] function createTypeMapActions( - mapActionGroup, - syncActionConfig: { shouldOmitEmptyString?: boolean } -) { - return function doMapActions(diff, next, previous) { - const allActions = [] + mapActionGroup: MapActionGroup, + syncActionConfig?: SyncActionConfig +): MapActionResult { + return function doMapActions(diff, newObj, oldObj) { + const allActions: Array<Array<UpdateAction>> = [] allActions.push( mapActionGroup('base', () => - actionsMapBase(diff, previous, next, syncActionConfig) + actionsMapBase(diff, oldObj, newObj, syncActionConfig) ), mapActionGroup('fieldDefinitions', () => actionsMapFieldDefinitions( diff.fieldDefinitions, - previous.fieldDefinitions, - next.fieldDefinitions, + oldObj.fieldDefinitions, + newObj.fieldDefinitions, findMatchingPairs( diff.fieldDefinitions, - previous.fieldDefinitions, - next.fieldDefinitions, + oldObj.fieldDefinitions, + newObj.fieldDefinitions, 'name' ) ) diff --git a/packages/sync-actions/src/utils/action-map-custom.ts b/packages/sync-actions/src/utils/action-map-custom.ts index d61f6b2c5..b753bbaae 100644 --- a/packages/sync-actions/src/utils/action-map-custom.ts +++ b/packages/sync-actions/src/utils/action-map-custom.ts @@ -1,24 +1,27 @@ import { getDeltaValue } from './diffpatcher' +import { UpdateAction } from '@commercetools/sdk-client-v2' const Actions = { setCustomType: 'setCustomType', setCustomField: 'setCustomField', } -const hasSingleCustomFieldChanged = (diff) => Array.isArray(diff.custom) -const haveMultipleCustomFieldsChanged = (diff) => Boolean(diff.custom.fields) -const hasCustomTypeChanged = (diff) => Boolean(diff.custom.type) -const extractCustomType = (diff, previousObject) => +const hasSingleCustomFieldChanged = (diff: any) => Array.isArray(diff.custom) +const haveMultipleCustomFieldsChanged = (diff: any) => + Boolean(diff.custom.fields) +const hasCustomTypeChanged = (diff: any) => Boolean(diff.custom.type) +const extractCustomType = (diff: any, previousObject: any) => Array.isArray(diff.custom.type) ? getDeltaValue(diff.custom.type, previousObject) : diff.custom.type -const extractTypeId = (type, nextObject) => +const extractTypeId = (type: any, nextObject: any) => Array.isArray(type.id) ? getDeltaValue(type.id) : nextObject.custom.type.id -const extractTypeKey = (type, nextObject) => +const extractTypeKey = (type: any, nextObject: any) => Array.isArray(type.key) ? getDeltaValue(type.key) : nextObject.custom.type.key -const extractTypeFields = (diffedFields, nextFields) => +const extractTypeFields = (diffedFields: any, nextFields: any) => Array.isArray(diffedFields) ? getDeltaValue(diffedFields) : nextFields -const extractFieldValue = (newFields, fieldName) => newFields[fieldName] +const extractFieldValue = (newFields: any, fieldName: string) => + newFields[fieldName] export default function actionsMapCustom( diff: any, @@ -28,7 +31,7 @@ export default function actionsMapCustom( actions: {}, } ) { - const actions = [] + const actions: Array<UpdateAction> = [] const { actions: customPropsActions, ...options } = customProps const actionGroup = { ...Actions, ...customPropsActions } diff --git a/packages/sync-actions/src/utils/clone.ts b/packages/sync-actions/src/utils/clone.ts index c28a356f5..315a6ba74 100644 --- a/packages/sync-actions/src/utils/clone.ts +++ b/packages/sync-actions/src/utils/clone.ts @@ -1,4 +1,4 @@ -export default function clone(obj) { +export default function clone(obj: any) { return JSON.parse(JSON.stringify(obj)) } diff --git a/packages/sync-actions/src/utils/combine-validity-actions.ts b/packages/sync-actions/src/utils/combine-validity-actions.ts index 4cb6b7222..24fb3c459 100644 --- a/packages/sync-actions/src/utils/combine-validity-actions.ts +++ b/packages/sync-actions/src/utils/combine-validity-actions.ts @@ -1,8 +1,13 @@ +import { UpdateAction } from '@commercetools/sdk-client-v2' + const validityActions = ['setValidFrom', 'setValidUntil'] -const isValidityActions = (actionName) => validityActions.includes(actionName) +const isValidityActions = (actionName: string) => + validityActions.includes(actionName) -export default function combineValidityActions(actions = []) { +export default function combineValidityActions( + actions: Array<UpdateAction> = [] +) { const [setValidFromAction, setValidUntilAction] = actions.filter((item) => isValidityActions(item.action) ) diff --git a/packages/sync-actions/src/utils/common-actions.ts b/packages/sync-actions/src/utils/common-actions.ts index 8a170a685..a728083a8 100644 --- a/packages/sync-actions/src/utils/common-actions.ts +++ b/packages/sync-actions/src/utils/common-actions.ts @@ -1,22 +1,23 @@ import clone, { notEmpty } from './clone' import { getDeltaValue, patch } from './diffpatcher' +import { UpdateAction } from '@commercetools/sdk-client-v2' -const normalizeValue = (value) => +const normalizeValue = (value: any) => typeof value === 'string' ? value.trim() : value -export const createIsEmptyValue = (emptyValues) => (value) => +export const createIsEmptyValue = (emptyValues: Array<any>) => (value: any) => emptyValues.some((emptyValue) => emptyValue === normalizeValue(value)) /** * Builds actions for simple object properties, given a list of actions * E.g. [{ action: `changeName`, key: 'name' }] * - * @param {Array} options.actions - a list of actions to be built + * @param {Array} actions - a list of actions to be built * based on the given property - * @param {Object} options.diff - the diff object - * @param {Object} options.oldObj - the object that needs to be updated - * @param {Object} options.newObj - the new representation of the object - * @param {Boolean} options.shouldOmitEmptyString - a flag to determine if we should treat an empty string a NON-value + * @param {Object} diff - the diff object + * @param {Object} oldObj - the object that needs to be updated + * @param {Object} newObj - the new representation of the object + * @param {Boolean} shouldOmitEmptyString - a flag to determine if we should treat an empty string a NON-value */ export function buildBaseAttributesActions({ actions, @@ -25,12 +26,12 @@ export function buildBaseAttributesActions({ newObj, shouldOmitEmptyString, }: { - actions: Array<any> + actions: Array<UpdateAction> diff: any oldObj: any newObj: any shouldOmitEmptyString?: boolean -}) { +}): Array<UpdateAction> { const isEmptyValue = createIsEmptyValue( shouldOmitEmptyString ? [undefined, null, ''] : [undefined, null] ) @@ -70,13 +71,23 @@ export function buildBaseAttributesActions({ * Builds actions for simple reference objects, given a list of actions * E.g. [{ action: `setTaxCategory`, key: 'taxCategory' }] * - * @param {Array} options.actions - a list of actions to be built + * @param {Array} actions - a list of actions to be built * based on the given property - * @param {Object} options.diff - the diff object - * @param {Object} options.oldObj - the object that needs to be updated - * @param {Object} options.newObj - the new representation of the object + * @param {Object} diff - the diff object + * @param {Object} oldObj - the object that needs to be updated + * @param {Object} newObj - the new representation of the object */ -export function buildReferenceActions({ actions, diff, oldObj, newObj }) { +export function buildReferenceActions({ + actions, + diff, + oldObj, + newObj, +}: { + actions: Array<UpdateAction> + diff: any + oldObj: any + newObj: any +}): Array<{ action: string }> { return actions .map((item) => { const action = item.action diff --git a/packages/sync-actions/src/utils/copy-empty-array-props.ts b/packages/sync-actions/src/utils/copy-empty-array-props.ts index cc9f6a7b5..299e2981a 100644 --- a/packages/sync-actions/src/utils/copy-empty-array-props.ts +++ b/packages/sync-actions/src/utils/copy-empty-array-props.ts @@ -8,7 +8,10 @@ const CUSTOM = 'custom' * @param {Object} newObj * @returns {Array} Ordered Array [oldObj, newObj] */ -export default function copyEmptyArrayProps(oldObj = {}, newObj = {}) { +export default function copyEmptyArrayProps( + oldObj: any = {}, + newObj: any = {} +): Array<any> { if (oldObj && newObj) { const nextObjectWithEmptyArray = Object.entries(oldObj).reduce( (merged, [key, value]) => { diff --git a/packages/sync-actions/src/utils/create-build-actions.ts b/packages/sync-actions/src/utils/create-build-actions.ts index c03becb09..31c558c43 100644 --- a/packages/sync-actions/src/utils/create-build-actions.ts +++ b/packages/sync-actions/src/utils/create-build-actions.ts @@ -1,12 +1,18 @@ import { UpdateAction } from '@commercetools/sdk-client-v2' import { deepEqual } from 'fast-equals' import { DeepPartial } from '../types/update-actions' +import { Price, ProductVariant } from '@commercetools/platform-sdk' +import { MapActionResult } from './create-map-action-group' -function applyOnBeforeDiff(before, now, fn?: (before, now) => Array<any>) { +function applyOnBeforeDiff( + before: any, + now: any, + fn?: (before: any, now: any) => Array<any> +) { return fn && typeof fn === 'function' ? fn(before, now) : [before, now] } -const createPriceComparator = (price) => ({ +const createPriceComparator = (price: Price) => ({ value: { currencyCode: price.value.currencyCode }, channel: price.channel, country: price.country, @@ -15,13 +21,16 @@ const createPriceComparator = (price) => ({ validUntil: price.validUntil, }) -function arePricesStructurallyEqual(oldPrice, newPrice) { +function arePricesStructurallyEqual(oldPrice: Price, newPrice: Price) { const oldPriceComparison = createPriceComparator(oldPrice) const newPriceComparison = createPriceComparator(newPrice) return deepEqual(newPriceComparison, oldPriceComparison) } -function extractPriceFromPreviousVariant(newPrice, previousVariant) { +function extractPriceFromPreviousVariant( + newPrice: Price, + previousVariant?: ProductVariant +) { if (!previousVariant) return null const price = previousVariant.prices.find((oldPrice) => arePricesStructurallyEqual(oldPrice, newPrice) @@ -29,7 +38,10 @@ function extractPriceFromPreviousVariant(newPrice, previousVariant) { return price || null } -function injectMissingPriceIds(nextVariants, previousVariants) { +function injectMissingPriceIds( + nextVariants: Array<ProductVariant>, + previousVariants: Array<ProductVariant> +) { return nextVariants.map((newVariant) => { const { prices, ...restOfVariant } = newVariant @@ -44,7 +56,7 @@ function injectMissingPriceIds(nextVariants, previousVariants) { return { ...restOfVariant, prices: prices.map((price) => { - const newPrice = { ...price } + let newPrice: any = { ...price } const oldPrice = extractPriceFromPreviousVariant(price, oldVariant) if (oldPrice) { @@ -69,8 +81,8 @@ function injectMissingPriceIds(nextVariants, previousVariants) { } export default function createBuildActions<S, T extends UpdateAction>( - differ, - doMapActions, + differ: any, + doMapActions: any, onBeforeDiff?: (before: DeepPartial<S>, now: DeepPartial<S>) => Array<any>, buildActionsConfig: any = {} ) { diff --git a/packages/sync-actions/src/utils/create-build-array-actions.ts b/packages/sync-actions/src/utils/create-build-array-actions.ts index b752b630c..526e7142f 100644 --- a/packages/sync-actions/src/utils/create-build-array-actions.ts +++ b/packages/sync-actions/src/utils/create-build-array-actions.ts @@ -1,3 +1,5 @@ +import { UpdateAction } from '@commercetools/sdk-client-v2' + const REGEX_NUMBER = new RegExp(/^\d+$/) const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/) @@ -17,7 +19,7 @@ export const CHANGE_ACTIONS = 'change' * @return {Boolean} Returns true if delta represents a create action, * false otherwise */ -function isCreateAction(obj, key) { +function isCreateAction(obj: { [key: string]: any }, key: string): boolean { return ( REGEX_NUMBER.test(key) && Array.isArray(obj[key]) && obj[key].length === 1 ) @@ -37,7 +39,7 @@ function isCreateAction(obj, key) { * @return {Boolean} Returns true if delta represents a change action, * false otherwise */ -function isChangeAction(obj, key) { +function isChangeAction(obj: any, key: string): boolean { return ( REGEX_NUMBER.test(key) && (typeof obj[key] === 'object' || typeof obj[key] === 'string') @@ -56,7 +58,7 @@ function isChangeAction(obj, key) { * @return {Boolean} Returns true if delta represents a remove action, * false otherwise */ -function isRemoveAction(obj, key) { +function isRemoveAction(obj: any, key: string): boolean { return ( REGEX_UNDERSCORE_NUMBER.test(key) && Array.isArray(obj[key]) && @@ -77,11 +79,29 @@ function isRemoveAction(obj, key) { * return an action object. * @return {Array} The generated array of actions */ -export default function createBuildArrayActions(key, config) { - return function buildArrayActions(diff, oldObj, newObj) { - const addActions = [] - const removeActions = [] - const changeActions = [] +export default function createBuildArrayActions( + key: string, + config: { + [ADD_ACTIONS]?: ( + newItem: any, + key?: number + ) => UpdateAction | Array<UpdateAction> + [REMOVE_ACTIONS]?: (oldItem: any, key?: number) => UpdateAction + [CHANGE_ACTIONS]?: ( + oldAsset: any, + newAsset: any, + key?: number + ) => UpdateAction | Array<UpdateAction> + } +) { + return function buildArrayActions( + diff: any, + oldObj: any, + newObj: any + ): Array<UpdateAction> { + let addActions: Array<UpdateAction> = [] + const removeActions: Array<UpdateAction> = [] + let changeActions: Array<UpdateAction> = [] if (diff[key]) { const arrayDelta = diff[key] @@ -95,7 +115,13 @@ export default function createBuildArrayActions(key, config) { parseInt(index, 10) ) - if (action) addActions.push(action) + if (action) { + if (Array.isArray(action)) { + addActions = addActions.concat(action) + } else { + addActions.push(action) + } + } } else if ( config[CHANGE_ACTIONS] && isChangeAction(arrayDelta, index) @@ -108,7 +134,13 @@ export default function createBuildArrayActions(key, config) { parseInt(index, 10) ) - if (action) changeActions.push(action) + if (action) { + if (Array.isArray(action)) { + changeActions = changeActions.concat(action) + } else { + changeActions.push(action) + } + } } else if ( config[REMOVE_ACTIONS] && isRemoveAction(arrayDelta, index) diff --git a/packages/sync-actions/src/utils/create-map-action-group.ts b/packages/sync-actions/src/utils/create-map-action-group.ts index a4cf8bb24..f22463966 100644 --- a/packages/sync-actions/src/utils/create-map-action-group.ts +++ b/packages/sync-actions/src/utils/create-map-action-group.ts @@ -5,12 +5,34 @@ // { type: 'prices', group: 'allow' }, // { type: 'variants', group: 'ignore' }, // ] -import { ActionGroup } from '@commercetools/sdk-client-v2' +import { ActionGroup, UpdateAction } from '@commercetools/sdk-client-v2' + +export type MapActionGroup = ( + type: string, + fn: () => Array<UpdateAction> +) => Array<UpdateAction> + +export type MapActionResult = ( + diff: any, + newObj: any, + oldObj: any, + config?: any +) => Array<UpdateAction> + +export type ActionMapBase = ( + diff: any, + oldObj: any, + newObj: any, + config?: { shouldOmitEmptyString?: boolean; [key: string]: any } +) => Array<UpdateAction> export default function createMapActionGroup( actionGroups: Array<ActionGroup> = [] -) { - return function mapActionGroup(type: string, fn: () => any) { +): MapActionGroup { + return function mapActionGroup( + type: string, + fn: () => Array<UpdateAction> + ): Array<UpdateAction> { if (!Object.keys(actionGroups).length) return fn() const found = actionGroups.find((c) => c.type === type) diff --git a/packages/sync-actions/src/utils/diffpatcher.ts b/packages/sync-actions/src/utils/diffpatcher.ts index 42dabb8ee..3530ebee8 100644 --- a/packages/sync-actions/src/utils/diffpatcher.ts +++ b/packages/sync-actions/src/utils/diffpatcher.ts @@ -3,7 +3,7 @@ // TODO create an issue here https://github.com/benjamine/jsondiffpatch/issues/new const DiffPatcher = require('jsondiffpatch').DiffPatcher -export function objectHash(obj, index) { +export function objectHash(obj: any, index: any) { const objIndex = `$$index:${index}` return typeof obj === 'object' && obj !== null ? obj.id || obj.name || obj.url || objIndex @@ -31,15 +31,15 @@ const diffpatcher = new DiffPatcher({ }, }) -export function diff(oldObj, newObj) { +export function diff(oldObj: any, newObj: any) { return diffpatcher.diff(oldObj, newObj) } -export function patch(obj, delta) { +export function patch(obj: any, delta: any) { return diffpatcher.patch(obj, delta) } -export function getDeltaValue(arr, originalObject?: any) { +export function getDeltaValue(arr: Array<any> | any, originalObject?: any) { if (!Array.isArray(arr)) throw new Error('Expected array to extract delta value') diff --git a/packages/sync-actions/src/utils/extract-matching-pairs.ts b/packages/sync-actions/src/utils/extract-matching-pairs.ts index 2818eb54a..b871ff651 100644 --- a/packages/sync-actions/src/utils/extract-matching-pairs.ts +++ b/packages/sync-actions/src/utils/extract-matching-pairs.ts @@ -1,4 +1,9 @@ -export default function extractMatchingPairs(hashMap, key, before, now) { +export default function extractMatchingPairs( + hashMap: any, + key: string, + before: any, + now: any +) { let oldObjPos let newObjPos let oldObj diff --git a/packages/sync-actions/src/utils/find-matching-pairs.ts b/packages/sync-actions/src/utils/find-matching-pairs.ts index f1abbf8a3..9e7c75b49 100644 --- a/packages/sync-actions/src/utils/find-matching-pairs.ts +++ b/packages/sync-actions/src/utils/find-matching-pairs.ts @@ -1,7 +1,7 @@ const REGEX_NUMBER = new RegExp(/^\d+$/) const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/) -function preProcessCollection(collection = [], identifier = 'id') { +function preProcessCollection(collection: Array<any> = [], identifier = 'id') { return collection.reduce( (acc, currentValue, currentIndex) => { acc.refByIndex[String(currentIndex)] = currentValue[identifier] @@ -17,10 +17,10 @@ function preProcessCollection(collection = [], identifier = 'id') { // creates a hash of a location of an item in collection1 and collection2 export default function findMatchingPairs( - diff, - before = [], - now = [], - identifier = 'id' + diff: any, + before: Array<any> = [], + now: Array<any> = [], + identifier: string = 'id' ) { const result = {} const { diff --git a/packages/sync-actions/src/zones-actions.ts b/packages/sync-actions/src/zones-actions.ts index 0fb10c257..a033ee7be 100644 --- a/packages/sync-actions/src/zones-actions.ts +++ b/packages/sync-actions/src/zones-actions.ts @@ -4,32 +4,30 @@ 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 { Location } from '@commercetools/platform-sdk' -export const baseActionsList = [ +export const baseActionsList: Array<UpdateAction> = [ { action: 'changeName', key: 'name' }, { action: 'setDescription', key: 'description' }, { action: 'setKey', key: 'key' }, ] -const hasLocation = (locations, otherLocation) => +const hasLocation = (locations: Array<Location>, otherLocation: Location) => locations.some((location) => location.country === otherLocation.country) -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 actionsMapLocations(diff, oldObj, newObj) { +export function actionsMapLocations(diff: any, oldObj: any, newObj: any) { const handler = createBuildArrayActions('locations', { [ADD_ACTIONS]: (newLocation) => ({ action: 'addLocation', @@ -44,7 +42,7 @@ export function actionsMapLocations(diff, oldObj, newObj) { } : null, [CHANGE_ACTIONS]: (oldLocation, newLocation) => { - const result = [] + const result: Array<UpdateAction> = [] // We only remove the location in case that the oldLocation is not // included in the new object diff --git a/packages/sync-actions/src/zones.ts b/packages/sync-actions/src/zones.ts index 272ea3793..fd82a153a 100644 --- a/packages/sync-actions/src/zones.ts +++ b/packages/sync-actions/src/zones.ts @@ -6,37 +6,29 @@ import type { } from '@commercetools/sdk-client-v2' 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' import * as zonesActions from './zones-actions' export const actionGroups = ['base', 'locations'] function createZonesMapActions( - mapActionGroup: ( - type: string, - fn: () => Array<UpdateAction> - ) => Array<UpdateAction>, + mapActionGroup: MapActionGroup, syncActionConfig?: SyncActionConfig -): (diff: any, next: any, previous: 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> => - zonesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig) + mapActionGroup('base', () => + zonesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig) ) ) allActions.push( - mapActionGroup( - 'locations', - (): Array<UpdateAction> => - zonesActions.actionsMapLocations(diff, oldObj, newObj) + mapActionGroup('locations', () => + zonesActions.actionsMapLocations(diff, oldObj, newObj) ).flat() ) return allActions.flat()