Skip to content

Commit 52449a0

Browse files
authored
Merge pull request #57 from commercetools/BMU-2684-add-standalone-price-staged-value-changed-actions
BMU-2684: standalone-prices - support staged value via changeValue and removeStagedChanges
2 parents c867416 + 151a822 commit 52449a0

4 files changed

Lines changed: 385 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/sync-actions': minor
3+
---
4+
5+
standalone-prices: support staged value via `changeValue` with `staged: true` and `removeStagedChanges`

packages/sync-actions/src/prices/prices-actions.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Delta, SyncActionConfig, UpdateAction } from '../utils/types';
1+
import {
2+
Delta,
3+
StandalonePrice,
4+
SyncActionConfig,
5+
UpdateAction,
6+
} from '../utils/types';
27
import { buildBaseAttributesActions } from '../utils/common-actions';
38

49
export const baseActionsList: Array<UpdateAction> = [
@@ -29,3 +34,32 @@ export function actionsMapBase<T>(
2934
config.shouldPreventUnsettingRequiredFields,
3035
});
3136
}
37+
38+
export function actionsMapStaged(
39+
diff: Delta,
40+
oldObj: Partial<StandalonePrice>,
41+
newObj: Partial<StandalonePrice>,
42+
config: SyncActionConfig = {}
43+
) {
44+
if (!diff || !diff.staged) return [];
45+
46+
const hasNewStaged = newObj && newObj.staged != null;
47+
const hadOldStaged = oldObj && oldObj.staged != null;
48+
49+
if (!hasNewStaged && hadOldStaged) {
50+
if (config.shouldUnsetOmittedProperties)
51+
return [{ action: 'removeStagedChanges' }];
52+
return [];
53+
}
54+
55+
if (hasNewStaged && newObj.staged.value)
56+
return [
57+
{
58+
action: 'changeValue',
59+
value: newObj.staged.value,
60+
staged: true,
61+
},
62+
];
63+
64+
return [];
65+
}

packages/sync-actions/src/prices/prices.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ function createPriceMapActions<T extends object>(
3535
): Array<UpdateAction> {
3636
const baseActions = mapActionGroup(
3737
'base',
38-
(): Array<UpdateAction> =>
39-
pricesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig)
38+
(): Array<UpdateAction> => [
39+
...pricesActions.actionsMapBase(diff, oldObj, newObj, syncActionConfig),
40+
...pricesActions.actionsMapStaged(diff, oldObj, newObj, syncActionConfig)
41+
]
4042
);
4143

4244
const customActions = mapActionGroup(

0 commit comments

Comments
 (0)