Skip to content

Commit 856929e

Browse files
authored
fix(sync-actions/orders): adjust diff calculation of returnInfo items (#1818)
* test(sync-actions/orders): add test to reproduce bug * fix(sync-actions/orders): adjust diff calculation of returnInfo items * docs(release): add changeset * refactor(sync-actions): apply pr reviews * fix(test): increase performance time test * fix(test): coverage
1 parent fe12dfa commit 856929e

3 files changed

Lines changed: 178 additions & 36 deletions

File tree

.changeset/flat-phones-explain.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+
fix(sync-actions/orders): adjust diff calculation of returnInfo items

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

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -146,42 +146,29 @@ export function actionsMapReturnsInfo(diff, oldObj, newObj) {
146146
}
147147
return []
148148
},
149-
[CHANGE_ACTIONS]: (oldSReturnInfo, newReturnInfo) => {
150-
const updateActions = Object.keys(returnInfoDiff).reduce(
151-
(itemActions, key) => {
152-
const { items = {} } = returnInfoDiff[key]
153-
if (Object.keys(items).length > 0) {
154-
return [
155-
...itemActions,
156-
...Object.keys(items).reduce((actions, index) => {
157-
const itActions = []
158-
const item = newReturnInfo.items[index]
159-
if (items[index].shipmentState) {
160-
itActions.push({
161-
action: 'setReturnShipmentState',
162-
returnItemId: item.id,
163-
shipmentState: item.shipmentState,
164-
})
165-
}
166-
if (items[index].paymentState) {
167-
itActions.push({
168-
action: 'setReturnPaymentState',
169-
returnItemId: item.id,
170-
paymentState: item.paymentState,
171-
})
172-
}
173-
174-
return [...actions, ...itActions]
175-
}, []),
176-
]
177-
}
178-
179-
return itemActions
180-
},
181-
[]
182-
)
183-
184-
return updateActions
149+
[CHANGE_ACTIONS]: (oldSReturnInfo, newReturnInfo, key) => {
150+
const { items = {} } = returnInfoDiff[key]
151+
if (Object.keys(items).length === 0) {
152+
return []
153+
}
154+
return Object.keys(items).reduce((actions, index) => {
155+
const item = newReturnInfo.items[index]
156+
if (items[index].shipmentState) {
157+
actions.push({
158+
action: 'setReturnShipmentState',
159+
returnItemId: item.id,
160+
shipmentState: item.shipmentState,
161+
})
162+
}
163+
if (items[index].paymentState) {
164+
actions.push({
165+
action: 'setReturnPaymentState',
166+
returnItemId: item.id,
167+
paymentState: item.paymentState,
168+
})
169+
}
170+
return actions
171+
}, [])
185172
},
186173
})
187174

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

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { performance } from 'perf_hooks'
12
import orderSyncFn, { actionGroups } from '../src/orders'
23
import { baseActionsList } from '../src/order-actions'
34

@@ -546,6 +547,155 @@ describe('Actions', () => {
546547
]
547548
expect(actual).toEqual(expected)
548549
})
550+
describe('when all items have changed its `paymentState`', () => {
551+
test('should build `returnInfoPaymentState` action', () => {
552+
const before = {
553+
returnInfo: [
554+
{
555+
items: [
556+
{
557+
id: 'id1',
558+
shipmentState: 'Returned',
559+
paymentState: 'Initial',
560+
},
561+
],
562+
},
563+
{
564+
items: [
565+
{
566+
id: 'id2',
567+
shipmentState: 'Returned',
568+
paymentState: 'Initial',
569+
},
570+
{
571+
id: 'id3',
572+
shipmentState: 'Returned',
573+
paymentState: 'Initial',
574+
},
575+
{
576+
id: 'id4',
577+
shipmentState: 'Returned',
578+
paymentState: 'Initial',
579+
},
580+
{
581+
id: 'id5',
582+
shipmentState: 'Returned',
583+
paymentState: 'Initial',
584+
},
585+
],
586+
returnDate: '2022-10-24T00:00:00.000Z',
587+
},
588+
],
589+
}
590+
const now = {
591+
returnInfo: [
592+
{
593+
items: [
594+
{
595+
id: 'id1',
596+
shipmentState: 'Returned',
597+
paymentState: 'NotRefunded',
598+
},
599+
],
600+
},
601+
{
602+
items: [
603+
{
604+
id: 'id2',
605+
shipmentState: 'Returned',
606+
paymentState: 'Refunded',
607+
},
608+
{
609+
id: 'id3',
610+
shipmentState: 'Returned',
611+
paymentState: 'Refunded',
612+
},
613+
{
614+
id: 'id4',
615+
shipmentState: 'Returned',
616+
paymentState: 'Refunded',
617+
},
618+
{
619+
id: 'id5',
620+
shipmentState: 'Returned',
621+
paymentState: 'Refunded',
622+
},
623+
],
624+
returnDate: '2022-10-24T00:00:00.000Z',
625+
},
626+
],
627+
}
628+
const actual = orderSync.buildActions(now, before)
629+
const expected = [
630+
{
631+
action: 'setReturnPaymentState',
632+
returnItemId: now.returnInfo[0].items[0].id,
633+
paymentState: now.returnInfo[0].items[0].paymentState,
634+
},
635+
{
636+
action: 'setReturnPaymentState',
637+
returnItemId: now.returnInfo[1].items[0].id,
638+
paymentState: now.returnInfo[1].items[0].paymentState,
639+
},
640+
{
641+
action: 'setReturnPaymentState',
642+
returnItemId: now.returnInfo[1].items[1].id,
643+
paymentState: now.returnInfo[1].items[1].paymentState,
644+
},
645+
{
646+
action: 'setReturnPaymentState',
647+
returnItemId: now.returnInfo[1].items[2].id,
648+
paymentState: now.returnInfo[1].items[2].paymentState,
649+
},
650+
{
651+
action: 'setReturnPaymentState',
652+
returnItemId: now.returnInfo[1].items[3].id,
653+
paymentState: now.returnInfo[1].items[3].paymentState,
654+
},
655+
]
656+
expect(actual).toEqual(expected)
657+
})
658+
describe('performance test', () => {
659+
it('should be performant for large arrays', () => {
660+
const before = {
661+
returnInfo: Array(100)
662+
.fill(null)
663+
.map((a, index) => ({
664+
items: Array(50)
665+
.fill(null)
666+
.map((b, index2) => {
667+
return {
668+
id: `id-${index}-${index2}`,
669+
shipmentState: 'Returned',
670+
paymentState: 'Initial',
671+
}
672+
}),
673+
})),
674+
}
675+
const now = {
676+
returnInfo: Array(100)
677+
.fill(null)
678+
.map((a, index) => ({
679+
items: Array(50)
680+
.fill(null)
681+
.map((b, index2) => {
682+
return {
683+
id: `id-${index}-${index2}`,
684+
shipmentState: 'Returned',
685+
paymentState: 'Refunded',
686+
}
687+
}),
688+
})),
689+
}
690+
691+
const start = performance.now()
692+
orderSync.buildActions(now, before)
693+
const end = performance.now()
694+
695+
expect(end - start).toBeLessThan(400)
696+
})
697+
})
698+
})
549699
})
550700
})
551701

0 commit comments

Comments
 (0)