Skip to content

Commit b07dbae

Browse files
authored
feat(sync-actions): add support for quotes (#1813)
* feat(sync-actions): add support for quotes * fix(sync-actions): add flow annotation * feat(sync-actions): add support for quotes
1 parent 737c4a6 commit b07dbae

3 files changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { buildBaseAttributesActions } from './utils/common-actions'
2+
3+
export const baseActionsList = [
4+
{ action: 'changeQuoteState', key: 'quoteState' },
5+
{ action: 'requestQuoteRenegotiation', key: 'buyerComment' },
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+
})
17+
}
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 QuotesActions from './quotes-actions'
13+
import * as diffpatcher from './utils/diffpatcher'
14+
15+
const actionGroups = [
16+
'base',
17+
'custom',
18+
]
19+
20+
function createQuotesMapActions(
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+
QuotesActions.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 = createQuotesMapActions(mapActionGroup, syncActionConfig)
63+
64+
const buildActions = createBuildActions(
65+
diffpatcher.diff,
66+
doMapActions,
67+
)
68+
69+
return { buildActions }
70+
}
71+
72+
export { actionGroups }
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import createQuotesSync, { actionGroups } from '../src/quotes'
2+
import { baseActionsList } from '../src/quotes-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 `changeQuoteState` action', () => {
11+
expect(baseActionsList).toEqual(
12+
expect.arrayContaining([{ action: 'changeQuoteState', key: 'quoteState' }])
13+
)
14+
})
15+
16+
test('should contain `requestQuoteRenegotiation` action', () => {
17+
expect(baseActionsList).toEqual(
18+
expect.arrayContaining([{ action: 'requestQuoteRenegotiation', key: 'buyerComment' }])
19+
)
20+
})
21+
22+
test('should contain `transitionState` action', () => {
23+
expect(baseActionsList).toEqual(
24+
expect.arrayContaining([{ action: 'transitionState', key: 'state' }])
25+
)
26+
})
27+
})
28+
})
29+
30+
describe('Actions', () => {
31+
let quotesSync
32+
beforeEach(() => {
33+
quotesSync = createQuotesSync()
34+
})
35+
36+
test('should build `changeQuoteState` action', () => {
37+
const before = { quoteState: 'Pending' }
38+
const now = { quoteState: 'Approved' }
39+
const actual = quotesSync.buildActions(now, before)
40+
const expected = [
41+
{
42+
action: 'changeQuoteState',
43+
...now
44+
}
45+
]
46+
expect(actual).toEqual(expected)
47+
})
48+
49+
test('should build `requestQuoteRenegotiation` action', () => {
50+
const before = { buyerComment: '' }
51+
const now = { buyerComment: 'give me a 10% discount' }
52+
const actual = quotesSync.buildActions(now, before)
53+
const expected = [
54+
{
55+
action: 'requestQuoteRenegotiation',
56+
...now
57+
}
58+
]
59+
expect(actual).toEqual(expected)
60+
})
61+
62+
test('should build `transitionState` action', () => {
63+
const before = {
64+
state : {
65+
typeId : 'state',
66+
id : 'sid1'
67+
}
68+
}
69+
const now = {
70+
state : {
71+
typeId : 'state',
72+
id : 'sid2'
73+
}
74+
}
75+
const actual = quotesSync.buildActions(now, before)
76+
const expected = [
77+
{
78+
action: 'transitionState',
79+
...now
80+
}
81+
]
82+
expect(actual).toEqual(expected)
83+
})
84+
85+
test('should build `setCustomType` action', () => {
86+
const before = {
87+
custom: {
88+
type: {
89+
typeId: 'type',
90+
id: 'customType1',
91+
},
92+
fields: {
93+
customField1: true,
94+
},
95+
},
96+
}
97+
const now = {
98+
custom: {
99+
type: {
100+
typeId: 'type',
101+
id: 'customType2',
102+
},
103+
fields: {
104+
customField1: true,
105+
},
106+
},
107+
}
108+
const actual = quotesSync.buildActions(now, before)
109+
const expected = [{ action: 'setCustomType', ...now.custom }]
110+
expect(actual).toEqual(expected)
111+
})
112+
113+
test('should build `setCustomField` action', () => {
114+
const before = {
115+
custom: {
116+
type: {
117+
typeId: 'type',
118+
id: 'customType1',
119+
},
120+
fields: {
121+
customField1: false,
122+
},
123+
},
124+
}
125+
const now = {
126+
custom: {
127+
type: {
128+
typeId: 'type',
129+
id: 'customType1',
130+
},
131+
fields: {
132+
customField1: true,
133+
},
134+
},
135+
}
136+
const actual = quotesSync.buildActions(now, before)
137+
const expected = [
138+
{
139+
action: 'setCustomField',
140+
name: 'customField1',
141+
value: true,
142+
},
143+
]
144+
expect(actual).toEqual(expected)
145+
})
146+
})

0 commit comments

Comments
 (0)