@commercetools/sync-actions 7.3.1 → 8.0.0-alpha.0

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/dist/commercetools-sync-actions.cjs.js +12085 -5
  3. package/dist/commercetools-sync-actions.esm.js +11295 -3213
  4. package/dist/commercetools-sync-actions.umd.js +1 -0
  5. package/dist/commercetools-sync-actions.umd.min.js +1 -0
  6. package/dist/declarations/api-extensions/api-extensions-actions.d.ts +9 -0
  7. package/dist/declarations/api-extensions/api-extensions.d.ts +4 -0
  8. package/dist/declarations/assets/assets-actions.d.ts +2 -0
  9. package/dist/declarations/attribute-groups/attribute-groups-actions.d.ts +10 -0
  10. package/dist/declarations/attribute-groups/attribute-groups.d.ts +3 -0
  11. package/dist/declarations/business-units/business-units-actions.d.ts +10 -0
  12. package/dist/declarations/business-units/business-units.d.ts +3 -0
  13. package/dist/declarations/cart-discounts/cart-discounts-actions.d.ts +9 -0
  14. package/dist/declarations/cart-discounts/cart-discounts.d.ts +4 -0
  15. package/dist/declarations/categories/categories.d.ts +4 -0
  16. package/dist/declarations/categories/category-actions.d.ts +18 -0
  17. package/dist/declarations/channels/channels-actions.d.ts +6 -0
  18. package/dist/declarations/channels/channels.d.ts +4 -0
  19. package/dist/declarations/customer-group/customer-group-actions.d.ts +6 -0
  20. package/dist/declarations/customer-group/customer-group.d.ts +4 -0
  21. package/dist/declarations/customers/customer-actions.d.ts +25 -0
  22. package/dist/declarations/customers/customers.d.ts +4 -0
  23. package/dist/declarations/discount-codes/discount-codes-actions.d.ts +6 -0
  24. package/dist/declarations/discount-codes/discount-codes.d.ts +6 -0
  25. package/dist/declarations/index.d.ts +26 -0
  26. package/dist/declarations/inventories/inventories.d.ts +4 -0
  27. package/dist/declarations/inventories/inventory-actions.d.ts +13 -0
  28. package/dist/declarations/orders/order-actions.d.ts +16 -0
  29. package/dist/declarations/orders/orders.d.ts +4 -0
  30. package/dist/declarations/prices/prices-actions.d.ts +6 -0
  31. package/dist/declarations/prices/prices.d.ts +4 -0
  32. package/dist/declarations/product-discounts/product-discounts-actions.d.ts +9 -0
  33. package/dist/declarations/product-discounts/product-discounts.d.ts +4 -0
  34. package/dist/declarations/product-selections/product-selections-actions.d.ts +9 -0
  35. package/dist/declarations/product-selections/product-selections.d.ts +4 -0
  36. package/dist/declarations/product-types/product-types-actions.d.ts +11 -0
  37. package/dist/declarations/product-types/product-types.d.ts +4 -0
  38. package/dist/declarations/products/product-actions.d.ts +38 -0
  39. package/dist/declarations/products/products.d.ts +4 -0
  40. package/dist/declarations/projects/projects-actions.d.ts +17 -0
  41. package/dist/declarations/projects/projects.d.ts +4 -0
  42. package/dist/declarations/quotes/quotes-actions.d.ts +6 -0
  43. package/dist/declarations/quotes/quotes.d.ts +5 -0
  44. package/dist/declarations/quotes-requests/quote-requests-actions.d.ts +6 -0
  45. package/dist/declarations/quotes-requests/quote-requests.d.ts +4 -0
  46. package/dist/declarations/recurring-orders/recurring-orders-actions.d.ts +16 -0
  47. package/dist/declarations/recurring-orders/recurring-orders.d.ts +5 -0
  48. package/dist/declarations/shipping-methods/shipping-methods-actions.d.ts +10 -0
  49. package/dist/declarations/shipping-methods/shipping-methods.d.ts +4 -0
  50. package/dist/declarations/staged-quotes/staged-quotes-actions.d.ts +6 -0
  51. package/dist/declarations/staged-quotes/staged-quotes.d.ts +5 -0
  52. package/dist/declarations/states/state-actions.d.ts +7 -0
  53. package/dist/declarations/states/states.d.ts +4 -0
  54. package/dist/declarations/stores/stores-actions.d.ts +6 -0
  55. package/dist/declarations/stores/stores.d.ts +4 -0
  56. package/dist/declarations/subscriptions/subscriptions-actions.d.ts +6 -0
  57. package/dist/declarations/subscriptions/subscriptions.d.ts +4 -0
  58. package/dist/declarations/tax-categories/tax-categories-actions.d.ts +7 -0
  59. package/dist/declarations/tax-categories/tax-categories.d.ts +4 -0
  60. package/dist/declarations/types/types-actions.d.ts +7 -0
  61. package/dist/declarations/types/types.d.ts +5 -0
  62. package/dist/declarations/utils/action-map-custom.d.ts +5 -0
  63. package/dist/declarations/utils/clone.d.ts +6 -0
  64. package/dist/declarations/utils/combine-validity-actions.d.ts +1 -0
  65. package/dist/declarations/utils/common-actions.d.ts +46 -0
  66. package/dist/declarations/utils/copy-empty-array-props.d.ts +9 -0
  67. package/dist/declarations/utils/create-build-actions.d.ts +4 -0
  68. package/dist/declarations/utils/create-build-array-actions.d.ts +15 -0
  69. package/dist/declarations/utils/create-map-action-group.d.ts +2 -0
  70. package/dist/declarations/utils/diffpatcher.d.ts +11 -0
  71. package/dist/declarations/utils/extract-matching-pairs.d.ts +4 -0
  72. package/dist/declarations/utils/find-matching-pairs.d.ts +2 -0
  73. package/dist/declarations/utils/remove-typename.d.ts +3 -0
  74. package/dist/declarations/utils/types.d.ts +31 -0
  75. package/dist/declarations/zones/zones-actions.d.ts +7 -0
  76. package/dist/declarations/zones/zones.d.ts +4 -0
  77. package/package.json +20 -29
  78. package/LICENSE +0 -21
  79. package/dist/commercetools-sync-actions.cjs.dev.js +0 -3997
  80. package/dist/commercetools-sync-actions.cjs.prod.js +0 -3997
