Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(syncactions): add support for apiextensions, businessunits and subscriptions #1909

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-grapes-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools/sync-actions': minor
---

Added support for extensions, business-units and subscriptions
18 changes: 18 additions & 0 deletions packages/sync-actions/src/api-extensions-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { buildBaseAttributesActions } from './utils/common-actions'

export const baseActionsList = [
{ action: 'setKey', key: 'key' },
{ action: 'changeTriggers', key: 'triggers' },
{ action: 'setTimeoutInMs', key: 'timeoutInMs' },
{ action: 'changeDestination', key: 'destination' },
]

export const actionsMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}
35 changes: 35 additions & 0 deletions packages/sync-actions/src/api-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import flatten from 'lodash.flatten'
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import { actionsMapBase } from './api-extensions-actions'
import * as diffpatcher from './utils/diffpatcher'

export const actionGroups = ['base']

const createApiExtensionsMapActions = (mapActionGroup, syncActionConfig) => {
return function doMapActions(diff, newObj, oldObj) {
const allActions = []

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

return flatten(allActions)
}
}

export default (
actionGroupList: Array<ActionGroup>,
syncActionConfig: SyncActionConfig
): SyncAction => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createApiExtensionsMapActions(
mapActionGroup,
syncActionConfig
)
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
return { buildActions }
}
55 changes: 55 additions & 0 deletions packages/sync-actions/src/business-units-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import createBuildArrayActions, {
ADD_ACTIONS,
CHANGE_ACTIONS,
REMOVE_ACTIONS,
} from './utils/create-build-array-actions'
import { buildBaseAttributesActions } from './utils/common-actions'

export const baseActionsList = [
{
action: 'setStores',
key: 'stores',
},
{ action: 'changeAssociateMode', key: 'associateMode' },
{ action: 'changeApprovalRuleMode', key: 'approvalRuleMode' },
{
action: 'changeName',
key: 'name',
},
{ action: 'changeParentUnit', key: 'parentUnit' },
{ action: 'changeStatus', key: 'status' },
{ action: 'setContactEmail', key: 'contactEmail' },
{ action: 'setStoreMode', key: 'storeMode' },
]

export const actionsMapAssociates = (diff, oldObj, newObj) => {
const handler = createBuildArrayActions('associates', {
[ADD_ACTIONS]: (newObject) => ({
action: 'addAssociate',
associate: newObject,
}),
[REMOVE_ACTIONS]: (objectToRemove) => ({
action: 'removeAssociate',
customer: {
typeId: 'customer',
id: objectToRemove.customer.id,
},
}),
[CHANGE_ACTIONS]: (oldObject, updatedObject) => ({
action: 'changeAssociate',
associate: updatedObject,
}),
})

return handler(diff, oldObj, newObj)
}

export const actionsMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}
79 changes: 79 additions & 0 deletions packages/sync-actions/src/business-units.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import flatten from 'lodash.flatten'
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
import createMapActionGroup from './utils/create-map-action-group'
import createBuildActions from './utils/create-build-actions'
import * as customerActions from './customer-actions'
import actionsMapCustom from './utils/action-map-custom'
import * as businessUnitActions from './business-units-actions'
import * as diffpatcher from './utils/diffpatcher'

const createCustomerMapActions = (mapActionGroup, syncActionConfig) => {
return function doMapActions(diff, newObj, oldObj) {
const allActions = []

allActions.push(
mapActionGroup('base', () =>
businessUnitActions.actionsMapBase(
diff,
oldObj,
newObj,
syncActionConfig
)
)
)

allActions.push(
mapActionGroup('addresses', () =>
customerActions.actionsMapAddresses(diff, oldObj, newObj)
)
)

allActions.push(
mapActionGroup('base', () =>
customerActions.actionsMapSetDefaultBase(
diff,
oldObj,
newObj,
syncActionConfig
)
)
)

allActions.push(
mapActionGroup('billingAddressIds', () =>
customerActions.actionsMapBillingAddresses(diff, oldObj, newObj)
)
)

allActions.push(
mapActionGroup('shippingAddressIds', () =>
customerActions.actionsMapShippingAddresses(diff, oldObj, newObj)
)
)

allActions.push(
mapActionGroup('associates', () =>
businessUnitActions.actionsMapAssociates(diff, oldObj, newObj)
)
)

allActions.push(
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
)

return flatten(allActions)
}
}

export default (
actionGroupList: Array<ActionGroup>,
syncActionConfig: SyncActionConfig
): SyncAction => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createCustomerMapActions(
mapActionGroup,
syncActionConfig
)
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
return { buildActions }
}
3 changes: 3 additions & 0 deletions packages/sync-actions/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ export { default as createSyncStores } from './stores'
export { default as createSyncProductSelections } from './product-selections'
export { default as createSyncStandalonePrices } from './prices'
export { default as createSyncAttributeGroups } from './attribute-groups'
export { default as createSyncApiExtensions } from './api-extensions'
export { default as createSyncBusinessUnits } from './business-units'
export { default as createSyncSubscriptions } from './subscriptions'
18 changes: 18 additions & 0 deletions packages/sync-actions/src/subscriptions-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { buildBaseAttributesActions } from './utils/common-actions'

