Skip to content

Commit b310e6e

Browse files
authored
feat(sync-actions): create more order update actions (#1687)
* feat(sync-actions): create more order update actions - add `returnInfo` update actions - add `parcel` update actions - write tests for both update actions * test(sync-actions): add test for when returnInfo items are not set
1 parent 9926e09 commit b310e6e

File tree

3 files changed

+320
-0
lines changed

3 files changed

+320
-0
lines changed

packages/sync-actions/src/order-actions.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
import forEach from 'lodash.foreach'
2+
import * as diffpatcher from './utils/diffpatcher'
13
import { buildBaseAttributesActions } from './utils/common-actions'
24
import createBuildArrayActions, {
35
ADD_ACTIONS,
46
CHANGE_ACTIONS,
57
} from './utils/create-build-array-actions'
8+
import extractMatchingPairs from './utils/extract-matching-pairs'
9+
import findMatchingPairs from './utils/find-matching-pairs'
10+
11+
const REGEX_NUMBER = new RegExp(/^\d+$/)
12+
const REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/)
13+
14+
const isAddAction = (key, resource) =>
15+
REGEX_NUMBER.test(key) && Array.isArray(resource) && resource.length
16+
17+
const isRemoveAction = (key, resource) =>
18+
REGEX_UNDERSCORE_NUMBER.test(key) && Number(resource[2]) === 0
619