@@ -1,3997 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var flatten = require('lodash.flatten');
6
- var isEqual = require('lodash.isequal');
7
- var isNil = require('lodash.isnil');
8
- var jsondiffpatch_cjs = require('jsondiffpatch/dist/jsondiffpatch.cjs');
9
- var forEach = require('lodash.foreach');
10
- var uniqWith = require('lodash.uniqwith');
11
- var intersection = require('lodash.intersection');
12
- var without = require('lodash.without');
13
- var fastEquals = require('fast-equals');
14
- var sortBy = require('lodash.sortby');
15
-
16
- function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
17
-
18
- var flatten__default = /*#__PURE__*/_interopDefault(flatten);
19
- var isEqual__default = /*#__PURE__*/_interopDefault(isEqual);
20
- var isNil__default = /*#__PURE__*/_interopDefault(isNil);
21
- var forEach__default = /*#__PURE__*/_interopDefault(forEach);
22
- var uniqWith__default = /*#__PURE__*/_interopDefault(uniqWith);
23
- var intersection__default = /*#__PURE__*/_interopDefault(intersection);
24
- var without__default = /*#__PURE__*/_interopDefault(without);
25
- var sortBy__default = /*#__PURE__*/_interopDefault(sortBy);
26
-
27
- function _arrayWithHoles(r) {
28
- if (Array.isArray(r)) return r;
29
- }
30
-
31
- function _iterableToArrayLimit(r, l) {
32
- var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
33
- if (null != t) {
34
- var e,
35
- n,
36
- i,
37
- u,
38
- a = [],
39
- f = !0,
40
- o = !1;
41
- try {
42
- if (i = (t = t.call(r)).next, 0 === l) {
43
- if (Object(t) !== t) return;
44
- f = !1;
45
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
46
- } catch (r) {
47
- o = !0, n = r;
48
- } finally {
49
- try {
50
- if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
51
- } finally {
52
- if (o) throw n;
53
- }
54
- }
55
- return a;
56
- }
57
- }
58
-
59
- function _arrayLikeToArray(r, a) {
60
- (null == a || a > r.length) && (a = r.length);
61
- for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
62
- return n;
63
- }
64
-
65
- function _unsupportedIterableToArray(r, a) {
66
- if (r) {
67
- if ("string" == typeof r) return _arrayLikeToArray(r, a);
68
- var t = {}.toString.call(r).slice(8, -1);
69
- return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
70
- }
71
- }
72
-
73
- function _nonIterableRest() {
74
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
75
- }
76
-
77
- function _slicedToArray(r, e) {
78
- return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
79
- }
80
-
81
- function _toPrimitive(t, r) {
82
- if ("object" != typeof t || !t) return t;
83
- var e = t[Symbol.toPrimitive];
84
- if (void 0 !== e) {
85
- var i = e.call(t, r || "default");
86
- if ("object" != typeof i) return i;
87
- throw new TypeError("@@toPrimitive must return a primitive value.");
88
- }
89
- return ("string" === r ? String : Number)(t);
90
- }
91
-
92
- function _toPropertyKey(t) {
93
- var i = _toPrimitive(t, "string");
94
- return "symbol" == typeof i ? i : i + "";
95
- }
96
-
97
- function _defineProperty(e, r, t) {
98
- return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
99
- value: t,
100
- enumerable: !0,
101
- configurable: !0,
102
- writable: !0
103
- }) : e[r] = t, e;
104
- }
105
-
106
- function ownKeys(e, r) {
107
- var t = Object.keys(e);
108
- if (Object.getOwnPropertySymbols) {
109
- var o = Object.getOwnPropertySymbols(e);
110
- r && (o = o.filter(function (r) {
111
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
112
- })), t.push.apply(t, o);
113
- }
114
- return t;
115
- }
116
- function _objectSpread2(e) {
117
- for (var r = 1; r < arguments.length; r++) {
118
- var t = null != arguments[r] ? arguments[r] : {};
119
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
120
- _defineProperty(e, r, t[r]);
121
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
122
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
123
- });
124
- }
125
- return e;
126
- }
127
-
128
- function _objectWithoutPropertiesLoose(r, e) {
129
- if (null == r) return {};
130
- var t = {};
131
- for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
132
- if (-1 !== e.indexOf(n)) continue;
133
- t[n] = r[n];
134
- }
135
- return t;
136
- }
137
-
138
- function _objectWithoutProperties(e, t) {
139
- if (null == e) return {};
140
- var o,
141
- r,
142
- i = _objectWithoutPropertiesLoose(e, t);
143
- if (Object.getOwnPropertySymbols) {
144
- var n = Object.getOwnPropertySymbols(e);
145
- for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
146
- }
147
- return i;
148
- }
149
-
150
- var _excluded$2 = ["prices"];
151
- function applyOnBeforeDiff(before, now, fn) {
152
- return fn && typeof fn === 'function' ? fn(before, now) : [before, now];
153
- }
154
- var createPriceComparator = function createPriceComparator(price) {
155
- return {
156
- value: {
157
- currencyCode: price.value.currencyCode
158
- },
159
- channel: price.channel,
160
- country: price.country,
161
- customerGroup: price.customerGroup,
162
- validFrom: price.validFrom,
163
- validUntil: price.validUntil
164
- };
165
- };
166
- function arePricesStructurallyEqual(oldPrice, newPrice) {
167
- var oldPriceComparison = createPriceComparator(oldPrice);
168
- var newPriceComparison = createPriceComparator(newPrice);
169
- return isEqual__default["default"](newPriceComparison, oldPriceComparison);
170
- }
171
- function extractPriceFromPreviousVariant(newPrice, previousVariant) {
172
- if (!previousVariant) return null;
173
- var price = previousVariant.prices.find(function (oldPrice) {
174
- return arePricesStructurallyEqual(oldPrice, newPrice);
175
- });
176
- return price || null;
177
- }
178
- function injectMissingPriceIds(nextVariants, previousVariants) {
179
- return nextVariants.map(function (newVariant) {
180
- var prices = newVariant.prices,
181
- restOfVariant = _objectWithoutProperties(newVariant, _excluded$2);
182
- if (!prices) return restOfVariant;
183
- var oldVariant = previousVariants.find(function (previousVariant) {
184
- return !isNil__default["default"](previousVariant.id) && previousVariant.id === newVariant.id || !isNil__default["default"](previousVariant.key) && previousVariant.key === newVariant.key || !isNil__default["default"](previousVariant.sku) && previousVariant.sku === newVariant.sku;
185
- });
186
- return _objectSpread2(_objectSpread2({}, restOfVariant), {}, {
187
- prices: prices.map(function (price) {
188
- var newPrice = _objectSpread2({}, price);
189
- var oldPrice = extractPriceFromPreviousVariant(price, oldVariant);
190
- if (oldPrice) {
191
- // copy ID if not provided
192
- if (!newPrice.id) newPrice.id = oldPrice.id;
193
- if (isNil__default["default"](newPrice.value.type)) newPrice.value.type = oldPrice.value.type;
194
- if (isNil__default["default"](newPrice.value.fractionDigits)) newPrice.value.fractionDigits = oldPrice.value.fractionDigits;
195
- }
196
- return newPrice;
197
- })
198
- });
199
- });
200
- }
201
- function createBuildActions(differ, doMapActions, onBeforeDiff) {
202
- var buildActionsConfig = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
203
- return function buildActions(now, before) {
204
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
205
- if (!now || !before) throw new Error('Missing either `newObj` or `oldObj` ' + 'in order to build update actions');
206
- var _applyOnBeforeDiff = applyOnBeforeDiff(before, now, onBeforeDiff),
207
- _applyOnBeforeDiff2 = _slicedToArray(_applyOnBeforeDiff, 2),
208
- processedBefore = _applyOnBeforeDiff2[0],
209
- processedNow = _applyOnBeforeDiff2[1];
210
- if (processedNow.variants && processedBefore.variants) processedNow.variants = injectMissingPriceIds(processedNow.variants, processedBefore.variants);
211
- var diffed = differ(processedBefore, processedNow);
212
- if (!buildActionsConfig.withHints && !diffed) return [];
213
- return doMapActions(diffed, processedNow, processedBefore, options);
214
- };
215
- }
216
-
217
- // Array of action groups which need to be allowed or ignored.
218
- // Example:
219
- // [
220
- // { type: 'base', group: 'ignore' },
221
- // { type: 'prices', group: 'allow' },
222
- // { type: 'variants', group: 'ignore' },
223
- // ]
224
- function createMapActionGroup() {
225
- var actionGroups = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
226
- return function mapActionGroup(type, fn) {
227
- if (!Object.keys(actionGroups).length) return fn();
228
- var found = actionGroups.find(function (c) {
229
- return c.type === type;
230
- });
231
- if (!found) return [];
232
-
233
- // Keep `black` for backwards compatibility.
234
- if (found.group === 'ignore' || found.group === 'black') return [];
235
- // Keep `white` for backwards compatibility.
236
- if (found.group === 'allow' || found.group === 'white') return fn();
237
- throw new Error("Action group '".concat(found.group, "' not supported. Use either \"allow\" or \"ignore\"."));
238
- };
239
- }
240
-
241
- function _arrayWithoutHoles(r) {
242
- if (Array.isArray(r)) return _arrayLikeToArray(r);
243
- }
244
-
245
- function _iterableToArray(r) {
246
- if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
247
- }
248
-
249
- function _nonIterableSpread() {
250
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
251
- }
252
-
253
- function _toConsumableArray(r) {
254
- return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
255
- }
256
-
257
- function _typeof(o) {
258
- "@babel/helpers - typeof";
259
-
260
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
261
- return typeof o;
262
- } : function (o) {
263
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
264
- }, _typeof(o);
265
- }
266
-
267
- function objectHash(obj, index) {
268
- var objIndex = "$$index:".concat(index);
269
- return _typeof(obj) === 'object' && obj !== null ? obj.id || obj.name || obj.url || objIndex : objIndex;
270
- }
271
- var diffpatcher = new jsondiffpatch_cjs.DiffPatcher({
272
- objectHash: objectHash,
273
- arrays: {
274
- // detect items moved inside the array
275
- detectMove: true,
276
- // value of items moved is not included in deltas
277
- includeValueOnMove: false
278
- },
279
- textDiff: {
280
- /**
281
- * jsondiffpatch uses a very fine-grained diffing algorithm for long strings to easily identify
282
- * what changed between strings. However, we don't actually care about what changed, just
283
- * if the string changed at all. So we set the minimum length to diff to a very large number to avoid
284
- * using the very slow algorithm.
285
- * See https://github.com/benjamine/jsondiffpatch/blob/master/docs/deltas.md#text-diffs.
286
- */
287
- minLength: Number.MAX_SAFE_INTEGER
288
- }
289
- });
290
- function diff(oldObj, newObj) {
291
- return diffpatcher.diff(oldObj, newObj);
292
- }
293
- function patch(obj, delta) {
294
- return diffpatcher.patch(obj, delta);
295
- }
296
- function getDeltaValue(arr, originalObject) {
297
- if (!Array.isArray(arr)) throw new Error('Expected array to extract delta value');
298
- if (arr.length === 1) return arr[0]; // new
299
-
300
- if (arr.length === 2) return arr[1]; // update
301
-
302
- if (arr.length === 3 && arr[2] === 0) return undefined; // delete
303
-
304
- if (arr.length === 3 && arr[2] === 2) {
305
- // text diff
306
- if (!originalObject) throw new Error('Cannot apply patch to long text diff. Missing original object.');
307
- // try to apply patch to given object based on delta value
308
- return patch(originalObject, arr);
309
- }
310
- if (arr.length === 3 && arr[2] === 3)
311
- // array move
312
- throw new Error('Detected an array move, it should not happen as ' + '`includeValueOnMove` should be set to false');
313
- throw new Error("Got unsupported number ".concat(arr[2], " in delta value"));
314
- }
315
-
316
- var _excluded$1 = ["actions"];
317
- var Actions = {
318
- setCustomType: 'setCustomType',
319
- setCustomField: 'setCustomField'
320
- };
321
- var hasSingleCustomFieldChanged = function hasSingleCustomFieldChanged(diff) {
322
- return Array.isArray(diff.custom);
323
- };
324
- var haveMultipleCustomFieldsChanged = function haveMultipleCustomFieldsChanged(diff) {
325
- return Boolean(diff.custom.fields);
326
- };
327
- var hasCustomTypeChanged = function hasCustomTypeChanged(diff) {
328
- return Boolean(diff.custom.type);
329
- };
330
- var extractCustomType = function extractCustomType(diff, previousObject) {
331
- return Array.isArray(diff.custom.type) ? getDeltaValue(diff.custom.type, previousObject) : diff.custom.type;
332
- };
333
- var extractTypeId = function extractTypeId(type, nextObject) {
334
- return Array.isArray(type.id) ? getDeltaValue(type.id) : nextObject.custom.type.id;
335
- };
336
- var extractTypeKey = function extractTypeKey(type, nextObject) {
337
- return Array.isArray(type.key) ? getDeltaValue(type.key) : nextObject.custom.type.key;
338
- };
339
- var extractTypeFields = function extractTypeFields(diffedFields, nextFields) {
340
- return Array.isArray(diffedFields) ? getDeltaValue(diffedFields) : nextFields;
341
- };
342
- var extractFieldValue = function extractFieldValue(newFields, fieldName) {
343
- return newFields[fieldName];
344
- };
345
- function actionsMapCustom(diff, newObj, oldObj) {
346
- var customProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
347
- actions: {}
348
- };
349
- var actions = [];
350
- var customPropsActions = customProps.actions,
351
- options = _objectWithoutProperties(customProps, _excluded$1);
352
- var actionGroup = _objectSpread2(_objectSpread2({}, Actions), customPropsActions);
353
- if (!diff.custom) return actions;
354
- if (hasSingleCustomFieldChanged(diff)) {
355
- // If custom is not defined on the new or old category
356
- var custom = getDeltaValue(diff.custom, oldObj);
357
- actions.push(_objectSpread2(_objectSpread2({
358
- action: actionGroup.setCustomType
359
- }, options), custom));
360
- } else if (hasCustomTypeChanged(diff)) {
361
- // If custom is set to an empty object on the new or old category
362
- var type = extractCustomType(diff, oldObj);
363
- if (!type) actions.push(_objectSpread2({
364
- action: actionGroup.setCustomType
365
- }, options));else if (type.id) actions.push(_objectSpread2(_objectSpread2({
366
- action: actionGroup.setCustomType
367
- }, options), {}, {
368
- type: {
369
- typeId: 'type',
370
- id: extractTypeId(type, newObj)
371
- },
372
- fields: extractTypeFields(diff.custom.fields, newObj.custom.fields)
373
- }));else if (type.key) actions.push(_objectSpread2(_objectSpread2({
374
- action: actionGroup.setCustomType
375
- }, options), {}, {
376
- type: {
377
- typeId: 'type',
378
- key: extractTypeKey(type, newObj)
379
- },
380
- fields: extractTypeFields(diff.custom.fields, newObj.custom.fields)
381
- }));
382
- } else if (haveMultipleCustomFieldsChanged(diff)) {
383
- var customFieldsActions = Object.keys(diff.custom.fields).map(function (name) {
384
- return _objectSpread2(_objectSpread2({
385
- action: actionGroup.setCustomField
386
- }, options), {}, {
387
- name: name,
388
- value: extractFieldValue(newObj.custom.fields, name)
389
- });
390
- });
391
- actions.push.apply(actions, _toConsumableArray(customFieldsActions));
392
- }
393
- return actions;
394
- }
395
-
396
- var REGEX_NUMBER$4 = new RegExp(/^\d+$/);
397
- var REGEX_UNDERSCORE_NUMBER$4 = new RegExp(/^_\d+$/);
398
- var ADD_ACTIONS = 'create';
399
- var REMOVE_ACTIONS = 'remove';
400
- var CHANGE_ACTIONS = 'change';
401
-
402
- /**
403
- * Tests a delta to see if it represents a create action.
404
- * eg. delta:
405
- * {
406
- * 0: [ { foo: 'bar' } ]
407
- * }
408
- *
409
- * @param {object} obj The delta generated by the diffpatcher
410
- * @param {string} key key of generated delta to examine
411
- * @return {Boolean} Returns true if delta represents a create action,
412
- * false otherwise
413
- */
414
- function isCreateAction(obj, key) {
415
- return REGEX_NUMBER$4.test(key) && Array.isArray(obj[key]) && obj[key].length === 1;
416
- }
417
-
418
- /**
419
- * Tests a delta to see if it represents a change action.
420
- * eg. delta:
421
- *
422
- * {
423
- * 0: {
424
- * foo: ['bar', 'baz']
425
- * }
426
- * }
427
- * @param {object} obj The delta generated by the diffpatcher
428
- * @param {string} key key of generated delta to examine
429
- * @return {Boolean} Returns true if delta represents a change action,
430
- * false otherwise
431
- */
432
- function isChangeAction(obj, key) {
433
- return REGEX_NUMBER$4.test(key) && (_typeof(obj[key]) === 'object' || typeof obj[key] === 'string');
434
- }
435
-
436
- /**
437
- * Tests a delta to see if it represents a remove action.
438
- * eg. delta:
439
- *
440
- * {
441
- * _0: [ 'foo', 0, 0 ]
442
- * }
443
- * @param {object} obj The delta generated by the diffpatcher
444
- * @param {string} key key of generated delta to examine
445
- * @return {Boolean} Returns true if delta represents a remove action,
446
- * false otherwise
447
- */
448
- function isRemoveAction$1(obj, key) {
449
- return REGEX_UNDERSCORE_NUMBER$4.test(key) && Array.isArray(obj[key]) && obj[key].length === 3 && (_typeof(obj[key][0]) === 'object' || typeof obj[key][0] === 'string') && obj[key][1] === 0 && obj[key][2] === 0;
450
- }
451
-
452
- /**
453
- * Generate + configure a function to build actions for nested objects
454
- * @param {string} key key of the attribute containing the array of
455
- * nested objects
456
- * @param {object} config configuration object that can contain the keys
457
- * [ADD_ACTIONS, REMOVE_ACTIONS, CHANGE_ACTIONS], each of
458
- * which is a function. The function should accept the old + new arrays and
459
- * return an action object.
460
- * @return {Array} The generated array of actions
461
- */
462
- function createBuildArrayActions(key, config) {
463
- return function buildArrayActions(diff, oldObj, newObj) {
464
- var addActions = [];
465
- var removeActions = [];
466
- var changeActions = [];
467
- if (diff[key]) {
468
- var arrayDelta = diff[key];
469
- Object.keys(arrayDelta).forEach(function (index) {
470
- if (config[ADD_ACTIONS] && isCreateAction(arrayDelta, index)) {
471
- var actionGenerator = config[ADD_ACTIONS];
472
- // When adding a new element you don't need the oldObj
473
- var action = actionGenerator(newObj[key][index], parseInt(index, 10));
474
- if (action) addActions.push(action);
475
- } else if (config[CHANGE_ACTIONS] && isChangeAction(arrayDelta, index)) {
476
- var _actionGenerator = config[CHANGE_ACTIONS];
477
- // When changing an existing element you need both old + new
478
- var _action = _actionGenerator(oldObj[key][index], newObj[key][index], parseInt(index, 10));
479
- if (_action) changeActions.push(_action);
480
- } else if (config[REMOVE_ACTIONS] && isRemoveAction$1(arrayDelta, index)) {
481
- var realIndex = index.replace('_', '');
482
- var _actionGenerator2 = config[REMOVE_ACTIONS];
483
- // When removing an existing element you don't need the newObj
484
- var _action2 = _actionGenerator2(oldObj[key][realIndex], parseInt(realIndex, 10));
485
- if (_action2) removeActions.push(_action2);
486
- }
487
- });
488
- }
489
- return changeActions.concat(removeActions, addActions);
490
- };
491
- }
492
-
493
- function toAssetIdentifier$1(asset) {
494
- var assetIdentifier = asset.id ? {
495
- assetId: asset.id
496
- } : {
497
- assetKey: asset.key
498
- };
499
- return assetIdentifier;
500
- }
501
- function actionsMapAssets$1(diff, oldObj, newObj) {
502
- var handler = createBuildArrayActions('assets', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newAsset) {
503
- return {
504
- action: 'addAsset',
505
- asset: newAsset
506
- };
507
- }), REMOVE_ACTIONS, function (oldAsset) {
508
- return _objectSpread2({
509
- action: 'removeAsset'
510
- }, toAssetIdentifier$1(oldAsset));
511
- }), CHANGE_ACTIONS, function (oldAsset, newAsset) {
512
- return (
513
- // here we could use more atomic update actions (e.g. changeAssetName)
514
- // but for now we use the simpler approach to first remove and then
515
- // re-add the asset - which reduces the code complexity
516
- [_objectSpread2({
517
- action: 'removeAsset'
518
- }, toAssetIdentifier$1(oldAsset)), {
519
- action: 'addAsset',
520
- asset: newAsset
521
- }]
522
- );
523
- }));
524
- return handler(diff, oldObj, newObj);
525
- }
526
-
527
- function clone(obj) {
528
- return JSON.parse(JSON.stringify(obj));
529
- }
530
-
531
- var normalizeValue = function normalizeValue(value) {
532
- return typeof value === 'string' ? value.trim() : value;
533
- };
534
- var createIsEmptyValue = function createIsEmptyValue(emptyValues) {
535
- return function (value) {
536
- return emptyValues.some(function (emptyValue) {
537
- return emptyValue === normalizeValue(value);
538
- });
539
- };
540
- };
541
-
542
- /* actions that start with 'set' can generate undefined valued */
543
- var isOptionalField = function isOptionalField(action) {
544
- return action.startsWith('set');
545
- };
546
-
547
- /**
548
- * Builds actions for simple object properties, given a list of actions
549
- * E.g. [{ action: `changeName`, key: 'name' }]
550
- *
551
- * @param {Array} options.actions - a list of actions to be built
552
- * based on the given property
553
- * @param {Object} options.diff - the diff object
554
- * @param {Object} options.oldObj - the object that needs to be updated
555
- * @param {Object} options.newObj - the new representation of the object
556
- * @param {Boolean} options.shouldOmitEmptyString - a flag to determine if we should treat an empty string a NON-value
557
- * @param {Boolean} options.shouldUnsetOmittedProperties - a flag to determine if we should unset fields which are omitted in the newObj
558
- * @param {Boolean} options.shouldPreventUnsettingRequiredFields - a flag to determine if required fields should be unset
559
- */
560
- function buildBaseAttributesActions(_ref) {
561
- var actions = _ref.actions,
562
- diff = _ref.diff,
563
- oldObj = _ref.oldObj,
564
- newObj = _ref.newObj,
565
- shouldOmitEmptyString = _ref.shouldOmitEmptyString,
566
- shouldUnsetOmittedProperties = _ref.shouldUnsetOmittedProperties,
567
- shouldPreventUnsettingRequiredFields = _ref.shouldPreventUnsettingRequiredFields;
568
- var isEmptyValue = createIsEmptyValue(shouldOmitEmptyString ? [undefined, null, ''] : [undefined, null]);
569
- return actions.map(function (item) {
570
- var key = item.key; // e.g.: name, description, ...
571
- var actionKey = item.actionKey || item.key;
572
- var delta = diff[key];
573
- var before = oldObj[key];
574
- var now = newObj[key];
575
- var isNotDefinedBefore = isEmptyValue(oldObj[key]);
576
- var isNotDefinedNow = isEmptyValue(newObj[key]);
577
- var isOmitted = !Object.keys(newObj).includes(key);
578
- if (!delta) return undefined;
579
- if (isNotDefinedNow && !isOptionalField(item.action) && shouldPreventUnsettingRequiredFields) return undefined;
580
- if (isNotDefinedNow && isNotDefinedBefore) return undefined;
581
- if (!isNotDefinedNow && isNotDefinedBefore)
582
- // no value previously set
583
- return _defineProperty({
584
- action: item.action
585
- }, actionKey, now);
586
-
587
- /* no new value */
588
- if (isNotDefinedNow && isOmitted && !shouldUnsetOmittedProperties) return undefined;
589
- if (isNotDefinedNow)
590
- // value unset
591
- return {
592
- action: item.action
593
- };
594
-
595
- // We need to clone `before` as `patch` will mutate it
596
- var patched = patch(clone(before), delta);
597
- return _defineProperty({
598
- action: item.action
599
- }, actionKey, patched);
600
- }).filter(function (action) {
601
- return !isNil__default["default"](action);
602
- });
603
- }
604
-
605
- /**
606
- * Builds actions for simple reference objects, given a list of actions
607
- * E.g. [{ action: `setTaxCategory`, key: 'taxCategory' }]
608
- *
609
- * @param {Array} options.actions - a list of actions to be built
610
- * based on the given property
611
- * @param {Object} options.diff - the diff object
612
- * @param {Object} options.oldObj - the object that needs to be updated
613
- * @param {Object} options.newObj - the new representation of the object
614
- */
615
- function buildReferenceActions(_ref4) {
616
- var actions = _ref4.actions,
617
- diff = _ref4.diff,
618
- newObj = _ref4.newObj;
619
- return actions.map(function (item) {
620
- var action = item.action;
621
- var key = item.key;
622
- if (diff[key] && (
623
- // The `key` value was added or removed
624
- Array.isArray(diff[key]) ||
625
- // The `key` value id changed
626
- diff[key].id)) {
627
- var newValue = Array.isArray(diff[key]) ? getDeltaValue(diff[key]) : newObj[key];
628
- if (!newValue) return {
629
- action: action
630
- };
631
-
632
- // When the `id` of the object is undefined
633
- if (!newValue.id) {
634
- return _defineProperty({
635
- action: action
636
- }, key, {
637
- typeId: newValue.typeId,
638
- key: newValue.key
639
- });
640
- }
641
- return _defineProperty({
642
- action: action
643
- }, key, {
644
- typeId: newValue.typeId,
645
- id: newValue.id
646
- });
647
- }
648
- return undefined;
649
- }).filter(function (action) {
650
- return action;
651
- });
652
- }
653
-
654
- var baseActionsList$o = [{
655
- action: 'changeName',
656
- key: 'name'
657
- }, {
658
- action: 'changeSlug',
659
- key: 'slug'
660
- }, {
661
- action: 'setDescription',
662
- key: 'description'
663
- }, {
664
- action: 'changeOrderHint',
665
- key: 'orderHint'
666
- }, {
667
- action: 'setExternalId',
668
- key: 'externalId'
669
- }, {
670
- action: 'setKey',
671
- key: 'key'
672
- }];
673
- var metaActionsList$1 = [{
674
- action: 'setMetaTitle',
675
- key: 'metaTitle'
676
- }, {
677
- action: 'setMetaKeywords',
678
- key: 'metaKeywords'
679
- }, {
680
- action: 'setMetaDescription',
681
- key: 'metaDescription'
682
- }];
683
- var referenceActionsList$3 = [{
684
- action: 'changeParent',
685
- key: 'parent'
686
- }];
687
-
688
- /**
689
- * SYNC FUNCTIONS
690
- */
691
-
692
- function actionsMapBase$o(diff, oldObj, newObj) {
693
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
694
- return buildBaseAttributesActions({
695
- actions: baseActionsList$o,
696
- diff: diff,
697
- oldObj: oldObj,
698
- newObj: newObj,
699
- shouldOmitEmptyString: config.shouldOmitEmptyString,
700
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
701
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
702
- });
703
- }
704
- function actionsMapReferences$3(diff, oldObj, newObj) {
705
- return buildReferenceActions({
706
- actions: referenceActionsList$3,
707
- diff: diff,
708
- oldObj: oldObj,
709
- newObj: newObj
710
- });
711
- }
712
- function actionsMapMeta$1(diff, oldObj, newObj) {
713
- return buildBaseAttributesActions({
714
- actions: metaActionsList$1,
715
- diff: diff,
716
- oldObj: oldObj,
717
- newObj: newObj
718
- });
719
- }
720
-
721
- var CUSTOM = 'custom';
722
-
723
- /**
724
- * @function copyEmptyArrayProps
725
- * @description Create new key with empty array value on `newobj` for the arrays exist on `oldObj` and doesnt exist on `newobj`
726
- * One use case is to easily compare two object without generating this error `Cannot read property '0' of undefined`
727
- * @param {Object} oldObj
728
- * @param {Object} newObj
729
- * @returns {Array} Ordered Array [oldObj, newObj]
730
- */
731
- function copyEmptyArrayProps() {
732
- var oldObj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
733
- var newObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
734
- if (!isNil__default["default"](oldObj) && !isNil__default["default"](newObj)) {
735
- var nextObjectWithEmptyArray = Object.entries(oldObj).reduce(function (merged, _ref) {
736
- var _ref2 = _slicedToArray(_ref, 2),
737
- key = _ref2[0],
738
- value = _ref2[1];
739
- // Ignore CUSTOM key as this object is dynamic and its up to the user to dynamically change it
740
- // todo, it would be better if we pass it as ignored keys param
741
- if (key === CUSTOM) return merged;
742
- if (Array.isArray(value) && newObj[key] && newObj[key].length >= 1) {
743
- /* eslint-disable no-plusplus */
744
- var hashMapValue = value.reduce(function (acc, val) {
745
- acc[val.id] = val;
746
- return acc;
747
- }, {});
748
- for (var i = 0; i < newObj[key].length; i++) {
749
- if (!isNil__default["default"](newObj[key][i]) && _typeof(newObj[key][i]) === 'object' && !isNil__default["default"](newObj[key][i].id)) {
750
- // Since its unordered array elements then check if the element on `oldObj` exists by id
751
- var foundObject = hashMapValue[newObj[key][i].id];
752
- if (!isNil__default["default"](foundObject)) {
753
- var _copyEmptyArrayProps = copyEmptyArrayProps(foundObject, newObj[key][i]),
754
- _copyEmptyArrayProps2 = _slicedToArray(_copyEmptyArrayProps, 2),
755
- nestedObject = _copyEmptyArrayProps2[1];
756
- if (Object.isFrozen(merged[key])) {
757
- /* eslint-disable no-param-reassign */
758
- merged[key] = merged[key].slice();
759
- }
760
- /* eslint-disable no-param-reassign */
761
- merged[key][i] = nestedObject;
762
- }
763
- }
764
- }
765
- return merged;
766
- }
767
- if (Array.isArray(value)) {
768
- merged[key] = isNil__default["default"](newObj[key]) ? [] : newObj[key];
769
- return merged;
770
- }
771
- if (!isNil__default["default"](newObj[key]) && _typeof(value) === 'object' &&
772
- // Ignore Date as this will create invalid object since typeof date === 'object' return true
773
- // ex: {date: new Date()} will result {date: {}}
774
- !(value instanceof Date)) {
775
- var _copyEmptyArrayProps3 = copyEmptyArrayProps(value, newObj[key]),
776
- _copyEmptyArrayProps4 = _slicedToArray(_copyEmptyArrayProps3, 2),
777
- _nestedObject = _copyEmptyArrayProps4[1];
778
- merged[key] = _nestedObject;
779
- return merged;
780
- }
781
- return merged;
782
- }, _objectSpread2({}, newObj));
783
- return [oldObj, nextObjectWithEmptyArray];
784
- }
785
- return [oldObj, newObj];
786
- }
787
-
788
- function createCategoryMapActions(mapActionGroup, syncActionConfig) {
789
- return function doMapActions(diff, newObj, oldObj) {
790
- var allActions = [];
791
- allActions.push(mapActionGroup('base', function () {
792
- return actionsMapBase$o(diff, oldObj, newObj, syncActionConfig);
793
- }));
794
- allActions.push(mapActionGroup('references', function () {
795
- return actionsMapReferences$3(diff, oldObj, newObj);
796
- }));
797
- allActions.push(mapActionGroup('meta', function () {
798
- return actionsMapMeta$1(diff, oldObj, newObj);
799
- }));
800
- allActions.push(mapActionGroup('custom', function () {
801
- return actionsMapCustom(diff, newObj, oldObj);
802
- }));
803
- allActions.push(mapActionGroup('assets', function () {
804
- return actionsMapAssets$1(diff, oldObj, newObj);
805
- }));
806
- return flatten__default["default"](allActions);
807
- };
808
- }
809
- var categories = (function (actionGroupList, syncActionConfig) {
810
- // actionGroupList contains information about which action groups
811
- // are allowed or ignored
812
-
813
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
814
- // - action group name
815
- // - callback function that should return a list of actions that correspond
816
- // to the for the action group
817
-
818
- // this resulting function mapActionGroup will call the callback function
819
- // for allowed action groups and return the return value of the callback
820
- // It will return an empty array for ignored action groups
821
- var mapActionGroup = createMapActionGroup(actionGroupList);
822
- var doMapActions = createCategoryMapActions(mapActionGroup, syncActionConfig);
823
- var buildActions = createBuildActions(diff, doMapActions, copyEmptyArrayProps);
824
- return {
825
- buildActions: buildActions
826
- };
827
- });
828
-
829
- var isEmptyValue = createIsEmptyValue([undefined, null, '']);
830
- var baseActionsList$n = [{
831
- action: 'setSalutation',
832
- key: 'salutation'
833
- }, {
834
- action: 'changeEmail',
835
- key: 'email'
836
- }, {
837
- action: 'setFirstName',
838
- key: 'firstName'
839
- }, {
840
- action: 'setLastName',
841
- key: 'lastName'
842
- }, {
843
- action: 'setMiddleName',
844
- key: 'middleName'
845
- }, {
846
- action: 'setTitle',
847
- key: 'title'
848
- }, {
849
- action: 'setCustomerNumber',
850
- key: 'customerNumber'
851
- }, {
852
- action: 'setExternalId',
853
- key: 'externalId'
854
- }, {
855
- action: 'setCompanyName',
856
- key: 'companyName'
857
- }, {
858
- action: 'setDateOfBirth',
859
- key: 'dateOfBirth'
860
- }, {
861
- action: 'setLocale',
862
- key: 'locale'
863
- }, {
864
- action: 'setVatId',
865
- key: 'vatId'
866
- }, {
867
- action: 'setStores',
868
- key: 'stores'
869
- }, {
870
- action: 'setKey',
871
- key: 'key'
872
- }];
873
- var setDefaultBaseActionsList = [{
874
- action: 'setDefaultBillingAddress',
875
- key: 'defaultBillingAddressId',
876
- actionKey: 'addressId'
877
- }, {
878
- action: 'setDefaultShippingAddress',
879
- key: 'defaultShippingAddressId',
880
- actionKey: 'addressId'
881
- }];
882
- var referenceActionsList$2 = [{
883
- action: 'setCustomerGroup',
884
- key: 'customerGroup'
885
- }];
886
- var authenticationModeActionsList = [{
887
- action: 'setAuthenticationMode',
888
- key: 'authenticationMode',
889
- value: 'password'
890
- }];
891
-
892
- /**
893
- * SYNC FUNCTIONS
894
- */
895
-
896
- function actionsMapBase$n(diff, oldObj, newObj) {
897
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
898
- return buildBaseAttributesActions({
899
- actions: baseActionsList$n,
900
- diff: diff,
901
- oldObj: oldObj,
902
- newObj: newObj,
903
- shouldOmitEmptyString: config.shouldOmitEmptyString,
904
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
905
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
906
- });
907
- }
908
- function actionsMapSetDefaultBase(diff, oldObj, newObj) {
909
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
910
- return buildBaseAttributesActions({
911
- actions: setDefaultBaseActionsList,
912
- diff: diff,
913
- oldObj: oldObj,
914
- newObj: newObj,
915
- shouldOmitEmptyString: config.shouldOmitEmptyString,
916
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
917
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
918
- });
919
- }
920
- function actionsMapReferences$2(diff, oldObj, newObj) {
921
- return buildReferenceActions({
922
- actions: referenceActionsList$2,
923
- diff: diff,
924
- oldObj: oldObj,
925
- newObj: newObj
926
- });
927
- }
928
- function actionsMapAddresses(diff, oldObj, newObj) {
929
- var handler = createBuildArrayActions('addresses', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newObject) {
930
- return {
931
- action: 'addAddress',
932
- address: newObject
933
- };
934
- }), REMOVE_ACTIONS, function (objectToRemove) {
935
- return {
936
- action: 'removeAddress',
937
- addressId: objectToRemove.id
938
- };
939
- }), CHANGE_ACTIONS, function (oldObject, updatedObject) {
940
- return {
941
- action: 'changeAddress',
942
- addressId: oldObject.id,
943
- address: updatedObject
944
- };
945
- }));
946
- return handler(diff, oldObj, newObj);
947
- }
948
- function actionsMapAddBillingAddresses(diff, oldObj, newObj) {
949
- var handler = createBuildArrayActions('billingAddressIds', _defineProperty({}, ADD_ACTIONS, function (addressId) {
950
- return {
951
- action: 'addBillingAddressId',
952
- addressId: addressId
953
- };
954
- }));
955
- return handler(diff, oldObj, newObj);
956
- }
957
- function actionsMapRemoveBillingAddresses(diff, oldObj, newObj) {
958
- var handler = createBuildArrayActions('billingAddressIds', _defineProperty({}, REMOVE_ACTIONS, function (addressId) {
959
- return {
960
- action: 'removeBillingAddressId',
961
- addressId: addressId
962
- };
963
- }));
964
- return handler(diff, oldObj, newObj);
965
- }
966
- function actionsMapAddShippingAddresses(diff, oldObj, newObj) {
967
- var handler = createBuildArrayActions('shippingAddressIds', _defineProperty({}, ADD_ACTIONS, function (addressId) {
968
- return {
969
- action: 'addShippingAddressId',
970
- addressId: addressId
971
- };
972
- }));
973
- return handler(diff, oldObj, newObj);
974
- }
975
- function actionsMapRemoveShippingAddresses(diff, oldObj, newObj) {
976
- var handler = createBuildArrayActions('shippingAddressIds', _defineProperty({}, REMOVE_ACTIONS, function (addressId) {
977
- return {
978
- action: 'removeShippingAddressId',
979
- addressId: addressId
980
- };
981
- }));
982
- return handler(diff, oldObj, newObj);
983
- }
984
- function actionsMapAuthenticationModes(diff, oldObj, newObj) {
985
- // eslint-disable-next-line no-use-before-define
986
- return buildAuthenticationModeActions({
987
- actions: authenticationModeActionsList,
988
- diff: diff,
989
- oldObj: oldObj,
990
- newObj: newObj
991
- });
992
- }
993
- function buildAuthenticationModeActions(_ref) {
994
- var actions = _ref.actions,
995
- diff = _ref.diff,
996
- oldObj = _ref.oldObj,
997
- newObj = _ref.newObj;
998
- return actions.map(function (item) {
999
- var key = item.key;
1000
- var value = item.value || item.key;
1001
- var delta = diff[key];
1002
- var before = oldObj[key];
1003
- var now = newObj[key];
1004
- var isNotDefinedBefore = isEmptyValue(oldObj[key]);
1005
- var isNotDefinedNow = isEmptyValue(newObj[key]);
1006
- var authenticationModes = ['Password', 'ExternalAuth'];
1007
- if (!delta) return undefined;
1008
- if (isNotDefinedNow && isNotDefinedBefore) return undefined;
1009
- if (newObj.authenticationMode === 'Password' && !newObj.password) throw new Error('Cannot set to Password authentication mode without password');
1010
- if ('authenticationMode' in newObj && !authenticationModes.includes(newObj.authenticationMode)) throw new Error('Invalid Authentication Mode');
1011
- if (!isNotDefinedNow && isNotDefinedBefore) {
1012
- // no value previously set
1013
- if (newObj.authenticationMode === 'ExternalAuth') return {
1014
- action: item.action,
1015
- authMode: now
1016
- };
1017
- return _defineProperty({
1018
- action: item.action,
1019
- authMode: now
1020
- }, value, newObj.password);
1021
- }
1022
-
1023
- /* no new value */
1024
- if (isNotDefinedNow && !{}.hasOwnProperty.call(newObj, key)) return undefined;
1025
- if (isNotDefinedNow && {}.hasOwnProperty.call(newObj, key))
1026
- // value unset
1027
- return undefined;
1028
-
1029
- // We need to clone `before` as `patch` will mutate it
1030
- var patched = patch(clone(before), delta);
1031
- if (newObj.authenticationMode === 'ExternalAuth') return {
1032
- action: item.action,
1033
- authMode: patched
1034
- };
1035
- return _defineProperty({
1036
- action: item.action,
1037
- authMode: patched
1038
- }, value, newObj.password);
1039
- }).filter(function (action) {
1040
- return !isNil__default["default"](action);
1041
- });
1042
- }
1043
-
1044
- function createCustomerMapActions$1(mapActionGroup, syncActionConfig) {
1045
- return function doMapActions(diff, newObj, oldObj) {
1046
- var allActions = [];
1047
- allActions.push(mapActionGroup('base', function () {
1048
- return actionsMapBase$n(diff, oldObj, newObj, syncActionConfig);
1049
- }));
1050
- allActions.push(mapActionGroup('references', function () {
1051
- return actionsMapReferences$2(diff, oldObj, newObj);
1052
- }));
1053
- allActions.push(mapActionGroup('billingAddressIds', function () {
1054
- return actionsMapRemoveBillingAddresses(diff, oldObj, newObj);
1055
- }));
1056
- allActions.push(mapActionGroup('shippingAddressIds', function () {
1057
- return actionsMapRemoveShippingAddresses(diff, oldObj, newObj);
1058
- }));
1059
- allActions.push(mapActionGroup('addresses', function () {
1060
- return actionsMapAddresses(diff, oldObj, newObj);
1061
- }));
1062
- allActions.push(mapActionGroup('base', function () {
1063
- return actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig);
1064
- }));
1065
- allActions.push(mapActionGroup('billingAddressIds', function () {
1066
- return actionsMapAddBillingAddresses(diff, oldObj, newObj);
1067
- }));
1068
- allActions.push(mapActionGroup('shippingAddressIds', function () {
1069
- return actionsMapAddShippingAddresses(diff, oldObj, newObj);
1070
- }));
1071
- allActions.push(mapActionGroup('custom', function () {
1072
- return actionsMapCustom(diff, newObj, oldObj);
1073
- }));
1074
- allActions.push(mapActionGroup('authenticationModes', function () {
1075
- return actionsMapAuthenticationModes(diff, oldObj, newObj);
1076
- }));
1077
- return flatten__default["default"](allActions);
1078
- };
1079
- }
1080
- var customers = (function (actionGroupList, syncActionConfig) {
1081
- // actionGroupList contains information about which action groups
1082
- // are allowed or ignored
1083
-
1084
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
1085
- // - action group name
1086
- // - callback function that should return a list of actions that correspond
1087
- // to the for the action group
1088
-
1089
- // this resulting function mapActionGroup will call the callback function
1090
- // for allowed action groups and return the return value of the callback
1091
- // It will return an empty array for ignored action groups
1092
- var mapActionGroup = createMapActionGroup(actionGroupList);
1093
- var doMapActions = createCustomerMapActions$1(mapActionGroup, syncActionConfig);
1094
- var buildActions = createBuildActions(diff, doMapActions, copyEmptyArrayProps);
1095
- return {
1096
- buildActions: buildActions
1097
- };
1098
- });
1099
-
1100
- var baseActionsList$m = [{
1101
- action: 'changeQuantity',
1102
- key: 'quantityOnStock',
1103
- actionKey: 'quantity'
1104
- }, {
1105
- action: 'setRestockableInDays',
1106
- key: 'restockableInDays'
1107
- }, {
1108
- action: 'setExpectedDelivery',
1109
- key: 'expectedDelivery'
1110
- }];
1111
- var referenceActionsList$1 = [{
1112
- action: 'setSupplyChannel',
1113
- key: 'supplyChannel'
1114
- }];
1115
-
1116
- /**
1117
- * SYNC FUNCTIONS
1118
- */
1119
-
1120
- function actionsMapBase$m(diff, oldObj, newObj) {
1121
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1122
- return buildBaseAttributesActions({
1123
- actions: baseActionsList$m,
1124
- diff: diff,
1125
- oldObj: oldObj,
1126
- newObj: newObj,
1127
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1128
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
1129
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
1130
- });
1131
- }
1132
- function actionsMapReferences$1(diff, oldObj, newObj) {
1133
- return buildReferenceActions({
1134
- actions: referenceActionsList$1,
1135
- diff: diff,
1136
- oldObj: oldObj,
1137
- newObj: newObj
1138
- });
1139
- }
1140
-
1141
- function createInventoryMapActions(mapActionGroup, syncActionConfig) {
1142
- return function doMapActions(diff, newObj, oldObj) {
1143
- var allActions = [];
1144
- allActions.push(mapActionGroup('base', function () {
1145
- return actionsMapBase$m(diff, oldObj, newObj, syncActionConfig);
1146
- }));
1147
- allActions.push(mapActionGroup('references', function () {
1148
- return actionsMapReferences$1(diff, oldObj, newObj);
1149
- }));
1150
- allActions.push(mapActionGroup('custom', function () {
1151
- return actionsMapCustom(diff, newObj, oldObj);
1152
- }));
1153
- return flatten__default["default"](allActions);
1154
- };
1155
- }
1156
- var inventories = (function (actionGroupList, syncActionConfig) {
1157
- // actionGroupList contains information about which action groups
1158
- // are allowed or ignored
1159
-
1160
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
1161
- // - action group name
1162
- // - callback function that should return a list of actions that correspond
1163
- // to the for the action group
1164
-
1165
- // this resulting function mapActionGroup will call the callback function
1166
- // for allowed action groups and return the return value of the callback
1167
- // It will return an empty array for ignored action groups
1168
- var mapActionGroup = createMapActionGroup(actionGroupList);
1169
- var doMapActions = createInventoryMapActions(mapActionGroup, syncActionConfig);
1170
- var buildActions = createBuildActions(diff, doMapActions);
1171
- return {
1172
- buildActions: buildActions
1173
- };
1174
- });
1175
-
1176
- function extractMatchingPairs(hashMap, key, before, now) {
1177
- var oldObjPos;
1178
- var newObjPos;
1179
- var oldObj;
1180
- var newObj;
1181
- if (hashMap[key]) {
1182
- oldObjPos = hashMap[key][0];
1183
- newObjPos = hashMap[key][1];
1184
- if (before && before[oldObjPos]) oldObj = before[oldObjPos];
1185
- if (now && now[newObjPos]) newObj = now[newObjPos];
1186
- }
1187
- return {
1188
- oldObj: oldObj,
1189
- newObj: newObj
1190
- };
1191
- }
1192
-
1193
- var REGEX_NUMBER$3 = new RegExp(/^\d+$/);
1194
- var REGEX_UNDERSCORE_NUMBER$3 = new RegExp(/^_\d+$/);
1195
- function preProcessCollection() {
1196
- var collection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1197
- var identifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id';
1198
- return collection.reduce(function (acc, currentValue, currentIndex) {
1199
- acc.refByIndex[String(currentIndex)] = currentValue[identifier];
1200
- acc.refByIdentifier[currentValue[identifier]] = String(currentIndex);
1201
- return acc;
1202
- }, {
1203
- refByIndex: {},
1204
- refByIdentifier: {}
1205
- });
1206
- }
1207
-
1208
- // creates a hash of a location of an item in collection1 and collection2
1209
- function findMatchingPairs(diff) {
1210
- var before = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1211
- var now = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1212
- var identifier = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'id';
1213
- var result = {};
1214
- var _preProcessCollection = preProcessCollection(before, identifier),
1215
- beforeObjRefByIdentifier = _preProcessCollection.refByIdentifier,
1216
- beforeObjRefByIndex = _preProcessCollection.refByIndex;
1217
- var _preProcessCollection2 = preProcessCollection(now, identifier),
1218
- nowObjRefByIdentifier = _preProcessCollection2.refByIdentifier,
1219
- nowObjRefByIndex = _preProcessCollection2.refByIndex;
1220
- forEach__default["default"](diff, function (item, key) {
1221
- if (REGEX_NUMBER$3.test(key)) {
1222
- var matchingIdentifier = nowObjRefByIndex[key];
1223
- result[key] = [beforeObjRefByIdentifier[matchingIdentifier], key];
1224
- } else if (REGEX_UNDERSCORE_NUMBER$3.test(key)) {
1225
- var index = key.substring(1);
1226
- var _matchingIdentifier = beforeObjRefByIndex[index];
1227
- result[key] = [index, nowObjRefByIdentifier[_matchingIdentifier]];
1228
- }
1229
- });
1230
- return result;
1231
- }
1232
-
1233
- var REGEX_NUMBER$2 = new RegExp(/^\d+$/);
1234
- var REGEX_UNDERSCORE_NUMBER$2 = new RegExp(/^_\d+$/);
1235
- var baseActionsList$l = [{
1236
- action: 'changeName',
1237
- key: 'name'
1238
- }, {
1239
- action: 'changeSlug',
1240
- key: 'slug'
1241
- }, {
1242
- action: 'setDescription',
1243
- key: 'description'
1244
- }, {
1245
- action: 'setSearchKeywords',
1246
- key: 'searchKeywords'
1247
- }, {
1248
- action: 'setKey',
1249
- key: 'key'
1250
- }, {
1251
- action: 'setPriceMode',
1252
- key: 'priceMode'
1253
- }];
1254
- var baseAssetActionsList = [{
1255
- action: 'setAssetKey',
1256
- key: 'key',
1257
- actionKey: 'assetKey'
1258
- }, {
1259
- action: 'changeAssetName',
1260
- key: 'name'
1261
- }, {
1262
- action: 'setAssetDescription',
1263
- key: 'description'
1264
- }, {
1265
- action: 'setAssetTags',
1266
- key: 'tags'
1267
- }, {
1268
- action: 'setAssetSources',
1269
- key: 'sources'
1270
- }];
1271
- var metaActionsList = [{
1272
- action: 'setMetaTitle',
1273
- key: 'metaTitle'
1274
- }, {
1275
- action: 'setMetaDescription',
1276
- key: 'metaDescription'
1277
- }, {
1278
- action: 'setMetaKeywords',
1279
- key: 'metaKeywords'
1280
- }];
1281
- var referenceActionsList = [{
1282
- action: 'setTaxCategory',
1283
- key: 'taxCategory'
1284
- }, {
1285
- action: 'transitionState',
1286
- key: 'state'
1287
- }];
1288
-
1289
- /**
1290
- * HELPER FUNCTIONS
1291
- */
1292
-
1293
- var getIsAddAction = function getIsAddAction(key, resource) {
1294
- return REGEX_NUMBER$2.test(key) && Array.isArray(resource) && resource.length;
1295
- };
1296
- var getIsUpdateAction = function getIsUpdateAction(key, resource) {
1297
- return REGEX_NUMBER$2.test(key) && Object.keys(resource).length;
1298
- };
1299
- var getIsRemoveAction = function getIsRemoveAction(key, resource) {
1300
- return REGEX_UNDERSCORE_NUMBER$2.test(key) && Number(resource[2]) === 0;
1301
- };
1302
- var getIsItemMovedAction = function getIsItemMovedAction(key, resource) {
1303
- return REGEX_UNDERSCORE_NUMBER$2.test(key) && Number(resource[2]) === 3;
1304
- };
1305
- function _buildSkuActions(variantDiff, oldVariant) {
1306
- if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
1307
- var newValue = getDeltaValue(variantDiff.sku);
1308
- if (!newValue && !oldVariant.sku) return null;
1309
- return {
1310
- action: 'setSku',
1311
- variantId: oldVariant.id,
1312
- sku: newValue || null
1313
- };
1314
- }
1315
- return null;
1316
- }
1317
- function _buildKeyActions(variantDiff, oldVariant) {
1318
- if ({}.hasOwnProperty.call(variantDiff, 'key')) {
1319
- var newValue = getDeltaValue(variantDiff.key);
1320
- if (!newValue && !oldVariant.key) return null;
1321
- return {
1322
- action: 'setProductVariantKey',
1323
- variantId: oldVariant.id,
1324
- key: newValue || null
1325
- };
1326
- }
1327
- return null;
1328
- }
1329
- function _buildAttributeValue(diffedValue, oldAttributeValue, newAttributeValue) {
1330
- var value;
1331
- if (Array.isArray(diffedValue)) value = getDeltaValue(diffedValue, oldAttributeValue);else if (typeof diffedValue === 'string')
1332
- // LText: value: {en: "", de: ""}
1333
- // Enum: value: {key: "foo", label: "Foo"}
1334
- // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1335
- // Money: value: {centAmount: 123, currencyCode: ""}
1336
- // *: value: ""
1337
-
1338
- // normal
1339
- value = getDeltaValue(diffedValue, oldAttributeValue);else if (diffedValue.centAmount || diffedValue.currencyCode)
1340
- // Money
1341
- value = {
1342
- centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : newAttributeValue.centAmount,
1343
- currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : newAttributeValue.currencyCode
1344
- };else if (diffedValue.key)
1345
- // Enum / LEnum (use only the key)
1346
- value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1347
- // set-typed attribute
1348
- value = newAttributeValue;
1349
- } else {
1350
- // LText
1351
-
1352
- var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1353
- var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1354
- return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1355
- }, _objectSpread2({}, oldAttributeValue));
1356
- value = updatedValue;
1357
- }
1358
- return value;
1359
- }
1360
- function _buildNewSetAttributeAction(variantId, attr, sameForAllAttributeNames) {
1361
- var attributeName = attr && attr.name;
1362
- if (!attributeName) return undefined;
1363
- var action = {
1364
- action: 'setAttribute',
1365
- variantId: variantId,
1366
- name: attributeName,
1367
- value: attr.value
1368
- };
1369
- if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
1370
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1371
- action: 'setAttributeInAllVariants'
1372
- });
1373
- delete action.variantId;
1374
- }
1375
- return action;
1376
- }
1377
- function _buildSetAttributeAction(diffedValue, oldVariant, attribute, sameForAllAttributeNames) {
1378
- if (!attribute) return undefined;
1379
- var action = {
1380
- action: 'setAttribute',
1381
- variantId: oldVariant.id,
1382
- name: attribute.name
1383
- };
1384
-
1385
- // Used as original object for patching long diff text
1386
- var oldAttribute = oldVariant.attributes.find(function (a) {
1387
- return a.name === attribute.name;
1388
- }) || {};
1389
- if (sameForAllAttributeNames.indexOf(attribute.name) !== -1) {
1390
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1391
- action: 'setAttributeInAllVariants'
1392
- });
1393
- delete action.variantId;
1394
- }
1395
- action.value = _buildAttributeValue(diffedValue, oldAttribute.value, attribute.value);
1396
- return action;
1397
- }
1398
- function _buildNewSetProductAttributeAction(attr) {
1399
- var attributeName = attr && attr.name;
1400
- if (!attributeName) return undefined;
1401
- var action = {
1402
- action: 'setProductAttribute',
1403
- name: attributeName,
1404
- value: attr.value
1405
- };
1406
- return action;
1407
- }
1408
- function _buildSetProductAttributeAction(diffedValue, oldProductData, newAttribute) {
1409
- if (!newAttribute) return undefined;
1410
- var action = {
1411
- action: 'setProductAttribute',
1412
- name: newAttribute.name
1413
- };
1414
-
1415
- // Used as original object for patching long diff text
1416
- var oldAttribute = oldProductData.attributes.find(function (a) {
1417
- return a.name === newAttribute.name;
1418
- }) || {};
1419
- action.value = _buildAttributeValue(diffedValue, oldAttribute.value, newAttribute.value);
1420
- return action;
1421
- }
1422
- function _buildVariantImagesAction(diffedImages) {
1423
- var oldVariant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1424
- var newVariant = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1425
- var actions = [];
1426
- // generate a hashMap to be able to reference the right image from both ends
1427
- var matchingImagePairs = findMatchingPairs(diffedImages, oldVariant.images, newVariant.images, 'url');
1428
- forEach__default["default"](diffedImages, function (image, key) {
1429
- var _extractMatchingPairs = extractMatchingPairs(matchingImagePairs, key, oldVariant.images, newVariant.images),
1430
- oldObj = _extractMatchingPairs.oldObj,
1431
- newObj = _extractMatchingPairs.newObj;
1432
- if (REGEX_NUMBER$2.test(key)) {
1433
- // New image
1434
- if (Array.isArray(image) && image.length) actions.push({
1435
- action: 'addExternalImage',
1436
- variantId: oldVariant.id,
1437
- image: getDeltaValue(image)
1438
- });else if (_typeof(image) === 'object') if ({}.hasOwnProperty.call(image, 'url') && image.url.length === 2) {
1439
- // There is a new image, remove the old one first.
1440
- actions.push({
1441
- action: 'removeImage',
1442
- variantId: oldVariant.id,
1443
- imageUrl: oldObj.url
1444
- });
1445
- actions.push({
1446
- action: 'addExternalImage',
1447
- variantId: oldVariant.id,
1448
- image: newObj
1449
- });
1450
- } else if ({}.hasOwnProperty.call(image, 'label') && (image.label.length === 1 || image.label.length === 2)) actions.push({
1451
- action: 'setImageLabel',
1452
- variantId: oldVariant.id,
1453
- imageUrl: oldObj.url,
1454
- label: getDeltaValue(image.label)
1455
- });
1456
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(image) && image.length === 3) {
1457
- if (Number(image[2]) === 3)
1458
- // image position changed
1459
- actions.push({
1460
- action: 'moveImageToPosition',
1461
- variantId: oldVariant.id,
1462
- imageUrl: oldObj.url,
1463
- position: Number(image[1])
1464
- });else if (Number(image[2]) === 0)
1465
- // image removed
1466
- actions.push({
1467
- action: 'removeImage',
1468
- variantId: oldVariant.id,
1469
- imageUrl: oldObj.url
1470
- });
1471
- }
1472
- });
1473
- return actions;
1474
- }
1475
- function _buildVariantPricesAction(diffedPrices) {
1476
- var oldVariant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1477
- var newVariant = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1478
- var enableDiscounted = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1479
- var addPriceActions = [];
1480
- var changePriceActions = [];
1481
- var removePriceActions = [];
1482
-
1483
- // generate a hashMap to be able to reference the right image from both ends
1484
- var matchingPricePairs = findMatchingPairs(diffedPrices, oldVariant.prices, newVariant.prices);
1485
- forEach__default["default"](diffedPrices, function (price, key) {
1486
- var _extractMatchingPairs2 = extractMatchingPairs(matchingPricePairs, key, oldVariant.prices, newVariant.prices),
1487
- oldObj = _extractMatchingPairs2.oldObj,
1488
- newObj = _extractMatchingPairs2.newObj;
1489
- if (getIsAddAction(key, price)) {
1490
- // Remove read-only fields
1491
- var patchedPrice = price.map(function (p) {
1492
- var shallowClone = _objectSpread2({}, p);
1493
- if (enableDiscounted !== true) delete shallowClone.discounted;
1494
- return shallowClone;
1495
- });
1496
- addPriceActions.push({
1497
- action: 'addPrice',
1498
- variantId: oldVariant.id,
1499
- price: getDeltaValue(patchedPrice)
1500
- });
1501
- return;
1502
- }
1503
- if (getIsUpdateAction(key, price)) {
1504
- // Remove the discounted field and make sure that the price
1505
- // still has other values, otherwise simply return
1506
- var filteredPrice = _objectSpread2({}, price);
1507
- if (enableDiscounted !== true) delete filteredPrice.discounted;
1508
- if (Object.keys(filteredPrice).length) {
1509
- // At this point price should have changed, simply pick the new one
1510
- var newPrice = _objectSpread2({}, newObj);
1511
- if (enableDiscounted !== true) delete newPrice.discounted;
1512
- changePriceActions.push({
1513
- action: 'changePrice',
1514
- priceId: oldObj.id,
1515
- price: newPrice
1516
- });
1517
- }
1518
- return;
1519
- }
1520
- if (getIsRemoveAction(key, price)) {
1521
- // price removed
1522
- removePriceActions.push({
1523
- action: 'removePrice',
1524
- priceId: oldObj.id
1525
- });
1526
- }
1527
- });
1528
- return [addPriceActions, changePriceActions, removePriceActions];
1529
- }
1530
- function _buildProductAttributesActions(diffedAttributes, oldProductData, newProductData) {
1531
- var actions = [];
1532
- if (!diffedAttributes) return actions;
1533
- forEach__default["default"](diffedAttributes, function (value, key) {
1534
- if (REGEX_NUMBER$2.test(key)) {
1535
- if (Array.isArray(value)) {
1536
- var setAction = _buildNewSetProductAttributeAction(getDeltaValue(value));
1537
- if (setAction) actions.push(setAction);
1538
- } else if (newProductData.attributes) {
1539
- var _setAction = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[key]);
1540
- if (_setAction) actions.push(_setAction);
1541
- }
1542
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) {
1543
- if (Array.isArray(value)) {
1544
- // Ignore pure array moves!
1545
- if (value.length === 3 && value[2] === 3) return;
1546
- var deltaValue = getDeltaValue(value);
1547
- if (!deltaValue) if (value[0] && value[0].name)
1548
- // unset attribute if
1549
- deltaValue = {
1550
- name: value[0].name
1551
- };else deltaValue = undefined;
1552
- var _setAction2 = _buildNewSetProductAttributeAction(deltaValue);
1553
- if (_setAction2) actions.push(_setAction2);
1554
- } else {
1555
- var index = key.substring(1);
1556
- if (newProductData.attributes) {
1557
- var _setAction3 = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[index]);
1558
- if (_setAction3) actions.push(_setAction3);
1559
- }
1560
- }
1561
- }
1562
- });
1563
- return actions;
1564
- }
1565
- function _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames) {
1566
- var actions = [];
1567
- if (!attributes) return actions;
1568
- forEach__default["default"](attributes, function (value, key) {
1569
- if (REGEX_NUMBER$2.test(key)) {
1570
- if (Array.isArray(value)) {
1571
- var id = oldVariant.id;
1572
- var deltaValue = getDeltaValue(value);
1573
- var setAction = _buildNewSetAttributeAction(id, deltaValue, sameForAllAttributeNames);
1574
- if (setAction) actions.push(setAction);
1575
- } else if (newVariant.attributes) {
1576
- var _setAction4 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1577
- if (_setAction4) actions.push(_setAction4);
1578
- }
1579
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(value)) {
1580
- // Ignore pure array moves!
1581
- if (value.length === 3 && value[2] === 3) return;
1582
- var _id = oldVariant.id;
1583
- var _deltaValue = getDeltaValue(value);
1584
- if (!_deltaValue) if (value[0] && value[0].name)
1585
- // unset attribute if
1586
- _deltaValue = {
1587
- name: value[0].name
1588
- };else _deltaValue = undefined;
1589
- var _setAction5 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1590
- if (_setAction5) actions.push(_setAction5);
1591
- } else {
1592
- var index = key.substring(1);
1593
- if (newVariant.attributes) {
1594
- var _setAction6 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1595
- if (_setAction6) actions.push(_setAction6);
1596
- }
1597
- }
1598
- });
1599
- return actions;
1600
- }
1601
- function toAssetIdentifier(asset) {
1602
- var assetIdentifier = asset.id ? {
1603
- assetId: asset.id
1604
- } : {
1605
- assetKey: asset.key
1606
- };
1607
- return assetIdentifier;
1608
- }
1609
- function toVariantIdentifier(variant) {
1610
- var id = variant.id,
1611
- sku = variant.sku;
1612
- return id ? {
1613
- variantId: id
1614
- } : {
1615
- sku: sku
1616
- };
1617
- }
1618
- function _buildVariantChangeAssetOrderAction(diffAssets, oldVariant, newVariant) {
1619
- var isAssetOrderChanged = Object.entries(diffAssets).find(function (entry) {
1620
- return getIsItemMovedAction(entry[0], entry[1]);
1621
- });
1622
- if (!isAssetOrderChanged) {
1623
- return [];
1624
- }
1625
- var assetIdsBefore = oldVariant.assets.map(function (_) {
1626
- return _.id;
1627
- });
1628
- var assetIdsCurrent = newVariant.assets.map(function (_) {
1629
- return _.id;
1630
- }).filter(function (_) {
1631
- return _ !== undefined;
1632
- });
1633
- var assetIdsToKeep = intersection__default["default"](assetIdsCurrent, assetIdsBefore);
1634
- var assetIdsToRemove = without__default["default"].apply(void 0, [assetIdsBefore].concat(_toConsumableArray(assetIdsToKeep)));
1635
- var changeAssetOrderAction = _objectSpread2({
1636
- action: 'changeAssetOrder',
1637
- assetOrder: assetIdsToKeep.concat(assetIdsToRemove)
1638
- }, toVariantIdentifier(oldVariant));
1639
- return [changeAssetOrderAction];
1640
- }
1641
- function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
1642
- var assetActions = [];
1643
-
1644
- // generate a hashMap to be able to reference the right asset from both ends
1645
- var matchingAssetPairs = findMatchingPairs(diffAssets, oldVariant.assets, newVariant.assets);
1646
- forEach__default["default"](diffAssets, function (asset, key) {
1647
- var _extractMatchingPairs3 = extractMatchingPairs(matchingAssetPairs, key, oldVariant.assets, newVariant.assets),
1648
- oldAsset = _extractMatchingPairs3.oldObj,
1649
- newAsset = _extractMatchingPairs3.newObj;
1650
- if (getIsAddAction(key, asset)) {
1651
- assetActions.push(_objectSpread2(_objectSpread2({
1652
- action: 'addAsset',
1653
- asset: getDeltaValue(asset)
1654
- }, toVariantIdentifier(newVariant)), {}, {
1655
- position: Number(key)
1656
- }));
1657
- return;
1658
- }
1659
- if (getIsUpdateAction(key, asset)) {
1660
- // todo add changeAssetOrder
1661
- var basicActions = buildBaseAttributesActions({
1662
- actions: baseAssetActionsList,
1663
- diff: asset,
1664
- oldObj: oldAsset,
1665
- newObj: newAsset
1666
- }).map(function (action) {
1667
- // in case of 'setAssetKey' then the identifier will be only 'assetId'
1668
- if (action.action === 'setAssetKey') {
1669
- return _objectSpread2(_objectSpread2(_objectSpread2({}, action), toVariantIdentifier(oldVariant)), {}, {
1670
- assetId: oldAsset.id
1671
- });
1672
- }
1673
- return _objectSpread2(_objectSpread2(_objectSpread2({}, action), toVariantIdentifier(oldVariant)), toAssetIdentifier(oldAsset));
1674
- });
1675
- assetActions.push.apply(assetActions, _toConsumableArray(basicActions));
1676
- if (asset.custom) {
1677
- var customActions = actionsMapCustom(asset, newAsset, oldAsset, _objectSpread2(_objectSpread2({
1678
- actions: {
1679
- setCustomType: 'setAssetCustomType',
1680
- setCustomField: 'setAssetCustomField'
1681
- }
1682
- }, toVariantIdentifier(oldVariant)), toAssetIdentifier(oldAsset)));
1683
- assetActions.push.apply(assetActions, _toConsumableArray(customActions));
1684
- }
1685
- return;
1686
- }
1687
- if (getIsRemoveAction(key, asset)) {
1688
- assetActions.push(_objectSpread2(_objectSpread2({
1689
- action: 'removeAsset'
1690
- }, toAssetIdentifier(oldAsset)), toVariantIdentifier(oldVariant)));
1691
- }
1692
- });
1693
- var changedAssetOrderAction = _buildVariantChangeAssetOrderAction(diffAssets, oldVariant, newVariant);
1694
- return [].concat(_toConsumableArray(changedAssetOrderAction), assetActions);
1695
- }
1696
-
1697
- /**
1698
- * SYNC FUNCTIONS
1699
- */
1700
-
1701
- function actionsMapBase$l(diff, oldObj, newObj) {
1702
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1703
- return buildBaseAttributesActions({
1704
- actions: baseActionsList$l,
1705
- diff: diff,
1706
- oldObj: oldObj,
1707
- newObj: newObj,
1708
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1709
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
1710
- });
1711
- }
1712
- function actionsMapMeta(diff, oldObj, newObj) {
1713
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1714
- return buildBaseAttributesActions({
1715
- actions: metaActionsList,
1716
- diff: diff,
1717
- oldObj: oldObj,
1718
- newObj: newObj,
1719
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1720
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
1721
- });
1722
- }
1723
- function actionsMapAddVariants(diff, oldObj, newObj) {
1724
- var handler = createBuildArrayActions('variants', _defineProperty({}, ADD_ACTIONS, function (newObject) {
1725
- return _objectSpread2(_objectSpread2({}, newObject), {}, {
1726
- action: 'addVariant'
1727
- });
1728
- }));
1729
- return handler(diff, oldObj, newObj);
1730
- }
1731
- function actionsMapRemoveVariants(diff, oldObj, newObj) {
1732
- var handler = createBuildArrayActions('variants', _defineProperty({}, REMOVE_ACTIONS, function (_ref) {
1733
- var id = _ref.id;
1734
- return {
1735
- action: 'removeVariant',
1736
- id: id
1737
- };
1738
- }));
1739
- return handler(diff, oldObj, newObj);
1740
- }
1741
- function actionsMapReferences(diff, oldObj, newObj) {
1742
- return buildReferenceActions({
1743
- actions: referenceActionsList,
1744
- diff: diff,
1745
- oldObj: oldObj,
1746
- newObj: newObj
1747
- });
1748
- }
1749
- function actionsMapCategories(diff) {
1750
- var actions = [];
1751
- if (!diff.categories) return actions;
1752
- var addToCategoryActions = [];
1753
- var removeFromCategoryActions = [];
1754
- forEach__default["default"](diff.categories, function (category) {
1755
- if (Array.isArray(category)) {
1756
- var action = {
1757
- category: category[0]
1758
- };
1759
- if (category.length === 3) {
1760
- // Ignore pure array moves!
1761
- if (category[2] !== 3) {
1762
- action.action = 'removeFromCategory';
1763
- removeFromCategoryActions.push(action);
1764
- }
1765
- } else if (category.length === 1) {
1766
- action.action = 'addToCategory';
1767
- addToCategoryActions.push(action);
1768
- }
1769
- }
1770
- });
1771
-
1772
- // Make sure `removeFromCategory` actions come first
1773
- return removeFromCategoryActions.concat(addToCategoryActions);
1774
- }
1775
- function actionsMapCategoryOrderHints(diff) {
1776
- if (!diff.categoryOrderHints) return [];
1777
- // Ignore this pattern as its means no changes happened [{},0,0]
1778
- if (Array.isArray(diff.categoryOrderHints)) return [];
1779
- return Object.keys(diff.categoryOrderHints).map(function (categoryId) {
1780
- var hintChange = diff.categoryOrderHints[categoryId];
1781
- var action = {
1782
- action: 'setCategoryOrderHint',
1783
- categoryId: categoryId
1784
- };
1785
- if (hintChange.length === 1)
1786
- // item was added
1787
- action.orderHint = hintChange[0];else if (hintChange.length === 2 && hintChange[1] !== 0)
1788
- // item was changed
1789
- action.orderHint = hintChange[1];
1790
-
1791
- // else item was removed -> do not set 'orderHint' property
1792
-
1793
- return action;
1794
- });
1795
- }
1796
- function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
1797
- var allAssetsActions = [];
1798
- var variants = diff.variants;
1799
- if (variants) forEach__default["default"](variants, function (variant, key) {
1800
- var _extractMatchingPairs4 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1801
- oldVariant = _extractMatchingPairs4.oldObj,
1802
- newVariant = _extractMatchingPairs4.newObj;
1803
- if (variant.assets && (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key))) {
1804
- var assetActions = _buildVariantAssetsActions(variant.assets, oldVariant, newVariant);
1805
- allAssetsActions = allAssetsActions.concat(assetActions);
1806
- }
1807
- });
1808
- return allAssetsActions;
1809
- }
1810
- function actionsMapProductAttributes(diffedProductData, oldProductData, newProductData) {
1811
- return _buildProductAttributesActions(diffedProductData.attributes, oldProductData, newProductData);
1812
- }
1813
- function actionsMapAttributes$1(diff, oldObj, newObj) {
1814
- var sameForAllAttributeNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1815
- var variantHashMap = arguments.length > 4 ? arguments[4] : undefined;
1816
- var actions = [];
1817
- var variants = diff.variants;
1818
- if (variants) forEach__default["default"](variants, function (variant, key) {
1819
- var _extractMatchingPairs5 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1820
- oldVariant = _extractMatchingPairs5.oldObj,
1821
- newVariant = _extractMatchingPairs5.newObj;
1822
- if (REGEX_NUMBER$2.test(key) && !Array.isArray(variant)) {
1823
- var skuAction = _buildSkuActions(variant, oldVariant);
1824
- var keyAction = _buildKeyActions(variant, oldVariant);
1825
- if (skuAction) actions.push(skuAction);
1826
- if (keyAction) actions.push(keyAction);
1827
- var attributes = variant.attributes;
1828
- var attrActions = _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames);
1829
- actions = actions.concat(attrActions);
1830
- }
1831
- });
1832
-
1833
- // Ensure that an action is unique.
1834
- // This is especially necessary for SFA attributes.
1835
- return uniqWith__default["default"](actions, function (a, b) {
1836
- return a.action === b.action && a.name === b.name && a.variantId === b.variantId;
1837
- });
1838
- }
1839
- function actionsMapImages(diff, oldObj, newObj, variantHashMap) {
1840
- var actions = [];
1841
- var variants = diff.variants;
1842
- if (variants) forEach__default["default"](variants, function (variant, key) {
1843
- var _extractMatchingPairs6 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1844
- oldVariant = _extractMatchingPairs6.oldObj,
1845
- newVariant = _extractMatchingPairs6.newObj;
1846
- if (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key)) {
1847
- var vActions = _buildVariantImagesAction(variant.images, oldVariant, newVariant);
1848
- actions = actions.concat(vActions);
1849
- }
1850
- });
1851
- return actions;
1852
- }
1853
- function actionsMapPrices(diff, oldObj, newObj, variantHashMap, enableDiscounted) {
1854
- var addPriceActions = [];
1855
- var changePriceActions = [];
1856
- var removePriceActions = [];
1857
- var variants = diff.variants;
1858
- if (variants) forEach__default["default"](variants, function (variant, key) {
1859
- var _extractMatchingPairs7 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1860
- oldVariant = _extractMatchingPairs7.oldObj,
1861
- newVariant = _extractMatchingPairs7.newObj;
1862
- if (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key)) {
1863
- var _buildVariantPricesAc = _buildVariantPricesAction(variant.prices, oldVariant, newVariant, enableDiscounted),
1864
- _buildVariantPricesAc2 = _slicedToArray(_buildVariantPricesAc, 3),
1865
- addPriceAction = _buildVariantPricesAc2[0],
1866
- changePriceAction = _buildVariantPricesAc2[1],
1867
- removePriceAction = _buildVariantPricesAc2[2];
1868
- addPriceActions = addPriceActions.concat(addPriceAction);
1869
- changePriceActions = changePriceActions.concat(changePriceAction);
1870
- removePriceActions = removePriceActions.concat(removePriceAction);
1871
- }
1872
- });
1873
-
1874
- // price actions need to be in this below order
1875
- return changePriceActions.concat(removePriceActions).concat(addPriceActions);
1876
- }
1877
- function actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) {
1878
- var actions = [];
1879
- var variants = diff.variants;
1880
- if (variants) forEach__default["default"](variants, function (variant, key) {
1881
- var _extractMatchingPairs8 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1882
- oldVariant = _extractMatchingPairs8.oldObj,
1883
- newVariant = _extractMatchingPairs8.newObj;
1884
- if (variant && variant.prices && (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key))) {
1885
- var priceHashMap = findMatchingPairs(variant.prices, oldVariant.prices, newVariant.prices);
1886
- forEach__default["default"](variant.prices, function (price, index) {
1887
- var _extractMatchingPairs9 = extractMatchingPairs(priceHashMap, index, oldVariant.prices, newVariant.prices),
1888
- oldPrice = _extractMatchingPairs9.oldObj,
1889
- newPrice = _extractMatchingPairs9.newObj;
1890
- if (price.custom && (REGEX_UNDERSCORE_NUMBER$2.test(index) || REGEX_NUMBER$2.test(index))) {
1891
- var generatedActions = actionsMapCustom(price, newPrice, oldPrice, {
1892
- actions: {
1893
- setCustomType: 'setProductPriceCustomType',
1894
- setCustomField: 'setProductPriceCustomField'
1895
- },
1896
- priceId: oldPrice.id
1897
- });
1898
- actions = actions.concat(generatedActions);
1899
- }
1900
- });
1901
- }
1902
- });
1903
- return actions;
1904
- }
1905
- function actionsMapMasterVariant(oldObj, newObj) {
1906
- var createChangeMasterVariantAction = function createChangeMasterVariantAction(variantId) {
1907
- return {
1908
- action: 'changeMasterVariant',
1909
- variantId: variantId
1910
- };
1911
- };
1912
- var extractMasterVariantId = function extractMasterVariantId(fromObj) {
1913
- var variants = Array.isArray(fromObj.variants) ? fromObj.variants : [];
1914
- return variants[0] ? variants[0].id : undefined;
1915
- };
1916
- var newMasterVariantId = extractMasterVariantId(newObj);
1917
- var oldMasterVariantId = extractMasterVariantId(oldObj);
1918
-
1919
- // Old and new master master variant differ and a new master variant id exists
1920
- if (newMasterVariantId && oldMasterVariantId !== newMasterVariantId) return [createChangeMasterVariantAction(newMasterVariantId)];
1921
- return [];
1922
- }
1923
-
1924
- function createProductMapActions(mapActionGroup, syncActionConfig) {
1925
- return function doMapActions(diff, newObj, oldObj) {
1926
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1927
- var allActions = [];
1928
- var sameForAllAttributeNames = options.sameForAllAttributeNames,
1929
- enableDiscounted = options.enableDiscounted;
1930
- var publish = newObj.publish,
1931
- staged = newObj.staged;
1932
- var variantHashMap = findMatchingPairs(diff.variants, oldObj.variants, newObj.variants);
1933
- allActions.push(mapActionGroup('productAttributes', function () {
1934
- return actionsMapProductAttributes(diff, oldObj, newObj);
1935
- }));
1936
- allActions.push(mapActionGroup('attributes', function () {
1937
- return actionsMapAttributes$1(diff, oldObj, newObj, sameForAllAttributeNames || [], variantHashMap);
1938
- }));
1939
- allActions.push(mapActionGroup('variants', function () {
1940
- return actionsMapAddVariants(diff, oldObj, newObj);
1941
- }));
1942
- allActions.push(actionsMapMasterVariant(oldObj, newObj));
1943
- allActions.push(mapActionGroup('variants', function () {
1944
- return actionsMapRemoveVariants(diff, oldObj, newObj);
1945
- }));
1946
- allActions.push(mapActionGroup('base', function () {
1947
- return actionsMapBase$l(diff, oldObj, newObj, syncActionConfig);
1948
- }));
1949
- allActions.push(mapActionGroup('meta', function () {
1950
- return actionsMapMeta(diff, oldObj, newObj);
1951
- }));
1952
- allActions.push(mapActionGroup('references', function () {
1953
- return actionsMapReferences(diff, oldObj, newObj);
1954
- }));
1955
- allActions.push(mapActionGroup('images', function () {
1956
- return actionsMapImages(diff, oldObj, newObj, variantHashMap);
1957
- }));
1958
- allActions.push(mapActionGroup('pricesCustom', function () {
1959
- return actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap);
1960
- }));
1961
- allActions.push(mapActionGroup('prices', function () {
1962
- return actionsMapPrices(diff, oldObj, newObj, variantHashMap, enableDiscounted);
1963
- }));
1964
- allActions.push(mapActionGroup('categories', function () {
1965
- return actionsMapCategories(diff);
1966
- }));
1967
- allActions.push(mapActionGroup('categoryOrderHints', function () {
1968
- return actionsMapCategoryOrderHints(diff);
1969
- }));
1970
- allActions.push(mapActionGroup('assets', function () {
1971
- return actionsMapAssets(diff, oldObj, newObj, variantHashMap);
1972
- }));
1973
- if (publish === true || staged === false) return flatten__default["default"](allActions).map(function (action) {
1974
- return _objectSpread2(_objectSpread2({}, action), {}, {
1975
- staged: false
1976
- });
1977
- });
1978
- return flatten__default["default"](allActions);
1979
- };
1980
- }
1981
- function moveMasterVariantsIntoVariants(before, now) {
1982
- var _copyEmptyArrayProps = copyEmptyArrayProps(before, now),
1983
- _copyEmptyArrayProps2 = _slicedToArray(_copyEmptyArrayProps, 2),
1984
- beforeCopy = _copyEmptyArrayProps2[0],
1985
- nowCopy = _copyEmptyArrayProps2[1];
1986
- var move = function move(obj) {
1987
- return _objectSpread2(_objectSpread2({}, obj), {}, {
1988
- masterVariant: undefined,
1989
- variants: [obj.masterVariant].concat(_toConsumableArray(obj.variants || []))
1990
- });
1991
- };
1992
- var hasMasterVariant = function hasMasterVariant(obj) {
1993
- return obj && obj.masterVariant;
1994
- };
1995
- return [hasMasterVariant(beforeCopy) ? move(beforeCopy) : beforeCopy, hasMasterVariant(nowCopy) ? move(nowCopy) : nowCopy];
1996
- }
1997
- var products = (function (actionGroupList, syncActionConfig) {
1998
- var mapActionGroup = createMapActionGroup(actionGroupList);
1999
- var doMapActions = createProductMapActions(mapActionGroup, syncActionConfig);
2000
- var buildActions = createBuildActions(diff, doMapActions, moveMasterVariantsIntoVariants);
2001
- return {
2002
- buildActions: buildActions
2003
- };
2004
- });
2005
-
2006
- var REGEX_NUMBER$1 = new RegExp(/^\d+$/);
2007
- var REGEX_UNDERSCORE_NUMBER$1 = new RegExp(/^_\d+$/);
2008
- var isAddAction = function isAddAction(key, resource) {
2009
- return REGEX_NUMBER$1.test(key) && Array.isArray(resource) && resource.length;
2010
- };
2011
- var isRemoveAction = function isRemoveAction(key, resource) {
2012
- return REGEX_UNDERSCORE_NUMBER$1.test(key) && Number(resource[2]) === 0;
2013
- };
2014
- var baseActionsList$k = [{
2015
- action: 'changeOrderState',
2016
- key: 'orderState'
2017
- }, {
2018
- action: 'changePaymentState',
2019
- key: 'paymentState'
2020
- }, {
2021
- action: 'changeShipmentState',
2022
- key: 'shipmentState'
2023
- }];
2024
-
2025
- /**
2026
- * SYNC FUNCTIONS
2027
- */
2028
-
2029
- function actionsMapBase$k(diff, oldObj, newObj) {
2030
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2031
- return buildBaseAttributesActions({
2032
- actions: baseActionsList$k,
2033
- diff: diff,
2034
- oldObj: oldObj,
2035
- newObj: newObj,
2036
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2037
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2038
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2039
- });
2040
- }
2041
- function actionsMapDeliveries(diff, oldObj, newObj) {
2042
- var deliveriesDiff = diff.shippingInfo;
2043
- if (!deliveriesDiff) return [];
2044
- var handler = createBuildArrayActions('deliveries', _defineProperty({}, ADD_ACTIONS, function (newObject) {
2045
- return {
2046
- action: 'addDelivery',
2047
- items: newObject.items,
2048
- parcels: newObject.parcels
2049
- };
2050
- }));
2051
- return handler(deliveriesDiff, oldObj.shippingInfo, newObj.shippingInfo);
2052
- }
2053
- function _buildDeliveryParcelsAction(diffedParcels) {
2054
- var oldDelivery = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2055
- var newDelivery = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2056
- var addParcelActions = [];
2057
- var removeParcelActions = [];
2058
-
2059
- // generate a hashMap to be able to reference the right image from both ends
2060
- var matchingParcelPairs = findMatchingPairs(diffedParcels, oldDelivery.parcels, newDelivery.parcels);
2061
- forEach__default["default"](diffedParcels, function (parcel, key) {
2062
- var _extractMatchingPairs = extractMatchingPairs(matchingParcelPairs, key, oldDelivery.parcels, newDelivery.parcels),
2063
- oldObj = _extractMatchingPairs.oldObj;
2064
- if (isAddAction(key, parcel)) {
2065
- addParcelActions.push(_objectSpread2({
2066
- action: 'addParcelToDelivery',
2067
- deliveryId: oldDelivery.id
2068
- }, getDeltaValue(parcel)));
2069
- return;
2070
- }
2071
- if (isRemoveAction(key, parcel)) {
2072
- removeParcelActions.push({
2073
- action: 'removeParcelFromDelivery',
2074
- parcelId: oldObj.id
2075
- });
2076
- }
2077
- });
2078
- return [addParcelActions, removeParcelActions];
2079
- }
2080
- function _buildDeliveryItemsAction(diffedItems) {
2081
- var newDelivery = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2082
- var setDeliveryItemsAction = [];
2083
- // If there is a diff it means that there were changes (update, adds or removes)
2084
- // over the items, which means that `setDeliveryItems` change has happened over
2085
- // the delivery
2086
- if (diffedItems && Object.keys(diffedItems).length > 0) {
2087
- setDeliveryItemsAction.push({
2088
- action: 'setDeliveryItems',
2089
- deliveryId: newDelivery.id,
2090
- deliveryKey: newDelivery.key,
2091
- items: newDelivery.items
2092
- });
2093
- }
2094
- return [setDeliveryItemsAction];
2095
- }
2096
- function actionsMapParcels(diff, oldObj, newObj, deliveryHashMap) {
2097
- var shippingInfo = diff.shippingInfo;
2098
- if (!shippingInfo) return [];
2099
- var deliveries = shippingInfo.deliveries;
2100
- if (!deliveries) return [];
2101
- var addParcelActions = [];
2102
- var removeParcelActions = [];
2103
- if (deliveries) forEach__default["default"](deliveries, function (delivery, key) {
2104
- var _extractMatchingPairs2 = extractMatchingPairs(deliveryHashMap, key, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries),
2105
- oldDelivery = _extractMatchingPairs2.oldObj,
2106
- newDelivery = _extractMatchingPairs2.newObj;
2107
- if (REGEX_UNDERSCORE_NUMBER$1.test(key) || REGEX_NUMBER$1.test(key)) {
2108
- var _buildDeliveryParcels = _buildDeliveryParcelsAction(delivery.parcels, oldDelivery, newDelivery),
2109
- _buildDeliveryParcels2 = _slicedToArray(_buildDeliveryParcels, 2),
2110
- addParcelAction = _buildDeliveryParcels2[0],
2111
- removeParcelAction = _buildDeliveryParcels2[1];
2112
- addParcelActions = addParcelActions.concat(addParcelAction);
2113
- removeParcelActions = removeParcelActions.concat(removeParcelAction);
2114
- }
2115
- });
2116
- return removeParcelActions.concat(addParcelActions);
2117
- }
2118
- function actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap) {
2119
- var shippingInfo = diff.shippingInfo;
2120
- if (!shippingInfo) return [];
2121
- var deliveries = shippingInfo.deliveries;
2122
- if (!deliveries) return [];
2123
- var setDeliveryItemsActions = [];
2124
- forEach__default["default"](deliveries, function (delivery, key) {
2125
- var _extractMatchingPairs3 = extractMatchingPairs(deliveryHashMap, key, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries),
2126
- newDelivery = _extractMatchingPairs3.newObj;
2127
- if (REGEX_UNDERSCORE_NUMBER$1.test(key) || REGEX_NUMBER$1.test(key)) {
2128
- var _buildDeliveryItemsAc = _buildDeliveryItemsAction(delivery.items, newDelivery),
2129
- _buildDeliveryItemsAc2 = _slicedToArray(_buildDeliveryItemsAc, 1),
2130
- setDeliveryItemsAction = _buildDeliveryItemsAc2[0];
2131
- setDeliveryItemsActions = setDeliveryItemsActions.concat(setDeliveryItemsAction);
2132
- }
2133
- });
2134
- return setDeliveryItemsActions;
2135
- }
2136
- function actionsMapReturnsInfo(diff, oldObj, newObj) {
2137
- var returnInfoDiff = diff.returnInfo;
2138
- if (!returnInfoDiff) return [];
2139
- var handler = createBuildArrayActions('returnInfo', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newReturnInfo) {
2140
- if (newReturnInfo.items) {
2141
- return [_objectSpread2({
2142
- action: 'addReturnInfo'
2143
- }, newReturnInfo)];
2144
- }
2145
- return [];
2146
- }), CHANGE_ACTIONS, function (oldSReturnInfo, newReturnInfo, key) {
2147
- var _returnInfoDiff$key$i = returnInfoDiff[key].items,
2148
- items = _returnInfoDiff$key$i === void 0 ? {} : _returnInfoDiff$key$i;
2149
- if (Object.keys(items).length === 0) {
2150
- return [];
2151
- }
2152
- return Object.keys(items).reduce(function (actions, index) {
2153
- var item = newReturnInfo.items[index];
2154
- if (items[index].shipmentState) {
2155
- actions.push({
2156
- action: 'setReturnShipmentState',
2157
- returnItemId: item.id,
2158
- shipmentState: item.shipmentState
2159
- });
2160
- }
2161
- if (items[index].paymentState) {
2162
- actions.push({
2163
- action: 'setReturnPaymentState',
2164
- returnItemId: item.id,
2165
- paymentState: item.paymentState
2166
- });
2167
- }
2168
- return actions;
2169
- }, []);
2170
- }));
2171
- return handler(diff, oldObj, newObj);
2172
- }
2173
-
2174
- function createOrderMapActions(mapActionGroup, syncActionConfig) {
2175
- return function doMapActions(diff, newObj, oldObj) {
2176
- var allActions = [];
2177
- var deliveryHashMap;
2178
- if (diff.shippingInfo && diff.shippingInfo.deliveries) {
2179
- deliveryHashMap = findMatchingPairs(diff.shippingInfo.deliveries, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries);
2180
- }
2181
- allActions.push(mapActionGroup('base', function () {
2182
- return actionsMapBase$k(diff, oldObj, newObj, syncActionConfig);
2183
- }));
2184
- allActions.push(mapActionGroup('deliveries', function () {
2185
- return actionsMapDeliveries(diff, oldObj, newObj);
2186
- }));
2187
- allActions.push(mapActionGroup('parcels', function () {
2188
- return actionsMapParcels(diff, oldObj, newObj, deliveryHashMap);
2189
- }));
2190
- allActions.push(mapActionGroup('items', function () {
2191
- return actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap);
2192
- }));
2193
- allActions.push(flatten__default["default"](mapActionGroup('returnInfo', function () {
2194
- return actionsMapReturnsInfo(diff, oldObj, newObj);
2195
- })));
2196
- allActions.push(mapActionGroup('custom', function () {
2197
- return actionsMapCustom(diff, newObj, oldObj);
2198
- }));
2199
- return flatten__default["default"](allActions);
2200
- };
2201
- }
2202
- var orders = (function (actionGroupList, syncActionConfig) {
2203
- // actionGroupList contains information about which action groups
2204
- // are allowed or ignored
2205
-
2206
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2207
- // - action group name
2208
- // - callback function that should return a list of actions that correspond
2209
- // to the for the action group
2210
-
2211
- // this resulting function mapActionGroup will call the callback function
2212
- // for allowed action groups and return the return value of the callback
2213
- // It will return an empty array for ignored action groups
2214
- var mapActionGroup = createMapActionGroup(actionGroupList);
2215
- var doMapActions = createOrderMapActions(mapActionGroup, syncActionConfig);
2216
- var buildActions = createBuildActions(diff, doMapActions);
2217
- return {
2218
- buildActions: buildActions
2219
- };
2220
- });
2221
-
2222
- var baseActionsList$j = [{
2223
- action: 'setKey',
2224
- key: 'key'
2225
- }, {
2226
- action: 'setRecurringOrderState',
2227
- key: 'recurringOrderState'
2228
- }, {
2229
- action: 'transitionState',
2230
- key: 'state'
2231
- }];
2232
- function actionsMapBase$j(diff, oldObj, newObj) {
2233
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2234
- return buildBaseAttributesActions({
2235
- actions: baseActionsList$j,
2236
- diff: diff,
2237
- oldObj: oldObj,
2238
- newObj: newObj,
2239
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2240
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2241
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2242
- });
2243
- }
2244
-
2245
- function createRecurringOrdersMapActions(mapActionGroup, syncActionConfig) {
2246
- return function doMapActions(diff, newObj, oldObj) {
2247
- var allActions = [];
2248
- allActions.push(mapActionGroup('base', function () {
2249
- return actionsMapBase$j(diff, oldObj, newObj, syncActionConfig);
2250
- }));
2251
- allActions.push(mapActionGroup('custom', function () {
2252
- return actionsMapCustom(diff, newObj, oldObj);
2253
- }));
2254
- return flatten__default["default"](allActions);
2255
- };
2256
- }
2257
- var recurringOrders = (function (actionGroupList, syncActionConfig) {
2258
- var mapActionGroup = createMapActionGroup(actionGroupList);
2259
- var doMapActions = createRecurringOrdersMapActions(mapActionGroup, syncActionConfig);
2260
- var buildActions = createBuildActions(diff, doMapActions);
2261
- return {
2262
- buildActions: buildActions
2263
- };
2264
- });
2265
-
2266
- var baseActionsList$i = [{
2267
- action: 'changeIsActive',
2268
- key: 'isActive'
2269
- }, {
2270
- action: 'changeName',
2271
- key: 'name'
2272
- }, {
2273
- action: 'changePredicate',
2274
- key: 'predicate'
2275
- }, {
2276
- action: 'changeSortOrder',
2277
- key: 'sortOrder'
2278
- }, {
2279
- action: 'changeValue',
2280
- key: 'value'
2281
- }, {
2282
- action: 'setDescription',
2283
- key: 'description'
2284
- }, {
2285
- action: 'setValidFrom',
2286
- key: 'validFrom'
2287
- }, {
2288
- action: 'setValidUntil',
2289
- key: 'validUntil'
2290
- }, {
2291
- action: 'setKey',
2292
- key: 'key'
2293
- }];
2294
- function actionsMapBase$i(diff, oldObj, newObj) {
2295
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2296
- return buildBaseAttributesActions({
2297
- actions: baseActionsList$i,
2298
- diff: diff,
2299
- oldObj: oldObj,
2300
- newObj: newObj,
2301
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2302
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2303
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2304
- });
2305
- }
2306
-
2307
- var validityActions = ['setValidFrom', 'setValidUntil'];
2308
- var isValidityActions = function isValidityActions(actionName) {
2309
- return validityActions.includes(actionName);
2310
- };
2311
- function combineValidityActions() {
2312
- var actions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2313
- var _actions$filter = actions.filter(function (item) {
2314
- return isValidityActions(item.action);
2315
- }),
2316
- _actions$filter2 = _slicedToArray(_actions$filter, 2),
2317
- setValidFromAction = _actions$filter2[0],
2318
- setValidUntilAction = _actions$filter2[1];
2319
- if (setValidFromAction && setValidUntilAction) {
2320
- return [].concat(_toConsumableArray(actions.filter(function (item) {
2321
- return !isValidityActions(item.action);
2322
- })), [{
2323
- action: 'setValidFromAndUntil',
2324
- validFrom: setValidFromAction.validFrom,
2325
- validUntil: setValidUntilAction.validUntil
2326
- }]);
2327
- }
2328
- return actions;
2329
- }
2330
-
2331
- function createProductDiscountsMapActions(mapActionGroup, syncActionConfig) {
2332
- return function doMapActions(diff, newObj, oldObj) {
2333
- var allActions = [];
2334
- allActions.push(mapActionGroup('base', function () {
2335
- return actionsMapBase$i(diff, oldObj, newObj, syncActionConfig);
2336
- }));
2337
- return combineValidityActions(flatten__default["default"](allActions));
2338
- };
2339
- }
2340
- var productDiscounts = (function (actionGroupList) {
2341
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2342
- var mapActionGroup = createMapActionGroup(actionGroupList);
2343
- var doMapActions = createProductDiscountsMapActions(mapActionGroup, syncActionConfig);
2344
- var buildActions = createBuildActions(diff, doMapActions);
2345
- return {
2346
- buildActions: buildActions
2347
- };
2348
- });
2349
-
2350
- var baseActionsList$h = [{
2351
- action: 'changeIsActive',
2352
- key: 'isActive'
2353
- }, {
2354
- action: 'setName',
2355
- key: 'name'
2356
- }, {
2357
- action: 'setDescription',
2358
- key: 'description'
2359
- }, {
2360
- action: 'setKey',
2361
- key: 'key'
2362
- }, {
2363
- action: 'setCartPredicate',
2364
- key: 'cartPredicate'
2365
- }, {
2366
- action: 'setMaxApplications',
2367
- key: 'maxApplications'
2368
- }, {
2369
- action: 'setMaxApplicationsPerCustomer',
2370
- key: 'maxApplicationsPerCustomer'
2371
- }, {
2372
- action: 'changeCartDiscounts',
2373
- key: 'cartDiscounts'
2374
- }, {
2375
- action: 'setValidFrom',
2376
- key: 'validFrom'
2377
- }, {
2378
- action: 'setValidUntil',
2379
- key: 'validUntil'
2380
- }, {
2381
- action: 'changeGroups',
2382
- key: 'groups'
2383
- }];
2384
- function actionsMapBase$h(diff, oldObj, newObj) {
2385
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2386
- return buildBaseAttributesActions({
2387
- actions: baseActionsList$h,
2388
- diff: diff,
2389
- oldObj: oldObj,
2390
- newObj: newObj,
2391
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2392
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2393
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2394
- });
2395
- }
2396
-
2397
- function createDiscountCodesMapActions(mapActionGroup, syncActionConfig) {
2398
- return function doMapActions(diff, newObj, oldObj) {
2399
- var allActions = [];
2400
- allActions.push(mapActionGroup('base', function () {
2401
- return actionsMapBase$h(diff, oldObj, newObj, syncActionConfig);
2402
- }));
2403
- allActions.push(mapActionGroup('custom', function () {
2404
- return actionsMapCustom(diff, newObj, oldObj);
2405
- }));
2406
- return combineValidityActions(flatten__default["default"](allActions));
2407
- };
2408
- }
2409
- var discountCodes = (function (actionGroupList) {
2410
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2411
- // actionGroupList contains information about which action groups
2412
- // are allowed or ignored
2413
-
2414
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2415
- // - action group name
2416
- // - callback function that should return a list of actions that correspond
2417
- // to the for the action group
2418
-
2419
- // this resulting function mapActionGroup will call the callback function
2420
- // for allowed action groups and return the return value of the callback
2421
- // It will return an empty array for ignored action groups
2422
- var mapActionGroup = createMapActionGroup(actionGroupList);
2423
- var doMapActions = createDiscountCodesMapActions(mapActionGroup, syncActionConfig);
2424
- var buildActions = createBuildActions(diff, doMapActions);
2425
- return {
2426
- buildActions: buildActions
2427
- };
2428
- });
2429
-
2430
- var baseActionsList$g = [{
2431
- action: 'changeName',
2432
- key: 'name'
2433
- }, {
2434
- action: 'setKey',
2435
- key: 'key'
2436
- }];
2437
- function actionsMapBase$g(diff, oldObj, newObj) {
2438
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2439
- return buildBaseAttributesActions({
2440
- actions: baseActionsList$g,
2441
- diff: diff,
2442
- oldObj: oldObj,
2443
- newObj: newObj,
2444
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2445
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2446
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2447
- });
2448
- }
2449
-
2450
- function createCustomerGroupMapActions(mapActionGroup, syncActionConfig) {
2451
- return function doMapActions(diff, newObj, oldObj) {
2452
- var allActions = [];
2453
- allActions.push(mapActionGroup('base', function () {
2454
- return actionsMapBase$g(diff, oldObj, newObj, syncActionConfig);
2455
- }));
2456
- allActions.push(mapActionGroup('custom', function () {
2457
- return actionsMapCustom(diff, newObj, oldObj);
2458
- }));
2459
- return flatten__default["default"](allActions);
2460
- };
2461
- }
2462
- var customerGroup = (function (actionGroupList) {
2463
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2464
- var mapActionGroup = createMapActionGroup(actionGroupList);
2465
- var doMapActions = createCustomerGroupMapActions(mapActionGroup, syncActionConfig);
2466
- var buildActions = createBuildActions(diff, doMapActions);
2467
- return {
2468
- buildActions: buildActions
2469
- };
2470
- });
2471
-
2472
- var baseActionsList$f = [{
2473
- action: 'changeIsActive',
2474
- key: 'isActive'
2475
- }, {
2476
- action: 'changeName',
2477
- key: 'name'
2478
- }, {
2479
- action: 'changeCartPredicate',
2480
- key: 'cartPredicate'
2481
- }, {
2482
- action: 'changeSortOrder',
2483
- key: 'sortOrder'
2484
- }, {
2485
- action: 'changeValue',
2486
- key: 'value'
2487
- }, {
2488
- action: 'changeRequiresDiscountCode',
2489
- key: 'requiresDiscountCode'
2490
- }, {
2491
- action: 'changeTarget',
2492
- key: 'target'
2493
- }, {
2494
- action: 'setDescription',
2495
- key: 'description'
2496
- }, {
2497
- action: 'setValidFrom',
2498
- key: 'validFrom'
2499
- }, {
2500
- action: 'setValidUntil',
2501
- key: 'validUntil'
2502
- }, {
2503
- action: 'changeStackingMode',
2504
- key: 'stackingMode'
2505
- }, {
2506
- action: 'setKey',
2507
- key: 'key'
2508
- }];
2509
- function actionsMapBase$f(diff, oldObj, newObj) {
2510
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2511
- return buildBaseAttributesActions({
2512
- actions: baseActionsList$f,
2513
- diff: diff,
2514
- oldObj: oldObj,
2515
- newObj: newObj,
2516
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2517
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2518
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2519
- });
2520
- }
2521
-
2522
- function createCartDiscountsMapActions(mapActionGroup, syncActionConfig) {
2523
- return function doMapActions(diff, newObj, oldObj) {
2524
- var allActions = [];
2525
- allActions.push(mapActionGroup('base', function () {
2526
- return actionsMapBase$f(diff, oldObj, newObj, syncActionConfig);
2527
- }));
2528
- allActions.push(mapActionGroup('custom', function () {
2529
- return actionsMapCustom(diff, newObj, oldObj);
2530
- }));
2531
- return combineValidityActions(flatten__default["default"](allActions));
2532
- };
2533
- }
2534
- var cartDiscounts = (function (actionGroupList) {
2535
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2536
- var mapActionGroup = createMapActionGroup(actionGroupList);
2537
- var doMapActions = createCartDiscountsMapActions(mapActionGroup, syncActionConfig);
2538
- var buildActions = createBuildActions(diff, doMapActions);
2539
- return {
2540
- buildActions: buildActions
2541
- };
2542
- });
2543
-
2544
- var _excluded = ["__typename"];
2545
- function removeTypename(obj) {
2546
- obj.__typename;
2547
- var objWithoutTypename = _objectWithoutProperties(obj, _excluded);
2548
- return objWithoutTypename;
2549
- }
2550
-
2551
- var baseActionsList$e = [{
2552
- action: 'changeName',
2553
- key: 'name'
2554
- }, {
2555
- action: 'setKey',
2556
- key: 'key'
2557
- }, {
2558
- action: 'setDescription',
2559
- key: 'description'
2560
- }];
2561
- function actionsMapBase$e(diff, oldObj, newObj) {
2562
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2563
- return buildBaseAttributesActions({
2564
- actions: baseActionsList$e,
2565
- diff: diff,
2566
- oldObj: oldObj,
2567
- newObj: newObj,
2568
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2569
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2570
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2571
- });
2572
- }
2573
- function actionsMapRates(diff, oldObj, newObj) {
2574
- var handler = createBuildArrayActions('rates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newObject) {
2575
- return {
2576
- action: 'addTaxRate',
2577
- taxRate: newObject
2578
- };
2579
- }), REMOVE_ACTIONS, function (objectToRemove) {
2580
- return {
2581
- action: 'removeTaxRate',
2582
- taxRateId: objectToRemove.id
2583
- };
2584
- }), CHANGE_ACTIONS, function (oldObject, updatedObject) {
2585
- var _oldObject$subRates;
2586
- // filter out taxRates that were not changed
2587
- // so the API doesn't return it with a different id
2588
- // we need to remove __typename from the object to compare them
2589
- var taxCategoryWithoutTypeName = removeTypename(oldObject);
2590
- var oldObjectSubRatesWithoutTypename = (_oldObject$subRates = oldObject.subRates) === null || _oldObject$subRates === void 0 ? void 0 : _oldObject$subRates.map(removeTypename);
2591
- var oldObjectWithoutTypename = _objectSpread2(_objectSpread2({}, taxCategoryWithoutTypeName), {}, {
2592
- subRates: oldObjectSubRatesWithoutTypename
2593
- });
2594
- if (fastEquals.deepEqual(oldObjectWithoutTypename, updatedObject)) return null;
2595
- return {
2596
- action: 'replaceTaxRate',
2597
- taxRateId: oldObject.id === updatedObject.id ? oldObject.id : updatedObject.id,
2598
- taxRate: updatedObject
2599
- };
2600
- }));
2601
- return handler(diff, oldObj, newObj);
2602
- }
2603
-
2604
- function createTaxCategoriesMapActions(mapActionGroup, syncActionConfig) {
2605
- return function doMapActions(diff, newObj, oldObj) {
2606
- var allActions = [];
2607
- allActions.push(mapActionGroup('base', function () {
2608
- return actionsMapBase$e(diff, oldObj, newObj, syncActionConfig);
2609
- }));
2610
- allActions.push(mapActionGroup('rates', function () {
2611
- return actionsMapRates(diff, oldObj, newObj);
2612
- }));
2613
- return flatten__default["default"](allActions);
2614
- };
2615
- }
2616
- var taxCategories = (function (actionGroupList, syncActionConfig) {
2617
- // config contains information about which action groups
2618
- // are allowed or ignored
2619
-
2620
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2621
- // - action group name
2622
- // - callback function that should return a list of actions that correspond
2623
- // to the for the action group
2624
-
2625
- // this resulting function mapActionGroup will call the callback function
2626
- // for allowed action groups and return the return value of the callback
2627
- // It will return an empty array for ignored action groups
2628
- var mapActionGroup = createMapActionGroup(actionGroupList);
2629
- var doMapActions = createTaxCategoriesMapActions(mapActionGroup, syncActionConfig);
2630
- var buildActions = createBuildActions(diff, doMapActions);
2631
- return {
2632
- buildActions: buildActions
2633
- };
2634
- });
2635
-
2636
- var baseActionsList$d = [{
2637
- action: 'changeName',
2638
- key: 'name'
2639
- }, {
2640
- action: 'setDescription',
2641
- key: 'description'
2642
- }, {
2643
- action: 'setKey',
2644
- key: 'key'
2645
- }];
2646
- var hasLocation = function hasLocation(locations, otherLocation) {
2647
- return locations.some(function (location) {
2648
- return location.country === otherLocation.country;
2649
- });
2650
- };
2651
- function actionsMapBase$d(diff, oldObj, newObj) {
2652
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2653
- return buildBaseAttributesActions({
2654
- actions: baseActionsList$d,
2655
- diff: diff,
2656
- oldObj: oldObj,
2657
- newObj: newObj,
2658
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2659
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2660
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2661
- });
2662
- }
2663
- function actionsMapLocations(diff, oldObj, newObj) {
2664
- var handler = createBuildArrayActions('locations', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newLocation) {
2665
- return {
2666
- action: 'addLocation',
2667
- location: newLocation
2668
- };
2669
- }), REMOVE_ACTIONS, function (oldLocation) {
2670
- return (
2671
- // We only add the action if the location is not included in the new object.
2672
- !hasLocation(newObj.locations, oldLocation) ? {
2673
- action: 'removeLocation',
2674
- location: oldLocation
2675
- } : null
2676
- );
2677
- }), CHANGE_ACTIONS, function (oldLocation, newLocation) {
2678
- var result = [];
2679
-
2680
- // We only remove the location in case that the oldLocation is not
2681
- // included in the new object
2682
- if (!hasLocation(newObj.locations, oldLocation)) result.push({
2683
- action: 'removeLocation',
2684
- location: oldLocation
2685
- });
2686
-
2687
- // We only add the location in case that the newLocation was not
2688
- // included in the old object
2689
- if (!hasLocation(oldObj.locations, newLocation)) result.push({
2690
- action: 'addLocation',
2691
- location: newLocation
2692
- });
2693
- return result;
2694
- }));
2695
- return handler(diff, oldObj, newObj);
2696
- }
2697
-
2698
- function createZonesMapActions(mapActionGroup, syncActionConfig) {
2699
- return function doMapActions(diff, newObj, oldObj) {
2700
- var allActions = [];
2701
- allActions.push(mapActionGroup('base', function () {
2702
- return actionsMapBase$d(diff, oldObj, newObj, syncActionConfig);
2703
- }));
2704
- allActions.push(flatten__default["default"](mapActionGroup('locations', function () {
2705
- return actionsMapLocations(diff, oldObj, newObj);
2706
- })));
2707
- return flatten__default["default"](allActions);
2708
- };
2709
- }
2710
- var zones = (function (actionGroupList, syncActionConfig) {
2711
- // config contains information about which action groups
2712
- // are allowed or ignored
2713
-
2714
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2715
- // - action group name
2716
- // - callback function that should return a list of actions that correspond
2717
- // to the for the action group
2718
-
2719
- // this resulting function mapActionGroup will call the callback function
2720
- // for allowed action groups and return the return value of the callback
2721
- // It will return an empty array for ignored action groups
2722
- var mapActionGroup = createMapActionGroup(actionGroupList);
2723
- var doMapActions = createZonesMapActions(mapActionGroup, syncActionConfig);
2724
- var buildActions = createBuildActions(diff, doMapActions);
2725
- return {
2726
- buildActions: buildActions
2727
- };
2728
- });
2729
-
2730
- var baseActionsList$c = [{
2731
- action: 'setKey',
2732
- key: 'key'
2733
- }, {
2734
- action: 'changeName',
2735
- key: 'name'
2736
- }, {
2737
- action: 'setLocalizedName',
2738
- key: 'localizedName'
2739
- }, {
2740
- action: 'setDescription',
2741
- key: 'description'
2742
- }, {
2743
- action: 'setLocalizedDescription',
2744
- key: 'localizedDescription'
2745
- }, {
2746
- action: 'changeIsDefault',
2747
- key: 'isDefault'
2748
- }, {
2749
- action: 'setPredicate',
2750
- key: 'predicate'
2751
- }, {
2752
- action: 'changeTaxCategory',
2753
- key: 'taxCategory'
2754
- }, {
2755
- action: 'changeActive',
2756
- key: 'active'
2757
- }];
2758
- function actionsMapBase$c(diff, oldObj, newObj) {
2759
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2760
- return buildBaseAttributesActions({
2761
- actions: baseActionsList$c,
2762
- diff: diff,
2763
- oldObj: oldObj,
2764
- newObj: newObj,
2765
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2766
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2767
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2768
- });
2769
- }
2770
- var addShippingRates = function addShippingRates(newZoneRate) {
2771
- return newZoneRate.shippingRates ? newZoneRate.shippingRates.map(function (shippingRate) {
2772
- return {
2773
- action: 'addShippingRate',
2774
- zone: newZoneRate.zone,
2775
- shippingRate: shippingRate
2776
- };
2777
- }) : [];
2778
- };
2779
- function actionsMapZoneRatesShippingRates(diff, oldObj, newObj) {
2780
- var handler = createBuildArrayActions('shippingRates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newShippingRate) {
2781
- return {
2782
- action: 'addShippingRate',
2783
- zone: newObj.zone,
2784
- shippingRate: newShippingRate
2785
- };
2786
- }), REMOVE_ACTIONS, function (oldShippingRate) {
2787
- return {
2788
- action: 'removeShippingRate',
2789
- zone: oldObj.zone,
2790
- shippingRate: oldShippingRate
2791
- };
2792
- }), CHANGE_ACTIONS, function (oldShippingRate, newShippingRate) {
2793
- return [{
2794
- action: 'removeShippingRate',
2795
- zone: oldObj.zone,
2796
- shippingRate: oldShippingRate
2797
- }, {
2798
- action: 'addShippingRate',
2799
- zone: newObj.zone,
2800
- shippingRate: newShippingRate
2801
- }];
2802
- }));
2803
- return handler(diff, oldObj, newObj);
2804
- }
2805
- function actionsMapZoneRates(diff, oldObj, newObj) {
2806
- var handler = createBuildArrayActions('zoneRates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newZoneRate) {
2807
- return [{
2808
- action: 'addZone',
2809
- zone: newZoneRate.zone
2810
- }].concat(_toConsumableArray(addShippingRates(newZoneRate)));
2811
- }), REMOVE_ACTIONS, function (oldZoneRate) {
2812
- return {
2813
- action: 'removeZone',
2814
- zone: oldZoneRate.zone
2815
- };
2816
- }), CHANGE_ACTIONS, function (oldZoneRate, newZoneRate) {
2817
- var hasZoneActions = false;
2818
- var shippingRateActions = Object.keys(diff.zoneRates).reduce(function (actions, key) {
2819
- if (diff.zoneRates[key].zone) hasZoneActions = true;
2820
- if (diff.zoneRates[key].shippingRates) return [].concat(_toConsumableArray(actions), _toConsumableArray(actionsMapZoneRatesShippingRates(diff.zoneRates[key], oldZoneRate, newZoneRate)));
2821
- return actions;
2822
- }, []);
2823
- return flatten__default["default"](hasZoneActions ? [].concat(_toConsumableArray(shippingRateActions), [{
2824
- action: 'removeZone',
2825
- zone: oldZoneRate.zone
2826
- }, {
2827
- action: 'addZone',
2828
- zone: newZoneRate.zone
2829
- }]) : shippingRateActions);
2830
- }));
2831
- return handler(diff, oldObj, newObj);
2832
- }
2833
-
2834
- function createShippingMethodsMapActions(mapActionGroup, syncActionConfig) {
2835
- return function doMapActions(diff, newObj, oldObj) {
2836
- var allActions = [];
2837
- allActions.push(mapActionGroup('base', function () {
2838
- return actionsMapBase$c(diff, oldObj, newObj, syncActionConfig);
2839
- }));
2840
- allActions.push(flatten__default["default"](mapActionGroup('zoneRates', function () {
2841
- return actionsMapZoneRates(diff, oldObj, newObj);
2842
- })));
2843
- allActions.push(mapActionGroup('custom', function () {
2844
- return actionsMapCustom(diff, newObj, oldObj);
2845
- }));
2846
- return flatten__default["default"](allActions);
2847
- };
2848
- }
2849
- var shippingMethods = (function (actionGroupList, syncActionConfig) {
2850
- // actionGroupList contains information about which action groups
2851
- // are allowed or ignored
2852
-
2853
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2854
- // - action group name
2855
- // - callback function that should return a list of actions that correspond
2856
- // to the for the action group
2857
-
2858
- // this resulting function mapActionGroup will call the callback function
2859
- // for allowed action groups and return the return value of the callback
2860
- // It will return an empty array for ignored action groups
2861
- var mapActionGroup = createMapActionGroup(actionGroupList);
2862
- var doMapActions = createShippingMethodsMapActions(mapActionGroup, syncActionConfig);
2863
- var buildActions = createBuildActions(diff, doMapActions);
2864
- return {
2865
- buildActions: buildActions
2866
- };
2867
- });
2868
-
2869
- var baseActionsList$b = [{
2870
- action: 'changeName',
2871
- key: 'name'
2872
- }, {
2873
- action: 'setKey',
2874
- key: 'key'
2875
- }, {
2876
- action: 'changeDescription',
2877
- key: 'description'
2878
- }];
2879
- function actionsMapBase$b(diff, previous, next) {
2880
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2881
- // when `diff` is undefined, then the underlying `buildActions` has returned any diff
2882
- // which given in product-types would mean that `buildActions` has run with `nestedValuesChanges` applied
2883
- // To allow continuation of update-action generation, we let this pass..
2884
- if (!diff) return [];
2885
- return buildBaseAttributesActions({
2886
- diff: diff,
2887
- actions: baseActionsList$b,
2888
- oldObj: previous,
2889
- newObj: next,
2890
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2891
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
2892
- });
2893
- }
2894
-
2895
- // this is nearly similar to `buildBaseAttributesActions`, however with a significant difference.
2896
- // `buildBasAttributesActions` generates update-actions with help of `diff`,
2897
- // which is an object consisting of flags which indicates different operations.
2898
- // `generateBaseFieldsUpdateActions` only generate based on `previous` and `next`.
2899
- var generateBaseFieldsUpdateActions = function generateBaseFieldsUpdateActions(previous, next, actionDefinition) {
2900
- var isEmpty = createIsEmptyValue([undefined, null, '']);
2901
- return Object.entries(actionDefinition).reduce(function (nextUpdateActions, _ref) {
2902
- var _ref2 = _slicedToArray(_ref, 2),
2903
- field = _ref2[0],
2904
- actionFieldDefinition = _ref2[1];
2905
- if (isEmpty(previous[field]) && isEmpty(next[field])) return nextUpdateActions;
2906
- if (!isEmpty(previous[field]) && isEmpty(next[field])) return [].concat(_toConsumableArray(nextUpdateActions), [actionFieldDefinition]);
2907
- if (!fastEquals.deepEqual(previous[field], next[field])) {
2908
- switch (field) {
2909
- // BEWARE that this is generates update-action only for key of enum attribute value,
2910
- // not key of product type. If we need to re-factor `product-types` sync actions to use
2911
- // `generateBaseFieldsUpdateActions`, we need to extract the following logic so we could
2912
- // cover both entity types.
2913
- case 'key':
2914
- return [].concat(_toConsumableArray(nextUpdateActions), [
2915
- // Another option is to have explicit name of `field` e.g `enumKey`, which we could use to
2916
- // generate appropriate update-action for respective entity type.
2917
- // An outline of this on the top of my head:
2918
- // ```js
2919
- // case 'enumKey':
2920
- // return [
2921
- // ...nextUpdateActions,
2922
- // {
2923
- // action: actionFieldDefinition.action,
2924
- // attributeName: actionFieldDefinition.attributeName,
2925
- // key: previous.key,
2926
- // newKey: next.key,
2927
- // },
2928
- // ]
2929
- // case 'productTypeKey':
2930
- // return [
2931
- // ...nextUpdateActions,
2932
- // {
2933
- // action: actionFieldDefinition.action,
2934
- // key: next.key
2935
- // },
2936
- // ]
2937
- // ```
2938
- {
2939
- action: actionFieldDefinition.action,
2940
- attributeName: actionFieldDefinition.attributeName,
2941
- key: previous[field],
2942
- newKey: next[field]
2943
- }]);
2944
- // attribute
2945
- case 'attributeConstraint':
2946
- case 'inputHint':
2947
- return [].concat(_toConsumableArray(nextUpdateActions), [{
2948
- action: actionFieldDefinition.action,
2949
- attributeName: actionFieldDefinition.attributeName,
2950
- newValue: next[field]
2951
- }]);
2952
- default:
2953
- return [].concat(_toConsumableArray(nextUpdateActions), [_defineProperty({
2954
- action: actionFieldDefinition.action,
2955
- attributeName: actionFieldDefinition.attributeName
2956
- }, field, next[field])]);
2957
- }
2958
- }
2959
- return nextUpdateActions;
2960
- }, []);
2961
- };
2962
- var generateUpdateActionsForAttributeDefinitions = function generateUpdateActionsForAttributeDefinitions() {
2963
- var attributeDefinitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2964
- var removedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2965
- return attributeDefinition.previous && !attributeDefinition.next;
2966
- });
2967
- var updatedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2968
- return attributeDefinition.previous && attributeDefinition.next;
2969
- });
2970
- var addedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2971
- return !attributeDefinition.previous && attributeDefinition.next;
2972
- });
2973
- return [].concat(_toConsumableArray(removedAttributeDefinitions.map(function (attributeDef) {
2974
- return {
2975
- action: 'removeAttributeDefinition',
2976
- name: attributeDef.previous.name
2977
- };
2978
- })), _toConsumableArray(flatten__default["default"](updatedAttributeDefinitions.map(function (updatedAttributeDefinition) {
2979
- return generateBaseFieldsUpdateActions(updatedAttributeDefinition.previous, updatedAttributeDefinition.next, {
2980
- label: {
2981
- action: 'changeLabel',
2982
- attributeName: updatedAttributeDefinition.previous.name
2983
- },
2984
- inputTip: {
2985
- action: 'setInputTip',
2986
- attributeName: updatedAttributeDefinition.previous.name
2987
- },
2988
- inputHint: {
2989
- action: 'changeInputHint',
2990
- attributeName: updatedAttributeDefinition.previous.name
2991
- },
2992
- isSearchable: {
2993
- action: 'changeIsSearchable',
2994
- attributeName: updatedAttributeDefinition.previous.name
2995
- },
2996
- attributeConstraint: {
2997
- action: 'changeAttributeConstraint',
2998
- attributeName: updatedAttributeDefinition.previous.name
2999
- }
3000
- });
3001
- }))), _toConsumableArray(addedAttributeDefinitions.map(function (attributeDef) {
3002
- return {
3003
- action: 'addAttributeDefinition',
3004
- attribute: attributeDef.next
3005
- };
3006
- })));
3007
- };
3008
- var generateUpdateActionsForAttributeEnumValues = function generateUpdateActionsForAttributeEnumValues() {
3009
- var attributeEnumValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
3010
- var removedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
3011
- return attributeEnumValue.previous && !attributeEnumValue.next;
3012
- });
3013
- var updatedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
3014
- return attributeEnumValue.next && attributeEnumValue.previous;
3015
- });
3016
- var addedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
3017
- return !attributeEnumValue.previous && attributeEnumValue.next;
3018
- });
3019
- return [].concat(_toConsumableArray(Object.values(removedAttributeEnumValues.reduce(function (nextEnumUpdateActions, removedAttributeEnumValue) {
3020
- var removedAttributeEnumValueOfSameAttributeName = nextEnumUpdateActions[removedAttributeEnumValue.hint.attributeName] || {
3021
- keys: [],
3022
- attributeName: removedAttributeEnumValue.hint.attributeName,
3023
- action: 'removeEnumValues'
3024
- };
3025
- return _objectSpread2(_objectSpread2({}, nextEnumUpdateActions), {}, _defineProperty({}, removedAttributeEnumValue.hint.attributeName, _objectSpread2(_objectSpread2({}, removedAttributeEnumValueOfSameAttributeName), {}, {
3026
- keys: [].concat(_toConsumableArray(removedAttributeEnumValueOfSameAttributeName.keys), [removedAttributeEnumValue.previous.key])
3027
- })));
3028
- }, {}))), _toConsumableArray(flatten__default["default"](updatedAttributeEnumValues.map(function (updatedAttributeEnumValue) {
3029
- var updateActions = generateBaseFieldsUpdateActions(updatedAttributeEnumValue.previous, updatedAttributeEnumValue.next, {
3030
- key: {
3031
- action: 'changeEnumKey',
3032
- attributeName: updatedAttributeEnumValue.hint.attributeName
3033
- }
3034
- });
3035
- if (!fastEquals.deepEqual(updatedAttributeEnumValue.previous.label, updatedAttributeEnumValue.next.label)) {
3036
- if (updatedAttributeEnumValue.hint.isLocalized) {
3037
- return [].concat(_toConsumableArray(updateActions), [{
3038
- action: 'changeLocalizedEnumValueLabel',
3039
- attributeName: updatedAttributeEnumValue.hint.attributeName,
3040
- newValue: updatedAttributeEnumValue.next
3041
- }]);
3042
- }
3043
- return [].concat(_toConsumableArray(updateActions), [{
3044
- action: 'changePlainEnumValueLabel',
3045
- attributeName: updatedAttributeEnumValue.hint.attributeName,
3046
- newValue: updatedAttributeEnumValue.next
3047
- }]);
3048
- }
3049
- return updateActions;
3050
- }))), _toConsumableArray(addedAttributeEnumValues.map(function (addedAttributeEnumValue) {
3051
- return {
3052
- action: addedAttributeEnumValue.hint.isLocalized ? 'addLocalizedEnumValue' : 'addPlainEnumValue',
3053
- attributeName: addedAttributeEnumValue.hint.attributeName,
3054
- value: addedAttributeEnumValue.next
3055
- };
3056
- })));
3057
- };
3058
- var generateChangeAttributeOrderAction = function generateChangeAttributeOrderAction() {
3059
- var attrsOld = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
3060
- var attrsNew = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
3061
- var updateActions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
3062
- if (!attrsOld.length || !attrsNew.length) return null;
3063
- var newAttributesOrder = attrsNew.map(function (attribute) {
3064
- return attribute.name;
3065
- });
3066
- var removedAttributeNames = updateActions.filter(function (action) {
3067
- return action.action === 'removeAttributeDefinition';
3068
- }).map(function (action) {
3069
- return action.name;
3070
- });
3071
- var addedAttributeNames = updateActions.filter(function (action) {
3072
- return action.action === 'addAttributeDefinition';
3073
- }).map(function (action) {
3074
- return action.attribute.name;
3075
- });
3076
-
3077
- // changeAttributeOrder action will be sent to CTP API as the last action so we have to
3078
- // calculate how the productType will look like after adding/removing attributes
3079
- var patchedOldAttributesOrder = attrsOld.map(function (attribute) {
3080
- return attribute.name;
3081
- }).filter(function (name) {
3082
- return !removedAttributeNames.includes(name);
3083
- }).concat(addedAttributeNames);
3084
- if (newAttributesOrder.join(',') !== patchedOldAttributesOrder.join(',')) return {
3085
- action: 'changeAttributeOrderByName',
3086
- attributeNames: newAttributesOrder
3087
- };
3088
- return null;
3089
- };
3090
- var actionsMapForHints = function actionsMapForHints() {
3091
- var nestedValuesChanges = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
3092
- var ptOld = arguments.length > 1 ? arguments[1] : undefined;
3093
- var ptNew = arguments.length > 2 ? arguments[2] : undefined;
3094
- var updateActions = [].concat(_toConsumableArray(generateUpdateActionsForAttributeDefinitions(nestedValuesChanges.attributeDefinitions)), _toConsumableArray(generateUpdateActionsForAttributeEnumValues(nestedValuesChanges.attributeEnumValues)));
3095
- var changeAttributeOrderAction = generateChangeAttributeOrderAction(ptOld.attributes, ptNew.attributes, updateActions);
3096
- if (changeAttributeOrderAction) updateActions.push(changeAttributeOrderAction);
3097
- return updateActions;
3098
- };
3099
-
3100
- function createProductTypeMapActions(mapActionGroup, syncActionConfig) {
3101
- return function doMapActions(diff, next, previous, options) {
3102
- return flatten__default["default"]([
3103
- // we support only base fields for the product type,
3104
- // for attributes, applying hints would be recommended
3105
- mapActionGroup('base', function () {
3106
- return actionsMapBase$b(diff, previous, next, syncActionConfig);
3107
- }), actionsMapForHints(options.nestedValuesChanges, previous, next)]);
3108
- };
3109
- }
3110
- var productTypes = (function (actionGroupList, syncActionConfig) {
3111
- var mapActionGroup = createMapActionGroup(actionGroupList);
3112
- var doMapActions = createProductTypeMapActions(mapActionGroup, syncActionConfig);
3113
- var onBeforeApplyingDiff = null;
3114
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff, {
3115
- withHints: true
3116
- });
3117
- return {
3118
- buildActions: buildActions
3119
- };
3120
- });
3121
-
3122
- var baseActionsList$a = [{
3123
- action: 'changeKey',
3124
- key: 'key'
3125
- }, {
3126
- action: 'setName',
3127
- key: 'name'
3128
- }, {
3129
- action: 'setDescription',
3130
- key: 'description'
3131
- }, {
3132
- action: 'changeType',
3133
- key: 'type'
3134
- }, {
3135
- action: 'changeInitial',
3136
- key: 'initial'
3137
- }, {
3138
- action: 'setTransitions',
3139
- key: 'transitions'
3140
- }];
3141
- function actionsMapBase$a(diff, oldObj, newObj) {
3142
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3143
- return buildBaseAttributesActions({
3144
- actions: baseActionsList$a,
3145
- diff: diff,
3146
- oldObj: oldObj,
3147
- newObj: newObj,
3148
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3149
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3150
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3151
- });
3152
- }
3153
- function actionsMapRoles(diff, oldObj, newObj) {
3154
- var buildArrayActions = createBuildArrayActions('roles', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newRole) {
3155
- return {
3156
- action: 'addRoles',
3157
- roles: newRole
3158
- };
3159
- }), REMOVE_ACTIONS, function (oldRole) {
3160
- return {
3161
- action: 'removeRoles',
3162
- roles: oldRole
3163
- };
3164
- }));
3165
- return buildArrayActions(diff, oldObj, newObj);
3166
- }
3167
-
3168
- // This function groups `addRoles` and `removeRoles` actions to one array
3169
- function groupRoleActions(_ref) {
3170
- var _ref2 = _slicedToArray(_ref, 1),
3171
- actions = _ref2[0];
3172
- var addActionRoles = [];
3173
- var removeActionRoles = [];
3174
- actions.forEach(function (action) {
3175
- if (action.action === 'removeRoles') removeActionRoles.push(action.roles);
3176
- if (action.action === 'addRoles') addActionRoles.push(action.roles);
3177
- });
3178
- return [{
3179
- action: 'removeRoles',
3180
- roles: removeActionRoles
3181
- }, {
3182
- action: 'addRoles',
3183
- roles: addActionRoles
3184
- }].filter(function (action) {
3185
- return action.roles.length;
3186
- });
3187
- }
3188
- function createStatesMapActions(mapActionGroup, syncActionConfig) {
3189
- return function doMapActions(diff, newObj, oldObj) {
3190
- var baseActions = [];
3191
- var roleActions = [];
3192
- baseActions.push(mapActionGroup('base', function () {
3193
- return actionsMapBase$a(diff, oldObj, newObj, syncActionConfig);
3194
- }));
3195
- roleActions.push(mapActionGroup('roles', function () {
3196
- return actionsMapRoles(diff, oldObj, newObj);
3197
- }));
3198
- return flatten__default["default"]([].concat(baseActions, _toConsumableArray(groupRoleActions(roleActions))));
3199
- };
3200
- }
3201
- var states = (function (actionGroupList, syncActionConfig) {
3202
- var mapActionGroup = createMapActionGroup(actionGroupList);
3203
- var doMapActions = createStatesMapActions(mapActionGroup, syncActionConfig);
3204
- var buildActions = createBuildActions(diff, doMapActions);
3205
- return {
3206
- buildActions: buildActions
3207
- };
3208
- });
3209
-
3210
- var baseActionsList$9 = [{
3211
- action: 'changeKey',
3212
- key: 'key'
3213
- }, {
3214
- action: 'changeName',
3215
- key: 'name'
3216
- }, {
3217
- action: 'changeDescription',
3218
- key: 'description'
3219
- }, {
3220
- action: 'setAddress',
3221
- key: 'address'
3222
- }, {
3223
- action: 'setGeoLocation',
3224
- key: 'geoLocation'
3225
- }, {
3226
- action: 'setRoles',
3227
- key: 'roles'
3228
- }];
3229
- function actionsMapBase$9(diff, oldObj, newObj) {
3230
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3231
- return buildBaseAttributesActions({
3232
- actions: baseActionsList$9,
3233
- diff: diff,
3234
- oldObj: oldObj,
3235
- newObj: newObj,
3236
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3237
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3238
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3239
- });
3240
- }
3241
-
3242
- function createChannelsMapActions$1(mapActionGroup, syncActionConfig) {
3243
- return function doMapActions(diff, newObj, oldObj) {
3244
- var allActions = [];
3245
- allActions.push(mapActionGroup('base', function () {
3246
- return actionsMapBase$9(diff, oldObj, newObj, syncActionConfig);
3247
- }));
3248
- allActions.push(mapActionGroup('custom', function () {
3249
- return actionsMapCustom(diff, newObj, oldObj);
3250
- }));
3251
- return flatten__default["default"](allActions);
3252
- };
3253
- }
3254
- var channels = (function (actionGroupList) {
3255
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3256
- var mapActionGroup = createMapActionGroup(actionGroupList);
3257
- var doMapActions = createChannelsMapActions$1(mapActionGroup, syncActionConfig);
3258
- var buildActions = createBuildActions(diff, doMapActions);
3259
- return {
3260
- buildActions: buildActions
3261
- };
3262
- });
3263
-
3264
- var REGEX_NUMBER = new RegExp(/^\d+$/);
3265
- var REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/);
3266
- var getIsChangedOperation = function getIsChangedOperation(key) {
3267
- return REGEX_NUMBER.test(key);
3268
- };
3269
- var getIsRemovedOperation = function getIsRemovedOperation(key) {
3270
- return REGEX_UNDERSCORE_NUMBER.test(key);
3271
- };
3272
- var baseActionsList$8 = [{
3273
- action: 'changeKey',
3274
- key: 'key'
3275
- }, {
3276
- action: 'changeName',
3277
- key: 'name'
3278
- }, {
3279
- action: 'setDescription',
3280
- key: 'description'
3281
- }];
3282
- function actionsMapBase$8(diff, oldObj, newObj) {
3283
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3284
- return buildBaseAttributesActions({
3285
- actions: baseActionsList$8,
3286
- diff: diff,
3287
- oldObj: oldObj,
3288
- newObj: newObj,
3289
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3290
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3291
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3292
- });
3293
- }
3294
- function actionsMapEnums(fieldName, attributeType, attributeDiff, previous, next) {
3295
- var addEnumActionName = attributeType === 'Enum' ? 'addEnumValue' : 'addLocalizedEnumValue';
3296
- var changeEnumValueLabelActionName = attributeType === 'Enum' ? 'changeEnumValueLabel' : 'changeLocalizedEnumValueLabel';
3297
- var changeEnumOrderActionName = attributeType === 'Enum' ? 'changeEnumValueOrder' : 'changeLocalizedEnumValueOrder';
3298
- var buildArrayActions = createBuildArrayActions('values', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newEnum) {
3299
- return {
3300
- fieldName: fieldName,
3301
- action: addEnumActionName,
3302
- value: newEnum
3303
- };
3304
- }), CHANGE_ACTIONS, function (oldEnum, newEnum) {
3305
- var oldEnumInNext = next.values.find(function (nextEnum) {
3306
- return nextEnum.key === oldEnum.key;
3307
- });
3308
-
3309
- // These `changeActions` would impose a nested structure among
3310
- // the accumulated `updateActions` generated by `buildArrayActions`
3311
- // In the end; we have to flatten the structure before we pass it back
3312
- // to the client.
3313
- var changeActions = [];
3314
- if (oldEnumInNext) {
3315
- // If the enum value is changed, we need to change the order first
3316
- var isKeyChanged = oldEnum.key !== newEnum.key;
3317
-
3318
- // check if the label is changed
3319
- var foundPreviousEnum = previous.values.find(function (previousEnum) {
3320
- return previousEnum.key === newEnum.key;
3321
- });
3322
- var isLabelEqual = isEqual__default["default"](foundPreviousEnum.label, newEnum.label);
3323
- if (isKeyChanged) {
3324
- // these actions is then flatten in the end
3325
- changeActions.push({
3326
- fieldName: fieldName,
3327
- action: changeEnumOrderActionName,
3328
- value: newEnum
3329
- });
3330
- }
3331
- if (!isLabelEqual) {
3332
- changeActions.push({
3333
- fieldName: fieldName,
3334
- action: changeEnumValueLabelActionName,
3335
- value: newEnum
3336
- });
3337
- }
3338
- } else {
3339
- changeActions.push({
3340
- fieldName: fieldName,
3341
- action: addEnumActionName,
3342
- value: newEnum
3343
- });
3344
- }
3345
- return changeActions;
3346
- }));
3347
- var actions = [];
3348
- // following lists are necessary to ensure that when we change the
3349
- // order of enumValues, we generate one updateAction instead of one at a time.
3350
- var newEnumValuesOrder = [];
3351
- flatten__default["default"](buildArrayActions(attributeDiff, previous, next)).forEach(function (updateAction) {
3352
- if (updateAction.action === changeEnumOrderActionName) {
3353
- newEnumValuesOrder = next.values.map(function (enumValue) {
3354
- return enumValue.key;
3355
- });
3356
- } else actions.push(updateAction);
3357
- });
3358
- return [].concat(actions, _toConsumableArray(newEnumValuesOrder.length > 0 ? [{
3359
- fieldName: fieldName,
3360
- action: changeEnumOrderActionName,
3361
- keys: newEnumValuesOrder
3362
- }] : []));
3363
- }
3364
- function actionsMapFieldDefinitions(fieldDefinitionsDiff, previous, next, diffPaths) {
3365
- var actions = [];
3366
- forEach__default["default"](fieldDefinitionsDiff, function (diffValue, diffKey) {
3367
- var extractedPairs = extractMatchingPairs(diffPaths, diffKey, previous, next);
3368
- if (getIsChangedOperation(diffKey)) {
3369
- var _diffValue$type;
3370
- if (Array.isArray(diffValue)) {
3371
- var deltaValue = getDeltaValue(diffValue);
3372
- if (deltaValue.name) {
3373
- actions.push({
3374
- action: 'addFieldDefinition',
3375
- fieldDefinition: deltaValue
3376
- });
3377
- }
3378
- } else if (diffValue.label) {
3379
- actions.push({
3380
- action: 'changeLabel',
3381
- label: extractedPairs.newObj.label,
3382
- fieldName: extractedPairs.oldObj.name
3383
- });
3384
- } else if (diffValue.inputHint) {
3385
- actions.push({
3386
- action: 'changeInputHint',
3387
- inputHint: extractedPairs.newObj.inputHint,
3388
- fieldName: extractedPairs.oldObj.name
3389
- });
3390
- } else if (diffValue !== null && diffValue !== void 0 && (_diffValue$type = diffValue.type) !== null && _diffValue$type !== void 0 && _diffValue$type.values) {
3391
- actions.push.apply(actions, _toConsumableArray(actionsMapEnums(extractedPairs.oldObj.name, extractedPairs.oldObj.type.name, diffValue.type, extractedPairs.oldObj.type, extractedPairs.newObj.type)));
3392
- }
3393
- } else if (getIsRemovedOperation(diffKey)) {
3394
- if (Array.isArray(diffValue)) {
3395
- if (diffValue.length === 3 && diffValue[2] === 3) {
3396
- actions.push({
3397
- action: 'changeFieldDefinitionOrder',
3398
- fieldNames: next.map(function (n) {
3399
- return n.name;
3400
- })
3401
- });
3402
- } else {
3403
- var _deltaValue = getDeltaValue(diffValue);
3404
- if (_deltaValue === undefined && diffValue[0].name) actions.push({
3405
- action: 'removeFieldDefinition',
3406
- fieldName: diffValue[0].name
3407
- });
3408
- }
3409
- }
3410
- }
3411
- });
3412
-
3413
- // Make sure to execute removeActions before creating new ones
3414
- // in order to prevent any eventual removal of `addAction`.
3415
- // List of `removeActions` can be found here
3416
- // https://docs.commercetools.com/api/projects/types#change-key
3417
- var sortedActions = sortBy__default["default"](actions, function (action) {
3418
- return action.action !== 'removeFieldDefinition';
3419
- });
3420
- return sortedActions;
3421
- }
3422
-
3423
- function createTypeMapActions(mapActionGroup, syncActionConfig) {
3424
- return function doMapActions(diff, next, previous) {
3425
- var allActions = [];
3426
- allActions.push(mapActionGroup('base', function () {
3427
- return actionsMapBase$8(diff, previous, next, syncActionConfig);
3428
- }), mapActionGroup('fieldDefinitions', function () {
3429
- return actionsMapFieldDefinitions(diff.fieldDefinitions, previous.fieldDefinitions, next.fieldDefinitions, findMatchingPairs(diff.fieldDefinitions, previous.fieldDefinitions, next.fieldDefinitions, 'name'));
3430
- }));
3431
- return flatten__default["default"](allActions);
3432
- };
3433
- }
3434
- var types = (function (actionGroupList, syncActionConfig) {
3435
- var mapActionGroup = createMapActionGroup(actionGroupList);
3436
- var doMapActions = createTypeMapActions(mapActionGroup, syncActionConfig);
3437
- var buildActions = createBuildActions(diff, doMapActions);
3438
- return {
3439
- buildActions: buildActions
3440
- };
3441
- });
3442
-
3443
- var baseActionsList$7 = [{
3444
- action: 'changeName',
3445
- key: 'name'
3446
- }, {
3447
- action: 'changeCurrencies',
3448
- key: 'currencies'
3449
- }, {
3450
- action: 'changeCountries',
3451
- key: 'countries'
3452
- }, {
3453
- action: 'changeLanguages',
3454
- key: 'languages'
3455
- }, {
3456
- action: 'changeMessagesConfiguration',
3457
- key: 'messagesConfiguration'
3458
- }, {
3459
- action: 'setShippingRateInputType',
3460
- key: 'shippingRateInputType'
3461
- }];
3462
- var myBusinessUnitActionsList = [{
3463
- action: 'changeMyBusinessUnitStatusOnCreation',
3464
- key: 'myBusinessUnitStatusOnCreation',
3465
- actionKey: 'status'
3466
- }, {
3467
- action: 'setMyBusinessUnitAssociateRoleOnCreation',
3468
- key: 'myBusinessUnitAssociateRoleOnCreation',
3469
- actionKey: 'associateRole'
3470
- }];
3471
- var customerSearchActionsList = [{
3472
- action: 'changeCustomerSearchStatus',
3473
- key: 'status'
3474
- }];
3475
- var businessUnitSearchActionsList = [{
3476
- action: 'changeBusinessUnitSearchStatus',
3477
- key: 'status'
3478
- }];
3479
- function actionsMapBase$7(diff, oldObj, newObj) {
3480
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3481
- return buildBaseAttributesActions({
3482
- actions: baseActionsList$7,
3483
- diff: diff,
3484
- oldObj: oldObj,
3485
- newObj: newObj,
3486
- shouldOmitEmptyString: config.shouldOmitEmptyString
3487
- });
3488
- }
3489
- var actionsMapBusinessUnit = function actionsMapBusinessUnit(diff, oldObj, newObj) {
3490
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3491
- var businessUnits = diff.businessUnits;
3492
- if (!businessUnits) {
3493
- return [];
3494
- }
3495
- return buildBaseAttributesActions({
3496
- actions: myBusinessUnitActionsList,
3497
- diff: businessUnits,
3498
- oldObj: oldObj.businessUnits,
3499
- newObj: newObj.businessUnits,
3500
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3501
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
3502
- });
3503
- };
3504
- function actionsMapSearchIndexingConfiguration(diff, oldObj, newObj) {
3505
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3506
- var searchIndexing = diff.searchIndexing;
3507
- if (!searchIndexing) {
3508
- return [];
3509
- }
3510
- var businessUnits = searchIndexing.businessUnits,
3511
- customers = searchIndexing.customers;
3512
- if (!customers && !businessUnits) {
3513
- return [];
3514
- }
3515
- var businessUnitsActions = businessUnits ? buildBaseAttributesActions({
3516
- actions: businessUnitSearchActionsList,
3517
- diff: diff.searchIndexing.businessUnits,
3518
- oldObj: oldObj.searchIndexing.businessUnits,
3519
- newObj: newObj.searchIndexing.businessUnits,
3520
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3521
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3522
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3523
- }) : [];
3524
- var customersActions = customers ? buildBaseAttributesActions({
3525
- actions: customerSearchActionsList,
3526
- diff: diff.searchIndexing.customers,
3527
- oldObj: oldObj.searchIndexing.customers,
3528
- newObj: newObj.searchIndexing.customers,
3529
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3530
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3531
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3532
- }) : [];
3533
- return [].concat(_toConsumableArray(businessUnitsActions), _toConsumableArray(customersActions));
3534
- }
3535
-
3536
- function createChannelsMapActions(mapActionGroup, syncActionConfig) {
3537
- return function doMapActions(diff, newObj, oldObj) {
3538
- var allActions = [];
3539
- allActions.push(mapActionGroup('base', function () {
3540
- return actionsMapBase$7(diff, oldObj, newObj, syncActionConfig);
3541
- }));
3542
- allActions.push(mapActionGroup('businessUnit', function () {
3543
- return actionsMapBusinessUnit(diff, oldObj, newObj);
3544
- }));
3545
- allActions.push(mapActionGroup('searchIndexingConfiguration', function () {
3546
- return actionsMapSearchIndexingConfiguration(diff, oldObj, newObj);
3547
- }));
3548
- return flatten__default["default"](allActions);
3549
- };
3550
- }
3551
- var projects = (function (actionGroupList) {
3552
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3553
- var mapActionGroup = createMapActionGroup(actionGroupList);
3554
- var doMapActions = createChannelsMapActions(mapActionGroup, syncActionConfig);
3555
- var buildActions = createBuildActions(diff, doMapActions);
3556
- return {
3557
- buildActions: buildActions
3558
- };
3559
- });
3560
-
3561
- var baseActionsList$6 = [{
3562
- action: 'setName',
3563
- key: 'name'
3564
- }, {
3565
- action: 'setLanguages',
3566
- key: 'languages'
3567
- }, {
3568
- action: 'setDistributionChannels',
3569
- key: 'distributionChannels'
3570
- }, {
3571
- action: 'setSupplyChannels',
3572
- key: 'supplyChannels'
3573
- }];
3574
- function actionsMapBase$6(diff, oldObj, newObj) {
3575
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3576
- return buildBaseAttributesActions({
3577
- actions: baseActionsList$6,
3578
- diff: diff,
3579
- oldObj: oldObj,
3580
- newObj: newObj,
3581
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3582
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3583
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3584
- });
3585
- }
3586
-
3587
- function createStoresMapActions(mapActionGroup) {
3588
- return function doMapActions(diff, next, previous) {
3589
- var allActions = [];
3590
- allActions.push(mapActionGroup('base', function () {
3591
- return actionsMapBase$6(diff, previous, next);
3592
- }));
3593
- allActions.push(mapActionGroup('custom', function () {
3594
- return actionsMapCustom(diff, next, previous);
3595
- }));
3596
- return flatten__default["default"](allActions);
3597
- };
3598
- }
3599
- var stores = (function (actionGroupList) {
3600
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3601
- var mapActionGroup = createMapActionGroup(actionGroupList);
3602
- var doMapActions = createStoresMapActions(mapActionGroup);
3603
- var onBeforeApplyingDiff = null;
3604
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff, options);
3605
- return {
3606
- buildActions: buildActions
3607
- };
3608
- });
3609
-
3610
- var baseActionsList$5 = [{
3611
- action: 'changeName',
3612
- key: 'name'
3613
- }, {
3614
- action: 'setKey',
3615
- key: 'key'
3616
- }];
3617
- function actionsMapBase$5(diff, oldObj, newObj) {
3618
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3619
- return buildBaseAttributesActions({
3620
- actions: baseActionsList$5,
3621
- diff: diff,
3622
- oldObj: oldObj,
3623
- newObj: newObj,
3624
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3625
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3626
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3627
- });
3628
- }
3629
-
3630
- function createProductSelectionsMapActions(mapActionGroup) {
3631
- return function doMapActions(diff, next, previous) {
3632
- var allActions = [];
3633
- allActions.push(mapActionGroup('base', function () {
3634
- return actionsMapBase$5(diff, previous, next);
3635
- }));
3636
- allActions.push(mapActionGroup('custom', function () {
3637
- return actionsMapCustom(diff, next, previous);
3638
- }));
3639
- return flatten__default["default"](allActions);
3640
- };
3641
- }
3642
- var productSelections = (function (actionGroupList) {
3643
- var mapActionGroup = createMapActionGroup(actionGroupList);
3644
- var doMapActions = createProductSelectionsMapActions(mapActionGroup);
3645
- var onBeforeApplyingDiff = null;
3646
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff);
3647
- return {
3648
- buildActions: buildActions
3649
- };
3650
- });
3651
-
3652
- var baseActionsList$4 = [{
3653
- action: 'changeValue',
3654
- key: 'value'
3655
- }, {
3656
- action: 'setDiscountedPrice',
3657
- key: 'discounted'
3658
- },
3659
- // TODO: Later add more accurate actions `addPriceTier`, `removePriceTier`
3660
- {
3661
- action: 'setPriceTiers',
3662
- key: 'tiers'
3663
- }, {
3664
- action: 'setKey',
3665
- key: 'key'
3666
- }, {
3667
- action: 'setValidFrom',
3668
- key: 'validFrom'
3669
- }, {
3670
- action: 'setValidUntil',
3671
- key: 'validUntil'
3672
- }, {
3673
- action: 'changeActive',
3674
- key: 'active'
3675
- }];
3676
- function actionsMapBase$4(diff, oldObj, newObj) {
3677
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3678
- return buildBaseAttributesActions({
3679
- actions: baseActionsList$4,
3680
- diff: diff,
3681
- oldObj: oldObj,
3682
- newObj: newObj,
3683
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3684
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3685
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3686
- });
3687
- }
3688
-
3689
- function createPriceMapActions(mapActionGroup, syncActionConfig) {
3690
- return function doMapActions(diff, newObj, oldObj) {
3691
- var baseActions = mapActionGroup('base', function () {
3692
- return actionsMapBase$4(diff, oldObj, newObj, syncActionConfig);
3693
- });
3694
- var customActions = mapActionGroup('custom', function () {
3695
- return actionsMapCustom(diff, newObj, oldObj);
3696
- });
3697
- return combineValidityActions([].concat(_toConsumableArray(baseActions), _toConsumableArray(customActions)));
3698
- };
3699
- }
3700
- var prices = (function (actionGroupList, syncActionConfig) {
3701
- var mapActionGroup = createMapActionGroup(actionGroupList);
3702
- var doMapActions = createPriceMapActions(mapActionGroup, syncActionConfig);
3703
- var buildActions = createBuildActions(diff, doMapActions);
3704
- return {
3705
- buildActions: buildActions
3706
- };
3707
- });
3708
-
3709
- var hasAttribute = function hasAttribute(attributes, newValue) {
3710
- return attributes.some(function (attribute) {
3711
- return attribute.key === newValue.key;
3712
- });
3713
- };
3714
- var baseActionsList$3 = [{
3715
- action: 'changeName',
3716
- key: 'name'
3717
- }, {
3718
- action: 'setKey',
3719
- key: 'key'
3720
- }, {
3721
- action: 'setDescription',
3722
- key: 'description'
3723
- }];
3724
- function actionsMapBase$3(diff, oldObj, newObj) {
3725
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3726
- return buildBaseAttributesActions({
3727
- actions: baseActionsList$3,
3728
- diff: diff,
3729
- oldObj: oldObj,
3730
- newObj: newObj,
3731
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3732
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3733
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3734
- });
3735
- }
3736
- function actionsMapAttributes(diff, oldObj, newObj) {
3737
- var handler = createBuildArrayActions('attributes', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newAttribute) {
3738
- return {
3739
- action: 'addAttribute',
3740
- attribute: newAttribute
3741
- };
3742
- }), REMOVE_ACTIONS, function (oldAttribute) {
3743
- // We only add the action if the attribute is not included in the new object.
3744
- return !hasAttribute(newObj.attributes, oldAttribute) ? {
3745
- action: 'removeAttribute',
3746
- attribute: oldAttribute
3747
- } : null;
3748
- }), CHANGE_ACTIONS, function (oldAttribute, newAttribute) {
3749
- var result = [];
3750
- // We only remove the attribute in case that the oldAttribute is not
3751
- // included in the new object
3752
- if (!hasAttribute(newObj.attributes, oldAttribute)) result.push({
3753
- action: 'removeAttribute',
3754
- attribute: oldAttribute
3755
- });
3756
-
3757
- // We only add the attribute in case that the newAttribute was not
3758
- // included in the old object
3759
- if (!hasAttribute(oldObj.attributes, newAttribute)) result.push({
3760
- action: 'addAttribute',
3761
- attribute: newAttribute
3762
- });
3763
- return result;
3764
- }));
3765
- return handler(diff, oldObj, newObj);
3766
- }
3767
-
3768
- function createAttributeGroupsMapActions(mapActionGroup, syncActionConfig) {
3769
- return function doMapActions(diff, newObj, oldObj) {
3770
- var allActions = [];
3771
- allActions.push(mapActionGroup('base', function () {
3772
- return actionsMapBase$3(diff, oldObj, newObj, syncActionConfig);
3773
- }));
3774
- allActions.push(flatten__default["default"](mapActionGroup('attributes', function () {
3775
- return actionsMapAttributes(diff, oldObj, newObj);
3776
- })));
3777
- return flatten__default["default"](allActions);
3778
- };
3779
- }
3780
- var attributeGroups = (function (actionGroupList, syncActionConfig) {
3781
- var mapActionGroup = createMapActionGroup(actionGroupList);
3782
- var doMapActions = createAttributeGroupsMapActions(mapActionGroup, syncActionConfig);
3783
- var buildActions = createBuildActions(diff, doMapActions);
3784
- return {
3785
- buildActions: buildActions
3786
- };
3787
- });
3788
-
3789
- var baseActionsList$2 = [{
3790
- action: 'setKey',
3791
- key: 'key'
3792
- }, {
3793
- action: 'changeTriggers',
3794
- key: 'triggers'
3795
- }, {
3796
- action: 'setTimeoutInMs',
3797
- key: 'timeoutInMs'
3798
- }, {
3799
- action: 'changeDestination',
3800
- key: 'destination'
3801
- }];
3802
- var actionsMapBase$2 = function actionsMapBase(diff, oldObj, newObj, config) {
3803
- return buildBaseAttributesActions({
3804
- actions: baseActionsList$2,
3805
- diff: diff,
3806
- oldObj: oldObj,
3807
- newObj: newObj,
3808
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3809
- });
3810
- };
3811
-
3812
- var createApiExtensionsMapActions = function createApiExtensionsMapActions(mapActionGroup, syncActionConfig) {
3813
- return function doMapActions(diff, newObj, oldObj) {
3814
- var allActions = [];
3815
- allActions.push(mapActionGroup('base', function () {
3816
- return actionsMapBase$2(diff, oldObj, newObj, syncActionConfig);
3817
- }));
3818
- return flatten__default["default"](allActions);
3819
- };
3820
- };
3821
- var apiExtensions = (function (actionGroupList, syncActionConfig) {
3822
- var mapActionGroup = createMapActionGroup(actionGroupList);
3823
- var doMapActions = createApiExtensionsMapActions(mapActionGroup, syncActionConfig);
3824
- var buildActions = createBuildActions(diff, doMapActions);
3825
- return {
3826
- buildActions: buildActions
3827
- };
3828
- });
3829
-
3830
- var baseActionsList$1 = [{
3831
- action: 'setStores',
3832
- key: 'stores'
3833
- }, {
3834
- action: 'changeAssociateMode',
3835
- key: 'associateMode'
3836
- }, {
3837
- action: 'changeApprovalRuleMode',
3838
- key: 'approvalRuleMode'
3839
- }, {
3840
- action: 'changeName',
3841
- key: 'name'
3842
- }, {
3843
- action: 'changeParentUnit',
3844
- key: 'parentUnit'
3845
- }, {
3846
- action: 'changeStatus',
3847
- key: 'status'
3848
- }, {
3849
- action: 'setContactEmail',
3850
- key: 'contactEmail'
3851
- }, {
3852
- action: 'setStoreMode',
3853
- key: 'storeMode'
3854
- }];
3855
- var actionsMapAssociates = function actionsMapAssociates(diff, oldObj, newObj) {
3856
- var handler = createBuildArrayActions('associates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newObject) {
3857
- return {
3858
- action: 'addAssociate',
3859
- associate: newObject
3860
- };
3861
- }), REMOVE_ACTIONS, function (objectToRemove) {
3862
- return {
3863
- action: 'removeAssociate',
3864
- customer: {
3865
- typeId: 'customer',
3866
- id: objectToRemove.customer.id
3867
- }
3868
- };
3869
- }), CHANGE_ACTIONS, function (oldObject, updatedObject) {
3870
- return {
3871
- action: 'changeAssociate',
3872
- associate: updatedObject
3873
- };
3874
- }));
3875
- return handler(diff, oldObj, newObj);
3876
- };
3877
- var actionsMapBase$1 = function actionsMapBase(diff, oldObj, newObj, config) {
3878
- return buildBaseAttributesActions({
3879
- actions: baseActionsList$1,
3880
- diff: diff,
3881
- oldObj: oldObj,
3882
- newObj: newObj,
3883
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3884
- });
3885
- };
3886
-
3887
- var createCustomerMapActions = function createCustomerMapActions(mapActionGroup, syncActionConfig) {
3888
- return function doMapActions(diff, newObj, oldObj) {
3889
- var allActions = [];
3890
- allActions.push(mapActionGroup('base', function () {
3891
- return actionsMapBase$1(diff, oldObj, newObj, syncActionConfig);
3892
- }));
3893
- allActions.push(mapActionGroup('billingAddressIds', function () {
3894
- return actionsMapRemoveBillingAddresses(diff, oldObj, newObj);
3895
- }));
3896
- allActions.push(mapActionGroup('shippingAddressIds', function () {
3897
- return actionsMapRemoveShippingAddresses(diff, oldObj, newObj);
3898
- }));
3899
- allActions.push(mapActionGroup('addresses', function () {
3900
- return actionsMapAddresses(diff, oldObj, newObj);
3901
- }));
3902
- allActions.push(mapActionGroup('base', function () {
3903
- return actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig);
3904
- }));
3905
- allActions.push(mapActionGroup('billingAddressIds', function () {
3906
- return actionsMapAddBillingAddresses(diff, oldObj, newObj);
3907
- }));
3908
- allActions.push(mapActionGroup('shippingAddressIds', function () {
3909
- return actionsMapAddShippingAddresses(diff, oldObj, newObj);
3910
- }));
3911
- allActions.push(mapActionGroup('associates', function () {
3912
- return actionsMapAssociates(diff, oldObj, newObj);
3913
- }));
3914
- allActions.push(mapActionGroup('custom', function () {
3915
- return actionsMapCustom(diff, newObj, oldObj);
3916
- }));
3917
- return flatten__default["default"](allActions);
3918
- };
3919
- };
3920
- var businessUnits = (function (actionGroupList, syncActionConfig) {
3921
- var mapActionGroup = createMapActionGroup(actionGroupList);
3922
- var doMapActions = createCustomerMapActions(mapActionGroup, syncActionConfig);
3923
- var buildActions = createBuildActions(diff, doMapActions);
3924
- return {
3925
- buildActions: buildActions
3926
- };
3927
- });
3928
-
3929
- var baseActionsList = [{
3930
- action: 'setKey',
3931
- key: 'key'
3932
- }, {
3933
- action: 'setMessages',
3934
- key: 'messages'
3935
- }, {
3936
- action: 'setChanges',
3937
- key: 'changes'
3938
- }, {
3939
- action: 'changeDestination',
3940
- key: 'destination'
3941
- }];
3942
- var actionsMapBase = function actionsMapBase(diff, oldObj, newObj, config) {
3943
- return buildBaseAttributesActions({
3944
- actions: baseActionsList,
3945
- diff: diff,
3946
- oldObj: oldObj,
3947
- newObj: newObj,
3948
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3949
- });
3950
- };
3951
-
3952
- var createSubscriptionsMapActions = function createSubscriptionsMapActions(mapActionGroup, syncActionConfig) {
3953
- return function doMapActions(diff, newObj, oldObj) {
3954
- var allActions = [];
3955
- allActions.push(mapActionGroup('base', function () {
3956
- return actionsMapBase(diff, oldObj, newObj, syncActionConfig);
3957
- }));
3958
- allActions.push(mapActionGroup('custom', function () {
3959
- return actionsMapCustom(diff, newObj, oldObj);
3960
- }));
3961
- return allActions.flat();
3962
- };
3963
- };
3964
- var subscriptions = (function (actionGroupList, syncActionConfig) {
3965
- var mapActionGroup = createMapActionGroup(actionGroupList);
3966
- var doMapActions = createSubscriptionsMapActions(mapActionGroup, syncActionConfig);
3967
- var buildActions = createBuildActions(diff, doMapActions);
3968
- return {
3969
- buildActions: buildActions
3970
- };
3971
- });
3972
-
3973
- exports.createSyncApiExtensions = apiExtensions;
3974
- exports.createSyncAttributeGroups = attributeGroups;
3975
- exports.createSyncBusinessUnits = businessUnits;
3976
- exports.createSyncCartDiscounts = cartDiscounts;
3977
- exports.createSyncCategories = categories;
3978
- exports.createSyncChannels = channels;
3979
- exports.createSyncCustomerGroup = customerGroup;
3980
- exports.createSyncCustomers = customers;
3981
- exports.createSyncDiscountCodes = discountCodes;
3982
- exports.createSyncInventories = inventories;
3983
- exports.createSyncOrders = orders;
3984
- exports.createSyncProductDiscounts = productDiscounts;
3985
- exports.createSyncProductSelections = productSelections;
3986
- exports.createSyncProductTypes = productTypes;
3987
- exports.createSyncProducts = products;
3988
- exports.createSyncProjects = projects;
3989
- exports.createSyncRecurringOrders = recurringOrders;
3990
- exports.createSyncShippingMethods = shippingMethods;
3991
- exports.createSyncStandalonePrices = prices;
3992
- exports.createSyncStates = states;
3993
- exports.createSyncStores = stores;
3994
- exports.createSyncSubscriptions = subscriptions;
3995
- exports.createSyncTaxCategories = taxCategories;
3996
- exports.createSyncTypes = types;
3997
- exports.createSyncZones = zones;