Skip to content

Commit 7c66fea

Browse files
authored
Add sync actions for recurring orders (#1974)
* chore: added recurring orders sync actions * chore: add changeset
1 parent a550762 commit 7c66fea

File tree

4 files changed

+142
-5
lines changed

4 files changed

+142
-5
lines changed

.changeset/bumpy-clouds-poke.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@commercetools/sync-actions': patch
3+
---
4+
5+
Added the following recurring orders sync actions
6+
7+
- setOrderSkipConfiguration
8+
- setStartsAt
9+
- setExpiresAt
10+
- setSchedule

packages/sync-actions/src/recurring-orders-actions.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { buildBaseAttributesActions } from './utils/common-actions'
1+
import {
2+
buildBaseAttributesActions,
3+
buildReferenceActions,
4+
} from './utils/common-actions'
25

36
export const baseActionsList = [
47
{ action: 'setKey', key: 'key' },
58
{ action: 'setRecurringOrderState', key: 'recurringOrderState' },
69
{ action: 'transitionState', key: 'state' },
10+
{ action: 'setOrderSkipConfiguration', key: 'skipConfiguration' },
11+
{ action: 'setStartsAt', key: 'startsAt' },
12+
{ action: 'setExpiresAt', key: 'expiresAt' },
13+
]
14+
15+
export const referenceActionsList = [
16+
{ action: 'setSchedule', key: 'recurrencePolicy' },
717
]
818

919
export function actionsMapBase(diff, oldObj, newObj, config = {}) {
@@ -18,3 +28,12 @@ export function actionsMapBase(diff, oldObj, newObj, config = {}) {
1828
config.shouldPreventUnsettingRequiredFields,
1929
})
2030
}
31+
32+
export function actionsMapReferences(diff, oldObj, newObj) {
33+
return buildReferenceActions({
34+
actions: referenceActionsList,
35+
diff,
36+
oldObj,
37+
newObj,
38+
})
39+
}

packages/sync-actions/src/recurring-orders.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import actionsMapCustom from './utils/action-map-custom'
1212
import * as RecurringOrdersActions from './recurring-orders-actions'
1313
import * as diffpatcher from './utils/diffpatcher'
1414

15-
const actionGroups = ['base', 'custom']
15+
const actionGroups = ['base', 'references', 'custom']
1616

1717
function createRecurringOrdersMapActions(
1818
mapActionGroup: Function,
@@ -41,6 +41,12 @@ function createRecurringOrdersMapActions(
4141
)
4242
)
4343

44+
allActions.push(
45+
mapActionGroup('references', (): Array<UpdateAction> =>
46+
RecurringOrdersActions.actionsMapReferences(diff, oldObj, newObj)
47+
)
48+
)
49+
4450
allActions.push(
4551
mapActionGroup('custom', (): Array<UpdateAction> =>
4652
actionsMapCustom(diff, newObj, oldObj)

packages/sync-actions/test/recurring-orders-sync.spec.js

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import createRecurringOrdersSync, {
22
actionGroups,
33
} from '../src/recurring-orders'
4-
import { baseActionsList } from '../src/recurring-orders-actions'
4+
import {
5+
baseActionsList,
6+
referenceActionsList,
7+
} from '../src/recurring-orders-actions'
58

69
describe('Exports', () => {
710
test('action group list', () => {
8-
expect(actionGroups).toEqual(['base', 'custom'])
11+
expect(actionGroups).toEqual(['base', 'references', 'custom'])
912
})
1013

11-
describe('action list', () => {
14+
describe('base action list', () => {
1215
test('should contain `setRecurringOrderState` action', () => {
1316
expect(baseActionsList).toEqual(
1417
expect.arrayContaining([
@@ -28,11 +31,42 @@ describe('Exports', () => {
2831
expect.arrayContaining([{ action: 'transitionState', key: 'state' }])
2932
)
3033
})
34+
35+
test('should contain `setOrderSkipConfiguration` action', () => {
36+
expect(baseActionsList).toEqual(
37+
expect.arrayContaining([
38+
{ action: 'setOrderSkipConfiguration', key: 'skipConfiguration' },
39+
])
40+
)
41+
})
42+
43+
test('should contain `setStartsAt` action', () => {
44+
expect(baseActionsList).toEqual(
45+
expect.arrayContaining([{ action: 'setStartsAt', key: 'startsAt' }])
46+
)
47+
})
48+
49+
test('should contain `setExpiresAt` action', () => {
50+
expect(baseActionsList).toEqual(
51+
expect.arrayContaining([{ action: 'setExpiresAt', key: 'expiresAt' }])
52+
)
53+
})
54+
})
55+
56+
describe('reference action list', () => {
57+
test('should contain `recurrencePolicy` action', () => {
58+
expect(referenceActionsList).toEqual(
59+
expect.arrayContaining([
60+
{ action: 'setSchedule', key: 'recurrencePolicy' },
61+
])
62+
)
63+
})
3164
})
3265
})
3366

3467
describe('Actions', () => {
3568
let recurringOrdersSync
69+
3670
beforeEach(() => {
3771
recurringOrdersSync = createRecurringOrdersSync()
3872
})
@@ -86,6 +120,50 @@ describe('Actions', () => {
86120
expect(actual).toEqual(expected)
87121
})
88122

123+
test('should build `setOrderSkipConfiguration` action', () => {
124+
const before = {
125+
skipConfiguration: { type: 'counter', totalToSkip: 2, skipped: 1 },
126+
}
127+
const now = {
128+
skipConfiguration: { type: 'counter', totalToSkip: 5 },
129+
}
130+
131+
const expected = [
132+
{
133+
action: 'setOrderSkipConfiguration',
134+
...now,
135+
},
136+
]
137+
const actual = recurringOrdersSync.buildActions(now, before)
138+
expect(actual).toEqual(expected)
139+
})
140+
141+
test('should build `setStartsAt` action', () => {
142+
const before = { startsAt: '2025-10-01T00:00:00.000Z' }
143+
const now = { startsAt: '2026-01-10T00:00:00.000Z' }
144+
const actual = recurringOrdersSync.buildActions(now, before)
145+
const expected = [
146+
{
147+
action: 'setStartsAt',
148+
...now,
149+
},
150+
]
151+
expect(actual).toEqual(expected)
152+
})
153+
154+
test('should build `setExpiresAt` action', () => {
155+
const before = { expiresAt: '2025-10-01T00:00:00.000Z' }
156+
const now = { expiresAt: '2026-01-10T00:00:00.000Z' }
157+
const actual = recurringOrdersSync.buildActions(now, before)
158+
const expected = [
159+
{
160+
action: 'setExpiresAt',
161+
...now,
162+
},
163+
]
164+
expect(actual).toEqual(expected)
165+
})
166+
89167
test('should build `setCustomType` action', () => {
90168
const before = {
91169
custom: {
@@ -147,4 +225,28 @@ describe('Actions', () => {
147225
]
148226
expect(actual).toEqual(expected)
149227
})
228+
229+
test('should build `setSchedule` action', () => {
230+
const before = {
231+
recurrencePolicy: {
232+
typeId: 'recurrence-policy',
233+
id: '999-9999-9999',
234+
},
235+
}
236+
const now = {
237+
recurrencePolicy: {
238+
typeId: 'recurrence-policy',
239+
id: '1212-1212-1212',
240+
},
241+
}
242+
243+
const actual = recurringOrdersSync.buildActions(now, before)
244+
const expected = [
245+
{
246+
action: 'setSchedule',
247+
...now,
248+
},
249+
]
250+
expect(actual).toEqual(expected)
251+
})
150252
})

0 commit comments

Comments
 (0)