Skip to content

Commit 4e14e72

Browse files
authored
fix(sync-actions): ensure proper address update actions order (#1971)
* fix(sync-actions): ensure proper address update actions order
1 parent f41a8fd commit 4e14e72

File tree

6 files changed

+125
-6
lines changed

6 files changed

+125
-6
lines changed

.changeset/early-loops-stay.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+
ensure proper order of address update actions

packages/sync-actions/src/business-units.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ const createCustomerMapActions = (mapActionGroup, syncActionConfig) => {
2222
)
2323
)
2424

25+
allActions.push(
26+
mapActionGroup('billingAddressIds', () =>
27+
customerActions.actionsMapRemoveBillingAddresses(diff, oldObj, newObj)
28+
)
29+
)
30+
31+
allActions.push(
32+
mapActionGroup('shippingAddressIds', () =>
33+
customerActions.actionsMapRemoveShippingAddresses(diff, oldObj, newObj)
34+
)
35+
)
36+
2537
allActions.push(
2638
mapActionGroup('addresses', () =>
2739
customerActions.actionsMapAddresses(diff, oldObj, newObj)
@@ -41,13 +53,13 @@ const createCustomerMapActions = (mapActionGroup, syncActionConfig) => {
4153

4254
allActions.push(
4355
mapActionGroup('billingAddressIds', () =>
44-
customerActions.actionsMapBillingAddresses(diff, oldObj, newObj)
56+
customerActions.actionsMapAddBillingAddresses(diff, oldObj, newObj)
4557
)
4658
)
4759

4860
allActions.push(
4961
mapActionGroup('shippingAddressIds', () =>
50-
customerActions.actionsMapShippingAddresses(diff, oldObj, newObj)
62+
customerActions.actionsMapAddShippingAddresses(diff, oldObj, newObj)
5163
)
5264
)
5365

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,19 @@ export function actionsMapAddresses(diff, oldObj, newObj) {
118118
return handler(diff, oldObj, newObj)
119119
}
120120

121-
export function actionsMapBillingAddresses(diff, oldObj, newObj) {
121+
export function actionsMapAddBillingAddresses(diff, oldObj, newObj) {
122122
const handler = createBuildArrayActions('billingAddressIds', {
123123
[ADD_ACTIONS]: (addressId) => ({
124124
action: 'addBillingAddressId',
125125
addressId,
126126
}),
127+
})
128+
129+
return handler(diff, oldObj, newObj)
130+
}
131+
132+
export function actionsMapRemoveBillingAddresses(diff, oldObj, newObj) {
133+
const handler = createBuildArrayActions('billingAddressIds', {
127134
[REMOVE_ACTIONS]: (addressId) => ({
128135
action: 'removeBillingAddressId',
129136
addressId,
@@ -133,12 +140,19 @@ export function actionsMapBillingAddresses(diff, oldObj, newObj) {
133140
return handler(diff, oldObj, newObj)
134141
}
135142

136-
export function actionsMapShippingAddresses(diff, oldObj, newObj) {
143+
export function actionsMapAddShippingAddresses(diff, oldObj, newObj) {
137144
const handler = createBuildArrayActions('shippingAddressIds', {
138145
[ADD_ACTIONS]: (addressId) => ({
139146
action: 'addShippingAddressId',
140147
addressId,
141148
}),
149+
})
150+
151+
return handler(diff, oldObj, newObj)
152+
}
153+
154+
export function actionsMapRemoveShippingAddresses(diff, oldObj, newObj) {
155+
const handler = createBuildArrayActions('shippingAddressIds', {
142156
[REMOVE_ACTIONS]: (addressId) => ({
143157
action: 'removeShippingAddressId',
144158
addressId,

packages/sync-actions/src/customers.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ function createCustomerMapActions(
4444
)
4545
)
4646

47+
allActions.push(
48+
mapActionGroup('billingAddressIds', (): Array<UpdateAction> =>
49+
customerActions.actionsMapRemoveBillingAddresses(diff, oldObj, newObj)
50+
)
51+
)
52+
53+
allActions.push(
54+
mapActionGroup('shippingAddressIds', (): Array<UpdateAction> =>
55+
customerActions.actionsMapRemoveShippingAddresses(diff, oldObj, newObj)
56+
)
57+
)
58+
4759
allActions.push(
4860
mapActionGroup('addresses', (): Array<UpdateAction> =>
4961
customerActions.actionsMapAddresses(diff, oldObj, newObj)
@@ -63,13 +75,13 @@ function createCustomerMapActions(
6375

6476
allActions.push(
6577
mapActionGroup('billingAddressIds', (): Array<UpdateAction> =>
66-
customerActions.actionsMapBillingAddresses(diff, oldObj, newObj)
78+
customerActions.actionsMapAddBillingAddresses(diff, oldObj, newObj)
6779
)
6880
)
6981

7082
allActions.push(
7183
mapActionGroup('shippingAddressIds', (): Array<UpdateAction> =>
72-
customerActions.actionsMapShippingAddresses(diff, oldObj, newObj)
84+
customerActions.actionsMapAddShippingAddresses(diff, oldObj, newObj)
7385
)
7486
)
7587

packages/sync-actions/test/business-units-sync.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,4 +710,42 @@ describe('Actions', () => {
710710
]
711711
expect(actual).toEqual(expected)
712712
})
713+
714+
test('should build actions for addresses in proper order', () => {
715+
const before = {
716+
addresses: [{ id: 'abc123' }, { id: 'abc456' }],
717+
billingAddressIds: ['abc123', 'abc456'],
718+
defaultBillingAddress: 'abc123',
719+
}
720+
const now = {
721+
addresses: [{ id: 'xyz123' }, { id: 'abc456' }],
722+
billingAddressIds: ['xyz123', 'abc456'],
723+
defaultBillingAddressId: 'xyz123',
724+
}
725+
expect(businessUnitsSync.buildActions(now, before)).toStrictEqual([
726+
// removeBillingAddressId action should be before removeAddress
727+
{
728+
action: 'removeBillingAddressId',
729+
addressId: 'abc123',
730+
},
731+
{
732+
action: 'removeAddress',
733+
addressId: 'abc123',
734+
},
735+
{
736+
action: 'addAddress',
737+
address: {
738+
id: 'xyz123',
739+
},
740+
},
741+
{
742+
action: 'setDefaultBillingAddress',
743+
addressId: 'xyz123',
744+
},
745+
{
746+
action: 'addBillingAddressId',
747+
addressId: 'xyz123',
748+
},
749+
])
750+
})
713751
})

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,4 +643,42 @@ describe('Actions', () => {
643643
customerSync.buildActions(now, before)
644644
}).toThrow('Invalid Authentication Mode')
645645
})
646+
647+
test('should build actions for addresses in proper order', () => {
648+
const before = {
649+
addresses: [{ id: 'abc123' }, { id: 'abc456' }],
650+
billingAddressIds: ['abc123', 'abc456'],
651+
defaultBillingAddress: 'abc123',
652+
}
653+
const now = {
654+
addresses: [{ id: 'xyz123' }, { id: 'abc456' }],
655+
billingAddressIds: ['xyz123', 'abc456'],
656+
defaultBillingAddressId: 'xyz123',
657+
}
658+
expect(customerSync.buildActions(now, before)).toStrictEqual([
659+
// removeBillingAddressId action should be before removeAddress
660+
{
661+
action: 'removeBillingAddressId',
662+
addressId: 'abc123',
663+
},
664+
{
665+
action: 'removeAddress',
666+
addressId: 'abc123',
667+
},
668+
{
669+
action: 'addAddress',
670+
address: {
671+
id: 'xyz123',
672+
},
673+
},
674+
{
675+
action: 'setDefaultBillingAddress',
676+
addressId: 'xyz123',
677+
},
678+
{
679+
action: 'addBillingAddressId',
680+
addressId: 'xyz123',
681+
},
682+
])
683+
})
646684
})

0 commit comments

Comments
 (0)