@commercetools/sync-actions 6.1.1 → 7.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,779 @@
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true,
5
+ })
6
+ exports.referenceActionsList = exports.metaActionsList = exports.baseActionsList = undefined
7
+
8
+ var _typeof =
9
+ typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
10
+ ? function(obj) {
11
+ return typeof obj
12
+ }
13
+ : function(obj) {
14
+ return obj &&
15
+ typeof Symbol === 'function' &&
16
+ obj.constructor === Symbol &&
17
+ obj !== Symbol.prototype
18
+ ? 'symbol'
19
+ : typeof obj
20
+ }
21
+
22
+ var _slicedToArray = (function() {
23
+ function sliceIterator(arr, i) {
24
+ var _arr = []
25
+ var _n = true
26
+ var _d = false
27
+ var _e = undefined
28
+ try {
29
+ for (
30
+ var _i = arr[Symbol.iterator](), _s;
31
+ !(_n = (_s = _i.next()).done);
32
+ _n = true
33
+ ) {
34
+ _arr.push(_s.value)
35
+ if (i && _arr.length === i) break
36
+ }
37
+ } catch (err) {
38
+ _d = true
39
+ _e = err
40
+ } finally {
41
+ try {
42
+ if (!_n && _i['return']) _i['return']()
43
+ } finally {
44
+ if (_d) throw _e
45
+ }
46
+ }
47
+ return _arr
48
+ }
49
+ return function(arr, i) {
50
+ if (Array.isArray(arr)) {
51
+ return arr
52
+ } else if (Symbol.iterator in Object(arr)) {
53
+ return sliceIterator(arr, i)
54
+ } else {
55
+ throw new TypeError(
56
+ 'Invalid attempt to destructure non-iterable instance'
57
+ )
58
+ }
59
+ }
60
+ })()
61
+
62
+ var _extends =
63
+ Object.assign ||
64
+ function(target) {
65
+ for (var i = 1; i < arguments.length; i++) {
66
+ var source = arguments[i]
67
+ for (var key in source) {
68
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
69
+ target[key] = source[key]
70
+ }
71
+ }
72
+ }
73
+ return target
74
+ } /* eslint-disable max-len */
75
+
76
+ exports.actionsMapBase = actionsMapBase
77
+ exports.actionsMapMeta = actionsMapMeta
78
+ exports.actionsMapVariants = actionsMapVariants
79
+ exports.actionsMapReferences = actionsMapReferences
80
+ exports.actionsMapCategories = actionsMapCategories
81
+ exports.actionsMapCategoryOrderHints = actionsMapCategoryOrderHints
82
+ exports.actionsMapAttributes = actionsMapAttributes
83
+ exports.actionsMapImages = actionsMapImages
84
+ exports.actionsMapPrices = actionsMapPrices
85
+ exports.actionsMapMasterVariant = actionsMapMasterVariant
86
+
87
+ var _lodash = require('lodash.foreach')
88
+
89
+ var _lodash2 = _interopRequireDefault(_lodash)
90
+
91
+ var _lodash3 = require('lodash.uniqwith')
92
+
93
+ var _lodash4 = _interopRequireDefault(_lodash3)
94
+
95
+ var _diffpatcher = require('./utils/diffpatcher')
96
+
97
+ var diffpatcher = _interopRequireWildcard(_diffpatcher)
98
+
99
+ var _commonActions = require('./utils/common-actions')
100
+
101
+ var _createBuildArrayActions = require('./utils/create-build-array-actions')
102
+
103
+ var _createBuildArrayActions2 = _interopRequireDefault(_createBuildArrayActions)
104
+
105
+ var _findMatchingPairs = require('./utils/find-matching-pairs')
106
+
107
+ var _findMatchingPairs2 = _interopRequireDefault(_findMatchingPairs)
108
+
109
+ function _interopRequireWildcard(obj) {
110
+ if (obj && obj.__esModule) {
111
+ return obj
112
+ } else {
113
+ var newObj = {}
114
+ if (obj != null) {
115
+ for (var key in obj) {
116
+ if (Object.prototype.hasOwnProperty.call(obj, key))
117
+ newObj[key] = obj[key]
118
+ }
119
+ }
120
+ newObj.default = obj
121
+ return newObj
122
+ }
123
+ }
124
+
125
+ function _interopRequireDefault(obj) {
126
+ return obj && obj.__esModule ? obj : { default: obj }
127
+ }
128
+
129
+ function _defineProperty(obj, key, value) {
130
+ if (key in obj) {
131
+ Object.defineProperty(obj, key, {
132
+ value: value,
133
+ enumerable: true,
134
+ configurable: true,
135
+ writable: true,
136
+ })
137
+ } else {
138
+ obj[key] = value
139
+ }
140
+ return obj
141
+ }
142
+
143
+ var REGEX_NUMBER = new RegExp(/^\d+$/)
144
+ var REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/)
145
+
146
+ var baseActionsList = (exports.baseActionsList = [
147
+ { action: 'changeName', key: 'name' },
148
+ { action: 'changeSlug', key: 'slug' },
149
+ { action: 'setDescription', key: 'description' },
150
+ { action: 'setSearchKeywords', key: 'searchKeywords' },
151
+ { action: 'setKey', key: 'key' },
152
+ ])
153
+
154
+ var metaActionsList = (exports.metaActionsList = [
155
+ { action: 'setMetaTitle', key: 'metaTitle' },
156
+ { action: 'setMetaDescription', key: 'metaDescription' },
157
+ { action: 'setMetaKeywords', key: 'metaKeywords' },
158
+ ])
159
+
160
+ var referenceActionsList = (exports.referenceActionsList = [
161
+ { action: 'setTaxCategory', key: 'taxCategory' },
162
+ { action: 'transitionState', key: 'state' },
163
+ ])
164
+
165
+ /**
166
+ * SYNC FUNCTIONS
167
+ */
168
+
169
+ function actionsMapBase(diff, oldObj, newObj) {
170
+ return (0, _commonActions.buildBaseAttributesActions)({
171
+ actions: baseActionsList,
172
+ diff: diff,
173
+ oldObj: oldObj,
174
+ newObj: newObj,
175
+ })
176
+ }
177
+
178
+ function actionsMapMeta(diff, oldObj, newObj) {
179
+ return (0, _commonActions.buildBaseAttributesActions)({
180
+ actions: metaActionsList,
181
+ diff: diff,
182
+ oldObj: oldObj,
183
+ newObj: newObj,
184
+ })
185
+ }
186
+
187
+ function actionsMapVariants(diff, oldObj, newObj) {
188
+ var _createBuildArrayActi
189
+
190
+ var handler = (0, _createBuildArrayActions2.default)(
191
+ 'variants',
192
+ ((_createBuildArrayActi = {}),
193
+ _defineProperty(
194
+ _createBuildArrayActi,
195
+ _createBuildArrayActions.ADD_ACTIONS,
196
+ function(newObject) {
197
+ return _extends({}, newObject, {
198
+ action: 'addVariant',
199
+ })
200
+ }
201
+ ),
202
+ _defineProperty(
203
+ _createBuildArrayActi,
204
+ _createBuildArrayActions.REMOVE_ACTIONS,
205
+ function(_ref) {
206
+ var id = _ref.id
207
+ return {
208
+ action: 'removeVariant',
209
+ id: id,
210
+ }
211
+ }
212
+ ),
213
+ _createBuildArrayActi)
214
+ )
215
+
216
+ return handler(diff, oldObj, newObj)
217
+ }
218
+
219
+ function actionsMapReferences(diff, oldObj, newObj) {
220
+ return (0, _commonActions.buildReferenceActions)({
221
+ actions: referenceActionsList,
222
+ diff: diff,
223
+ oldObj: oldObj,
224
+ newObj: newObj,
225
+ })
226
+ }
227
+
228
+ function actionsMapCategories(diff) {
229
+ var actions = []
230
+ if (!diff.categories) return actions
231
+
232
+ var addToCategoryActions = []
233
+ var removeFromCategoryActions = []
234
+ ;(0, _lodash2.default)(diff.categories, function(category) {
235
+ if (Array.isArray(category)) {
236
+ var action = { category: category[0] }
237
+
238
+ if (category.length === 3) {
239
+ // Ignore pure array moves!
240
+ if (category[2] !== 3) {
241
+ action.action = 'removeFromCategory'
242
+ removeFromCategoryActions.push(action)
243
+ }
244
+ } else if (category.length === 1) {
245
+ action.action = 'addToCategory'
246
+ addToCategoryActions.push(action)
247
+ }
248
+ }
249
+ })
250
+
251
+ // Make sure `removeFromCategory` actions come first
252
+ return removeFromCategoryActions.concat(addToCategoryActions)
253
+ }
254
+
255
+ function actionsMapCategoryOrderHints(diff) {
256
+ if (!diff.categoryOrderHints) return []
257
+
258
+ return Object.keys(diff.categoryOrderHints).map(function(categoryId) {
259
+ var hintChange = diff.categoryOrderHints[categoryId]
260
+
261
+ var action = {
262
+ action: 'setCategoryOrderHint',
263
+ categoryId: categoryId,
264
+ }
265
+
266
+ if (hintChange.length === 1)
267
+ // item was added
268
+ action.orderHint = hintChange[0]
269
+ else if (hintChange.length === 2 && hintChange[1] !== 0)
270
+ // item was changed
271
+ action.orderHint = hintChange[1]
272
+
273
+ // else item was removed -> do not set 'orderHint' property
274
+
275
+ return action
276
+ })
277
+ }
278
+
279
+ function actionsMapAttributes(diff, oldObj, newObj) {
280
+ var sameForAllAttributeNames =
281
+ arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []
282
+ var variantHashMap = arguments[4]
283
+
284
+ var actions = []
285
+ var variants = diff.variants
286
+
287
+ if (variants)
288
+ (0, _lodash2.default)(variants, function(variant, key) {
289
+ var _extractMatchingNewAn = _extractMatchingNewAndOld(
290
+ variantHashMap,
291
+ key,
292
+ oldObj.variants,
293
+ newObj.variants
294
+ ),
295
+ oldVariant = _extractMatchingNewAn.oldObj,
296
+ newVariant = _extractMatchingNewAn.newObj
297
+
298
+ if (REGEX_NUMBER.test(key) && !Array.isArray(variant)) {
299
+ var skuAction = _buildSkuActions(variant, oldVariant)
300
+ var keyAction = _buildKeyActions(variant, oldVariant)
301
+ if (skuAction) actions.push(skuAction)
302
+ if (keyAction) actions.push(keyAction)
303
+
304
+ var attributes = variant.attributes
305
+
306
+ var attrActions = _buildVariantAttributesActions(
307
+ attributes,
308
+ oldVariant,
309
+ newVariant,
310
+ sameForAllAttributeNames
311
+ )
312
+ actions = actions.concat(attrActions)
313
+ }
314
+ })
315
+
316
+ // Ensure that an action is unique.
317
+ // This is especially necessary for SFA attributes.
318
+ return (0, _lodash4.default)(actions, function(a, b) {
319
+ return (
320
+ a.action === b.action && a.name === b.name && a.variantId === b.variantId
321
+ )
322
+ })
323
+ }
324
+
325
+ function actionsMapImages(diff, oldObj, newObj, variantHashMap) {
326
+ var actions = []
327
+ var variants = diff.variants
328
+
329
+ if (variants)
330
+ (0, _lodash2.default)(variants, function(variant, key) {
331
+ var _extractMatchingNewAn2 = _extractMatchingNewAndOld(
332
+ variantHashMap,
333
+ key,
334
+ oldObj.variants,
335
+ newObj.variants
336
+ ),
337
+ oldVariant = _extractMatchingNewAn2.oldObj,
338
+ newVariant = _extractMatchingNewAn2.newObj
339
+
340
+ if (REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key)) {
341
+ var vActions = _buildVariantImagesAction(
342
+ variant.images,
343
+ oldVariant,
344
+ newVariant
345
+ )
346
+ actions = actions.concat(vActions)
347
+ }
348
+ })
349
+
350
+ return actions
351
+ }
352
+
353
+ function actionsMapPrices(diff, oldObj, newObj, variantHashMap) {
354
+ var addPriceActions = []
355
+ var changePriceActions = []
356
+ var removePriceActions = []
357
+
358
+ var variants = diff.variants
359
+
360
+ if (variants)
361
+ (0, _lodash2.default)(variants, function(variant, key) {
362
+ var _extractMatchingNewAn3 = _extractMatchingNewAndOld(
363
+ variantHashMap,
364
+ key,
365
+ oldObj.variants,
366
+ newObj.variants
367
+ ),
368
+ oldVariant = _extractMatchingNewAn3.oldObj,
369
+ newVariant = _extractMatchingNewAn3.newObj
370
+
371
+ if (REGEX_UNDERSCORE_NUMBER.test(key) || REGEX_NUMBER.test(key)) {
372
+ var _buildVariantPricesAc = _buildVariantPricesAction(
373
+ variant.prices,
374
+ oldVariant,
375
+ newVariant
376
+ ),
377
+ _buildVariantPricesAc2 = _slicedToArray(_buildVariantPricesAc, 3),
378
+ a = _buildVariantPricesAc2[0],
379
+ c = _buildVariantPricesAc2[1],
380
+ r = _buildVariantPricesAc2[2]
381
+
382
+ addPriceActions = addPriceActions.concat(a)
383
+ changePriceActions = changePriceActions.concat(c)
384
+ removePriceActions = removePriceActions.concat(r)
385
+ }
386
+ })
387
+
388
+ return changePriceActions.concat(removePriceActions).concat(addPriceActions)
389
+ }
390
+
391
+ function actionsMapMasterVariant(oldObj, newObj) {
392
+ var createChangeMasterVariantAction = function createChangeMasterVariantAction(
393
+ variantId
394
+ ) {
395
+ return {
396
+ action: 'changeMasterVariant',
397
+ variantId: variantId,
398
+ }
399
+ }
400
+ var extractMasterVariantId = function extractMasterVariantId(fromObj) {
401
+ var variants = Array.isArray(fromObj.variants) ? fromObj.variants : []
402
+
403
+ return variants[0] ? variants[0].id : undefined
404
+ }
405
+
406
+ var newMasterVariantId = extractMasterVariantId(newObj)
407
+ var oldMasterVariantId = extractMasterVariantId(oldObj)
408
+
409
+ // Old and new master master variant differ and a new master variant id exists
410
+ if (newMasterVariantId && oldMasterVariantId !== newMasterVariantId)
411
+ return [createChangeMasterVariantAction(newMasterVariantId)]
412
+
413
+ return []
414
+ }
415
+
416
+ /**
417
+ * HELPER FUNCTIONS
418
+ */
419
+
420
+ function _buildSkuActions(variantDiff, oldVariant) {
421
+ if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
422
+ var newValue = diffpatcher.getDeltaValue(variantDiff.sku)
423
+ if (!newValue && !oldVariant.sku) return null
424
+
425
+ return {
426
+ action: 'setSku',
427
+ variantId: oldVariant.id,
428
+ sku: newValue || null,
429
+ }
430
+ }
431
+ return null
432
+ }
433
+
434
+ function _buildKeyActions(variantDiff, oldVariant) {
435
+ if ({}.hasOwnProperty.call(variantDiff, 'key')) {
436
+ var newValue = diffpatcher.getDeltaValue(variantDiff.key)
437
+ if (!newValue && !oldVariant.key) return null
438
+
439
+ return {
440
+ action: 'setProductVariantKey',
441
+ variantId: oldVariant.id,
442
+ key: newValue || null,
443
+ }
444
+ }
445
+ return null
446
+ }
447
+
448
+ function _buildVariantAttributesActions(
449
+ attributes,
450
+ oldVariant,
451
+ newVariant,
452
+ sameForAllAttributeNames
453
+ ) {
454
+ var actions = []
455
+
456
+ if (!attributes) return actions
457
+ ;(0, _lodash2.default)(attributes, function(value, key) {
458
+ if (REGEX_NUMBER.test(key)) {
459
+ if (Array.isArray(value)) {
460
+ var id = oldVariant.id
461
+
462
+ var deltaValue = diffpatcher.getDeltaValue(value)
463
+ var setAction = _buildNewSetAttributeAction(
464
+ id,
465
+ deltaValue,
466
+ sameForAllAttributeNames
467
+ )
468
+
469
+ if (setAction) actions.push(setAction)
470
+ } else if (newVariant.attributes) {
471
+ var _setAction = _buildSetAttributeAction(
472
+ value.value,
473
+ oldVariant,
474
+ newVariant.attributes[key],
475
+ sameForAllAttributeNames
476
+ )
477
+ if (_setAction) actions.push(_setAction)
478
+ }
479
+ } else if (REGEX_UNDERSCORE_NUMBER.test(key))
480
+ if (Array.isArray(value)) {
481
+ // Ignore pure array moves!
482
+ if (value.length === 3 && value[2] === 3) return
483
+
484
+ var _id = oldVariant.id
485
+
486
+ var _deltaValue = diffpatcher.getDeltaValue(value)
487
+ if (!_deltaValue)
488
+ if (value[0] && value[0].name)
489
+ // unset attribute if
490
+ _deltaValue = { name: value[0].name }
491
+ else _deltaValue = undefined
492
+
493
+ var _setAction2 = _buildNewSetAttributeAction(
494
+ _id,
495
+ _deltaValue,
496
+ sameForAllAttributeNames
497
+ )
498
+
499
+ if (_setAction2) actions.push(_setAction2)
500
+ } else {
501
+ var index = key.substring(1)
502
+ if (newVariant.attributes) {
503
+ var _setAction3 = _buildSetAttributeAction(
504
+ value.value,
505
+ oldVariant,
506
+ newVariant.attributes[index],
507
+ sameForAllAttributeNames
508
+ )
509
+ if (_setAction3) actions.push(_setAction3)
510
+ }
511
+ }
512
+ })
513
+
514
+ return actions
515
+ }
516
+
517
+ function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) {
518
+ var attributeName = el && el.name
519
+ if (!attributeName) return undefined
520
+
521
+ var action = {
522
+ action: 'setAttribute',
523
+ variantId: id,
524
+ name: attributeName,
525
+ value: el.value,
526
+ }
527
+
528
+ if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
529
+ Object.assign(action, { action: 'setAttributeInAllVariants' })
530
+ delete action.variantId
531
+ }
532
+
533
+ return action
534
+ }
535
+
536
+ function _buildSetAttributeAction(
537
+ diffedValue,
538
+ oldVariant,
539
+ attribute,
540
+ sameForAllAttributeNames
541
+ ) {
542
+ if (!attribute) return undefined
543
+
544
+ var action = {
545
+ action: 'setAttribute',
546
+ variantId: oldVariant.id,
547
+ name: attribute.name,
548
+
549
+ // Used as original object for patching long diff text
550
+ }
551
+ var oldAttribute =
552
+ oldVariant.attributes.find(function(a) {
553
+ return a.name === attribute.name
554
+ }) || {}
555
+
556
+ if (sameForAllAttributeNames.indexOf(attribute.name) !== -1) {
557
+ Object.assign(action, { action: 'setAttributeInAllVariants' })
558
+ delete action.variantId
559
+ }
560
+
561
+ if (Array.isArray(diffedValue))
562
+ action.value = diffpatcher.getDeltaValue(diffedValue, oldAttribute.value)
563
+ else if (typeof diffedValue === 'string')
564
+ // LText: value: {en: "", de: ""}
565
+ // Enum: value: {key: "foo", label: "Foo"}
566
+ // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
567
+ // Money: value: {centAmount: 123, currencyCode: ""}
568
+ // *: value: ""
569
+
570
+ // normal
571
+ action.value = diffpatcher.getDeltaValue(diffedValue, oldAttribute.value)
572
+ else if (diffedValue.centAmount || diffedValue.currencyCode)
573
+ // Money
574
+ action.value = {
575
+ centAmount: diffedValue.centAmount
576
+ ? diffpatcher.getDeltaValue(diffedValue.centAmount)
577
+ : attribute.value.centAmount,
578
+ currencyCode: diffedValue.currencyCode
579
+ ? diffpatcher.getDeltaValue(diffedValue.currencyCode)
580
+ : attribute.value.currencyCode,
581
+ }
582
+ else if (diffedValue.key)
583
+ // Enum / LEnum (use only the key)
584
+ action.value = diffpatcher.getDeltaValue(diffedValue.key)
585
+ else if (
586
+ (typeof diffedValue === 'undefined'
587
+ ? 'undefined'
588
+ : _typeof(diffedValue)) === 'object'
589
+ )
590
+ if (
591
+ {}.hasOwnProperty.call(diffedValue, '_t') &&
592
+ diffedValue['_t'] === 'a'
593
+ ) {
594
+ // set-typed attribute
595
+ Object.assign(action, { value: attribute.value })
596
+ } else {
597
+ // LText
598
+
599
+ var updatedValue = Object.keys(diffedValue).reduce(function(acc, lang) {
600
+ var patchedValue = diffpatcher.getDeltaValue(
601
+ diffedValue[lang],
602
+ acc[lang]
603
+ )
604
+ return Object.assign(acc, _defineProperty({}, lang, patchedValue))
605
+ }, Object.assign({}, oldAttribute.value))
606
+
607
+ action.value = updatedValue
608
+ }
609
+
610
+ return action
611
+ }
612
+
613
+ function _buildVariantImagesAction(diffedImages) {
614
+ var oldVariant =
615
+ arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
616
+ var newVariant =
617
+ arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}
618
+
619
+ var actions = []
620
+ // generate a hashMap to be able to reference the right image from both ends
621
+ var matchingImagePairs = (0, _findMatchingPairs2.default)(
622
+ diffedImages,
623
+ oldVariant.images,
624
+ newVariant.images,
625
+ 'url'
626
+ )
627
+ ;(0, _lodash2.default)(diffedImages, function(image, key) {
628
+ var _extractMatchingNewAn4 = _extractMatchingNewAndOld(
629
+ matchingImagePairs,
630
+ key,
631
+ oldVariant.images,
632
+ newVariant.images
633
+ ),
634
+ oldObj = _extractMatchingNewAn4.oldObj,
635
+ newObj = _extractMatchingNewAn4.newObj
636
+
637
+ if (REGEX_NUMBER.test(key)) {
638
+ // New image
639
+ if (Array.isArray(image) && image.length)
640
+ actions.push({
641
+ action: 'addExternalImage',
642
+ variantId: oldVariant.id,
643
+ image: diffpatcher.getDeltaValue(image),
644
+ })
645
+ else if (
646
+ (typeof image === 'undefined' ? 'undefined' : _typeof(image)) ===
647
+ 'object'
648
+ )
649
+ if ({}.hasOwnProperty.call(image, 'url') && image.url.length === 2) {
650
+ // There is a new image, remove the old one first.
651
+ actions.push({
652
+ action: 'removeImage',
653
+ variantId: oldVariant.id,
654
+ imageUrl: oldObj.url,
655
+ })
656
+ actions.push({
657
+ action: 'addExternalImage',
658
+ variantId: oldVariant.id,
659
+ image: newObj,
660
+ })
661
+ } else if (
662
+ {}.hasOwnProperty.call(image, 'label') &&
663
+ (image.label.length === 1 || image.label.length === 2)
664
+ )
665
+ actions.push({
666
+ action: 'setImageLabel',
667
+ variantId: oldVariant.id,
668
+ imageUrl: oldObj.url,
669
+ label: diffpatcher.getDeltaValue(image.label),
670
+ })
671
+ } else if (REGEX_UNDERSCORE_NUMBER.test(key))
672
+ if (Array.isArray(image) && image.length === 3) {
673
+ if (Number(image[2]) === 3)
674
+ // image position changed
675
+ actions.push({
676
+ action: 'moveImagetoPosition',
677
+ variantId: oldVariant.id,
678
+ imageUrl: oldObj.url,
679
+ position: Number(image[1]),
680
+ })
681
+ else if (Number(image[2]) === 0)
682
+ // image removed
683
+ actions.push({
684
+ action: 'removeImage',
685
+ variantId: oldVariant.id,
686
+ imageUrl: oldObj.url,
687
+ })
688
+ }
689
+ })
690
+
691
+ return actions
692
+ }
693
+
694
+ // safely extract oldObj and newObj
695
+ function _extractMatchingNewAndOld(hashMap, key, before, now) {
696
+ var oldObjPos = void 0
697
+ var newObjPos = void 0
698
+ var oldObj = void 0
699
+ var newObj = void 0
700
+
701
+ if (hashMap[key]) {
702
+ oldObjPos = hashMap[key][0]
703
+ newObjPos = hashMap[key][1]
704
+ if (before && before[oldObjPos]) oldObj = before[oldObjPos]
705
+
706
+ if (now && now[newObjPos]) newObj = now[newObjPos]
707
+ }
708
+ return { oldObj: oldObj, newObj: newObj }
709
+ }
710
+
711
+ function _buildVariantPricesAction(diffedPrices) {
712
+ var oldVariant =
713
+ arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}
714
+ var newVariant =
715
+ arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}
716
+
717
+ var addPriceActions = []
718
+ var changePriceActions = []
719
+ var removePriceActions = []
720
+
721
+ // generate a hashMap to be able to reference the right image from both ends
722
+ var matchingPricePairs = (0, _findMatchingPairs2.default)(
723
+ diffedPrices,
724
+ oldVariant.prices,
725
+ newVariant.prices
726
+ )
727
+ ;(0, _lodash2.default)(diffedPrices, function(price, key) {
728
+ var _extractMatchingNewAn5 = _extractMatchingNewAndOld(
729
+ matchingPricePairs,
730
+ key,
731
+ oldVariant.prices,
732
+ newVariant.prices
733
+ ),
734
+ oldObj = _extractMatchingNewAn5.oldObj,
735
+ newObj = _extractMatchingNewAn5.newObj
736
+
737
+ if (REGEX_NUMBER.test(key)) {
738
+ if (Array.isArray(price) && price.length) {
739
+ // Remove read-only fields
740
+ var patchedPrice = price.map(function(p) {
741
+ var shallowClone = Object.assign({}, p)
742
+ delete shallowClone.discounted
743
+ return shallowClone
744
+ })
745
+
746
+ addPriceActions.push({
747
+ action: 'addPrice',
748
+ variantId: oldVariant.id,
749
+ price: diffpatcher.getDeltaValue(patchedPrice),
750
+ })
751
+ } else if (Object.keys(price).length) {
752
+ // Remove the discounted field and make sure that the price
753
+ // still has other values, otherwise simply return
754
+ var filteredPrice = Object.assign({}, price)
755
+ delete filteredPrice.discounted
756
+ if (Object.keys(filteredPrice).length) {
757
+ // At this point price should have changed, simply pick the new one
758
+ var newPrice = Object.assign({}, newObj)
759
+ delete newPrice.discounted
760
+
761
+ changePriceActions.push({
762
+ action: 'changePrice',
763
+ priceId: oldObj.id,
764
+ price: newPrice,
765
+ })
766
+ }
767
+ }
768
+ } else if (REGEX_UNDERSCORE_NUMBER.test(key))
769
+ if (Number(price[2]) === 0) {
770
+ // price removed
771
+ removePriceActions.push({
772
+ action: 'removePrice',
773
+ priceId: oldObj.id,
774
+ })
775
+ }
776
+ })
777
+
778
+ return [addPriceActions, changePriceActions, removePriceActions]
779
+ }