Skip to content

Commit acdf899

Browse files
authored
chore: add recurring orders sync actions (#1957)
* chore: add recurring orders sync actions * chore: add changeset
1 parent 586ce1d commit acdf899

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

.changeset/quick-cycles-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/sync-actions': patch
3+
---
4+
5+
added recurring orders sync actions

packages/sync-actions/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { default as createSyncCustomers } from './customers'
33
export { default as createSyncInventories } from './inventories'
44
export { default as createSyncProducts } from './products'
55
export { default as createSyncOrders } from './orders'
6+
export { default as createSyncRecurringOrders } from './recurring-orders'
67
export { default as createSyncProductDiscounts } from './product-discounts'
78
export { default as createSyncDiscountCodes } from './discount-codes'
89
export { default as createSyncCustomerGroup } from './customer-group'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { buildBaseAttributesActions } from './utils/common-actions'
2+
3+
export const baseActionsList = [
4+
{ action: 'setKey', key: 'key' },
5+
{ action: 'setRecurringOrderState', key: 'recurringOrderState' },
6+
{ action: 'transitionState', key: 'state' },
7+
]
8+
9+
export function actionsMapBase(diff, oldObj, newObj, config = {}) {
10+
return buildBaseAttributesActions({
11+
actions: baseActionsList,
12+
diff,
13+
oldObj,
14+
newObj,
15+
shouldOmitEmptyString: config.shouldOmitEmptyString,
16+
shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
17+
shouldPreventUnsettingRequiredFields:
18+
config.shouldPreventUnsettingRequiredFields,
19+
})
20+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* @flow */
2+
import flatten from 'lodash.flatten'
3+
import type {
4+
SyncAction,
5+
SyncActionConfig,
6+
ActionGroup,
7+
UpdateAction,
8+
} from 'types/sdk'
9+
import createBuildActions from './utils/create-build-actions'
10+
import createMapActionGroup from './utils/create-map-action-group'
11+
import actionsMapCustom from './utils/action-map-custom'
12+
import * as RecurringOrdersActions from './recurring-orders-actions'
13+
import * as diffpatcher from './utils/diffpatcher'
14+
15+
const actionGroups = ['base', 'custom']
16+
17+
function createRecurringOrdersMapActions(
18+
mapActionGroup: Function,
19+
syncActionConfig: SyncActionConfig
20+
): (
21+
diff: Object,
22+
newObj: Object,
23+
oldObj: Object,
24+
options: Object
25+
) => Array<UpdateAction> {
26+
return function doMapActions(
27+
diff: Object,
28+
newObj: Object,
29+
oldObj: Object
30+
): Array<UpdateAction> {
31+
const allActions = []
32+
33+
allActions.push(
34+
mapActionGroup('base', (): Array<UpdateAction> =>
35+
RecurringOrdersActions.actionsMapBase(
36+
diff,
37+
oldObj,
38+
newObj,
39+
syncActionConfig
40+
)
41+
)
42+
)
43+
44+
allActions.push(
45+
mapActionGroup('custom', (): Array<UpdateAction> =>
46+
actionsMapCustom(diff, newObj, oldObj)
47+
)
48+
)
49+
50+
return flatten(allActions)
51+
}
52+
}
53+
54+
export default (
55+
actionGroupList: Array<ActionGroup>,
56+
syncActionConfig: SyncActionConfig
57+
): SyncAction => {
58+
const mapActionGroup = createMapActionGroup(actionGroupList)
59+
const doMapActions = createRecurringOrdersMapActions(
60+
mapActionGroup,
61+
syncActionConfig
62+
)
63+
64+
const buildActions = createBuildActions(diffpatcher.diff, doMapActions)
65+
66+
return { buildActions }
67+
}
68+
69+
export { actionGroups }
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import createRecurringOrdersSync, {
2+
actionGroups,
3+
} from '../src/recurring-orders'
4+
import { baseActionsList } from '../src/recurring-orders-actions'
5+
6+
describe('Exports', () => {
7+
test('action group list', () => {
8+
expect(actionGroups).toEqual(['base', 'custom'])
9+
})
10+
11+
describe('action list', () => {
12+
test('should contain `setRecurringOrderState` action', () => {
13+
expect(baseActionsList).toEqual(
14+
expect.arrayContaining([
15+
{ action: 'setRecurringOrderState', key: 'recurringOrderState' },
16+
])
17+
)
18+
})
19+
20+
test('should contain `setKey` action', () => {
21+
expect(baseActionsList).toEqual(
22+
expect.arrayContaining([{ action: 'setKey', key: 'key' }])
23+
)
24+
})
25+
26+
test('should contain `transitionState` action', () => {
27+
expect(baseActionsList).toEqual(
28+
expect.arrayContaining([{ action: 'transitionState', key: 'state' }])
29+
)
30+
})
31+
})
32+
})
33+
34+
describe('Actions', () => {
35+
let recurringOrdersSync
36+
beforeEach(() => {
37+
recurringOrdersSync = createRecurringOrdersSync()
38+
})
39+
40+
test('should build `setRecurringOrderState` action', () => {
41+
const before = { recurringOrderState: 'Paused' }
42+
const now = { recurringOrderState: 'Active' }
43+
const actual = recurringOrdersSync.buildActions(now, before)
44+
const expected = [
45+
{
46+
action: 'setRecurringOrderState',
47+
...now,
48+
},
49+
]
50+
expect(actual).toEqual(expected)
51+
})
52+
53+
test('should build `setKey` action', () => {
54+
const before = { key: '' }
55+
const now = { key: 'recurring-order-key-1' }
56+
const actual = recurringOrdersSync.buildActions(now, before)
57+
const expected = [
58+
{
59+
action: 'setKey',
60+
...now,
61+
},
62+
]
63+
expect(actual).toEqual(expected)
64+
})
65+
66+
test('should build `transitionState` action', () => {
67+
const before = {
68+
state: {
69+
typeId: 'state',
70+
id: 'sid1',
71+
},
72+
}
73+
const now = {
74+
state: {
75+
typeId: 'state',
76+
id: 'sid2',
77+
},
78+
}
79+
const actual = recurringOrdersSync.buildActions(now, before)
80+
const expected = [
81+
{
82+
action: 'transitionState',
83+
...now,
84+
},
85+
]
86+
expect(actual).toEqual(expected)
87+
})
88+
89+
test('should build `setCustomType` action', () => {
90+
const before = {
91+
custom: {
92+
type: {
93+
typeId: 'type',
94+
id: 'customType1',
95+
},
96+
fields: {
97+
customField1: true,
98+
},
99+
},
100+
}
101+
const now = {
102+
custom: {
103+
type: {
104+
typeId: 'type',
105+
id: 'customType2',
106+
},
107+
fields: {
108+
customField1: true,
109+
},
110+
},
111+
}
112+
const actual = recurringOrdersSync.buildActions(now, before)
113+
const expected = [{ action: 'setCustomType', ...now.custom }]
114+
expect(actual).toEqual(expected)
115+
})
116+
117+
test('should build `setCustomField` action', () => {
118+
const before = {
119+
custom: {
120+
type: {
121+
typeId: 'type',
122+
id: 'customType1',
123+
},
124+
fields: {
125+
customField1: false,
126+
},
127+
},
128+
}
129+
const now = {
130+
custom: {
131+
type: {
132+
typeId: 'type',
133+
id: 'customType1',
134+
},
135+
fields: {
136+
customField1: true,
137+
},
138+
},
139+
}
140+
const actual = recurringOrdersSync.buildActions(now, before)
141+
const expected = [
142+
{
143+
action: 'setCustomField',
144+
name: 'customField1',
145+
value: true,
146+
},
147+
]
148+
expect(actual).toEqual(expected)
149+
})
150+
})

0 commit comments

Comments
 (0)