720
export const baseActionsList = [
821
{ action: 'changeOrderState', key: 'orderState' },
@@ -39,11 +52,100 @@ export function actionsMapDeliveries(diff, oldObj, newObj) {
3952
return handler(deliveriesDiff, oldObj.shippingInfo, newObj.shippingInfo)
4053
}
4154

55+
function _buildDeliveryParcelsAction(
56+
diffedParcels,
57+
oldDelivery = {},
58+
newDelivery = {}
59+
) {
60+
const addParcelActions = []
61+
const removeParcelActions = []
62+
63+
// generate a hashMap to be able to reference the right image from both ends
64+
const matchingParcelPairs = findMatchingPairs(
65+
diffedParcels,
66+
oldDelivery.parcels,
67+
newDelivery.parcels
68+
)
69+
forEach(diffedParcels, (parcel, key) => {
70+
const { oldObj } = extractMatchingPairs(
71+
matchingParcelPairs,
72+
key,
73+
oldDelivery.parcels,
74+
newDelivery.parcels
75+
)
76+
77+
if (isAddAction(key, parcel)) {
78+
addParcelActions.push({
79+
action: 'addParcelToDelivery',
80+
deliveryId: oldDelivery.id,
81+
...diffpatcher.getDeltaValue(parcel),
82+
})
83+
return
84+
}
85+
86+
if (isRemoveAction(key, parcel)) {
87+
removeParcelActions.push({
88+
action: 'removeParcelFromDelivery',
89+
parcelId: oldObj.id,
90+
})
91+
}
92+
})
93+
94+
return [addParcelActions, removeParcelActions]
95+
}
96+
97+
export function actionsMapParcels(diff, oldObj, newObj, deliveryHashMap) {
98+
const shippingInfo = diff.shippingInfo
99+
if (!shippingInfo) return []
100+
101+
const deliveries = shippingInfo.deliveries
102+
if (!deliveries) return []
103+
104+
let addParcelActions = []
105+
let removeParcelActions = []
106+
107+
if (deliveries)
108+
forEach(deliveries, (delivery, key) => {
109+
const { oldObj: oldDelivery, newObj: newDelivery } = extractMatchingPairs(
110+
deliveryHashMap,
111+
key,
112+
oldObj.shippingInfo.deliveries,
113+
newObj.shippingInfo.deliveries
114+
)
115+
if (REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key)) {
116+
const [
117+
addParcelAction,
118+
removeParcelAction,
119+
] = _buildDeliveryParcelsAction(
120+
delivery.parcels,
121+
oldDelivery,
122+
newDelivery
123+
)
124+
125+
addParcelActions = addParcelActions.concat(addParcelAction)
126+
removeParcelActions = removeParcelActions.concat(removeParcelAction)
127+
}
128+
})
129+
130+
return removeParcelActions.concat(addParcelActions)
131+
}
132+
42133
export function actionsMapReturnsInfo(diff, oldObj, newObj) {
43134
const returnInfoDiff = diff.returnInfo
44135
if (!returnInfoDiff) return []
45136

46137
const handler = createBuildArrayActions('returnInfo', {
138+
[ADD_ACTIONS]: (newReturnInfo) => {
139+
if (newReturnInfo.items) {
140+
return [
141+
{
142+
action: 'addReturnInfo',
143+
...newReturnInfo,
144+
},
145+
]
146+
}
147+
return []
148+
},
47149
[CHANGE_ACTIONS]: (oldSReturnInfo, newReturnInfo) => {
48150
const updateActions = Object.keys(returnInfoDiff).reduce(
49151
(itemActions, key) => {

packages/sync-actions/src/orders.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import createMapActionGroup from './utils/create-map-action-group'
1111
import actionsMapCustom from './utils/action-map-custom'
1212
import * as orderActions from './order-actions'
1313
import * as diffpatcher from './utils/diffpatcher'
14+
import findMatchingPairs from './utils/find-matching-pairs'
1415

1516
export const actionGroups = ['base', 'deliveries']
1617

@@ -24,6 +25,15 @@ function createOrderMapActions(
2425
oldObj: Object /* , options */
2526
): Array<UpdateAction> {
2627
const allActions = []
28+
let deliveryHashMap
29+
30+
if (diff.shippingInfo && diff.shippingInfo.deliveries) {
31+
deliveryHashMap = findMatchingPairs(
32+
diff.shippingInfo.deliveries,
33+
oldObj.shippingInfo.deliveries,
34+
newObj.shippingInfo.deliveries
35+
)
36+
}
2737

2838
allActions.push(
2939
mapActionGroup('base', (): Array<UpdateAction> =>
@@ -37,6 +47,12 @@ function createOrderMapActions(
3747
)
3848
)
3949

50+
allActions.push(
51+
mapActionGroup('parcels', (): Array<UpdateAction> =>
52+
orderActions.actionsMapParcels(diff, oldObj, newObj, deliveryHashMap)
53+
)
54+
)
55+
4056
allActions.push(
4157
flatten(
4258
mapActionGroup('returnInfo', (): Array<UpdateAction> =>

packages/sync-actions/test/order-sync.spec.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,209 @@ describe('Actions', () => {
8989
})
9090
})
9191

92+
describe('parcels', () => {
93+
test('should add `parcel` action', () => {
94+
const before = {
95+
shippingInfo: {
96+
deliveries: [
97+
{
98+
id: 'id-1',
99+
parcels: [
100+
{
101+
id: 'unique-id-1',
102+
measurements: {
103+
heightInMillimeter: 20,
104+
lengthInMillimeter: 40,
105+
widthInMillimeter: 5,
106+
weightInGram: 10,
107+
},
108+
trackingData: {
109+
trackingId: 'tracking-id-1',
110+
},
111+
},
112+
],
113+
},
114+
],
115+
},
116+
}
117+
118+
const now = {
119+
shippingInfo: {
120+
deliveries: [
121+
{
122+
id: 'id-1',
123+
parcels: [
124+
{
125+
id: 'unique-id-1',
126+
measurements: {
127+
heightInMillimeter: 20,
128+
lengthInMillimeter: 40,
129+
widthInMillimeter: 5,
130+
weightInGram: 10,
131+
},
132+
trackingData: {
133+
trackingId: 'tracking-id-1',
134+
},
135+
},
136+
{
137+
measurements: {
138+
heightInMillimeter: 10,
139+
lengthInMillimeter: 20,
140+
widthInMillimeter: 2,
141+
weightInGram: 5,
142+
},
143+
trackingData: {
144+
trackingId: 'tracking-id-2',
145+
},
146+
},
147+
],
148+
},
149+
],
150+
},
151+
}
152+
153+
const actual = orderSync.buildActions(now, before)
154+
const expected = [
155+
{
156+
action: 'addParcelToDelivery',
157+
deliveryId: now.shippingInfo.deliveries[0].id,
158+
measurements: now.shippingInfo.deliveries[0].parcels[1].measurements,
159+
trackingData: now.shippingInfo.deliveries[0].parcels[1].trackingData,
160+
},
161+
]
162+
163+
expect(actual).toEqual(expected)
164+
})
165+
166+
test('should create remove `parcel` action', () => {
167+
const before = {
168+
shippingInfo: {
169+
deliveries: [
170+
{
171+
id: 'id-1',
172+
parcels: [
173+
{
174+
id: 'unique-id-1',
175+
measurements: {
176+
heightInMillimeter: 20,
177+
lengthInMillimeter: 40,
178+
widthInMillimeter: 5,
179+
weightInGram: 10,
180+
},
181+
trackingData: {
182+
trackingId: 'tracking-id-1',
183+
},
184+
},
185+
],
186+
},
187+
],
188+
},
189+
}
190+
191+
const now = {
192+
shippingInfo: {
193+
deliveries: [
194+
{
195+
id: 'id-1',
196+
parcels: [],
197+
},
198+
],
199+
},
200+
}
201+
202+
const actual = orderSync.buildActions(now, before)
203+
const expected = [
204+
{
205+
action: 'removeParcelFromDelivery',
206+
parcelId: before.shippingInfo.deliveries[0].parcels[0].id,
207+
},
208+
]
209+
210+
expect(actual).toEqual(expected)
211+
})
212+
})
213+
92214
describe('returnInfo', () => {
215+
test('should not build `returnInfo` action if items are not set', () => {
216+
const before = {
217+
returnInfo: [],
218+
}
219+
220+
const now = {
221+
returnInfo: [
222+
{
223+
returnTrackingId: 'tracking-id-1',
224+
returnDate: '21-04-30T09:21:15.003Z',
225+
},
226+
],
227+
}
228+
229+
const actual = orderSync.buildActions(now, before)
230+
const expected = []
231+
expect(actual).toEqual(expected)
232+
})
233+
234+
test('should add `returnInfo` action', () => {
235+
const before = {
236+
returnInfo: [],
237+
}
238+
239+
const now = {
240+
returnInfo: [
241+
{
242+
returnTrackingId: 'tracking-id-1',
243+
items: [
244+
{
245+
id: 'test-1',
246+
type: 'LineItemReturnItem',
247+
quantity: 1,
248+
lineItemId: '1',
249+
shipmentState: 'Advised',
250+
paymentState: 'Initial',
251+
},
252+
{
253+
id: 'test-2',
254+
type: 'LineItemReturnItem',
255+
quantity: 1,
256+
lineItemId: '1',
257+
shipmentState: 'Advised',
258+
paymentState: 'Initial',
259+
},
260+
],
261+
},
262+
{
263+
returnTrackingId: 'tracking-id-2',
264+
items: [
265+
{
266+
id: 'test-3',
267+
type: 'LineItemReturnItem',
268+
quantity: 2,
269+
lineItemId: '2',
270+
shipmentState: 'Advised',
271+
paymentState: 'Initial',
272+
},
273+
],
274+
},
275+
],
276+
}
277+
278+
const actual = orderSync.buildActions(now, before)
279+
const expected = [
280+
{
281+
action: 'addReturnInfo',
282+
returnTrackingId: now.returnInfo[0].returnTrackingId,
283+
items: now.returnInfo[0].items,
284+
},
285+
{
286+
action: 'addReturnInfo',
287+
returnTrackingId: now.returnInfo[1].returnTrackingId,
288+
items: now.returnInfo[1].items,
289+
},
290+
]
291+
292+
expect(actual).toEqual(expected)
293+
})
294+
93295
test('should build `setReturnShipmentState` action', () => {
94296
const before = {
95297
returnInfo: [

0 commit comments

Comments
 (0)