Skip to content

Commit 91bbf3c

Browse files
authored
Merge pull request #1626 from commercetools/enableDiscounted-option
feat(sync-actions): add enableDiscounted option to product
2 parents 2a4222b + eceddc7 commit 91bbf3c

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ function _buildVariantImagesAction(
236236
function _buildVariantPricesAction(
237237
diffedPrices,
238238
oldVariant = {},
239-
newVariant = {}
239+
newVariant = {},
240+
enableDiscounted = false
240241
) {
241242
const addPriceActions = []
242243
const changePriceActions = []
@@ -260,7 +261,7 @@ function _buildVariantPricesAction(
260261
// Remove read-only fields
261262
const patchedPrice = price.map((p) => {
262263
const shallowClone = { ...p }
263-
delete shallowClone.discounted
264+
if (enableDiscounted !== true) delete shallowClone.discounted
264265
return shallowClone
265266
})
266267

@@ -273,11 +274,11 @@ function _buildVariantPricesAction(
273274
// Remove the discounted field and make sure that the price
274275
// still has other values, otherwise simply return
275276
const filteredPrice = { ...price }
276-
delete filteredPrice.discounted
277+
if (enableDiscounted !== true) delete filteredPrice.discounted
277278
if (Object.keys(filteredPrice).length) {
278279
// At this point price should have changed, simply pick the new one
279280
const newPrice = { ...newObj }
280-
delete newPrice.discounted
281+
if (enableDiscounted !== true) delete newPrice.discounted
281282

282283
changePriceActions.push({
283284
action: 'changePrice',
@@ -607,7 +608,13 @@ export function actionsMapImages(diff, oldObj, newObj, variantHashMap) {
607608
return actions
608609
}
609610

610-
export function actionsMapPrices(diff, oldObj, newObj, variantHashMap) {
611+
export function actionsMapPrices(
612+
diff,
613+
oldObj,
614+
newObj,
615+
variantHashMap,
616+
enableDiscounted
617+
) {
611618
let addPriceActions = []
612619
let changePriceActions = []
613620
let removePriceActions = []
@@ -626,7 +633,8 @@ export function actionsMapPrices(diff, oldObj, newObj, variantHashMap) {
626633
const [a, c, r] = _buildVariantPricesAction(
627634
variant.prices,
628635
oldVariant,
629-
newVariant
636+
newVariant,
637+
enableDiscounted
630638
)
631639

632640
addPriceActions = addPriceActions.concat(a)

packages/sync-actions/src/products.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function createProductMapActions(
4242
options: Object = {}
4343
): Array<UpdateAction> {
4444
const allActions = []
45-
const { sameForAllAttributeNames } = options
45+
const { sameForAllAttributeNames, enableDiscounted } = options
4646
const { publish } = newObj
4747

4848
const variantHashMap = findMatchingPairs(
@@ -108,7 +108,13 @@ function createProductMapActions(
108108

109109
allActions.push(
110110
mapActionGroup('prices', (): Array<UpdateAction> =>
111-
productActions.actionsMapPrices(diff, oldObj, newObj, variantHashMap)
111+
productActions.actionsMapPrices(
112+
diff,
113+
oldObj,
114+
newObj,
115+
variantHashMap,
116+
enableDiscounted
117+
)
112118
)
113119
)
114120

packages/sync-actions/test/product-sync-prices.spec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,61 @@ describe('Actions', () => {
147147
])
148148
})
149149

150+
test('should build actions for prices with discounted when enableDiscounted is set to true', () => {
151+
const actions = productsSync.buildActions(now, before, {
152+
enableDiscounted: true,
153+
})
154+
expect(actions).toEqual([
155+
{
156+
action: 'changePrice',
157+
priceId: '111',
158+
price: {
159+
country: 'US',
160+
id: '111',
161+
value: { currencyCode: 'EUR', centAmount: 2000 },
162+
discounted: {
163+
value: { centAmount: 4000, currencyCode: 'EUR' },
164+
discount: { typeId: 'product-discount', id: 'pd1' },
165+
},
166+
},
167+
},
168+
{
169+
action: 'changePrice',
170+
price: {
171+
channel: {
172+
id: 'ch1',
173+
typeId: 'channel',
174+
},
175+
country: 'DE',
176+
customerGroup: {
177+
id: 'cg1',
178+
typeId: 'customer-group',
179+
},
180+
id: '444',
181+
value: {
182+
centAmount: 1000,
183+
currencyCode: 'EUR',
184+
fractionDigits: undefined,
185+
type: undefined,
186+
},
187+
},
188+
priceId: '444',
189+
},
190+
{ action: 'removePrice', priceId: '222' },
191+
{
192+
action: 'addPrice',
193+
variantId: 3,
194+
price: {
195+
value: { currencyCode: 'USD', centAmount: 5000 },
196+
country: 'US',
197+
customerGroup: { typeId: 'customer-group', id: 'cg1' },
198+
channel: { typeId: 'channel', id: 'ch1' },
199+
validFrom,
200+
},
201+
},
202+
])
203+
})
204+
150205
test('should not delete the discounted field from the original object', () => {
151206
expect('discounted' in before.masterVariant.prices[0]).toBeTruthy()
152207
expect('discounted' in now.masterVariant.prices[0]).toBeTruthy()

0 commit comments

Comments
 (0)