@commercetools/sync-actions 7.3.0 → 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 +11299 -3197
  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 -3977
  80. package/dist/commercetools-sync-actions.cjs.prod.js +0 -3977
@@ -1,3977 +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 actionsMapBillingAddresses(diff, oldObj, newObj) {
949
- var handler = createBuildArrayActions('billingAddressIds', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (addressId) {
950
- return {
951
- action: 'addBillingAddressId',
952
- addressId: addressId
953
- };
954
- }), REMOVE_ACTIONS, function (addressId) {
955
- return {
956
- action: 'removeBillingAddressId',
957
- addressId: addressId
958
- };
959
- }));
960
- return handler(diff, oldObj, newObj);
961
- }
962
- function actionsMapShippingAddresses(diff, oldObj, newObj) {
963
- var handler = createBuildArrayActions('shippingAddressIds', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (addressId) {
964
- return {
965
- action: 'addShippingAddressId',
966
- addressId: addressId
967
- };
968
- }), REMOVE_ACTIONS, function (addressId) {
969
- return {
970
- action: 'removeShippingAddressId',
971
- addressId: addressId
972
- };
973
- }));
974
- return handler(diff, oldObj, newObj);
975
- }
976
- function actionsMapAuthenticationModes(diff, oldObj, newObj) {
977
- // eslint-disable-next-line no-use-before-define
978
- return buildAuthenticationModeActions({
979
- actions: authenticationModeActionsList,
980
- diff: diff,
981
- oldObj: oldObj,
982
- newObj: newObj
983
- });
984
- }
985
- function buildAuthenticationModeActions(_ref) {
986
- var actions = _ref.actions,
987
- diff = _ref.diff,
988
- oldObj = _ref.oldObj,
989
- newObj = _ref.newObj;
990
- return actions.map(function (item) {
991
- var key = item.key;
992
- var value = item.value || item.key;
993
- var delta = diff[key];
994
- var before = oldObj[key];
995
- var now = newObj[key];
996
- var isNotDefinedBefore = isEmptyValue(oldObj[key]);
997
- var isNotDefinedNow = isEmptyValue(newObj[key]);
998
- var authenticationModes = ['Password', 'ExternalAuth'];
999
- if (!delta) return undefined;
1000
- if (isNotDefinedNow && isNotDefinedBefore) return undefined;
1001
- if (newObj.authenticationMode === 'Password' && !newObj.password) throw new Error('Cannot set to Password authentication mode without password');
1002
- if ('authenticationMode' in newObj && !authenticationModes.includes(newObj.authenticationMode)) throw new Error('Invalid Authentication Mode');
1003
- if (!isNotDefinedNow && isNotDefinedBefore) {
1004
- // no value previously set
1005
- if (newObj.authenticationMode === 'ExternalAuth') return {
1006
- action: item.action,
1007
- authMode: now
1008
- };
1009
- return _defineProperty({
1010
- action: item.action,
1011
- authMode: now
1012
- }, value, newObj.password);
1013
- }
1014
-
1015
- /* no new value */
1016
- if (isNotDefinedNow && !{}.hasOwnProperty.call(newObj, key)) return undefined;
1017
- if (isNotDefinedNow && {}.hasOwnProperty.call(newObj, key))
1018
- // value unset
1019
- return undefined;
1020
-
1021
- // We need to clone `before` as `patch` will mutate it
1022
- var patched = patch(clone(before), delta);
1023
- if (newObj.authenticationMode === 'ExternalAuth') return {
1024
- action: item.action,
1025
- authMode: patched
1026
- };
1027
- return _defineProperty({
1028
- action: item.action,
1029
- authMode: patched
1030
- }, value, newObj.password);
1031
- }).filter(function (action) {
1032
- return !isNil__default["default"](action);
1033
- });
1034
- }
1035
-
1036
- function createCustomerMapActions$1(mapActionGroup, syncActionConfig) {
1037
- return function doMapActions(diff, newObj, oldObj) {
1038
- var allActions = [];
1039
- allActions.push(mapActionGroup('base', function () {
1040
- return actionsMapBase$n(diff, oldObj, newObj, syncActionConfig);
1041
- }));
1042
- allActions.push(mapActionGroup('references', function () {
1043
- return actionsMapReferences$2(diff, oldObj, newObj);
1044
- }));
1045
- allActions.push(mapActionGroup('addresses', function () {
1046
- return actionsMapAddresses(diff, oldObj, newObj);
1047
- }));
1048
- allActions.push(mapActionGroup('base', function () {
1049
- return actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig);
1050
- }));
1051
- allActions.push(mapActionGroup('billingAddressIds', function () {
1052
- return actionsMapBillingAddresses(diff, oldObj, newObj);
1053
- }));
1054
- allActions.push(mapActionGroup('shippingAddressIds', function () {
1055
- return actionsMapShippingAddresses(diff, oldObj, newObj);
1056
- }));
1057
- allActions.push(mapActionGroup('custom', function () {
1058
- return actionsMapCustom(diff, newObj, oldObj);
1059
- }));
1060
- allActions.push(mapActionGroup('authenticationModes', function () {
1061
- return actionsMapAuthenticationModes(diff, oldObj, newObj);
1062
- }));
1063
- return flatten__default["default"](allActions);
1064
- };
1065
- }
1066
- var customers = (function (actionGroupList, syncActionConfig) {
1067
- // actionGroupList contains information about which action groups
1068
- // are allowed or ignored
1069
-
1070
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
1071
- // - action group name
1072
- // - callback function that should return a list of actions that correspond
1073
- // to the for the action group
1074
-
1075
- // this resulting function mapActionGroup will call the callback function
1076
- // for allowed action groups and return the return value of the callback
1077
- // It will return an empty array for ignored action groups
1078
- var mapActionGroup = createMapActionGroup(actionGroupList);
1079
- var doMapActions = createCustomerMapActions$1(mapActionGroup, syncActionConfig);
1080
- var buildActions = createBuildActions(diff, doMapActions, copyEmptyArrayProps);
1081
- return {
1082
- buildActions: buildActions
1083
- };
1084
- });
1085
-
1086
- var baseActionsList$m = [{
1087
- action: 'changeQuantity',
1088
- key: 'quantityOnStock',
1089
- actionKey: 'quantity'
1090
- }, {
1091
- action: 'setRestockableInDays',
1092
- key: 'restockableInDays'
1093
- }, {
1094
- action: 'setExpectedDelivery',
1095
- key: 'expectedDelivery'
1096
- }];
1097
- var referenceActionsList$1 = [{
1098
- action: 'setSupplyChannel',
1099
- key: 'supplyChannel'
1100
- }];
1101
-
1102
- /**
1103
- * SYNC FUNCTIONS
1104
- */
1105
-
1106
- function actionsMapBase$m(diff, oldObj, newObj) {
1107
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1108
- return buildBaseAttributesActions({
1109
- actions: baseActionsList$m,
1110
- diff: diff,
1111
- oldObj: oldObj,
1112
- newObj: newObj,
1113
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1114
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
1115
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
1116
- });
1117
- }
1118
- function actionsMapReferences$1(diff, oldObj, newObj) {
1119
- return buildReferenceActions({
1120
- actions: referenceActionsList$1,
1121
- diff: diff,
1122
- oldObj: oldObj,
1123
- newObj: newObj
1124
- });
1125
- }
1126
-
1127
- function createInventoryMapActions(mapActionGroup, syncActionConfig) {
1128
- return function doMapActions(diff, newObj, oldObj) {
1129
- var allActions = [];
1130
- allActions.push(mapActionGroup('base', function () {
1131
- return actionsMapBase$m(diff, oldObj, newObj, syncActionConfig);
1132
- }));
1133
- allActions.push(mapActionGroup('references', function () {
1134
- return actionsMapReferences$1(diff, oldObj, newObj);
1135
- }));
1136
- allActions.push(mapActionGroup('custom', function () {
1137
- return actionsMapCustom(diff, newObj, oldObj);
1138
- }));
1139
- return flatten__default["default"](allActions);
1140
- };
1141
- }
1142
- var inventories = (function (actionGroupList, syncActionConfig) {
1143
- // actionGroupList contains information about which action groups
1144
- // are allowed or ignored
1145
-
1146
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
1147
- // - action group name
1148
- // - callback function that should return a list of actions that correspond
1149
- // to the for the action group
1150
-
1151
- // this resulting function mapActionGroup will call the callback function
1152
- // for allowed action groups and return the return value of the callback
1153
- // It will return an empty array for ignored action groups
1154
- var mapActionGroup = createMapActionGroup(actionGroupList);
1155
- var doMapActions = createInventoryMapActions(mapActionGroup, syncActionConfig);
1156
- var buildActions = createBuildActions(diff, doMapActions);
1157
- return {
1158
- buildActions: buildActions
1159
- };
1160
- });
1161
-
1162
- function extractMatchingPairs(hashMap, key, before, now) {
1163
- var oldObjPos;
1164
- var newObjPos;
1165
- var oldObj;
1166
- var newObj;
1167
- if (hashMap[key]) {
1168
- oldObjPos = hashMap[key][0];
1169
- newObjPos = hashMap[key][1];
1170
- if (before && before[oldObjPos]) oldObj = before[oldObjPos];
1171
- if (now && now[newObjPos]) newObj = now[newObjPos];
1172
- }
1173
- return {
1174
- oldObj: oldObj,
1175
- newObj: newObj
1176
- };
1177
- }
1178
-
1179
- var REGEX_NUMBER$3 = new RegExp(/^\d+$/);
1180
- var REGEX_UNDERSCORE_NUMBER$3 = new RegExp(/^_\d+$/);
1181
- function preProcessCollection() {
1182
- var collection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1183
- var identifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id';
1184
- return collection.reduce(function (acc, currentValue, currentIndex) {
1185
- acc.refByIndex[String(currentIndex)] = currentValue[identifier];
1186
- acc.refByIdentifier[currentValue[identifier]] = String(currentIndex);
1187
- return acc;
1188
- }, {
1189
- refByIndex: {},
1190
- refByIdentifier: {}
1191
- });
1192
- }
1193
-
1194
- // creates a hash of a location of an item in collection1 and collection2
1195
- function findMatchingPairs(diff) {
1196
- var before = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1197
- var now = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1198
- var identifier = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'id';
1199
- var result = {};
1200
- var _preProcessCollection = preProcessCollection(before, identifier),
1201
- beforeObjRefByIdentifier = _preProcessCollection.refByIdentifier,
1202
- beforeObjRefByIndex = _preProcessCollection.refByIndex;
1203
- var _preProcessCollection2 = preProcessCollection(now, identifier),
1204
- nowObjRefByIdentifier = _preProcessCollection2.refByIdentifier,
1205
- nowObjRefByIndex = _preProcessCollection2.refByIndex;
1206
- forEach__default["default"](diff, function (item, key) {
1207
- if (REGEX_NUMBER$3.test(key)) {
1208
- var matchingIdentifier = nowObjRefByIndex[key];
1209
- result[key] = [beforeObjRefByIdentifier[matchingIdentifier], key];
1210
- } else if (REGEX_UNDERSCORE_NUMBER$3.test(key)) {
1211
- var index = key.substring(1);
1212
- var _matchingIdentifier = beforeObjRefByIndex[index];
1213
- result[key] = [index, nowObjRefByIdentifier[_matchingIdentifier]];
1214
- }
1215
- });
1216
- return result;
1217
- }
1218
-
1219
- var REGEX_NUMBER$2 = new RegExp(/^\d+$/);
1220
- var REGEX_UNDERSCORE_NUMBER$2 = new RegExp(/^_\d+$/);
1221
- var baseActionsList$l = [{
1222
- action: 'changeName',
1223
- key: 'name'
1224
- }, {
1225
- action: 'changeSlug',
1226
- key: 'slug'
1227
- }, {
1228
- action: 'setDescription',
1229
- key: 'description'
1230
- }, {
1231
- action: 'setSearchKeywords',
1232
- key: 'searchKeywords'
1233
- }, {
1234
- action: 'setKey',
1235
- key: 'key'
1236
- }, {
1237
- action: 'setPriceMode',
1238
- key: 'priceMode'
1239
- }];
1240
- var baseAssetActionsList = [{
1241
- action: 'setAssetKey',
1242
- key: 'key',
1243
- actionKey: 'assetKey'
1244
- }, {
1245
- action: 'changeAssetName',
1246
- key: 'name'
1247
- }, {
1248
- action: 'setAssetDescription',
1249
- key: 'description'
1250
- }, {
1251
- action: 'setAssetTags',
1252
- key: 'tags'
1253
- }, {
1254
- action: 'setAssetSources',
1255
- key: 'sources'
1256
- }];
1257
- var metaActionsList = [{
1258
- action: 'setMetaTitle',
1259
- key: 'metaTitle'
1260
- }, {
1261
- action: 'setMetaDescription',
1262
- key: 'metaDescription'
1263
- }, {
1264
- action: 'setMetaKeywords',
1265
- key: 'metaKeywords'
1266
- }];
1267
- var referenceActionsList = [{
1268
- action: 'setTaxCategory',
1269
- key: 'taxCategory'
1270
- }, {
1271
- action: 'transitionState',
1272
- key: 'state'
1273
- }];
1274
-
1275
- /**
1276
- * HELPER FUNCTIONS
1277
- */
1278
-
1279
- var getIsAddAction = function getIsAddAction(key, resource) {
1280
- return REGEX_NUMBER$2.test(key) && Array.isArray(resource) && resource.length;
1281
- };
1282
- var getIsUpdateAction = function getIsUpdateAction(key, resource) {
1283
- return REGEX_NUMBER$2.test(key) && Object.keys(resource).length;
1284
- };
1285
- var getIsRemoveAction = function getIsRemoveAction(key, resource) {
1286
- return REGEX_UNDERSCORE_NUMBER$2.test(key) && Number(resource[2]) === 0;
1287
- };
1288
- var getIsItemMovedAction = function getIsItemMovedAction(key, resource) {
1289
- return REGEX_UNDERSCORE_NUMBER$2.test(key) && Number(resource[2]) === 3;
1290
- };
1291
- function _buildSkuActions(variantDiff, oldVariant) {
1292
- if ({}.hasOwnProperty.call(variantDiff, 'sku')) {
1293
- var newValue = getDeltaValue(variantDiff.sku);
1294
- if (!newValue && !oldVariant.sku) return null;
1295
- return {
1296
- action: 'setSku',
1297
- variantId: oldVariant.id,
1298
- sku: newValue || null
1299
- };
1300
- }
1301
- return null;
1302
- }
1303
- function _buildKeyActions(variantDiff, oldVariant) {
1304
- if ({}.hasOwnProperty.call(variantDiff, 'key')) {
1305
- var newValue = getDeltaValue(variantDiff.key);
1306
- if (!newValue && !oldVariant.key) return null;
1307
- return {
1308
- action: 'setProductVariantKey',
1309
- variantId: oldVariant.id,
1310
- key: newValue || null
1311
- };
1312
- }
1313
- return null;
1314
- }
1315
- function _buildAttributeValue(diffedValue, oldAttributeValue, newAttributeValue) {
1316
- var value;
1317
- if (Array.isArray(diffedValue)) value = getDeltaValue(diffedValue, oldAttributeValue);else if (typeof diffedValue === 'string')
1318
- // LText: value: {en: "", de: ""}
1319
- // Enum: value: {key: "foo", label: "Foo"}
1320
- // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1321
- // Money: value: {centAmount: 123, currencyCode: ""}
1322
- // *: value: ""
1323
-
1324
- // normal
1325
- value = getDeltaValue(diffedValue, oldAttributeValue);else if (diffedValue.centAmount || diffedValue.currencyCode)
1326
- // Money
1327
- value = {
1328
- centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : newAttributeValue.centAmount,
1329
- currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : newAttributeValue.currencyCode
1330
- };else if (diffedValue.key)
1331
- // Enum / LEnum (use only the key)
1332
- value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1333
- // set-typed attribute
1334
- value = newAttributeValue;
1335
- } else {
1336
- // LText
1337
-
1338
- var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1339
- var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1340
- return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1341
- }, _objectSpread2({}, oldAttributeValue));
1342
- value = updatedValue;
1343
- }
1344
- return value;
1345
- }
1346
- function _buildNewSetAttributeAction(variantId, attr, sameForAllAttributeNames) {
1347
- var attributeName = attr && attr.name;
1348
- if (!attributeName) return undefined;
1349
- var action = {
1350
- action: 'setAttribute',
1351
- variantId: variantId,
1352
- name: attributeName,
1353
- value: attr.value
1354
- };
1355
- if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
1356
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1357
- action: 'setAttributeInAllVariants'
1358
- });
1359
- delete action.variantId;
1360
- }
1361
- return action;
1362
- }
1363
- function _buildSetAttributeAction(diffedValue, oldVariant, attribute, sameForAllAttributeNames) {
1364
- if (!attribute) return undefined;
1365
- var action = {
1366
- action: 'setAttribute',
1367
- variantId: oldVariant.id,
1368
- name: attribute.name
1369
- };
1370
-
1371
- // Used as original object for patching long diff text
1372
- var oldAttribute = oldVariant.attributes.find(function (a) {
1373
- return a.name === attribute.name;
1374
- }) || {};
1375
- if (sameForAllAttributeNames.indexOf(attribute.name) !== -1) {
1376
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1377
- action: 'setAttributeInAllVariants'
1378
- });
1379
- delete action.variantId;
1380
- }
1381
- action.value = _buildAttributeValue(diffedValue, oldAttribute.value, attribute.value);
1382
- return action;
1383
- }
1384
- function _buildNewSetProductAttributeAction(attr) {
1385
- var attributeName = attr && attr.name;
1386
- if (!attributeName) return undefined;
1387
- var action = {
1388
- action: 'setProductAttribute',
1389
- name: attributeName,
1390
- value: attr.value
1391
- };
1392
- return action;
1393
- }
1394
- function _buildSetProductAttributeAction(diffedValue, oldProductData, newAttribute) {
1395
- if (!newAttribute) return undefined;
1396
- var action = {
1397
- action: 'setProductAttribute',
1398
- name: newAttribute.name
1399
- };
1400
-
1401
- // Used as original object for patching long diff text
1402
- var oldAttribute = oldProductData.attributes.find(function (a) {
1403
- return a.name === newAttribute.name;
1404
- }) || {};
1405
- action.value = _buildAttributeValue(diffedValue, oldAttribute.value, newAttribute.value);
1406
- return action;
1407
- }
1408
- function _buildVariantImagesAction(diffedImages) {
1409
- var oldVariant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1410
- var newVariant = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1411
- var actions = [];
1412
- // generate a hashMap to be able to reference the right image from both ends
1413
- var matchingImagePairs = findMatchingPairs(diffedImages, oldVariant.images, newVariant.images, 'url');
1414
- forEach__default["default"](diffedImages, function (image, key) {
1415
- var _extractMatchingPairs = extractMatchingPairs(matchingImagePairs, key, oldVariant.images, newVariant.images),
1416
- oldObj = _extractMatchingPairs.oldObj,
1417
- newObj = _extractMatchingPairs.newObj;
1418
- if (REGEX_NUMBER$2.test(key)) {
1419
- // New image
1420
- if (Array.isArray(image) && image.length) actions.push({
1421
- action: 'addExternalImage',
1422
- variantId: oldVariant.id,
1423
- image: getDeltaValue(image)
1424
- });else if (_typeof(image) === 'object') if ({}.hasOwnProperty.call(image, 'url') && image.url.length === 2) {
1425
- // There is a new image, remove the old one first.
1426
- actions.push({
1427
- action: 'removeImage',
1428
- variantId: oldVariant.id,
1429
- imageUrl: oldObj.url
1430
- });
1431
- actions.push({
1432
- action: 'addExternalImage',
1433
- variantId: oldVariant.id,
1434
- image: newObj
1435
- });
1436
- } else if ({}.hasOwnProperty.call(image, 'label') && (image.label.length === 1 || image.label.length === 2)) actions.push({
1437
- action: 'setImageLabel',
1438
- variantId: oldVariant.id,
1439
- imageUrl: oldObj.url,
1440
- label: getDeltaValue(image.label)
1441
- });
1442
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(image) && image.length === 3) {
1443
- if (Number(image[2]) === 3)
1444
- // image position changed
1445
- actions.push({
1446
- action: 'moveImageToPosition',
1447
- variantId: oldVariant.id,
1448
- imageUrl: oldObj.url,
1449
- position: Number(image[1])
1450
- });else if (Number(image[2]) === 0)
1451
- // image removed
1452
- actions.push({
1453
- action: 'removeImage',
1454
- variantId: oldVariant.id,
1455
- imageUrl: oldObj.url
1456
- });
1457
- }
1458
- });
1459
- return actions;
1460
- }
1461
- function _buildVariantPricesAction(diffedPrices) {
1462
- var oldVariant = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1463
- var newVariant = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1464
- var enableDiscounted = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1465
- var addPriceActions = [];
1466
- var changePriceActions = [];
1467
- var removePriceActions = [];
1468
-
1469
- // generate a hashMap to be able to reference the right image from both ends
1470
- var matchingPricePairs = findMatchingPairs(diffedPrices, oldVariant.prices, newVariant.prices);
1471
- forEach__default["default"](diffedPrices, function (price, key) {
1472
- var _extractMatchingPairs2 = extractMatchingPairs(matchingPricePairs, key, oldVariant.prices, newVariant.prices),
1473
- oldObj = _extractMatchingPairs2.oldObj,
1474
- newObj = _extractMatchingPairs2.newObj;
1475
- if (getIsAddAction(key, price)) {
1476
- // Remove read-only fields
1477
- var patchedPrice = price.map(function (p) {
1478
- var shallowClone = _objectSpread2({}, p);
1479
- if (enableDiscounted !== true) delete shallowClone.discounted;
1480
- return shallowClone;
1481
- });
1482
- addPriceActions.push({
1483
- action: 'addPrice',
1484
- variantId: oldVariant.id,
1485
- price: getDeltaValue(patchedPrice)
1486
- });
1487
- return;
1488
- }
1489
- if (getIsUpdateAction(key, price)) {
1490
- // Remove the discounted field and make sure that the price
1491
- // still has other values, otherwise simply return
1492
- var filteredPrice = _objectSpread2({}, price);
1493
- if (enableDiscounted !== true) delete filteredPrice.discounted;
1494
- if (Object.keys(filteredPrice).length) {
1495
- // At this point price should have changed, simply pick the new one
1496
- var newPrice = _objectSpread2({}, newObj);
1497
- if (enableDiscounted !== true) delete newPrice.discounted;
1498
- changePriceActions.push({
1499
- action: 'changePrice',
1500
- priceId: oldObj.id,
1501
- price: newPrice
1502
- });
1503
- }
1504
- return;
1505
- }
1506
- if (getIsRemoveAction(key, price)) {
1507
- // price removed
1508
- removePriceActions.push({
1509
- action: 'removePrice',
1510
- priceId: oldObj.id
1511
- });
1512
- }
1513
- });
1514
- return [addPriceActions, changePriceActions, removePriceActions];
1515
- }
1516
- function _buildProductAttributesActions(diffedAttributes, oldProductData, newProductData) {
1517
- var actions = [];
1518
- if (!diffedAttributes) return actions;
1519
- forEach__default["default"](diffedAttributes, function (value, key) {
1520
- if (REGEX_NUMBER$2.test(key)) {
1521
- if (Array.isArray(value)) {
1522
- var setAction = _buildNewSetProductAttributeAction(getDeltaValue(value));
1523
- if (setAction) actions.push(setAction);
1524
- } else if (newProductData.attributes) {
1525
- var _setAction = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[key]);
1526
- if (_setAction) actions.push(_setAction);
1527
- }
1528
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) {
1529
- if (Array.isArray(value)) {
1530
- // Ignore pure array moves!
1531
- if (value.length === 3 && value[2] === 3) return;
1532
- var deltaValue = getDeltaValue(value);
1533
- if (!deltaValue) if (value[0] && value[0].name)
1534
- // unset attribute if
1535
- deltaValue = {
1536
- name: value[0].name
1537
- };else deltaValue = undefined;
1538
- var _setAction2 = _buildNewSetProductAttributeAction(deltaValue);
1539
- if (_setAction2) actions.push(_setAction2);
1540
- } else {
1541
- var index = key.substring(1);
1542
- if (newProductData.attributes) {
1543
- var _setAction3 = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[index]);
1544
- if (_setAction3) actions.push(_setAction3);
1545
- }
1546
- }
1547
- }
1548
- });
1549
- return actions;
1550
- }
1551
- function _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames) {
1552
- var actions = [];
1553
- if (!attributes) return actions;
1554
- forEach__default["default"](attributes, function (value, key) {
1555
- if (REGEX_NUMBER$2.test(key)) {
1556
- if (Array.isArray(value)) {
1557
- var id = oldVariant.id;
1558
- var deltaValue = getDeltaValue(value);
1559
- var setAction = _buildNewSetAttributeAction(id, deltaValue, sameForAllAttributeNames);
1560
- if (setAction) actions.push(setAction);
1561
- } else if (newVariant.attributes) {
1562
- var _setAction4 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1563
- if (_setAction4) actions.push(_setAction4);
1564
- }
1565
- } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(value)) {
1566
- // Ignore pure array moves!
1567
- if (value.length === 3 && value[2] === 3) return;
1568
- var _id = oldVariant.id;
1569
- var _deltaValue = getDeltaValue(value);
1570
- if (!_deltaValue) if (value[0] && value[0].name)
1571
- // unset attribute if
1572
- _deltaValue = {
1573
- name: value[0].name
1574
- };else _deltaValue = undefined;
1575
- var _setAction5 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1576
- if (_setAction5) actions.push(_setAction5);
1577
- } else {
1578
- var index = key.substring(1);
1579
- if (newVariant.attributes) {
1580
- var _setAction6 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1581
- if (_setAction6) actions.push(_setAction6);
1582
- }
1583
- }
1584
- });
1585
- return actions;
1586
- }
1587
- function toAssetIdentifier(asset) {
1588
- var assetIdentifier = asset.id ? {
1589
- assetId: asset.id
1590
- } : {
1591
- assetKey: asset.key
1592
- };
1593
- return assetIdentifier;
1594
- }
1595
- function toVariantIdentifier(variant) {
1596
- var id = variant.id,
1597
- sku = variant.sku;
1598
- return id ? {
1599
- variantId: id
1600
- } : {
1601
- sku: sku
1602
- };
1603
- }
1604
- function _buildVariantChangeAssetOrderAction(diffAssets, oldVariant, newVariant) {
1605
- var isAssetOrderChanged = Object.entries(diffAssets).find(function (entry) {
1606
- return getIsItemMovedAction(entry[0], entry[1]);
1607
- });
1608
- if (!isAssetOrderChanged) {
1609
- return [];
1610
- }
1611
- var assetIdsBefore = oldVariant.assets.map(function (_) {
1612
- return _.id;
1613
- });
1614
- var assetIdsCurrent = newVariant.assets.map(function (_) {
1615
- return _.id;
1616
- }).filter(function (_) {
1617
- return _ !== undefined;
1618
- });
1619
- var assetIdsToKeep = intersection__default["default"](assetIdsCurrent, assetIdsBefore);
1620
- var assetIdsToRemove = without__default["default"].apply(void 0, [assetIdsBefore].concat(_toConsumableArray(assetIdsToKeep)));
1621
- var changeAssetOrderAction = _objectSpread2({
1622
- action: 'changeAssetOrder',
1623
- assetOrder: assetIdsToKeep.concat(assetIdsToRemove)
1624
- }, toVariantIdentifier(oldVariant));
1625
- return [changeAssetOrderAction];
1626
- }
1627
- function _buildVariantAssetsActions(diffAssets, oldVariant, newVariant) {
1628
- var assetActions = [];
1629
-
1630
- // generate a hashMap to be able to reference the right asset from both ends
1631
- var matchingAssetPairs = findMatchingPairs(diffAssets, oldVariant.assets, newVariant.assets);
1632
- forEach__default["default"](diffAssets, function (asset, key) {
1633
- var _extractMatchingPairs3 = extractMatchingPairs(matchingAssetPairs, key, oldVariant.assets, newVariant.assets),
1634
- oldAsset = _extractMatchingPairs3.oldObj,
1635
- newAsset = _extractMatchingPairs3.newObj;
1636
- if (getIsAddAction(key, asset)) {
1637
- assetActions.push(_objectSpread2(_objectSpread2({
1638
- action: 'addAsset',
1639
- asset: getDeltaValue(asset)
1640
- }, toVariantIdentifier(newVariant)), {}, {
1641
- position: Number(key)
1642
- }));
1643
- return;
1644
- }
1645
- if (getIsUpdateAction(key, asset)) {
1646
- // todo add changeAssetOrder
1647
- var basicActions = buildBaseAttributesActions({
1648
- actions: baseAssetActionsList,
1649
- diff: asset,
1650
- oldObj: oldAsset,
1651
- newObj: newAsset
1652
- }).map(function (action) {
1653
- // in case of 'setAssetKey' then the identifier will be only 'assetId'
1654
- if (action.action === 'setAssetKey') {
1655
- return _objectSpread2(_objectSpread2(_objectSpread2({}, action), toVariantIdentifier(oldVariant)), {}, {
1656
- assetId: oldAsset.id
1657
- });
1658
- }
1659
- return _objectSpread2(_objectSpread2(_objectSpread2({}, action), toVariantIdentifier(oldVariant)), toAssetIdentifier(oldAsset));
1660
- });
1661
- assetActions.push.apply(assetActions, _toConsumableArray(basicActions));
1662
- if (asset.custom) {
1663
- var customActions = actionsMapCustom(asset, newAsset, oldAsset, _objectSpread2(_objectSpread2({
1664
- actions: {
1665
- setCustomType: 'setAssetCustomType',
1666
- setCustomField: 'setAssetCustomField'
1667
- }
1668
- }, toVariantIdentifier(oldVariant)), toAssetIdentifier(oldAsset)));
1669
- assetActions.push.apply(assetActions, _toConsumableArray(customActions));
1670
- }
1671
- return;
1672
- }
1673
- if (getIsRemoveAction(key, asset)) {
1674
- assetActions.push(_objectSpread2(_objectSpread2({
1675
- action: 'removeAsset'
1676
- }, toAssetIdentifier(oldAsset)), toVariantIdentifier(oldVariant)));
1677
- }
1678
- });
1679
- var changedAssetOrderAction = _buildVariantChangeAssetOrderAction(diffAssets, oldVariant, newVariant);
1680
- return [].concat(_toConsumableArray(changedAssetOrderAction), assetActions);
1681
- }
1682
-
1683
- /**
1684
- * SYNC FUNCTIONS
1685
- */
1686
-
1687
- function actionsMapBase$l(diff, oldObj, newObj) {
1688
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1689
- return buildBaseAttributesActions({
1690
- actions: baseActionsList$l,
1691
- diff: diff,
1692
- oldObj: oldObj,
1693
- newObj: newObj,
1694
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1695
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
1696
- });
1697
- }
1698
- function actionsMapMeta(diff, oldObj, newObj) {
1699
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1700
- return buildBaseAttributesActions({
1701
- actions: metaActionsList,
1702
- diff: diff,
1703
- oldObj: oldObj,
1704
- newObj: newObj,
1705
- shouldOmitEmptyString: config.shouldOmitEmptyString,
1706
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
1707
- });
1708
- }
1709
- function actionsMapAddVariants(diff, oldObj, newObj) {
1710
- var handler = createBuildArrayActions('variants', _defineProperty({}, ADD_ACTIONS, function (newObject) {
1711
- return _objectSpread2(_objectSpread2({}, newObject), {}, {
1712
- action: 'addVariant'
1713
- });
1714
- }));
1715
- return handler(diff, oldObj, newObj);
1716
- }
1717
- function actionsMapRemoveVariants(diff, oldObj, newObj) {
1718
- var handler = createBuildArrayActions('variants', _defineProperty({}, REMOVE_ACTIONS, function (_ref) {
1719
- var id = _ref.id;
1720
- return {
1721
- action: 'removeVariant',
1722
- id: id
1723
- };
1724
- }));
1725
- return handler(diff, oldObj, newObj);
1726
- }
1727
- function actionsMapReferences(diff, oldObj, newObj) {
1728
- return buildReferenceActions({
1729
- actions: referenceActionsList,
1730
- diff: diff,
1731
- oldObj: oldObj,
1732
- newObj: newObj
1733
- });
1734
- }
1735
- function actionsMapCategories(diff) {
1736
- var actions = [];
1737
- if (!diff.categories) return actions;
1738
- var addToCategoryActions = [];
1739
- var removeFromCategoryActions = [];
1740
- forEach__default["default"](diff.categories, function (category) {
1741
- if (Array.isArray(category)) {
1742
- var action = {
1743
- category: category[0]
1744
- };
1745
- if (category.length === 3) {
1746
- // Ignore pure array moves!
1747
- if (category[2] !== 3) {
1748
- action.action = 'removeFromCategory';
1749
- removeFromCategoryActions.push(action);
1750
- }
1751
- } else if (category.length === 1) {
1752
- action.action = 'addToCategory';
1753
- addToCategoryActions.push(action);
1754
- }
1755
- }
1756
- });
1757
-
1758
- // Make sure `removeFromCategory` actions come first
1759
- return removeFromCategoryActions.concat(addToCategoryActions);
1760
- }
1761
- function actionsMapCategoryOrderHints(diff) {
1762
- if (!diff.categoryOrderHints) return [];
1763
- // Ignore this pattern as its means no changes happened [{},0,0]
1764
- if (Array.isArray(diff.categoryOrderHints)) return [];
1765
- return Object.keys(diff.categoryOrderHints).map(function (categoryId) {
1766
- var hintChange = diff.categoryOrderHints[categoryId];
1767
- var action = {
1768
- action: 'setCategoryOrderHint',
1769
- categoryId: categoryId
1770
- };
1771
- if (hintChange.length === 1)
1772
- // item was added
1773
- action.orderHint = hintChange[0];else if (hintChange.length === 2 && hintChange[1] !== 0)
1774
- // item was changed
1775
- action.orderHint = hintChange[1];
1776
-
1777
- // else item was removed -> do not set 'orderHint' property
1778
-
1779
- return action;
1780
- });
1781
- }
1782
- function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
1783
- var allAssetsActions = [];
1784
- var variants = diff.variants;
1785
- if (variants) forEach__default["default"](variants, function (variant, key) {
1786
- var _extractMatchingPairs4 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1787
- oldVariant = _extractMatchingPairs4.oldObj,
1788
- newVariant = _extractMatchingPairs4.newObj;
1789
- if (variant.assets && (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key))) {
1790
- var assetActions = _buildVariantAssetsActions(variant.assets, oldVariant, newVariant);
1791
- allAssetsActions = allAssetsActions.concat(assetActions);
1792
- }
1793
- });
1794
- return allAssetsActions;
1795
- }
1796
- function actionsMapProductAttributes(diffedProductData, oldProductData, newProductData) {
1797
- return _buildProductAttributesActions(diffedProductData.attributes, oldProductData, newProductData);
1798
- }
1799
- function actionsMapAttributes$1(diff, oldObj, newObj) {
1800
- var sameForAllAttributeNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1801
- var variantHashMap = arguments.length > 4 ? arguments[4] : undefined;
1802
- var actions = [];
1803
- var variants = diff.variants;
1804
- if (variants) forEach__default["default"](variants, function (variant, key) {
1805
- var _extractMatchingPairs5 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1806
- oldVariant = _extractMatchingPairs5.oldObj,
1807
- newVariant = _extractMatchingPairs5.newObj;
1808
- if (REGEX_NUMBER$2.test(key) && !Array.isArray(variant)) {
1809
- var skuAction = _buildSkuActions(variant, oldVariant);
1810
- var keyAction = _buildKeyActions(variant, oldVariant);
1811
- if (skuAction) actions.push(skuAction);
1812
- if (keyAction) actions.push(keyAction);
1813
- var attributes = variant.attributes;
1814
- var attrActions = _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames);
1815
- actions = actions.concat(attrActions);
1816
- }
1817
- });
1818
-
1819
- // Ensure that an action is unique.
1820
- // This is especially necessary for SFA attributes.
1821
- return uniqWith__default["default"](actions, function (a, b) {
1822
- return a.action === b.action && a.name === b.name && a.variantId === b.variantId;
1823
- });
1824
- }
1825
- function actionsMapImages(diff, oldObj, newObj, variantHashMap) {
1826
- var actions = [];
1827
- var variants = diff.variants;
1828
- if (variants) forEach__default["default"](variants, function (variant, key) {
1829
- var _extractMatchingPairs6 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1830
- oldVariant = _extractMatchingPairs6.oldObj,
1831
- newVariant = _extractMatchingPairs6.newObj;
1832
- if (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key)) {
1833
- var vActions = _buildVariantImagesAction(variant.images, oldVariant, newVariant);
1834
- actions = actions.concat(vActions);
1835
- }
1836
- });
1837
- return actions;
1838
- }
1839
- function actionsMapPrices(diff, oldObj, newObj, variantHashMap, enableDiscounted) {
1840
- var addPriceActions = [];
1841
- var changePriceActions = [];
1842
- var removePriceActions = [];
1843
- var variants = diff.variants;
1844
- if (variants) forEach__default["default"](variants, function (variant, key) {
1845
- var _extractMatchingPairs7 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1846
- oldVariant = _extractMatchingPairs7.oldObj,
1847
- newVariant = _extractMatchingPairs7.newObj;
1848
- if (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key)) {
1849
- var _buildVariantPricesAc = _buildVariantPricesAction(variant.prices, oldVariant, newVariant, enableDiscounted),
1850
- _buildVariantPricesAc2 = _slicedToArray(_buildVariantPricesAc, 3),
1851
- addPriceAction = _buildVariantPricesAc2[0],
1852
- changePriceAction = _buildVariantPricesAc2[1],
1853
- removePriceAction = _buildVariantPricesAc2[2];
1854
- addPriceActions = addPriceActions.concat(addPriceAction);
1855
- changePriceActions = changePriceActions.concat(changePriceAction);
1856
- removePriceActions = removePriceActions.concat(removePriceAction);
1857
- }
1858
- });
1859
-
1860
- // price actions need to be in this below order
1861
- return changePriceActions.concat(removePriceActions).concat(addPriceActions);
1862
- }
1863
- function actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap) {
1864
- var actions = [];
1865
- var variants = diff.variants;
1866
- if (variants) forEach__default["default"](variants, function (variant, key) {
1867
- var _extractMatchingPairs8 = extractMatchingPairs(variantHashMap, key, oldObj.variants, newObj.variants),
1868
- oldVariant = _extractMatchingPairs8.oldObj,
1869
- newVariant = _extractMatchingPairs8.newObj;
1870
- if (variant && variant.prices && (REGEX_UNDERSCORE_NUMBER$2.test(key) || REGEX_NUMBER$2.test(key))) {
1871
- var priceHashMap = findMatchingPairs(variant.prices, oldVariant.prices, newVariant.prices);
1872
- forEach__default["default"](variant.prices, function (price, index) {
1873
- var _extractMatchingPairs9 = extractMatchingPairs(priceHashMap, index, oldVariant.prices, newVariant.prices),
1874
- oldPrice = _extractMatchingPairs9.oldObj,
1875
- newPrice = _extractMatchingPairs9.newObj;
1876
- if (price.custom && (REGEX_UNDERSCORE_NUMBER$2.test(index) || REGEX_NUMBER$2.test(index))) {
1877
- var generatedActions = actionsMapCustom(price, newPrice, oldPrice, {
1878
- actions: {
1879
- setCustomType: 'setProductPriceCustomType',
1880
- setCustomField: 'setProductPriceCustomField'
1881
- },
1882
- priceId: oldPrice.id
1883
- });
1884
- actions = actions.concat(generatedActions);
1885
- }
1886
- });
1887
- }
1888
- });
1889
- return actions;
1890
- }
1891
- function actionsMapMasterVariant(oldObj, newObj) {
1892
- var createChangeMasterVariantAction = function createChangeMasterVariantAction(variantId) {
1893
- return {
1894
- action: 'changeMasterVariant',
1895
- variantId: variantId
1896
- };
1897
- };
1898
- var extractMasterVariantId = function extractMasterVariantId(fromObj) {
1899
- var variants = Array.isArray(fromObj.variants) ? fromObj.variants : [];
1900
- return variants[0] ? variants[0].id : undefined;
1901
- };
1902
- var newMasterVariantId = extractMasterVariantId(newObj);
1903
- var oldMasterVariantId = extractMasterVariantId(oldObj);
1904
-
1905
- // Old and new master master variant differ and a new master variant id exists
1906
- if (newMasterVariantId && oldMasterVariantId !== newMasterVariantId) return [createChangeMasterVariantAction(newMasterVariantId)];
1907
- return [];
1908
- }
1909
-
1910
- function createProductMapActions(mapActionGroup, syncActionConfig) {
1911
- return function doMapActions(diff, newObj, oldObj) {
1912
- var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
1913
- var allActions = [];
1914
- var sameForAllAttributeNames = options.sameForAllAttributeNames,
1915
- enableDiscounted = options.enableDiscounted;
1916
- var publish = newObj.publish,
1917
- staged = newObj.staged;
1918
- var variantHashMap = findMatchingPairs(diff.variants, oldObj.variants, newObj.variants);
1919
- allActions.push(mapActionGroup('productAttributes', function () {
1920
- return actionsMapProductAttributes(diff, oldObj, newObj);
1921
- }));
1922
- allActions.push(mapActionGroup('attributes', function () {
1923
- return actionsMapAttributes$1(diff, oldObj, newObj, sameForAllAttributeNames || [], variantHashMap);
1924
- }));
1925
- allActions.push(mapActionGroup('variants', function () {
1926
- return actionsMapAddVariants(diff, oldObj, newObj);
1927
- }));
1928
- allActions.push(actionsMapMasterVariant(oldObj, newObj));
1929
- allActions.push(mapActionGroup('variants', function () {
1930
- return actionsMapRemoveVariants(diff, oldObj, newObj);
1931
- }));
1932
- allActions.push(mapActionGroup('base', function () {
1933
- return actionsMapBase$l(diff, oldObj, newObj, syncActionConfig);
1934
- }));
1935
- allActions.push(mapActionGroup('meta', function () {
1936
- return actionsMapMeta(diff, oldObj, newObj);
1937
- }));
1938
- allActions.push(mapActionGroup('references', function () {
1939
- return actionsMapReferences(diff, oldObj, newObj);
1940
- }));
1941
- allActions.push(mapActionGroup('images', function () {
1942
- return actionsMapImages(diff, oldObj, newObj, variantHashMap);
1943
- }));
1944
- allActions.push(mapActionGroup('pricesCustom', function () {
1945
- return actionsMapPricesCustom(diff, oldObj, newObj, variantHashMap);
1946
- }));
1947
- allActions.push(mapActionGroup('prices', function () {
1948
- return actionsMapPrices(diff, oldObj, newObj, variantHashMap, enableDiscounted);
1949
- }));
1950
- allActions.push(mapActionGroup('categories', function () {
1951
- return actionsMapCategories(diff);
1952
- }));
1953
- allActions.push(mapActionGroup('categoryOrderHints', function () {
1954
- return actionsMapCategoryOrderHints(diff);
1955
- }));
1956
- allActions.push(mapActionGroup('assets', function () {
1957
- return actionsMapAssets(diff, oldObj, newObj, variantHashMap);
1958
- }));
1959
- if (publish === true || staged === false) return flatten__default["default"](allActions).map(function (action) {
1960
- return _objectSpread2(_objectSpread2({}, action), {}, {
1961
- staged: false
1962
- });
1963
- });
1964
- return flatten__default["default"](allActions);
1965
- };
1966
- }
1967
- function moveMasterVariantsIntoVariants(before, now) {
1968
- var _copyEmptyArrayProps = copyEmptyArrayProps(before, now),
1969
- _copyEmptyArrayProps2 = _slicedToArray(_copyEmptyArrayProps, 2),
1970
- beforeCopy = _copyEmptyArrayProps2[0],
1971
- nowCopy = _copyEmptyArrayProps2[1];
1972
- var move = function move(obj) {
1973
- return _objectSpread2(_objectSpread2({}, obj), {}, {
1974
- masterVariant: undefined,
1975
- variants: [obj.masterVariant].concat(_toConsumableArray(obj.variants || []))
1976
- });
1977
- };
1978
- var hasMasterVariant = function hasMasterVariant(obj) {
1979
- return obj && obj.masterVariant;
1980
- };
1981
- return [hasMasterVariant(beforeCopy) ? move(beforeCopy) : beforeCopy, hasMasterVariant(nowCopy) ? move(nowCopy) : nowCopy];
1982
- }
1983
- var products = (function (actionGroupList, syncActionConfig) {
1984
- var mapActionGroup = createMapActionGroup(actionGroupList);
1985
- var doMapActions = createProductMapActions(mapActionGroup, syncActionConfig);
1986
- var buildActions = createBuildActions(diff, doMapActions, moveMasterVariantsIntoVariants);
1987
- return {
1988
- buildActions: buildActions
1989
- };
1990
- });
1991
-
1992
- var REGEX_NUMBER$1 = new RegExp(/^\d+$/);
1993
- var REGEX_UNDERSCORE_NUMBER$1 = new RegExp(/^_\d+$/);
1994
- var isAddAction = function isAddAction(key, resource) {
1995
- return REGEX_NUMBER$1.test(key) && Array.isArray(resource) && resource.length;
1996
- };
1997
- var isRemoveAction = function isRemoveAction(key, resource) {
1998
- return REGEX_UNDERSCORE_NUMBER$1.test(key) && Number(resource[2]) === 0;
1999
- };
2000
- var baseActionsList$k = [{
2001
- action: 'changeOrderState',
2002
- key: 'orderState'
2003
- }, {
2004
- action: 'changePaymentState',
2005
- key: 'paymentState'
2006
- }, {
2007
- action: 'changeShipmentState',
2008
- key: 'shipmentState'
2009
- }];
2010
-
2011
- /**
2012
- * SYNC FUNCTIONS
2013
- */
2014
-
2015
- function actionsMapBase$k(diff, oldObj, newObj) {
2016
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2017
- return buildBaseAttributesActions({
2018
- actions: baseActionsList$k,
2019
- diff: diff,
2020
- oldObj: oldObj,
2021
- newObj: newObj,
2022
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2023
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2024
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2025
- });
2026
- }
2027
- function actionsMapDeliveries(diff, oldObj, newObj) {
2028
- var deliveriesDiff = diff.shippingInfo;
2029
- if (!deliveriesDiff) return [];
2030
- var handler = createBuildArrayActions('deliveries', _defineProperty({}, ADD_ACTIONS, function (newObject) {
2031
- return {
2032
- action: 'addDelivery',
2033
- items: newObject.items,
2034
- parcels: newObject.parcels
2035
- };
2036
- }));
2037
- return handler(deliveriesDiff, oldObj.shippingInfo, newObj.shippingInfo);
2038
- }
2039
- function _buildDeliveryParcelsAction(diffedParcels) {
2040
- var oldDelivery = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2041
- var newDelivery = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
2042
- var addParcelActions = [];
2043
- var removeParcelActions = [];
2044
-
2045
- // generate a hashMap to be able to reference the right image from both ends
2046
- var matchingParcelPairs = findMatchingPairs(diffedParcels, oldDelivery.parcels, newDelivery.parcels);
2047
- forEach__default["default"](diffedParcels, function (parcel, key) {
2048
- var _extractMatchingPairs = extractMatchingPairs(matchingParcelPairs, key, oldDelivery.parcels, newDelivery.parcels),
2049
- oldObj = _extractMatchingPairs.oldObj;
2050
- if (isAddAction(key, parcel)) {
2051
- addParcelActions.push(_objectSpread2({
2052
- action: 'addParcelToDelivery',
2053
- deliveryId: oldDelivery.id
2054
- }, getDeltaValue(parcel)));
2055
- return;
2056
- }
2057
- if (isRemoveAction(key, parcel)) {
2058
- removeParcelActions.push({
2059
- action: 'removeParcelFromDelivery',
2060
- parcelId: oldObj.id
2061
- });
2062
- }
2063
- });
2064
- return [addParcelActions, removeParcelActions];
2065
- }
2066
- function _buildDeliveryItemsAction(diffedItems) {
2067
- var newDelivery = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2068
- var setDeliveryItemsAction = [];
2069
- // If there is a diff it means that there were changes (update, adds or removes)
2070
- // over the items, which means that `setDeliveryItems` change has happened over
2071
- // the delivery
2072
- if (diffedItems && Object.keys(diffedItems).length > 0) {
2073
- setDeliveryItemsAction.push({
2074
- action: 'setDeliveryItems',
2075
- deliveryId: newDelivery.id,
2076
- deliveryKey: newDelivery.key,
2077
- items: newDelivery.items
2078
- });
2079
- }
2080
- return [setDeliveryItemsAction];
2081
- }
2082
- function actionsMapParcels(diff, oldObj, newObj, deliveryHashMap) {
2083
- var shippingInfo = diff.shippingInfo;
2084
- if (!shippingInfo) return [];
2085
- var deliveries = shippingInfo.deliveries;
2086
- if (!deliveries) return [];
2087
- var addParcelActions = [];
2088
- var removeParcelActions = [];
2089
- if (deliveries) forEach__default["default"](deliveries, function (delivery, key) {
2090
- var _extractMatchingPairs2 = extractMatchingPairs(deliveryHashMap, key, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries),
2091
- oldDelivery = _extractMatchingPairs2.oldObj,
2092
- newDelivery = _extractMatchingPairs2.newObj;
2093
- if (REGEX_UNDERSCORE_NUMBER$1.test(key) || REGEX_NUMBER$1.test(key)) {
2094
- var _buildDeliveryParcels = _buildDeliveryParcelsAction(delivery.parcels, oldDelivery, newDelivery),
2095
- _buildDeliveryParcels2 = _slicedToArray(_buildDeliveryParcels, 2),
2096
- addParcelAction = _buildDeliveryParcels2[0],
2097
- removeParcelAction = _buildDeliveryParcels2[1];
2098
- addParcelActions = addParcelActions.concat(addParcelAction);
2099
- removeParcelActions = removeParcelActions.concat(removeParcelAction);
2100
- }
2101
- });
2102
- return removeParcelActions.concat(addParcelActions);
2103
- }
2104
- function actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap) {
2105
- var shippingInfo = diff.shippingInfo;
2106
- if (!shippingInfo) return [];
2107
- var deliveries = shippingInfo.deliveries;
2108
- if (!deliveries) return [];
2109
- var setDeliveryItemsActions = [];
2110
- forEach__default["default"](deliveries, function (delivery, key) {
2111
- var _extractMatchingPairs3 = extractMatchingPairs(deliveryHashMap, key, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries),
2112
- newDelivery = _extractMatchingPairs3.newObj;
2113
- if (REGEX_UNDERSCORE_NUMBER$1.test(key) || REGEX_NUMBER$1.test(key)) {
2114
- var _buildDeliveryItemsAc = _buildDeliveryItemsAction(delivery.items, newDelivery),
2115
- _buildDeliveryItemsAc2 = _slicedToArray(_buildDeliveryItemsAc, 1),
2116
- setDeliveryItemsAction = _buildDeliveryItemsAc2[0];
2117
- setDeliveryItemsActions = setDeliveryItemsActions.concat(setDeliveryItemsAction);
2118
- }
2119
- });
2120
- return setDeliveryItemsActions;
2121
- }
2122
- function actionsMapReturnsInfo(diff, oldObj, newObj) {
2123
- var returnInfoDiff = diff.returnInfo;
2124
- if (!returnInfoDiff) return [];
2125
- var handler = createBuildArrayActions('returnInfo', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newReturnInfo) {
2126
- if (newReturnInfo.items) {
2127
- return [_objectSpread2({
2128
- action: 'addReturnInfo'
2129
- }, newReturnInfo)];
2130
- }
2131
- return [];
2132
- }), CHANGE_ACTIONS, function (oldSReturnInfo, newReturnInfo, key) {
2133
- var _returnInfoDiff$key$i = returnInfoDiff[key].items,
2134
- items = _returnInfoDiff$key$i === void 0 ? {} : _returnInfoDiff$key$i;
2135
- if (Object.keys(items).length === 0) {
2136
- return [];
2137
- }
2138
- return Object.keys(items).reduce(function (actions, index) {
2139
- var item = newReturnInfo.items[index];
2140
- if (items[index].shipmentState) {
2141
- actions.push({
2142
- action: 'setReturnShipmentState',
2143
- returnItemId: item.id,
2144
- shipmentState: item.shipmentState
2145
- });
2146
- }
2147
- if (items[index].paymentState) {
2148
- actions.push({
2149
- action: 'setReturnPaymentState',
2150
- returnItemId: item.id,
2151
- paymentState: item.paymentState
2152
- });
2153
- }
2154
- return actions;
2155
- }, []);
2156
- }));
2157
- return handler(diff, oldObj, newObj);
2158
- }
2159
-
2160
- function createOrderMapActions(mapActionGroup, syncActionConfig) {
2161
- return function doMapActions(diff, newObj, oldObj) {
2162
- var allActions = [];
2163
- var deliveryHashMap;
2164
- if (diff.shippingInfo && diff.shippingInfo.deliveries) {
2165
- deliveryHashMap = findMatchingPairs(diff.shippingInfo.deliveries, oldObj.shippingInfo.deliveries, newObj.shippingInfo.deliveries);
2166
- }
2167
- allActions.push(mapActionGroup('base', function () {
2168
- return actionsMapBase$k(diff, oldObj, newObj, syncActionConfig);
2169
- }));
2170
- allActions.push(mapActionGroup('deliveries', function () {
2171
- return actionsMapDeliveries(diff, oldObj, newObj);
2172
- }));
2173
- allActions.push(mapActionGroup('parcels', function () {
2174
- return actionsMapParcels(diff, oldObj, newObj, deliveryHashMap);
2175
- }));
2176
- allActions.push(mapActionGroup('items', function () {
2177
- return actionsMapDeliveryItems(diff, oldObj, newObj, deliveryHashMap);
2178
- }));
2179
- allActions.push(flatten__default["default"](mapActionGroup('returnInfo', function () {
2180
- return actionsMapReturnsInfo(diff, oldObj, newObj);
2181
- })));
2182
- allActions.push(mapActionGroup('custom', function () {
2183
- return actionsMapCustom(diff, newObj, oldObj);
2184
- }));
2185
- return flatten__default["default"](allActions);
2186
- };
2187
- }
2188
- var orders = (function (actionGroupList, syncActionConfig) {
2189
- // actionGroupList contains information about which action groups
2190
- // are allowed or ignored
2191
-
2192
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2193
- // - action group name
2194
- // - callback function that should return a list of actions that correspond
2195
- // to the for the action group
2196
-
2197
- // this resulting function mapActionGroup will call the callback function
2198
- // for allowed action groups and return the return value of the callback
2199
- // It will return an empty array for ignored action groups
2200
- var mapActionGroup = createMapActionGroup(actionGroupList);
2201
- var doMapActions = createOrderMapActions(mapActionGroup, syncActionConfig);
2202
- var buildActions = createBuildActions(diff, doMapActions);
2203
- return {
2204
- buildActions: buildActions
2205
- };
2206
- });
2207
-
2208
- var baseActionsList$j = [{
2209
- action: 'setKey',
2210
- key: 'key'
2211
- }, {
2212
- action: 'setRecurringOrderState',
2213
- key: 'recurringOrderState'
2214
- }, {
2215
- action: 'transitionState',
2216
- key: 'state'
2217
- }];
2218
- function actionsMapBase$j(diff, oldObj, newObj) {
2219
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2220
- return buildBaseAttributesActions({
2221
- actions: baseActionsList$j,
2222
- diff: diff,
2223
- oldObj: oldObj,
2224
- newObj: newObj,
2225
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2226
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2227
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2228
- });
2229
- }
2230
-
2231
- function createRecurringOrdersMapActions(mapActionGroup, syncActionConfig) {
2232
- return function doMapActions(diff, newObj, oldObj) {
2233
- var allActions = [];
2234
- allActions.push(mapActionGroup('base', function () {
2235
- return actionsMapBase$j(diff, oldObj, newObj, syncActionConfig);
2236
- }));
2237
- allActions.push(mapActionGroup('custom', function () {
2238
- return actionsMapCustom(diff, newObj, oldObj);
2239
- }));
2240
- return flatten__default["default"](allActions);
2241
- };
2242
- }
2243
- var recurringOrders = (function (actionGroupList, syncActionConfig) {
2244
- var mapActionGroup = createMapActionGroup(actionGroupList);
2245
- var doMapActions = createRecurringOrdersMapActions(mapActionGroup, syncActionConfig);
2246
- var buildActions = createBuildActions(diff, doMapActions);
2247
- return {
2248
- buildActions: buildActions
2249
- };
2250
- });
2251
-
2252
- var baseActionsList$i = [{
2253
- action: 'changeIsActive',
2254
- key: 'isActive'
2255
- }, {
2256
- action: 'changeName',
2257
- key: 'name'
2258
- }, {
2259
- action: 'changePredicate',
2260
- key: 'predicate'
2261
- }, {
2262
- action: 'changeSortOrder',
2263
- key: 'sortOrder'
2264
- }, {
2265
- action: 'changeValue',
2266
- key: 'value'
2267
- }, {
2268
- action: 'setDescription',
2269
- key: 'description'
2270
- }, {
2271
- action: 'setValidFrom',
2272
- key: 'validFrom'
2273
- }, {
2274
- action: 'setValidUntil',
2275
- key: 'validUntil'
2276
- }, {
2277
- action: 'setKey',
2278
- key: 'key'
2279
- }];
2280
- function actionsMapBase$i(diff, oldObj, newObj) {
2281
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2282
- return buildBaseAttributesActions({
2283
- actions: baseActionsList$i,
2284
- diff: diff,
2285
- oldObj: oldObj,
2286
- newObj: newObj,
2287
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2288
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2289
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2290
- });
2291
- }
2292
-
2293
- var validityActions = ['setValidFrom', 'setValidUntil'];
2294
- var isValidityActions = function isValidityActions(actionName) {
2295
- return validityActions.includes(actionName);
2296
- };
2297
- function combineValidityActions() {
2298
- var actions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2299
- var _actions$filter = actions.filter(function (item) {
2300
- return isValidityActions(item.action);
2301
- }),
2302
- _actions$filter2 = _slicedToArray(_actions$filter, 2),
2303
- setValidFromAction = _actions$filter2[0],
2304
- setValidUntilAction = _actions$filter2[1];
2305
- if (setValidFromAction && setValidUntilAction) {
2306
- return [].concat(_toConsumableArray(actions.filter(function (item) {
2307
- return !isValidityActions(item.action);
2308
- })), [{
2309
- action: 'setValidFromAndUntil',
2310
- validFrom: setValidFromAction.validFrom,
2311
- validUntil: setValidUntilAction.validUntil
2312
- }]);
2313
- }
2314
- return actions;
2315
- }
2316
-
2317
- function createProductDiscountsMapActions(mapActionGroup, syncActionConfig) {
2318
- return function doMapActions(diff, newObj, oldObj) {
2319
- var allActions = [];
2320
- allActions.push(mapActionGroup('base', function () {
2321
- return actionsMapBase$i(diff, oldObj, newObj, syncActionConfig);
2322
- }));
2323
- return combineValidityActions(flatten__default["default"](allActions));
2324
- };
2325
- }
2326
- var productDiscounts = (function (actionGroupList) {
2327
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2328
- var mapActionGroup = createMapActionGroup(actionGroupList);
2329
- var doMapActions = createProductDiscountsMapActions(mapActionGroup, syncActionConfig);
2330
- var buildActions = createBuildActions(diff, doMapActions);
2331
- return {
2332
- buildActions: buildActions
2333
- };
2334
- });
2335
-
2336
- var baseActionsList$h = [{
2337
- action: 'changeIsActive',
2338
- key: 'isActive'
2339
- }, {
2340
- action: 'setName',
2341
- key: 'name'
2342
- }, {
2343
- action: 'setDescription',
2344
- key: 'description'
2345
- }, {
2346
- action: 'setKey',
2347
- key: 'key'
2348
- }, {
2349
- action: 'setCartPredicate',
2350
- key: 'cartPredicate'
2351
- }, {
2352
- action: 'setMaxApplications',
2353
- key: 'maxApplications'
2354
- }, {
2355
- action: 'setMaxApplicationsPerCustomer',
2356
- key: 'maxApplicationsPerCustomer'
2357
- }, {
2358
- action: 'changeCartDiscounts',
2359
- key: 'cartDiscounts'
2360
- }, {
2361
- action: 'setValidFrom',
2362
- key: 'validFrom'
2363
- }, {
2364
- action: 'setValidUntil',
2365
- key: 'validUntil'
2366
- }, {
2367
- action: 'changeGroups',
2368
- key: 'groups'
2369
- }];
2370
- function actionsMapBase$h(diff, oldObj, newObj) {
2371
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2372
- return buildBaseAttributesActions({
2373
- actions: baseActionsList$h,
2374
- diff: diff,
2375
- oldObj: oldObj,
2376
- newObj: newObj,
2377
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2378
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2379
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2380
- });
2381
- }
2382
-
2383
- function createDiscountCodesMapActions(mapActionGroup, syncActionConfig) {
2384
- return function doMapActions(diff, newObj, oldObj) {
2385
- var allActions = [];
2386
- allActions.push(mapActionGroup('base', function () {
2387
- return actionsMapBase$h(diff, oldObj, newObj, syncActionConfig);
2388
- }));
2389
- allActions.push(mapActionGroup('custom', function () {
2390
- return actionsMapCustom(diff, newObj, oldObj);
2391
- }));
2392
- return combineValidityActions(flatten__default["default"](allActions));
2393
- };
2394
- }
2395
- var discountCodes = (function (actionGroupList) {
2396
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2397
- // actionGroupList contains information about which action groups
2398
- // are allowed or ignored
2399
-
2400
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2401
- // - action group name
2402
- // - callback function that should return a list of actions that correspond
2403
- // to the for the action group
2404
-
2405
- // this resulting function mapActionGroup will call the callback function
2406
- // for allowed action groups and return the return value of the callback
2407
- // It will return an empty array for ignored action groups
2408
- var mapActionGroup = createMapActionGroup(actionGroupList);
2409
- var doMapActions = createDiscountCodesMapActions(mapActionGroup, syncActionConfig);
2410
- var buildActions = createBuildActions(diff, doMapActions);
2411
- return {
2412
- buildActions: buildActions
2413
- };
2414
- });
2415
-
2416
- var baseActionsList$g = [{
2417
- action: 'changeName',
2418
- key: 'name'
2419
- }, {
2420
- action: 'setKey',
2421
- key: 'key'
2422
- }];
2423
- function actionsMapBase$g(diff, oldObj, newObj) {
2424
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2425
- return buildBaseAttributesActions({
2426
- actions: baseActionsList$g,
2427
- diff: diff,
2428
- oldObj: oldObj,
2429
- newObj: newObj,
2430
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2431
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2432
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2433
- });
2434
- }
2435
-
2436
- function createCustomerGroupMapActions(mapActionGroup, syncActionConfig) {
2437
- return function doMapActions(diff, newObj, oldObj) {
2438
- var allActions = [];
2439
- allActions.push(mapActionGroup('base', function () {
2440
- return actionsMapBase$g(diff, oldObj, newObj, syncActionConfig);
2441
- }));
2442
- allActions.push(mapActionGroup('custom', function () {
2443
- return actionsMapCustom(diff, newObj, oldObj);
2444
- }));
2445
- return flatten__default["default"](allActions);
2446
- };
2447
- }
2448
- var customerGroup = (function (actionGroupList) {
2449
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2450
- var mapActionGroup = createMapActionGroup(actionGroupList);
2451
- var doMapActions = createCustomerGroupMapActions(mapActionGroup, syncActionConfig);
2452
- var buildActions = createBuildActions(diff, doMapActions);
2453
- return {
2454
- buildActions: buildActions
2455
- };
2456
- });
2457
-
2458
- var baseActionsList$f = [{
2459
- action: 'changeIsActive',
2460
- key: 'isActive'
2461
- }, {
2462
- action: 'changeName',
2463
- key: 'name'
2464
- }, {
2465
- action: 'changeCartPredicate',
2466
- key: 'cartPredicate'
2467
- }, {
2468
- action: 'changeSortOrder',
2469
- key: 'sortOrder'
2470
- }, {
2471
- action: 'changeValue',
2472
- key: 'value'
2473
- }, {
2474
- action: 'changeRequiresDiscountCode',
2475
- key: 'requiresDiscountCode'
2476
- }, {
2477
- action: 'changeTarget',
2478
- key: 'target'
2479
- }, {
2480
- action: 'setDescription',
2481
- key: 'description'
2482
- }, {
2483
- action: 'setValidFrom',
2484
- key: 'validFrom'
2485
- }, {
2486
- action: 'setValidUntil',
2487
- key: 'validUntil'
2488
- }, {
2489
- action: 'changeStackingMode',
2490
- key: 'stackingMode'
2491
- }, {
2492
- action: 'setKey',
2493
- key: 'key'
2494
- }];
2495
- function actionsMapBase$f(diff, oldObj, newObj) {
2496
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2497
- return buildBaseAttributesActions({
2498
- actions: baseActionsList$f,
2499
- diff: diff,
2500
- oldObj: oldObj,
2501
- newObj: newObj,
2502
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2503
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2504
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2505
- });
2506
- }
2507
-
2508
- function createCartDiscountsMapActions(mapActionGroup, syncActionConfig) {
2509
- return function doMapActions(diff, newObj, oldObj) {
2510
- var allActions = [];
2511
- allActions.push(mapActionGroup('base', function () {
2512
- return actionsMapBase$f(diff, oldObj, newObj, syncActionConfig);
2513
- }));
2514
- allActions.push(mapActionGroup('custom', function () {
2515
- return actionsMapCustom(diff, newObj, oldObj);
2516
- }));
2517
- return combineValidityActions(flatten__default["default"](allActions));
2518
- };
2519
- }
2520
- var cartDiscounts = (function (actionGroupList) {
2521
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2522
- var mapActionGroup = createMapActionGroup(actionGroupList);
2523
- var doMapActions = createCartDiscountsMapActions(mapActionGroup, syncActionConfig);
2524
- var buildActions = createBuildActions(diff, doMapActions);
2525
- return {
2526
- buildActions: buildActions
2527
- };
2528
- });
2529
-
2530
- var _excluded = ["__typename"];
2531
- function removeTypename(obj) {
2532
- obj.__typename;
2533
- var objWithoutTypename = _objectWithoutProperties(obj, _excluded);
2534
- return objWithoutTypename;
2535
- }
2536
-
2537
- var baseActionsList$e = [{
2538
- action: 'changeName',
2539
- key: 'name'
2540
- }, {
2541
- action: 'setKey',
2542
- key: 'key'
2543
- }, {
2544
- action: 'setDescription',
2545
- key: 'description'
2546
- }];
2547
- function actionsMapBase$e(diff, oldObj, newObj) {
2548
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2549
- return buildBaseAttributesActions({
2550
- actions: baseActionsList$e,
2551
- diff: diff,
2552
- oldObj: oldObj,
2553
- newObj: newObj,
2554
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2555
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2556
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2557
- });
2558
- }
2559
- function actionsMapRates(diff, oldObj, newObj) {
2560
- var handler = createBuildArrayActions('rates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newObject) {
2561
- return {
2562
- action: 'addTaxRate',
2563
- taxRate: newObject
2564
- };
2565
- }), REMOVE_ACTIONS, function (objectToRemove) {
2566
- return {
2567
- action: 'removeTaxRate',
2568
- taxRateId: objectToRemove.id
2569
- };
2570
- }), CHANGE_ACTIONS, function (oldObject, updatedObject) {
2571
- var _oldObject$subRates;
2572
- // filter out taxRates that were not changed
2573
- // so the API doesn't return it with a different id
2574
- // we need to remove __typename from the object to compare them
2575
- var taxCategoryWithoutTypeName = removeTypename(oldObject);
2576
- var oldObjectSubRatesWithoutTypename = (_oldObject$subRates = oldObject.subRates) === null || _oldObject$subRates === void 0 ? void 0 : _oldObject$subRates.map(removeTypename);
2577
- var oldObjectWithoutTypename = _objectSpread2(_objectSpread2({}, taxCategoryWithoutTypeName), {}, {
2578
- subRates: oldObjectSubRatesWithoutTypename
2579
- });
2580
- if (fastEquals.deepEqual(oldObjectWithoutTypename, updatedObject)) return null;
2581
- return {
2582
- action: 'replaceTaxRate',
2583
- taxRateId: oldObject.id === updatedObject.id ? oldObject.id : updatedObject.id,
2584
- taxRate: updatedObject
2585
- };
2586
- }));
2587
- return handler(diff, oldObj, newObj);
2588
- }
2589
-
2590
- function createTaxCategoriesMapActions(mapActionGroup, syncActionConfig) {
2591
- return function doMapActions(diff, newObj, oldObj) {
2592
- var allActions = [];
2593
- allActions.push(mapActionGroup('base', function () {
2594
- return actionsMapBase$e(diff, oldObj, newObj, syncActionConfig);
2595
- }));
2596
- allActions.push(mapActionGroup('rates', function () {
2597
- return actionsMapRates(diff, oldObj, newObj);
2598
- }));
2599
- return flatten__default["default"](allActions);
2600
- };
2601
- }
2602
- var taxCategories = (function (actionGroupList, syncActionConfig) {
2603
- // config contains information about which action groups
2604
- // are allowed or ignored
2605
-
2606
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2607
- // - action group name
2608
- // - callback function that should return a list of actions that correspond
2609
- // to the for the action group
2610
-
2611
- // this resulting function mapActionGroup will call the callback function
2612
- // for allowed action groups and return the return value of the callback
2613
- // It will return an empty array for ignored action groups
2614
- var mapActionGroup = createMapActionGroup(actionGroupList);
2615
- var doMapActions = createTaxCategoriesMapActions(mapActionGroup, syncActionConfig);
2616
- var buildActions = createBuildActions(diff, doMapActions);
2617
- return {
2618
- buildActions: buildActions
2619
- };
2620
- });
2621
-
2622
- var baseActionsList$d = [{
2623
- action: 'changeName',
2624
- key: 'name'
2625
- }, {
2626
- action: 'setDescription',
2627
- key: 'description'
2628
- }, {
2629
- action: 'setKey',
2630
- key: 'key'
2631
- }];
2632
- var hasLocation = function hasLocation(locations, otherLocation) {
2633
- return locations.some(function (location) {
2634
- return location.country === otherLocation.country;
2635
- });
2636
- };
2637
- function actionsMapBase$d(diff, oldObj, newObj) {
2638
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2639
- return buildBaseAttributesActions({
2640
- actions: baseActionsList$d,
2641
- diff: diff,
2642
- oldObj: oldObj,
2643
- newObj: newObj,
2644
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2645
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2646
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2647
- });
2648
- }
2649
- function actionsMapLocations(diff, oldObj, newObj) {
2650
- var handler = createBuildArrayActions('locations', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newLocation) {
2651
- return {
2652
- action: 'addLocation',
2653
- location: newLocation
2654
- };
2655
- }), REMOVE_ACTIONS, function (oldLocation) {
2656
- return (
2657
- // We only add the action if the location is not included in the new object.
2658
- !hasLocation(newObj.locations, oldLocation) ? {
2659
- action: 'removeLocation',
2660
- location: oldLocation
2661
- } : null
2662
- );
2663
- }), CHANGE_ACTIONS, function (oldLocation, newLocation) {
2664
- var result = [];
2665
-
2666
- // We only remove the location in case that the oldLocation is not
2667
- // included in the new object
2668
- if (!hasLocation(newObj.locations, oldLocation)) result.push({
2669
- action: 'removeLocation',
2670
- location: oldLocation
2671
- });
2672
-
2673
- // We only add the location in case that the newLocation was not
2674
- // included in the old object
2675
- if (!hasLocation(oldObj.locations, newLocation)) result.push({
2676
- action: 'addLocation',
2677
- location: newLocation
2678
- });
2679
- return result;
2680
- }));
2681
- return handler(diff, oldObj, newObj);
2682
- }
2683
-
2684
- function createZonesMapActions(mapActionGroup, syncActionConfig) {
2685
- return function doMapActions(diff, newObj, oldObj) {
2686
- var allActions = [];
2687
- allActions.push(mapActionGroup('base', function () {
2688
- return actionsMapBase$d(diff, oldObj, newObj, syncActionConfig);
2689
- }));
2690
- allActions.push(flatten__default["default"](mapActionGroup('locations', function () {
2691
- return actionsMapLocations(diff, oldObj, newObj);
2692
- })));
2693
- return flatten__default["default"](allActions);
2694
- };
2695
- }
2696
- var zones = (function (actionGroupList, syncActionConfig) {
2697
- // config contains information about which action groups
2698
- // are allowed or ignored
2699
-
2700
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2701
- // - action group name
2702
- // - callback function that should return a list of actions that correspond
2703
- // to the for the action group
2704
-
2705
- // this resulting function mapActionGroup will call the callback function
2706
- // for allowed action groups and return the return value of the callback
2707
- // It will return an empty array for ignored action groups
2708
- var mapActionGroup = createMapActionGroup(actionGroupList);
2709
- var doMapActions = createZonesMapActions(mapActionGroup, syncActionConfig);
2710
- var buildActions = createBuildActions(diff, doMapActions);
2711
- return {
2712
- buildActions: buildActions
2713
- };
2714
- });
2715
-
2716
- var baseActionsList$c = [{
2717
- action: 'setKey',
2718
- key: 'key'
2719
- }, {
2720
- action: 'changeName',
2721
- key: 'name'
2722
- }, {
2723
- action: 'setLocalizedName',
2724
- key: 'localizedName'
2725
- }, {
2726
- action: 'setDescription',
2727
- key: 'description'
2728
- }, {
2729
- action: 'setLocalizedDescription',
2730
- key: 'localizedDescription'
2731
- }, {
2732
- action: 'changeIsDefault',
2733
- key: 'isDefault'
2734
- }, {
2735
- action: 'setPredicate',
2736
- key: 'predicate'
2737
- }, {
2738
- action: 'changeTaxCategory',
2739
- key: 'taxCategory'
2740
- }, {
2741
- action: 'changeActive',
2742
- key: 'active'
2743
- }];
2744
- function actionsMapBase$c(diff, oldObj, newObj) {
2745
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2746
- return buildBaseAttributesActions({
2747
- actions: baseActionsList$c,
2748
- diff: diff,
2749
- oldObj: oldObj,
2750
- newObj: newObj,
2751
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2752
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
2753
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
2754
- });
2755
- }
2756
- var addShippingRates = function addShippingRates(newZoneRate) {
2757
- return newZoneRate.shippingRates ? newZoneRate.shippingRates.map(function (shippingRate) {
2758
- return {
2759
- action: 'addShippingRate',
2760
- zone: newZoneRate.zone,
2761
- shippingRate: shippingRate
2762
- };
2763
- }) : [];
2764
- };
2765
- function actionsMapZoneRatesShippingRates(diff, oldObj, newObj) {
2766
- var handler = createBuildArrayActions('shippingRates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newShippingRate) {
2767
- return {
2768
- action: 'addShippingRate',
2769
- zone: newObj.zone,
2770
- shippingRate: newShippingRate
2771
- };
2772
- }), REMOVE_ACTIONS, function (oldShippingRate) {
2773
- return {
2774
- action: 'removeShippingRate',
2775
- zone: oldObj.zone,
2776
- shippingRate: oldShippingRate
2777
- };
2778
- }), CHANGE_ACTIONS, function (oldShippingRate, newShippingRate) {
2779
- return [{
2780
- action: 'removeShippingRate',
2781
- zone: oldObj.zone,
2782
- shippingRate: oldShippingRate
2783
- }, {
2784
- action: 'addShippingRate',
2785
- zone: newObj.zone,
2786
- shippingRate: newShippingRate
2787
- }];
2788
- }));
2789
- return handler(diff, oldObj, newObj);
2790
- }
2791
- function actionsMapZoneRates(diff, oldObj, newObj) {
2792
- var handler = createBuildArrayActions('zoneRates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newZoneRate) {
2793
- return [{
2794
- action: 'addZone',
2795
- zone: newZoneRate.zone
2796
- }].concat(_toConsumableArray(addShippingRates(newZoneRate)));
2797
- }), REMOVE_ACTIONS, function (oldZoneRate) {
2798
- return {
2799
- action: 'removeZone',
2800
- zone: oldZoneRate.zone
2801
- };
2802
- }), CHANGE_ACTIONS, function (oldZoneRate, newZoneRate) {
2803
- var hasZoneActions = false;
2804
- var shippingRateActions = Object.keys(diff.zoneRates).reduce(function (actions, key) {
2805
- if (diff.zoneRates[key].zone) hasZoneActions = true;
2806
- if (diff.zoneRates[key].shippingRates) return [].concat(_toConsumableArray(actions), _toConsumableArray(actionsMapZoneRatesShippingRates(diff.zoneRates[key], oldZoneRate, newZoneRate)));
2807
- return actions;
2808
- }, []);
2809
- return flatten__default["default"](hasZoneActions ? [].concat(_toConsumableArray(shippingRateActions), [{
2810
- action: 'removeZone',
2811
- zone: oldZoneRate.zone
2812
- }, {
2813
- action: 'addZone',
2814
- zone: newZoneRate.zone
2815
- }]) : shippingRateActions);
2816
- }));
2817
- return handler(diff, oldObj, newObj);
2818
- }
2819
-
2820
- function createShippingMethodsMapActions(mapActionGroup, syncActionConfig) {
2821
- return function doMapActions(diff, newObj, oldObj) {
2822
- var allActions = [];
2823
- allActions.push(mapActionGroup('base', function () {
2824
- return actionsMapBase$c(diff, oldObj, newObj, syncActionConfig);
2825
- }));
2826
- allActions.push(flatten__default["default"](mapActionGroup('zoneRates', function () {
2827
- return actionsMapZoneRates(diff, oldObj, newObj);
2828
- })));
2829
- allActions.push(mapActionGroup('custom', function () {
2830
- return actionsMapCustom(diff, newObj, oldObj);
2831
- }));
2832
- return flatten__default["default"](allActions);
2833
- };
2834
- }
2835
- var shippingMethods = (function (actionGroupList, syncActionConfig) {
2836
- // actionGroupList contains information about which action groups
2837
- // are allowed or ignored
2838
-
2839
- // createMapActionGroup returns function 'mapActionGroup' that takes params:
2840
- // - action group name
2841
- // - callback function that should return a list of actions that correspond
2842
- // to the for the action group
2843
-
2844
- // this resulting function mapActionGroup will call the callback function
2845
- // for allowed action groups and return the return value of the callback
2846
- // It will return an empty array for ignored action groups
2847
- var mapActionGroup = createMapActionGroup(actionGroupList);
2848
- var doMapActions = createShippingMethodsMapActions(mapActionGroup, syncActionConfig);
2849
- var buildActions = createBuildActions(diff, doMapActions);
2850
- return {
2851
- buildActions: buildActions
2852
- };
2853
- });
2854
-
2855
- var baseActionsList$b = [{
2856
- action: 'changeName',
2857
- key: 'name'
2858
- }, {
2859
- action: 'setKey',
2860
- key: 'key'
2861
- }, {
2862
- action: 'changeDescription',
2863
- key: 'description'
2864
- }];
2865
- function actionsMapBase$b(diff, previous, next) {
2866
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
2867
- // when `diff` is undefined, then the underlying `buildActions` has returned any diff
2868
- // which given in product-types would mean that `buildActions` has run with `nestedValuesChanges` applied
2869
- // To allow continuation of update-action generation, we let this pass..
2870
- if (!diff) return [];
2871
- return buildBaseAttributesActions({
2872
- diff: diff,
2873
- actions: baseActionsList$b,
2874
- oldObj: previous,
2875
- newObj: next,
2876
- shouldOmitEmptyString: config.shouldOmitEmptyString,
2877
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
2878
- });
2879
- }
2880
-
2881
- // this is nearly similar to `buildBaseAttributesActions`, however with a significant difference.
2882
- // `buildBasAttributesActions` generates update-actions with help of `diff`,
2883
- // which is an object consisting of flags which indicates different operations.
2884
- // `generateBaseFieldsUpdateActions` only generate based on `previous` and `next`.
2885
- var generateBaseFieldsUpdateActions = function generateBaseFieldsUpdateActions(previous, next, actionDefinition) {
2886
- var isEmpty = createIsEmptyValue([undefined, null, '']);
2887
- return Object.entries(actionDefinition).reduce(function (nextUpdateActions, _ref) {
2888
- var _ref2 = _slicedToArray(_ref, 2),
2889
- field = _ref2[0],
2890
- actionFieldDefinition = _ref2[1];
2891
- if (isEmpty(previous[field]) && isEmpty(next[field])) return nextUpdateActions;
2892
- if (!isEmpty(previous[field]) && isEmpty(next[field])) return [].concat(_toConsumableArray(nextUpdateActions), [actionFieldDefinition]);
2893
- if (!fastEquals.deepEqual(previous[field], next[field])) {
2894
- switch (field) {
2895
- // BEWARE that this is generates update-action only for key of enum attribute value,
2896
- // not key of product type. If we need to re-factor `product-types` sync actions to use
2897
- // `generateBaseFieldsUpdateActions`, we need to extract the following logic so we could
2898
- // cover both entity types.
2899
- case 'key':
2900
- return [].concat(_toConsumableArray(nextUpdateActions), [
2901
- // Another option is to have explicit name of `field` e.g `enumKey`, which we could use to
2902
- // generate appropriate update-action for respective entity type.
2903
- // An outline of this on the top of my head:
2904
- // ```js
2905
- // case 'enumKey':
2906
- // return [
2907
- // ...nextUpdateActions,
2908
- // {
2909
- // action: actionFieldDefinition.action,
2910
- // attributeName: actionFieldDefinition.attributeName,
2911
- // key: previous.key,
2912
- // newKey: next.key,
2913
- // },
2914
- // ]
2915
- // case 'productTypeKey':
2916
- // return [
2917
- // ...nextUpdateActions,
2918
- // {
2919
- // action: actionFieldDefinition.action,
2920
- // key: next.key
2921
- // },
2922
- // ]
2923
- // ```
2924
- {
2925
- action: actionFieldDefinition.action,
2926
- attributeName: actionFieldDefinition.attributeName,
2927
- key: previous[field],
2928
- newKey: next[field]
2929
- }]);
2930
- // attribute
2931
- case 'attributeConstraint':
2932
- case 'inputHint':
2933
- return [].concat(_toConsumableArray(nextUpdateActions), [{
2934
- action: actionFieldDefinition.action,
2935
- attributeName: actionFieldDefinition.attributeName,
2936
- newValue: next[field]
2937
- }]);
2938
- default:
2939
- return [].concat(_toConsumableArray(nextUpdateActions), [_defineProperty({
2940
- action: actionFieldDefinition.action,
2941
- attributeName: actionFieldDefinition.attributeName
2942
- }, field, next[field])]);
2943
- }
2944
- }
2945
- return nextUpdateActions;
2946
- }, []);
2947
- };
2948
- var generateUpdateActionsForAttributeDefinitions = function generateUpdateActionsForAttributeDefinitions() {
2949
- var attributeDefinitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2950
- var removedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2951
- return attributeDefinition.previous && !attributeDefinition.next;
2952
- });
2953
- var updatedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2954
- return attributeDefinition.previous && attributeDefinition.next;
2955
- });
2956
- var addedAttributeDefinitions = attributeDefinitions.filter(function (attributeDefinition) {
2957
- return !attributeDefinition.previous && attributeDefinition.next;
2958
- });
2959
- return [].concat(_toConsumableArray(removedAttributeDefinitions.map(function (attributeDef) {
2960
- return {
2961
- action: 'removeAttributeDefinition',
2962
- name: attributeDef.previous.name
2963
- };
2964
- })), _toConsumableArray(flatten__default["default"](updatedAttributeDefinitions.map(function (updatedAttributeDefinition) {
2965
- return generateBaseFieldsUpdateActions(updatedAttributeDefinition.previous, updatedAttributeDefinition.next, {
2966
- label: {
2967
- action: 'changeLabel',
2968
- attributeName: updatedAttributeDefinition.previous.name
2969
- },
2970
- inputTip: {
2971
- action: 'setInputTip',
2972
- attributeName: updatedAttributeDefinition.previous.name
2973
- },
2974
- inputHint: {
2975
- action: 'changeInputHint',
2976
- attributeName: updatedAttributeDefinition.previous.name
2977
- },
2978
- isSearchable: {
2979
- action: 'changeIsSearchable',
2980
- attributeName: updatedAttributeDefinition.previous.name
2981
- },
2982
- attributeConstraint: {
2983
- action: 'changeAttributeConstraint',
2984
- attributeName: updatedAttributeDefinition.previous.name
2985
- }
2986
- });
2987
- }))), _toConsumableArray(addedAttributeDefinitions.map(function (attributeDef) {
2988
- return {
2989
- action: 'addAttributeDefinition',
2990
- attribute: attributeDef.next
2991
- };
2992
- })));
2993
- };
2994
- var generateUpdateActionsForAttributeEnumValues = function generateUpdateActionsForAttributeEnumValues() {
2995
- var attributeEnumValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2996
- var removedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
2997
- return attributeEnumValue.previous && !attributeEnumValue.next;
2998
- });
2999
- var updatedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
3000
- return attributeEnumValue.next && attributeEnumValue.previous;
3001
- });
3002
- var addedAttributeEnumValues = attributeEnumValues.filter(function (attributeEnumValue) {
3003
- return !attributeEnumValue.previous && attributeEnumValue.next;
3004
- });
3005
- return [].concat(_toConsumableArray(Object.values(removedAttributeEnumValues.reduce(function (nextEnumUpdateActions, removedAttributeEnumValue) {
3006
- var removedAttributeEnumValueOfSameAttributeName = nextEnumUpdateActions[removedAttributeEnumValue.hint.attributeName] || {
3007
- keys: [],
3008
- attributeName: removedAttributeEnumValue.hint.attributeName,
3009
- action: 'removeEnumValues'
3010
- };
3011
- return _objectSpread2(_objectSpread2({}, nextEnumUpdateActions), {}, _defineProperty({}, removedAttributeEnumValue.hint.attributeName, _objectSpread2(_objectSpread2({}, removedAttributeEnumValueOfSameAttributeName), {}, {
3012
- keys: [].concat(_toConsumableArray(removedAttributeEnumValueOfSameAttributeName.keys), [removedAttributeEnumValue.previous.key])
3013
- })));
3014
- }, {}))), _toConsumableArray(flatten__default["default"](updatedAttributeEnumValues.map(function (updatedAttributeEnumValue) {
3015
- var updateActions = generateBaseFieldsUpdateActions(updatedAttributeEnumValue.previous, updatedAttributeEnumValue.next, {
3016
- key: {
3017
- action: 'changeEnumKey',
3018
- attributeName: updatedAttributeEnumValue.hint.attributeName
3019
- }
3020
- });
3021
- if (!fastEquals.deepEqual(updatedAttributeEnumValue.previous.label, updatedAttributeEnumValue.next.label)) {
3022
- if (updatedAttributeEnumValue.hint.isLocalized) {
3023
- return [].concat(_toConsumableArray(updateActions), [{
3024
- action: 'changeLocalizedEnumValueLabel',
3025
- attributeName: updatedAttributeEnumValue.hint.attributeName,
3026
- newValue: updatedAttributeEnumValue.next
3027
- }]);
3028
- }
3029
- return [].concat(_toConsumableArray(updateActions), [{
3030
- action: 'changePlainEnumValueLabel',
3031
- attributeName: updatedAttributeEnumValue.hint.attributeName,
3032
- newValue: updatedAttributeEnumValue.next
3033
- }]);
3034
- }
3035
- return updateActions;
3036
- }))), _toConsumableArray(addedAttributeEnumValues.map(function (addedAttributeEnumValue) {
3037
- return {
3038
- action: addedAttributeEnumValue.hint.isLocalized ? 'addLocalizedEnumValue' : 'addPlainEnumValue',
3039
- attributeName: addedAttributeEnumValue.hint.attributeName,
3040
- value: addedAttributeEnumValue.next
3041
- };
3042
- })));
3043
- };
3044
- var generateChangeAttributeOrderAction = function generateChangeAttributeOrderAction() {
3045
- var attrsOld = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
3046
- var attrsNew = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
3047
- var updateActions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
3048
- if (!attrsOld.length || !attrsNew.length) return null;
3049
- var newAttributesOrder = attrsNew.map(function (attribute) {
3050
- return attribute.name;
3051
- });
3052
- var removedAttributeNames = updateActions.filter(function (action) {
3053
- return action.action === 'removeAttributeDefinition';
3054
- }).map(function (action) {
3055
- return action.name;
3056
- });
3057
- var addedAttributeNames = updateActions.filter(function (action) {
3058
- return action.action === 'addAttributeDefinition';
3059
- }).map(function (action) {
3060
- return action.attribute.name;
3061
- });
3062
-
3063
- // changeAttributeOrder action will be sent to CTP API as the last action so we have to
3064
- // calculate how the productType will look like after adding/removing attributes
3065
- var patchedOldAttributesOrder = attrsOld.map(function (attribute) {
3066
- return attribute.name;
3067
- }).filter(function (name) {
3068
- return !removedAttributeNames.includes(name);
3069
- }).concat(addedAttributeNames);
3070
- if (newAttributesOrder.join(',') !== patchedOldAttributesOrder.join(',')) return {
3071
- action: 'changeAttributeOrderByName',
3072
- attributeNames: newAttributesOrder
3073
- };
3074
- return null;
3075
- };
3076
- var actionsMapForHints = function actionsMapForHints() {
3077
- var nestedValuesChanges = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
3078
- var ptOld = arguments.length > 1 ? arguments[1] : undefined;
3079
- var ptNew = arguments.length > 2 ? arguments[2] : undefined;
3080
- var updateActions = [].concat(_toConsumableArray(generateUpdateActionsForAttributeDefinitions(nestedValuesChanges.attributeDefinitions)), _toConsumableArray(generateUpdateActionsForAttributeEnumValues(nestedValuesChanges.attributeEnumValues)));
3081
- var changeAttributeOrderAction = generateChangeAttributeOrderAction(ptOld.attributes, ptNew.attributes, updateActions);
3082
- if (changeAttributeOrderAction) updateActions.push(changeAttributeOrderAction);
3083
- return updateActions;
3084
- };
3085
-
3086
- function createProductTypeMapActions(mapActionGroup, syncActionConfig) {
3087
- return function doMapActions(diff, next, previous, options) {
3088
- return flatten__default["default"]([
3089
- // we support only base fields for the product type,
3090
- // for attributes, applying hints would be recommended
3091
- mapActionGroup('base', function () {
3092
- return actionsMapBase$b(diff, previous, next, syncActionConfig);
3093
- }), actionsMapForHints(options.nestedValuesChanges, previous, next)]);
3094
- };
3095
- }
3096
- var productTypes = (function (actionGroupList, syncActionConfig) {
3097
- var mapActionGroup = createMapActionGroup(actionGroupList);
3098
- var doMapActions = createProductTypeMapActions(mapActionGroup, syncActionConfig);
3099
- var onBeforeApplyingDiff = null;
3100
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff, {
3101
- withHints: true
3102
- });
3103
- return {
3104
- buildActions: buildActions
3105
- };
3106
- });
3107
-
3108
- var baseActionsList$a = [{
3109
- action: 'changeKey',
3110
- key: 'key'
3111
- }, {
3112
- action: 'setName',
3113
- key: 'name'
3114
- }, {
3115
- action: 'setDescription',
3116
- key: 'description'
3117
- }, {
3118
- action: 'changeType',
3119
- key: 'type'
3120
- }, {
3121
- action: 'changeInitial',
3122
- key: 'initial'
3123
- }, {
3124
- action: 'setTransitions',
3125
- key: 'transitions'
3126
- }];
3127
- function actionsMapBase$a(diff, oldObj, newObj) {
3128
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3129
- return buildBaseAttributesActions({
3130
- actions: baseActionsList$a,
3131
- diff: diff,
3132
- oldObj: oldObj,
3133
- newObj: newObj,
3134
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3135
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3136
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3137
- });
3138
- }
3139
- function actionsMapRoles(diff, oldObj, newObj) {
3140
- var buildArrayActions = createBuildArrayActions('roles', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newRole) {
3141
- return {
3142
- action: 'addRoles',
3143
- roles: newRole
3144
- };
3145
- }), REMOVE_ACTIONS, function (oldRole) {
3146
- return {
3147
- action: 'removeRoles',
3148
- roles: oldRole
3149
- };
3150
- }));
3151
- return buildArrayActions(diff, oldObj, newObj);
3152
- }
3153
-
3154
- // This function groups `addRoles` and `removeRoles` actions to one array
3155
- function groupRoleActions(_ref) {
3156
- var _ref2 = _slicedToArray(_ref, 1),
3157
- actions = _ref2[0];
3158
- var addActionRoles = [];
3159
- var removeActionRoles = [];
3160
- actions.forEach(function (action) {
3161
- if (action.action === 'removeRoles') removeActionRoles.push(action.roles);
3162
- if (action.action === 'addRoles') addActionRoles.push(action.roles);
3163
- });
3164
- return [{
3165
- action: 'removeRoles',
3166
- roles: removeActionRoles
3167
- }, {
3168
- action: 'addRoles',
3169
- roles: addActionRoles
3170
- }].filter(function (action) {
3171
- return action.roles.length;
3172
- });
3173
- }
3174
- function createStatesMapActions(mapActionGroup, syncActionConfig) {
3175
- return function doMapActions(diff, newObj, oldObj) {
3176
- var baseActions = [];
3177
- var roleActions = [];
3178
- baseActions.push(mapActionGroup('base', function () {
3179
- return actionsMapBase$a(diff, oldObj, newObj, syncActionConfig);
3180
- }));
3181
- roleActions.push(mapActionGroup('roles', function () {
3182
- return actionsMapRoles(diff, oldObj, newObj);
3183
- }));
3184
- return flatten__default["default"]([].concat(baseActions, _toConsumableArray(groupRoleActions(roleActions))));
3185
- };
3186
- }
3187
- var states = (function (actionGroupList, syncActionConfig) {
3188
- var mapActionGroup = createMapActionGroup(actionGroupList);
3189
- var doMapActions = createStatesMapActions(mapActionGroup, syncActionConfig);
3190
- var buildActions = createBuildActions(diff, doMapActions);
3191
- return {
3192
- buildActions: buildActions
3193
- };
3194
- });
3195
-
3196
- var baseActionsList$9 = [{
3197
- action: 'changeKey',
3198
- key: 'key'
3199
- }, {
3200
- action: 'changeName',
3201
- key: 'name'
3202
- }, {
3203
- action: 'changeDescription',
3204
- key: 'description'
3205
- }, {
3206
- action: 'setAddress',
3207
- key: 'address'
3208
- }, {
3209
- action: 'setGeoLocation',
3210
- key: 'geoLocation'
3211
- }, {
3212
- action: 'setRoles',
3213
- key: 'roles'
3214
- }];
3215
- function actionsMapBase$9(diff, oldObj, newObj) {
3216
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3217
- return buildBaseAttributesActions({
3218
- actions: baseActionsList$9,
3219
- diff: diff,
3220
- oldObj: oldObj,
3221
- newObj: newObj,
3222
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3223
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3224
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3225
- });
3226
- }
3227
-
3228
- function createChannelsMapActions$1(mapActionGroup, syncActionConfig) {
3229
- return function doMapActions(diff, newObj, oldObj) {
3230
- var allActions = [];
3231
- allActions.push(mapActionGroup('base', function () {
3232
- return actionsMapBase$9(diff, oldObj, newObj, syncActionConfig);
3233
- }));
3234
- allActions.push(mapActionGroup('custom', function () {
3235
- return actionsMapCustom(diff, newObj, oldObj);
3236
- }));
3237
- return flatten__default["default"](allActions);
3238
- };
3239
- }
3240
- var channels = (function (actionGroupList) {
3241
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3242
- var mapActionGroup = createMapActionGroup(actionGroupList);
3243
- var doMapActions = createChannelsMapActions$1(mapActionGroup, syncActionConfig);
3244
- var buildActions = createBuildActions(diff, doMapActions);
3245
- return {
3246
- buildActions: buildActions
3247
- };
3248
- });
3249
-
3250
- var REGEX_NUMBER = new RegExp(/^\d+$/);
3251
- var REGEX_UNDERSCORE_NUMBER = new RegExp(/^_\d+$/);
3252
- var getIsChangedOperation = function getIsChangedOperation(key) {
3253
- return REGEX_NUMBER.test(key);
3254
- };
3255
- var getIsRemovedOperation = function getIsRemovedOperation(key) {
3256
- return REGEX_UNDERSCORE_NUMBER.test(key);
3257
- };
3258
- var baseActionsList$8 = [{
3259
- action: 'changeKey',
3260
- key: 'key'
3261
- }, {
3262
- action: 'changeName',
3263
- key: 'name'
3264
- }, {
3265
- action: 'setDescription',
3266
- key: 'description'
3267
- }];
3268
- function actionsMapBase$8(diff, oldObj, newObj) {
3269
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3270
- return buildBaseAttributesActions({
3271
- actions: baseActionsList$8,
3272
- diff: diff,
3273
- oldObj: oldObj,
3274
- newObj: newObj,
3275
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3276
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3277
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3278
- });
3279
- }
3280
- function actionsMapEnums(fieldName, attributeType, attributeDiff, previous, next) {
3281
- var addEnumActionName = attributeType === 'Enum' ? 'addEnumValue' : 'addLocalizedEnumValue';
3282
- var changeEnumValueLabelActionName = attributeType === 'Enum' ? 'changeEnumValueLabel' : 'changeLocalizedEnumValueLabel';
3283
- var changeEnumOrderActionName = attributeType === 'Enum' ? 'changeEnumValueOrder' : 'changeLocalizedEnumValueOrder';
3284
- var buildArrayActions = createBuildArrayActions('values', _defineProperty(_defineProperty({}, ADD_ACTIONS, function (newEnum) {
3285
- return {
3286
- fieldName: fieldName,
3287
- action: addEnumActionName,
3288
- value: newEnum
3289
- };
3290
- }), CHANGE_ACTIONS, function (oldEnum, newEnum) {
3291
- var oldEnumInNext = next.values.find(function (nextEnum) {
3292
- return nextEnum.key === oldEnum.key;
3293
- });
3294
-
3295
- // These `changeActions` would impose a nested structure among
3296
- // the accumulated `updateActions` generated by `buildArrayActions`
3297
- // In the end; we have to flatten the structure before we pass it back
3298
- // to the client.
3299
- var changeActions = [];
3300
- if (oldEnumInNext) {
3301
- // If the enum value is changed, we need to change the order first
3302
- var isKeyChanged = oldEnum.key !== newEnum.key;
3303
-
3304
- // check if the label is changed
3305
- var foundPreviousEnum = previous.values.find(function (previousEnum) {
3306
- return previousEnum.key === newEnum.key;
3307
- });
3308
- var isLabelEqual = isEqual__default["default"](foundPreviousEnum.label, newEnum.label);
3309
- if (isKeyChanged) {
3310
- // these actions is then flatten in the end
3311
- changeActions.push({
3312
- fieldName: fieldName,
3313
- action: changeEnumOrderActionName,
3314
- value: newEnum
3315
- });
3316
- }
3317
- if (!isLabelEqual) {
3318
- changeActions.push({
3319
- fieldName: fieldName,
3320
- action: changeEnumValueLabelActionName,
3321
- value: newEnum
3322
- });
3323
- }
3324
- } else {
3325
- changeActions.push({
3326
- fieldName: fieldName,
3327
- action: addEnumActionName,
3328
- value: newEnum
3329
- });
3330
- }
3331
- return changeActions;
3332
- }));
3333
- var actions = [];
3334
- // following lists are necessary to ensure that when we change the
3335
- // order of enumValues, we generate one updateAction instead of one at a time.
3336
- var newEnumValuesOrder = [];
3337
- flatten__default["default"](buildArrayActions(attributeDiff, previous, next)).forEach(function (updateAction) {
3338
- if (updateAction.action === changeEnumOrderActionName) {
3339
- newEnumValuesOrder = next.values.map(function (enumValue) {
3340
- return enumValue.key;
3341
- });
3342
- } else actions.push(updateAction);
3343
- });
3344
- return [].concat(actions, _toConsumableArray(newEnumValuesOrder.length > 0 ? [{
3345
- fieldName: fieldName,
3346
- action: changeEnumOrderActionName,
3347
- keys: newEnumValuesOrder
3348
- }] : []));
3349
- }
3350
- function actionsMapFieldDefinitions(fieldDefinitionsDiff, previous, next, diffPaths) {
3351
- var actions = [];
3352
- forEach__default["default"](fieldDefinitionsDiff, function (diffValue, diffKey) {
3353
- var extractedPairs = extractMatchingPairs(diffPaths, diffKey, previous, next);
3354
- if (getIsChangedOperation(diffKey)) {
3355
- var _diffValue$type;
3356
- if (Array.isArray(diffValue)) {
3357
- var deltaValue = getDeltaValue(diffValue);
3358
- if (deltaValue.name) {
3359
- actions.push({
3360
- action: 'addFieldDefinition',
3361
- fieldDefinition: deltaValue
3362
- });
3363
- }
3364
- } else if (diffValue.label) {
3365
- actions.push({
3366
- action: 'changeLabel',
3367
- label: extractedPairs.newObj.label,
3368
- fieldName: extractedPairs.oldObj.name
3369
- });
3370
- } else if (diffValue.inputHint) {
3371
- actions.push({
3372
- action: 'changeInputHint',
3373
- inputHint: extractedPairs.newObj.inputHint,
3374
- fieldName: extractedPairs.oldObj.name
3375
- });
3376
- } else if (diffValue !== null && diffValue !== void 0 && (_diffValue$type = diffValue.type) !== null && _diffValue$type !== void 0 && _diffValue$type.values) {
3377
- actions.push.apply(actions, _toConsumableArray(actionsMapEnums(extractedPairs.oldObj.name, extractedPairs.oldObj.type.name, diffValue.type, extractedPairs.oldObj.type, extractedPairs.newObj.type)));
3378
- }
3379
- } else if (getIsRemovedOperation(diffKey)) {
3380
- if (Array.isArray(diffValue)) {
3381
- if (diffValue.length === 3 && diffValue[2] === 3) {
3382
- actions.push({
3383
- action: 'changeFieldDefinitionOrder',
3384
- fieldNames: next.map(function (n) {
3385
- return n.name;
3386
- })
3387
- });
3388
- } else {
3389
- var _deltaValue = getDeltaValue(diffValue);
3390
- if (_deltaValue === undefined && diffValue[0].name) actions.push({
3391
- action: 'removeFieldDefinition',
3392
- fieldName: diffValue[0].name
3393
- });
3394
- }
3395
- }
3396
- }
3397
- });
3398
-
3399
- // Make sure to execute removeActions before creating new ones
3400
- // in order to prevent any eventual removal of `addAction`.
3401
- // List of `removeActions` can be found here
3402
- // https://docs.commercetools.com/api/projects/types#change-key
3403
- var sortedActions = sortBy__default["default"](actions, function (action) {
3404
- return action.action !== 'removeFieldDefinition';
3405
- });
3406
- return sortedActions;
3407
- }
3408
-
3409
- function createTypeMapActions(mapActionGroup, syncActionConfig) {
3410
- return function doMapActions(diff, next, previous) {
3411
- var allActions = [];
3412
- allActions.push(mapActionGroup('base', function () {
3413
- return actionsMapBase$8(diff, previous, next, syncActionConfig);
3414
- }), mapActionGroup('fieldDefinitions', function () {
3415
- return actionsMapFieldDefinitions(diff.fieldDefinitions, previous.fieldDefinitions, next.fieldDefinitions, findMatchingPairs(diff.fieldDefinitions, previous.fieldDefinitions, next.fieldDefinitions, 'name'));
3416
- }));
3417
- return flatten__default["default"](allActions);
3418
- };
3419
- }
3420
- var types = (function (actionGroupList, syncActionConfig) {
3421
- var mapActionGroup = createMapActionGroup(actionGroupList);
3422
- var doMapActions = createTypeMapActions(mapActionGroup, syncActionConfig);
3423
- var buildActions = createBuildActions(diff, doMapActions);
3424
- return {
3425
- buildActions: buildActions
3426
- };
3427
- });
3428
-
3429
- var baseActionsList$7 = [{
3430
- action: 'changeName',
3431
- key: 'name'
3432
- }, {
3433
- action: 'changeCurrencies',
3434
- key: 'currencies'
3435
- }, {
3436
- action: 'changeCountries',
3437
- key: 'countries'
3438
- }, {
3439
- action: 'changeLanguages',
3440
- key: 'languages'
3441
- }, {
3442
- action: 'changeMessagesConfiguration',
3443
- key: 'messagesConfiguration'
3444
- }, {
3445
- action: 'setShippingRateInputType',
3446
- key: 'shippingRateInputType'
3447
- }];
3448
- var myBusinessUnitActionsList = [{
3449
- action: 'changeMyBusinessUnitStatusOnCreation',
3450
- key: 'myBusinessUnitStatusOnCreation',
3451
- actionKey: 'status'
3452
- }, {
3453
- action: 'setMyBusinessUnitAssociateRoleOnCreation',
3454
- key: 'myBusinessUnitAssociateRoleOnCreation',
3455
- actionKey: 'associateRole'
3456
- }];
3457
- var customerSearchActionsList = [{
3458
- action: 'changeCustomerSearchStatus',
3459
- key: 'status'
3460
- }];
3461
- var businessUnitSearchActionsList = [{
3462
- action: 'changeBusinessUnitSearchStatus',
3463
- key: 'status'
3464
- }];
3465
- function actionsMapBase$7(diff, oldObj, newObj) {
3466
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3467
- return buildBaseAttributesActions({
3468
- actions: baseActionsList$7,
3469
- diff: diff,
3470
- oldObj: oldObj,
3471
- newObj: newObj,
3472
- shouldOmitEmptyString: config.shouldOmitEmptyString
3473
- });
3474
- }
3475
- var actionsMapBusinessUnit = function actionsMapBusinessUnit(diff, oldObj, newObj) {
3476
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3477
- var businessUnits = diff.businessUnits;
3478
- if (!businessUnits) {
3479
- return [];
3480
- }
3481
- return buildBaseAttributesActions({
3482
- actions: myBusinessUnitActionsList,
3483
- diff: businessUnits,
3484
- oldObj: oldObj.businessUnits,
3485
- newObj: newObj.businessUnits,
3486
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3487
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties
3488
- });
3489
- };
3490
- function actionsMapSearchIndexingConfiguration(diff, oldObj, newObj) {
3491
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3492
- var searchIndexing = diff.searchIndexing;
3493
- if (!searchIndexing) {
3494
- return [];
3495
- }
3496
- var businessUnits = searchIndexing.businessUnits,
3497
- customers = searchIndexing.customers;
3498
- if (!customers && !businessUnits) {
3499
- return [];
3500
- }
3501
- var businessUnitsActions = businessUnits ? buildBaseAttributesActions({
3502
- actions: businessUnitSearchActionsList,
3503
- diff: diff.searchIndexing.businessUnits,
3504
- oldObj: oldObj.searchIndexing.businessUnits,
3505
- newObj: newObj.searchIndexing.businessUnits,
3506
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3507
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3508
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3509
- }) : [];
3510
- var customersActions = customers ? buildBaseAttributesActions({
3511
- actions: customerSearchActionsList,
3512
- diff: diff.searchIndexing.customers,
3513
- oldObj: oldObj.searchIndexing.customers,
3514
- newObj: newObj.searchIndexing.customers,
3515
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3516
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3517
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3518
- }) : [];
3519
- return [].concat(_toConsumableArray(businessUnitsActions), _toConsumableArray(customersActions));
3520
- }
3521
-
3522
- function createChannelsMapActions(mapActionGroup, syncActionConfig) {
3523
- return function doMapActions(diff, newObj, oldObj) {
3524
- var allActions = [];
3525
- allActions.push(mapActionGroup('base', function () {
3526
- return actionsMapBase$7(diff, oldObj, newObj, syncActionConfig);
3527
- }));
3528
- allActions.push(mapActionGroup('businessUnit', function () {
3529
- return actionsMapBusinessUnit(diff, oldObj, newObj);
3530
- }));
3531
- allActions.push(mapActionGroup('searchIndexingConfiguration', function () {
3532
- return actionsMapSearchIndexingConfiguration(diff, oldObj, newObj);
3533
- }));
3534
- return flatten__default["default"](allActions);
3535
- };
3536
- }
3537
- var projects = (function (actionGroupList) {
3538
- var syncActionConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3539
- var mapActionGroup = createMapActionGroup(actionGroupList);
3540
- var doMapActions = createChannelsMapActions(mapActionGroup, syncActionConfig);
3541
- var buildActions = createBuildActions(diff, doMapActions);
3542
- return {
3543
- buildActions: buildActions
3544
- };
3545
- });
3546
-
3547
- var baseActionsList$6 = [{
3548
- action: 'setName',
3549
- key: 'name'
3550
- }, {
3551
- action: 'setLanguages',
3552
- key: 'languages'
3553
- }, {
3554
- action: 'setDistributionChannels',
3555
- key: 'distributionChannels'
3556
- }, {
3557
- action: 'setSupplyChannels',
3558
- key: 'supplyChannels'
3559
- }];
3560
- function actionsMapBase$6(diff, oldObj, newObj) {
3561
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3562
- return buildBaseAttributesActions({
3563
- actions: baseActionsList$6,
3564
- diff: diff,
3565
- oldObj: oldObj,
3566
- newObj: newObj,
3567
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3568
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3569
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3570
- });
3571
- }
3572
-
3573
- function createStoresMapActions(mapActionGroup) {
3574
- return function doMapActions(diff, next, previous) {
3575
- var allActions = [];
3576
- allActions.push(mapActionGroup('base', function () {
3577
- return actionsMapBase$6(diff, previous, next);
3578
- }));
3579
- allActions.push(mapActionGroup('custom', function () {
3580
- return actionsMapCustom(diff, next, previous);
3581
- }));
3582
- return flatten__default["default"](allActions);
3583
- };
3584
- }
3585
- var stores = (function (actionGroupList) {
3586
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3587
- var mapActionGroup = createMapActionGroup(actionGroupList);
3588
- var doMapActions = createStoresMapActions(mapActionGroup);
3589
- var onBeforeApplyingDiff = null;
3590
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff, options);
3591
- return {
3592
- buildActions: buildActions
3593
- };
3594
- });
3595
-
3596
- var baseActionsList$5 = [{
3597
- action: 'changeName',
3598
- key: 'name'
3599
- }, {
3600
- action: 'setKey',
3601
- key: 'key'
3602
- }];
3603
- function actionsMapBase$5(diff, oldObj, newObj) {
3604
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3605
- return buildBaseAttributesActions({
3606
- actions: baseActionsList$5,
3607
- diff: diff,
3608
- oldObj: oldObj,
3609
- newObj: newObj,
3610
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3611
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3612
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3613
- });
3614
- }
3615
-
3616
- function createProductSelectionsMapActions(mapActionGroup) {
3617
- return function doMapActions(diff, next, previous) {
3618
- var allActions = [];
3619
- allActions.push(mapActionGroup('base', function () {
3620
- return actionsMapBase$5(diff, previous, next);
3621
- }));
3622
- allActions.push(mapActionGroup('custom', function () {
3623
- return actionsMapCustom(diff, next, previous);
3624
- }));
3625
- return flatten__default["default"](allActions);
3626
- };
3627
- }
3628
- var productSelections = (function (actionGroupList) {
3629
- var mapActionGroup = createMapActionGroup(actionGroupList);
3630
- var doMapActions = createProductSelectionsMapActions(mapActionGroup);
3631
- var onBeforeApplyingDiff = null;
3632
- var buildActions = createBuildActions(diff, doMapActions, onBeforeApplyingDiff);
3633
- return {
3634
- buildActions: buildActions
3635
- };
3636
- });
3637
-
3638
- var baseActionsList$4 = [{
3639
- action: 'changeValue',
3640
- key: 'value'
3641
- }, {
3642
- action: 'setDiscountedPrice',
3643
- key: 'discounted'
3644
- },
3645
- // TODO: Later add more accurate actions `addPriceTier`, `removePriceTier`
3646
- {
3647
- action: 'setPriceTiers',
3648
- key: 'tiers'
3649
- }, {
3650
- action: 'setKey',
3651
- key: 'key'
3652
- }, {
3653
- action: 'setValidFrom',
3654
- key: 'validFrom'
3655
- }, {
3656
- action: 'setValidUntil',
3657
- key: 'validUntil'
3658
- }, {
3659
- action: 'changeActive',
3660
- key: 'active'
3661
- }];
3662
- function actionsMapBase$4(diff, oldObj, newObj) {
3663
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3664
- return buildBaseAttributesActions({
3665
- actions: baseActionsList$4,
3666
- diff: diff,
3667
- oldObj: oldObj,
3668
- newObj: newObj,
3669
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3670
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3671
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3672
- });
3673
- }
3674
-
3675
- function createPriceMapActions(mapActionGroup, syncActionConfig) {
3676
- return function doMapActions(diff, newObj, oldObj) {
3677
- var baseActions = mapActionGroup('base', function () {
3678
- return actionsMapBase$4(diff, oldObj, newObj, syncActionConfig);
3679
- });
3680
- var customActions = mapActionGroup('custom', function () {
3681
- return actionsMapCustom(diff, newObj, oldObj);
3682
- });
3683
- return combineValidityActions([].concat(_toConsumableArray(baseActions), _toConsumableArray(customActions)));
3684
- };
3685
- }
3686
- var prices = (function (actionGroupList, syncActionConfig) {
3687
- var mapActionGroup = createMapActionGroup(actionGroupList);
3688
- var doMapActions = createPriceMapActions(mapActionGroup, syncActionConfig);
3689
- var buildActions = createBuildActions(diff, doMapActions);
3690
- return {
3691
- buildActions: buildActions
3692
- };
3693
- });
3694
-
3695
- var hasAttribute = function hasAttribute(attributes, newValue) {
3696
- return attributes.some(function (attribute) {
3697
- return attribute.key === newValue.key;
3698
- });
3699
- };
3700
- var baseActionsList$3 = [{
3701
- action: 'changeName',
3702
- key: 'name'
3703
- }, {
3704
- action: 'setKey',
3705
- key: 'key'
3706
- }, {
3707
- action: 'setDescription',
3708
- key: 'description'
3709
- }];
3710
- function actionsMapBase$3(diff, oldObj, newObj) {
3711
- var config = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3712
- return buildBaseAttributesActions({
3713
- actions: baseActionsList$3,
3714
- diff: diff,
3715
- oldObj: oldObj,
3716
- newObj: newObj,
3717
- shouldOmitEmptyString: config.shouldOmitEmptyString,
3718
- shouldUnsetOmittedProperties: config.shouldUnsetOmittedProperties,
3719
- shouldPreventUnsettingRequiredFields: config.shouldPreventUnsettingRequiredFields
3720
- });
3721
- }
3722
- function actionsMapAttributes(diff, oldObj, newObj) {
3723
- var handler = createBuildArrayActions('attributes', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newAttribute) {
3724
- return {
3725
- action: 'addAttribute',
3726
- attribute: newAttribute
3727
- };
3728
- }), REMOVE_ACTIONS, function (oldAttribute) {
3729
- // We only add the action if the attribute is not included in the new object.
3730
- return !hasAttribute(newObj.attributes, oldAttribute) ? {
3731
- action: 'removeAttribute',
3732
- attribute: oldAttribute
3733
- } : null;
3734
- }), CHANGE_ACTIONS, function (oldAttribute, newAttribute) {
3735
- var result = [];
3736
- // We only remove the attribute in case that the oldAttribute is not
3737
- // included in the new object
3738
- if (!hasAttribute(newObj.attributes, oldAttribute)) result.push({
3739
- action: 'removeAttribute',
3740
- attribute: oldAttribute
3741
- });
3742
-
3743
- // We only add the attribute in case that the newAttribute was not
3744
- // included in the old object
3745
- if (!hasAttribute(oldObj.attributes, newAttribute)) result.push({
3746
- action: 'addAttribute',
3747
- attribute: newAttribute
3748
- });
3749
- return result;
3750
- }));
3751
- return handler(diff, oldObj, newObj);
3752
- }
3753
-
3754
- function createAttributeGroupsMapActions(mapActionGroup, syncActionConfig) {
3755
- return function doMapActions(diff, newObj, oldObj) {
3756
- var allActions = [];
3757
- allActions.push(mapActionGroup('base', function () {
3758
- return actionsMapBase$3(diff, oldObj, newObj, syncActionConfig);
3759
- }));
3760
- allActions.push(flatten__default["default"](mapActionGroup('attributes', function () {
3761
- return actionsMapAttributes(diff, oldObj, newObj);
3762
- })));
3763
- return flatten__default["default"](allActions);
3764
- };
3765
- }
3766
- var attributeGroups = (function (actionGroupList, syncActionConfig) {
3767
- var mapActionGroup = createMapActionGroup(actionGroupList);
3768
- var doMapActions = createAttributeGroupsMapActions(mapActionGroup, syncActionConfig);
3769
- var buildActions = createBuildActions(diff, doMapActions);
3770
- return {
3771
- buildActions: buildActions
3772
- };
3773
- });
3774
-
3775
- var baseActionsList$2 = [{
3776
- action: 'setKey',
3777
- key: 'key'
3778
- }, {
3779
- action: 'changeTriggers',
3780
- key: 'triggers'
3781
- }, {
3782
- action: 'setTimeoutInMs',
3783
- key: 'timeoutInMs'
3784
- }, {
3785
- action: 'changeDestination',
3786
- key: 'destination'
3787
- }];
3788
- var actionsMapBase$2 = function actionsMapBase(diff, oldObj, newObj, config) {
3789
- return buildBaseAttributesActions({
3790
- actions: baseActionsList$2,
3791
- diff: diff,
3792
- oldObj: oldObj,
3793
- newObj: newObj,
3794
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3795
- });
3796
- };
3797
-
3798
- var createApiExtensionsMapActions = function createApiExtensionsMapActions(mapActionGroup, syncActionConfig) {
3799
- return function doMapActions(diff, newObj, oldObj) {
3800
- var allActions = [];
3801
- allActions.push(mapActionGroup('base', function () {
3802
- return actionsMapBase$2(diff, oldObj, newObj, syncActionConfig);
3803
- }));
3804
- return flatten__default["default"](allActions);
3805
- };
3806
- };
3807
- var apiExtensions = (function (actionGroupList, syncActionConfig) {
3808
- var mapActionGroup = createMapActionGroup(actionGroupList);
3809
- var doMapActions = createApiExtensionsMapActions(mapActionGroup, syncActionConfig);
3810
- var buildActions = createBuildActions(diff, doMapActions);
3811
- return {
3812
- buildActions: buildActions
3813
- };
3814
- });
3815
-
3816
- var baseActionsList$1 = [{
3817
- action: 'setStores',
3818
- key: 'stores'
3819
- }, {
3820
- action: 'changeAssociateMode',
3821
- key: 'associateMode'
3822
- }, {
3823
- action: 'changeApprovalRuleMode',
3824
- key: 'approvalRuleMode'
3825
- }, {
3826
- action: 'changeName',
3827
- key: 'name'
3828
- }, {
3829
- action: 'changeParentUnit',
3830
- key: 'parentUnit'
3831
- }, {
3832
- action: 'changeStatus',
3833
- key: 'status'
3834
- }, {
3835
- action: 'setContactEmail',
3836
- key: 'contactEmail'
3837
- }, {
3838
- action: 'setStoreMode',
3839
- key: 'storeMode'
3840
- }];
3841
- var actionsMapAssociates = function actionsMapAssociates(diff, oldObj, newObj) {
3842
- var handler = createBuildArrayActions('associates', _defineProperty(_defineProperty(_defineProperty({}, ADD_ACTIONS, function (newObject) {
3843
- return {
3844
- action: 'addAssociate',
3845
- associate: newObject
3846
- };
3847
- }), REMOVE_ACTIONS, function (objectToRemove) {
3848
- return {
3849
- action: 'removeAssociate',
3850
- customer: {
3851
- typeId: 'customer',
3852
- id: objectToRemove.customer.id
3853
- }
3854
- };
3855
- }), CHANGE_ACTIONS, function (oldObject, updatedObject) {
3856
- return {
3857
- action: 'changeAssociate',
3858
- associate: updatedObject
3859
- };
3860
- }));
3861
- return handler(diff, oldObj, newObj);
3862
- };
3863
- var actionsMapBase$1 = function actionsMapBase(diff, oldObj, newObj, config) {
3864
- return buildBaseAttributesActions({
3865
- actions: baseActionsList$1,
3866
- diff: diff,
3867
- oldObj: oldObj,
3868
- newObj: newObj,
3869
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3870
- });
3871
- };
3872
-
3873
- var createCustomerMapActions = function createCustomerMapActions(mapActionGroup, syncActionConfig) {
3874
- return function doMapActions(diff, newObj, oldObj) {
3875
- var allActions = [];
3876
- allActions.push(mapActionGroup('base', function () {
3877
- return actionsMapBase$1(diff, oldObj, newObj, syncActionConfig);
3878
- }));
3879
- allActions.push(mapActionGroup('addresses', function () {
3880
- return actionsMapAddresses(diff, oldObj, newObj);
3881
- }));
3882
- allActions.push(mapActionGroup('base', function () {
3883
- return actionsMapSetDefaultBase(diff, oldObj, newObj, syncActionConfig);
3884
- }));
3885
- allActions.push(mapActionGroup('billingAddressIds', function () {
3886
- return actionsMapBillingAddresses(diff, oldObj, newObj);
3887
- }));
3888
- allActions.push(mapActionGroup('shippingAddressIds', function () {
3889
- return actionsMapShippingAddresses(diff, oldObj, newObj);
3890
- }));
3891
- allActions.push(mapActionGroup('associates', function () {
3892
- return actionsMapAssociates(diff, oldObj, newObj);
3893
- }));
3894
- allActions.push(mapActionGroup('custom', function () {
3895
- return actionsMapCustom(diff, newObj, oldObj);
3896
- }));
3897
- return flatten__default["default"](allActions);
3898
- };
3899
- };
3900
- var businessUnits = (function (actionGroupList, syncActionConfig) {
3901
- var mapActionGroup = createMapActionGroup(actionGroupList);
3902
- var doMapActions = createCustomerMapActions(mapActionGroup, syncActionConfig);
3903
- var buildActions = createBuildActions(diff, doMapActions);
3904
- return {
3905
- buildActions: buildActions
3906
- };
3907
- });
3908
-
3909
- var baseActionsList = [{
3910
- action: 'setKey',
3911
- key: 'key'
3912
- }, {
3913
- action: 'setMessages',
3914
- key: 'messages'
3915
- }, {
3916
- action: 'setChanges',
3917
- key: 'changes'
3918
- }, {
3919
- action: 'changeDestination',
3920
- key: 'destination'
3921
- }];
3922
- var actionsMapBase = function actionsMapBase(diff, oldObj, newObj, config) {
3923
- return buildBaseAttributesActions({
3924
- actions: baseActionsList,
3925
- diff: diff,
3926
- oldObj: oldObj,
3927
- newObj: newObj,
3928
- shouldOmitEmptyString: config === null || config === void 0 ? void 0 : config.shouldOmitEmptyString
3929
- });
3930
- };
3931
-
3932
- var createSubscriptionsMapActions = function createSubscriptionsMapActions(mapActionGroup, syncActionConfig) {
3933
- return function doMapActions(diff, newObj, oldObj) {
3934
- var allActions = [];
3935
- allActions.push(mapActionGroup('base', function () {
3936
- return actionsMapBase(diff, oldObj, newObj, syncActionConfig);
3937
- }));
3938
- allActions.push(mapActionGroup('custom', function () {
3939
- return actionsMapCustom(diff, newObj, oldObj);
3940
- }));
3941
- return allActions.flat();
3942
- };
3943
- };
3944
- var subscriptions = (function (actionGroupList, syncActionConfig) {
3945
- var mapActionGroup = createMapActionGroup(actionGroupList);
3946
- var doMapActions = createSubscriptionsMapActions(mapActionGroup, syncActionConfig);
3947
- var buildActions = createBuildActions(diff, doMapActions);
3948
- return {
3949
- buildActions: buildActions
3950
- };
3951
- });
3952
-
3953
- exports.createSyncApiExtensions = apiExtensions;
3954
- exports.createSyncAttributeGroups = attributeGroups;
3955
- exports.createSyncBusinessUnits = businessUnits;
3956
- exports.createSyncCartDiscounts = cartDiscounts;
3957
- exports.createSyncCategories = categories;
3958
- exports.createSyncChannels = channels;
3959
- exports.createSyncCustomerGroup = customerGroup;
3960
- exports.createSyncCustomers = customers;
3961
- exports.createSyncDiscountCodes = discountCodes;
3962
- exports.createSyncInventories = inventories;
3963
- exports.createSyncOrders = orders;
3964
- exports.createSyncProductDiscounts = productDiscounts;
3965
- exports.createSyncProductSelections = productSelections;
3966
- exports.createSyncProductTypes = productTypes;
3967
- exports.createSyncProducts = products;
3968
- exports.createSyncProjects = projects;
3969
- exports.createSyncRecurringOrders = recurringOrders;
3970
- exports.createSyncShippingMethods = shippingMethods;
3971
- exports.createSyncStandalonePrices = prices;
3972
- exports.createSyncStates = states;
3973
- exports.createSyncStores = stores;
3974
- exports.createSyncSubscriptions = subscriptions;
3975
- exports.createSyncTaxCategories = taxCategories;
3976
- exports.createSyncTypes = types;
3977
- exports.createSyncZones = zones;