export const baseActionsList = [
{ action: 'setKey', key: 'key' },
{ action: 'setMessages', key: 'messages' },
{ action: 'setChanges', key: 'changes' },
{ action: 'changeDestination', key: 'destination' },
]

export const actionsMapBase = (diff, oldObj, newObj, config) => {
return buildBaseAttributesActions({
actions: baseActionsList,
diff,
oldObj,
newObj,
shouldOmitEmptyString: config?.shouldOmitEmptyString,
})
}
44 changes: 44 additions & 0 deletions packages/sync-actions/src/subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
import createBuildActions from './utils/create-build-actions'
import createMapActionGroup from './utils/create-map-action-group'
import actionsMapCustom from './utils/action-map-custom'
import * as subscriptionsActions from './subscriptions-actions'
import * as diffpatcher from './utils/diffpatcher'

export const actionGroups = ['base']

const createSubscriptionsMapActions = (mapActionGroup, syncActionConfig) => {
return function doMapActions(diff, newObj, oldObj) {
const allActions = []

allActions.push(
mapActionGroup('base', () =>
subscriptionsActions.actionsMapBase(
diff,
oldObj,
newObj,
syncActionConfig
)
)
)

allActions.push(
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
)

return allActions.flat()
}
}

export default (
actionGroupList: Array<ActionGroup>,
syncActionConfig: SyncActionConfig
): SyncAction => {
const mapActionGroup = createMapActionGroup(actionGroupList)
const doMapActions = createSubscriptionsMapActions(
mapActionGroup,
syncActionConfig
)
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
return { buildActions }
}
118 changes: 118 additions & 0 deletions packages/sync-actions/test/api-extensions-sync.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import apiExtensionsSyncFn, { actionGroups } from '../src/api-extensions'
import { baseActionsList } from '../src/api-extensions-actions'

describe('Exports', () => {
test('action group list', () => {
expect(actionGroups).toEqual(['base'])
})

describe('action list', () => {
test('should contain `setKey` action', () => {
expect(baseActionsList).toEqual(
expect.arrayContaining([{ action: 'setKey', key: 'key' }])
)
})
test('should contain `changeTriggers` action', () => {
expect(baseActionsList).toEqual(
expect.arrayContaining([{ action: 'changeTriggers', key: 'triggers' }])
)
})
test('should contain `setTimeoutInMs` action', () => {
expect(baseActionsList).toEqual(
expect.arrayContaining([
{ action: 'setTimeoutInMs', key: 'timeoutInMs' },
])
)
})
test('should contain `changeDestination` action', () => {
expect(baseActionsList).toEqual(
expect.arrayContaining([
{ action: 'changeDestination', key: 'destination' },
])
)
})
})
})

describe('Actions', () => {
let apiExtensionsSync = apiExtensionsSyncFn()
beforeEach(() => {
apiExtensionsSync = apiExtensionsSyncFn()
})

test('should build `setKey` action', () => {
const before = { key: 'keyBefore' }
const now = { key: 'keyAfter' }
const actual = apiExtensionsSync.buildActions(now, before)
const expected = [
{
action: 'setKey',
...now,
},
]
expect(actual).toEqual(expected)
})

test('should build `changeDestination` action', () => {
const before = {}
const now = {
destination: {
type: 'GoogleCloudFunction',
url: 'url',
},
}
const actual = apiExtensionsSync.buildActions(now, before)
const expected = [
{
action: 'changeDestination',
destination: {
type: 'GoogleCloudFunction',
url: 'url',
},
},
]
expect(actual).toEqual(expected)
})

test('should build `setTimeoutInMs` action', () => {
const before = { timeoutInMs: 5 }
const now = {
timeoutInMs: 10,
}
const actual = apiExtensionsSync.buildActions(now, before)
const expected = [
{
action: 'setTimeoutInMs',
timeoutInMs: 10,
},
]
expect(actual).toEqual(expected)
})

test('should build `changeTriggers` action', () => {
const before = {}
const now = {
triggers: [
{
resourceTypeId: 'cart',
actions: ['Create', 'Update'],
condition: 'field is defined and field has changed',
},
],
}
const actual = apiExtensionsSync.buildActions(now, before)
const expected = [
{
action: 'changeTriggers',
triggers: [
{
resourceTypeId: 'cart',
actions: ['Create', 'Update'],
condition: 'field is defined and field has changed',
},
],
},
]
expect(actual).toEqual(expected)
})
})
Loading