Skip to content

Commit 97c21ea

Browse files
authored
feat(sync-action): add support for quote requests (#1815)
1 parent 201f38c commit 97c21ea

3 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { buildBaseAttributesActions } from './utils/common-actions'
2+
3+
export const baseActionsList = [
4+
{ action: 'changeQuoteRequestState', key: 'quoteRequestState' },
5+
{ action: 'transitionState', key: 'state'},
6+
]
7+
8+
export function actionsMapBase(diff, oldObj, newObj, config = {}) {
9+
return buildBaseAttributesActions({
10+
actions: baseActionsList,
11+
diff,
12+
oldObj,
13+
newObj,
14+
shouldOmitEmptyString: config.shouldOmitEmptyString,
15+
})
16+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 QuoteRequestsActions from './quote-requests-actions'
13+
import * as diffpatcher from './utils/diffpatcher'
14+
15+
const actionGroups = [
16+
'base',
17+
'custom',
18+
]
19+
20+
function createQuoteRequestsMapActions(
21+
mapActionGroup: Function,
22+
syncActionConfig: SyncActionConfig
23+
): (
24+
diff: Object,
25+
newObj: Object,
26+
oldObj: Object,
27+
options: Object
28+
) => Array<UpdateAction> {
29+
return function doMapActions(
30+
diff: Object,
31+
newObj: Object,
32+
oldObj: Object,
33+
): Array<UpdateAction> {
34+
const allActions = []
35+
36+
allActions.push(
37+
mapActionGroup('base', (): Array<UpdateAction> =>
38+
QuoteRequestsActions.actionsMapBase(
39+
diff,
40+
oldObj,
41+
newObj,
42+
syncActionConfig
43+
)
44+
)
45+
)
46+
47+
allActions.push(
48+
mapActionGroup('custom', (): Array<UpdateAction> =>
49+
actionsMapCustom(diff, newObj, oldObj)
50+
)
51+
)
52+
53+
return flatten(allActions)
54+
}
55+
}
56+
57+
export default (
58+
actionGroupList: Array<ActionGroup>,
59+
syncActionConfig: SyncActionConfig
60+
): SyncAction => {
61+
const mapActionGroup = createMapActionGroup(actionGroupList)
62+
const doMapActions = createQuoteRequestsMapActions(mapActionGroup, syncActionConfig)
63+
64+
const buildActions = createBuildActions(
65+
diffpatcher.diff,
66+
doMapActions,
67+
)
68+
69+
return { buildActions }
70+
}
71+
72+
export { actionGroups }
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import createQuoteRequestsSync, { actionGroups } from '../src/quote-requests'
2+
import { baseActionsList } from '../src/quote-requests-actions'
3+
4+
describe('Exports', () => {
5+
test('action group list', () => {
6+
expect(actionGroups).toEqual(['base', 'custom'])
7+
})
8+
9+
describe('action list', () => {
10+
test('should contain `changeQuoteRequestState` action', () => {
11+
expect(baseActionsList).toEqual(
12+
expect.arrayContaining([{ action: 'changeQuoteRequestState', key: 'quoteRequestState' }])
13+
)
14+
})
15+
16+
test('should contain `transitionState` action', () => {
17+
expect(baseActionsList).toEqual(
18+
expect.arrayContaining([{ action: 'transitionState', key: 'state' }])
19+
)
20+
})
21+
})
22+
})
23+
24+
describe('Actions', () => {
25+
let quoteRequestsSync
26+
beforeEach(() => {
27+
quoteRequestsSync = createQuoteRequestsSync()
28+
})
29+
30+
test('should build `changeQuoteRequestState` action', () => {
31+
const before = { quoteRequestState: 'Submitted' }
32+
const now = { quoteRequestState: 'Accepted' }
33+
const actual = quoteRequestsSync.buildActions(now, before)
34+
const expected = [
35+
{
36+
action: 'changeQuoteRequestState',
37+
...now
38+
}
39+
]
40+
expect(actual).toEqual(expected)
41+
})
42+
43+
test('should build `transitionState` action', () => {
44+
const before = {
45+
state : {
46+
typeId : 'state',
47+
id : 'sid1'
48+
}
49+
}
50+
const now = {
51+
state : {
52+
typeId : 'state',
53+
id : 'sid2'
54+
}
55+
}
56+
const actual = quoteRequestsSync.buildActions(now, before)
57+
const expected = [
58+
{
59+
action: 'transitionState',
60+
...now
61+
}
62+
]
63+
expect(actual).toEqual(expected)
64+
})
65+
66+
test('should build `setCustomType` action', () => {
67+
const before = {
68+
custom: {
69+
type: {
70+
typeId: 'type',
71+
id: 'customType1',
72+
},
73+
fields: {
74+
customField1: true,
75+
},
76+
},
77+
}
78+
const now = {
79+
custom: {
80+
type: {
81+
typeId: 'type',
82+
id: 'customType2',
83+
},
84+
fields: {
85+
customField1: true,
86+
},
87+
},
88+
}
89+
const actual = quoteRequestsSync.buildActions(now, before)
90+
const expected = [{ action: 'setCustomType', ...now.custom }]
91+
expect(actual).toEqual(expected)
92+
})
93+
94+
test('should build `setCustomField` action', () => {
95+
const before = {
96+
custom: {
97+
type: {
98+
typeId: 'type',
99+
id: 'customType1',
100+
},
101+
fields: {
102+
customField1: false,
103+
},
104+
},
105+
}
106+
const now = {
107+
custom: {
108+
type: {
109+
typeId: 'type',
110+
id: 'customType1',
111+
},
112+
fields: {
113+
customField1: true,
114+
},
115+
},
116+
}
117+
const actual = quoteRequestsSync.buildActions(now, before)
118+
const expected = [
119+
{
120+
action: 'setCustomField',
121+
name: 'customField1',
122+
value: true,
123+
},
124+
]
125+
expect(actual).toEqual(expected)
126+
})
127+
})

0 commit comments

Comments
 (0)