Skip to content

Commit 6740404

Browse files
authored
feat(syncactions): add support for apiextensions, businessunits and subscriptions (#1909)
* feat(syncactions): add support for apiextensions, businessunits and subscriptions * chore(syncactions): add changeset
1 parent ae5f22c commit 6740404

11 files changed

+1282
-0
lines changed

.changeset/great-grapes-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/sync-actions': minor
3+
---
4+
5+
Added support for extensions, business-units and subscriptions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { buildBaseAttributesActions } from './utils/common-actions'
2+
3+
export const baseActionsList = [
4+
{ action: 'setKey', key: 'key' },
5+
{ action: 'changeTriggers', key: 'triggers' },
6+
{ action: 'setTimeoutInMs', key: 'timeoutInMs' },
7+
{ action: 'changeDestination', key: 'destination' },
8+
]
9+
10+
export const actionsMapBase = (diff, oldObj, newObj, config) => {
11+
return buildBaseAttributesActions({
12+
actions: baseActionsList,
13+
diff,
14+
oldObj,
15+
newObj,
16+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
17+
})
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import flatten from 'lodash.flatten'
2+
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
3+
import createBuildActions from './utils/create-build-actions'
4+
import createMapActionGroup from './utils/create-map-action-group'
5+
import { actionsMapBase } from './api-extensions-actions'
6+
import * as diffpatcher from './utils/diffpatcher'
7+
8+
export const actionGroups = ['base']
9+
10+
const createApiExtensionsMapActions = (mapActionGroup, syncActionConfig) => {
11+
return function doMapActions(diff, newObj, oldObj) {
12+
const allActions = []
13+
14+
allActions.push(
15+
mapActionGroup('base', () =>
16+
actionsMapBase(diff, oldObj, newObj, syncActionConfig)
17+
)
18+
)
19+
20+
return flatten(allActions)
21+
}
22+
}
23+
24+
export default (
25+
actionGroupList: Array<ActionGroup>,
26+
syncActionConfig: SyncActionConfig
27+
): SyncAction => {
28+
const mapActionGroup = createMapActionGroup(actionGroupList)
29+
const doMapActions = createApiExtensionsMapActions(
30+
mapActionGroup,
31+
syncActionConfig
32+
)
33+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
34+
return { buildActions }
35+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import createBuildArrayActions, {
2+
ADD_ACTIONS,
3+
CHANGE_ACTIONS,
4+
REMOVE_ACTIONS,
5+
} from './utils/create-build-array-actions'
6+
import { buildBaseAttributesActions } from './utils/common-actions'
7+
8+
export const baseActionsList = [
9+
{
10+
action: 'setStores',
11+
key: 'stores',
12+
},
13+
{ action: 'changeAssociateMode', key: 'associateMode' },
14+
{ action: 'changeApprovalRuleMode', key: 'approvalRuleMode' },
15+
{
16+
action: 'changeName',
17+
key: 'name',
18+
},
19+
{ action: 'changeParentUnit', key: 'parentUnit' },
20+
{ action: 'changeStatus', key: 'status' },
21+
{ action: 'setContactEmail', key: 'contactEmail' },
22+
{ action: 'setStoreMode', key: 'storeMode' },
23+
]
24+
25+
export const actionsMapAssociates = (diff, oldObj, newObj) => {
26+
const handler = createBuildArrayActions('associates', {
27+
[ADD_ACTIONS]: (newObject) => ({
28+
action: 'addAssociate',
29+
associate: newObject,
30+
}),
31+
[REMOVE_ACTIONS]: (objectToRemove) => ({
32+
action: 'removeAssociate',
33+
customer: {
34+
typeId: 'customer',
35+
id: objectToRemove.customer.id,
36+
},
37+
}),
38+
[CHANGE_ACTIONS]: (oldObject, updatedObject) => ({
39+
action: 'changeAssociate',
40+
associate: updatedObject,
41+
}),
42+
})
43+
44+
return handler(diff, oldObj, newObj)
45+
}
46+
47+
export const actionsMapBase = (diff, oldObj, newObj, config) => {
48+
return buildBaseAttributesActions({
49+
actions: baseActionsList,
50+
diff,
51+
oldObj,
52+
newObj,
53+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
54+
})
55+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import flatten from 'lodash.flatten'
2+
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
3+
import createMapActionGroup from './utils/create-map-action-group'
4+
import createBuildActions from './utils/create-build-actions'
5+
import * as customerActions from './customer-actions'
6+
import actionsMapCustom from './utils/action-map-custom'
7+
import * as businessUnitActions from './business-units-actions'
8+
import * as diffpatcher from './utils/diffpatcher'
9+
10+
const createCustomerMapActions = (mapActionGroup, syncActionConfig) => {
11+
return function doMapActions(diff, newObj, oldObj) {
12+
const allActions = []
13+
14+
allActions.push(
15+
mapActionGroup('base', () =>
16+
businessUnitActions.actionsMapBase(
17+
diff,
18+
oldObj,
19+
newObj,
20+
syncActionConfig
21+
)
22+
)
23+
)
24+
25+
allActions.push(
26+
mapActionGroup('addresses', () =>
27+
customerActions.actionsMapAddresses(diff, oldObj, newObj)
28+
)
29+
)
30+
31+
allActions.push(
32+
mapActionGroup('base', () =>
33+
customerActions.actionsMapSetDefaultBase(
34+
diff,
35+
oldObj,
36+
newObj,
37+
syncActionConfig
38+
)
39+
)
40+
)
41+
42+
allActions.push(
43+
mapActionGroup('billingAddressIds', () =>
44+
customerActions.actionsMapBillingAddresses(diff, oldObj, newObj)
45+
)
46+
)
47+
48+
allActions.push(
49+
mapActionGroup('shippingAddressIds', () =>
50+
customerActions.actionsMapShippingAddresses(diff, oldObj, newObj)
51+
)
52+
)
53+
54+
allActions.push(
55+
mapActionGroup('associates', () =>
56+
businessUnitActions.actionsMapAssociates(diff, oldObj, newObj)
57+
)
58+
)
59+
60+
allActions.push(
61+
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
62+
)
63+
64+
return flatten(allActions)
65+
}
66+
}
67+
68+
export default (
69+
actionGroupList: Array<ActionGroup>,
70+
syncActionConfig: SyncActionConfig
71+
): SyncAction => {
72+
const mapActionGroup = createMapActionGroup(actionGroupList)
73+
const doMapActions = createCustomerMapActions(
74+
mapActionGroup,
75+
syncActionConfig
76+
)
77+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
78+
return { buildActions }
79+
}

packages/sync-actions/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ export { default as createSyncStores } from './stores'
2020
export { default as createSyncProductSelections } from './product-selections'
2121
export { default as createSyncStandalonePrices } from './prices'
2222
export { default as createSyncAttributeGroups } from './attribute-groups'
23+
export { default as createSyncApiExtensions } from './api-extensions'
24+
export { default as createSyncBusinessUnits } from './business-units'
25+
export { default as createSyncSubscriptions } from './subscriptions'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { buildBaseAttributesActions } from './utils/common-actions'
2+
3+
export const baseActionsList = [
4+
{ action: 'setKey', key: 'key' },
5+
{ action: 'setMessages', key: 'messages' },
6+
{ action: 'setChanges', key: 'changes' },
7+
{ action: 'changeDestination', key: 'destination' },
8+
]
9+
10+
export const actionsMapBase = (diff, oldObj, newObj, config) => {
11+
return buildBaseAttributesActions({
12+
actions: baseActionsList,
13+
diff,
14+
oldObj,
15+
newObj,
16+
shouldOmitEmptyString: config?.shouldOmitEmptyString,
17+
})
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { SyncAction, ActionGroup, SyncActionConfig } from 'types/sdk'
2+
import createBuildActions from './utils/create-build-actions'
3+
import createMapActionGroup from './utils/create-map-action-group'
4+
import actionsMapCustom from './utils/action-map-custom'
5+
import * as subscriptionsActions from './subscriptions-actions'
6+
import * as diffpatcher from './utils/diffpatcher'
7+
8+
export const actionGroups = ['base']
9+
10+
const createSubscriptionsMapActions = (mapActionGroup, syncActionConfig) => {
11+
return function doMapActions(diff, newObj, oldObj) {
12+
const allActions = []
13+
14+
allActions.push(
15+
mapActionGroup('base', () =>
16+
subscriptionsActions.actionsMapBase(
17+
diff,
18+
oldObj,
19+
newObj,
20+
syncActionConfig
21+
)
22+
)
23+
)
24+
25+
allActions.push(
26+
mapActionGroup('custom', () => actionsMapCustom(diff, newObj, oldObj))
27+
)
28+
29+
return allActions.flat()
30+
}
31+
}
32+
33+
export default (
34+
actionGroupList: Array<ActionGroup>,
35+
syncActionConfig: SyncActionConfig
36+
): SyncAction => {
37+
const mapActionGroup = createMapActionGroup(actionGroupList)
38+
const doMapActions = createSubscriptionsMapActions(
39+
mapActionGroup,
40+
syncActionConfig
41+
)
42+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
43+
return { buildActions }
44+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import apiExtensionsSyncFn, { actionGroups } from '../src/api-extensions'
2+
import { baseActionsList } from '../src/api-extensions-actions'
3+
4+
describe('Exports', () => {
5+
test('action group list', () => {
6+
expect(actionGroups).toEqual(['base'])
7+
})
8+
9+
describe('action list', () => {
10+
test('should contain `setKey` action', () => {
11+
expect(baseActionsList).toEqual(
12+
expect.arrayContaining([{ action: 'setKey', key: 'key' }])
13+
)
14+
})
15+
test('should contain `changeTriggers` action', () => {
16+
expect(baseActionsList).toEqual(
17+
expect.arrayContaining([{ action: 'changeTriggers', key: 'triggers' }])
18+
)
19+
})
20+
test('should contain `setTimeoutInMs` action', () => {
21+
expect(baseActionsList).toEqual(
22+
expect.arrayContaining([
23+
{ action: 'setTimeoutInMs', key: 'timeoutInMs' },
24+
])
25+
)
26+
})
27+
test('should contain `changeDestination` action', () => {
28+
expect(baseActionsList).toEqual(
29+
expect.arrayContaining([
30+
{ action: 'changeDestination', key: 'destination' },
31+
])
32+
)
33+
})
34+
})
35+
})
36+
37+
describe('Actions', () => {
38+
let apiExtensionsSync = apiExtensionsSyncFn()
39+
beforeEach(() => {
40+
apiExtensionsSync = apiExtensionsSyncFn()
41+
})
42+
43+
test('should build `setKey` action', () => {
44+
const before = { key: 'keyBefore' }
45+
const now = { key: 'keyAfter' }
46+
const actual = apiExtensionsSync.buildActions(now, before)
47+
const expected = [
48+
{
49+
action: 'setKey',
50+
...now,
51+
},
52+
]
53+
expect(actual).toEqual(expected)
54+
})
55+
56+
test('should build `changeDestination` action', () => {
57+
const before = {}
58+
const now = {
59+
destination: {
60+
type: 'GoogleCloudFunction',
61+
url: 'url',
62+
},
63+
}
64+
const actual = apiExtensionsSync.buildActions(now, before)
65+
const expected = [
66+
{
67+
action: 'changeDestination',
68+
destination: {
69+
type: 'GoogleCloudFunction',
70+
url: 'url',
71+
},
72+
},
73+
]
74+
expect(actual).toEqual(expected)
75+
})
76+
77+
test('should build `setTimeoutInMs` action', () => {
78+
const before = { timeoutInMs: 5 }
79+
const now = {
80+
timeoutInMs: 10,
81+
}
82+
const actual = apiExtensionsSync.buildActions(now, before)
83+
const expected = [
84+
{
85+
action: 'setTimeoutInMs',
86+
timeoutInMs: 10,
87+
},
88+
]
89+
expect(actual).toEqual(expected)
90+
})
91+
92+
test('should build `changeTriggers` action', () => {
93+
const before = {}
94+
const now = {
95+
triggers: [
96+
{
97+
resourceTypeId: 'cart',
98+
actions: ['Create', 'Update'],
99+
condition: 'field is defined and field has changed',
100+
},
101+
],
102+
}
103+
const actual = apiExtensionsSync.buildActions(now, before)
104+
const expected = [
105+
{
106+
action: 'changeTriggers',
107+
triggers: [
108+
{
109+
resourceTypeId: 'cart',
110+
actions: ['Create', 'Update'],
111+
condition: 'field is defined and field has changed',
112+
},
113+
],
114+
},
115+
]
116+
expect(actual).toEqual(expected)
117+
})
118+
})

0 commit comments

Comments
 (0)