@commercetools/sync-actions 7.2.0 → 7.3.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.
@@ -1312,14 +1312,45 @@ function _buildKeyActions(variantDiff, oldVariant) {
1312
1312
  }
1313
1313
  return null;
1314
1314
  }
1315
- function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) {
1316
- var attributeName = el && el.name;
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;
1317
1348
  if (!attributeName) return undefined;
1318
1349
  var action = {
1319
1350
  action: 'setAttribute',
1320
- variantId: id,
1351
+ variantId: variantId,
1321
1352
  name: attributeName,
1322
- value: el.value
1353
+ value: attr.value
1323
1354
  };
1324
1355
  if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
1325
1356
  action = _objectSpread2(_objectSpread2({}, action), {}, {
@@ -1347,35 +1378,31 @@ function _buildSetAttributeAction(diffedValue, oldVariant, attribute, sameForAll
1347
1378
  });
1348
1379
  delete action.variantId;
1349
1380
  }
1350
- if (Array.isArray(diffedValue)) action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (typeof diffedValue === 'string')
1351
- // LText: value: {en: "", de: ""}
1352
- // Enum: value: {key: "foo", label: "Foo"}
1353
- // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1354
- // Money: value: {centAmount: 123, currencyCode: ""}
1355
- // *: value: ""
1356
-
1357
- // normal
1358
- action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (diffedValue.centAmount || diffedValue.currencyCode)
1359
- // Money
1360
- action.value = {
1361
- centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : attribute.value.centAmount,
1362
- currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : attribute.value.currencyCode
1363
- };else if (diffedValue.key)
1364
- // Enum / LEnum (use only the key)
1365
- action.value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1366
- // set-typed attribute
1367
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1368
- value: attribute.value
1369
- });
1370
- } else {
1371
- // LText
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
+ };
1372
1400
 
1373
- var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1374
- var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1375
- return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1376
- }, _objectSpread2({}, oldAttribute.value));
1377
- action.value = updatedValue;
1378
- }
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);
1379
1406
  return action;
1380
1407
  }
1381
1408
  function _buildVariantImagesAction(diffedImages) {
@@ -1486,6 +1513,41 @@ function _buildVariantPricesAction(diffedPrices) {
1486
1513
  });
1487
1514
  return [addPriceActions, changePriceActions, removePriceActions];
1488
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
+ }
1489
1551
  function _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames) {
1490
1552
  var actions = [];
1491
1553
  if (!attributes) return actions;
@@ -1497,8 +1559,8 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1497
1559
  var setAction = _buildNewSetAttributeAction(id, deltaValue, sameForAllAttributeNames);
1498
1560
  if (setAction) actions.push(setAction);
1499
1561
  } else if (newVariant.attributes) {
1500
- var _setAction = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1501
- if (_setAction) actions.push(_setAction);
1562
+ var _setAction4 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1563
+ if (_setAction4) actions.push(_setAction4);
1502
1564
  }
1503
1565
  } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(value)) {
1504
1566
  // Ignore pure array moves!
@@ -1510,13 +1572,13 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1510
1572
  _deltaValue = {
1511
1573
  name: value[0].name
1512
1574
  };else _deltaValue = undefined;
1513
- var _setAction2 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1514
- if (_setAction2) actions.push(_setAction2);
1575
+ var _setAction5 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1576
+ if (_setAction5) actions.push(_setAction5);
1515
1577
  } else {
1516
1578
  var index = key.substring(1);
1517
1579
  if (newVariant.attributes) {
1518
- var _setAction3 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1519
- if (_setAction3) actions.push(_setAction3);
1580
+ var _setAction6 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1581
+ if (_setAction6) actions.push(_setAction6);
1520
1582
  }
1521
1583
  }
1522
1584
  });
@@ -1731,6 +1793,9 @@ function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
1731
1793
  });
1732
1794
  return allAssetsActions;
1733
1795
  }
1796
+ function actionsMapProductAttributes(diffedProductData, oldProductData, newProductData) {
1797
+ return _buildProductAttributesActions(diffedProductData.attributes, oldProductData, newProductData);
1798
+ }
1734
1799
  function actionsMapAttributes$1(diff, oldObj, newObj) {
1735
1800
  var sameForAllAttributeNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1736
1801
  var variantHashMap = arguments.length > 4 ? arguments[4] : undefined;
@@ -1851,6 +1916,9 @@ function createProductMapActions(mapActionGroup, syncActionConfig) {
1851
1916
  var publish = newObj.publish,
1852
1917
  staged = newObj.staged;
1853
1918
  var variantHashMap = findMatchingPairs(diff.variants, oldObj.variants, newObj.variants);
1919
+ allActions.push(mapActionGroup('productAttributes', function () {
1920
+ return actionsMapProductAttributes(diff, oldObj, newObj);
1921
+ }));
1854
1922
  allActions.push(mapActionGroup('attributes', function () {
1855
1923
  return actionsMapAttributes$1(diff, oldObj, newObj, sameForAllAttributeNames || [], variantHashMap);
1856
1924
  }));
@@ -1312,14 +1312,45 @@ function _buildKeyActions(variantDiff, oldVariant) {
1312
1312
  }
1313
1313
  return null;
1314
1314
  }
1315
- function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) {
1316
- var attributeName = el && el.name;
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;
1317
1348
  if (!attributeName) return undefined;
1318
1349
  var action = {
1319
1350
  action: 'setAttribute',
1320
- variantId: id,
1351
+ variantId: variantId,
1321
1352
  name: attributeName,
1322
- value: el.value
1353
+ value: attr.value
1323
1354
  };
1324
1355
  if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
1325
1356
  action = _objectSpread2(_objectSpread2({}, action), {}, {
@@ -1347,35 +1378,31 @@ function _buildSetAttributeAction(diffedValue, oldVariant, attribute, sameForAll
1347
1378
  });
1348
1379
  delete action.variantId;
1349
1380
  }
1350
- if (Array.isArray(diffedValue)) action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (typeof diffedValue === 'string')
1351
- // LText: value: {en: "", de: ""}
1352
- // Enum: value: {key: "foo", label: "Foo"}
1353
- // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1354
- // Money: value: {centAmount: 123, currencyCode: ""}
1355
- // *: value: ""
1356
-
1357
- // normal
1358
- action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (diffedValue.centAmount || diffedValue.currencyCode)
1359
- // Money
1360
- action.value = {
1361
- centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : attribute.value.centAmount,
1362
- currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : attribute.value.currencyCode
1363
- };else if (diffedValue.key)
1364
- // Enum / LEnum (use only the key)
1365
- action.value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1366
- // set-typed attribute
1367
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1368
- value: attribute.value
1369
- });
1370
- } else {
1371
- // LText
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
+ };
1372
1400
 
1373
- var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1374
- var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1375
- return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1376
- }, _objectSpread2({}, oldAttribute.value));
1377
- action.value = updatedValue;
1378
- }
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);
1379
1406
  return action;
1380
1407
  }
1381
1408
  function _buildVariantImagesAction(diffedImages) {
@@ -1486,6 +1513,41 @@ function _buildVariantPricesAction(diffedPrices) {
1486
1513
  });
1487
1514
  return [addPriceActions, changePriceActions, removePriceActions];
1488
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
+ }
1489
1551
  function _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames) {
1490
1552
  var actions = [];
1491
1553
  if (!attributes) return actions;
@@ -1497,8 +1559,8 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1497
1559
  var setAction = _buildNewSetAttributeAction(id, deltaValue, sameForAllAttributeNames);
1498
1560
  if (setAction) actions.push(setAction);
1499
1561
  } else if (newVariant.attributes) {
1500
- var _setAction = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1501
- if (_setAction) actions.push(_setAction);
1562
+ var _setAction4 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1563
+ if (_setAction4) actions.push(_setAction4);
1502
1564
  }
1503
1565
  } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(value)) {
1504
1566
  // Ignore pure array moves!
@@ -1510,13 +1572,13 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1510
1572
  _deltaValue = {
1511
1573
  name: value[0].name
1512
1574
  };else _deltaValue = undefined;
1513
- var _setAction2 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1514
- if (_setAction2) actions.push(_setAction2);
1575
+ var _setAction5 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1576
+ if (_setAction5) actions.push(_setAction5);
1515
1577
  } else {
1516
1578
  var index = key.substring(1);
1517
1579
  if (newVariant.attributes) {
1518
- var _setAction3 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1519
- if (_setAction3) actions.push(_setAction3);
1580
+ var _setAction6 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1581
+ if (_setAction6) actions.push(_setAction6);
1520
1582
  }
1521
1583
  }
1522
1584
  });
@@ -1731,6 +1793,9 @@ function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
1731
1793
  });
1732
1794
  return allAssetsActions;
1733
1795
  }
1796
+ function actionsMapProductAttributes(diffedProductData, oldProductData, newProductData) {
1797
+ return _buildProductAttributesActions(diffedProductData.attributes, oldProductData, newProductData);
1798
+ }
1734
1799
  function actionsMapAttributes$1(diff, oldObj, newObj) {
1735
1800
  var sameForAllAttributeNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1736
1801
  var variantHashMap = arguments.length > 4 ? arguments[4] : undefined;
@@ -1851,6 +1916,9 @@ function createProductMapActions(mapActionGroup, syncActionConfig) {
1851
1916
  var publish = newObj.publish,
1852
1917
  staged = newObj.staged;
1853
1918
  var variantHashMap = findMatchingPairs(diff.variants, oldObj.variants, newObj.variants);
1919
+ allActions.push(mapActionGroup('productAttributes', function () {
1920
+ return actionsMapProductAttributes(diff, oldObj, newObj);
1921
+ }));
1854
1922
  allActions.push(mapActionGroup('attributes', function () {
1855
1923
  return actionsMapAttributes$1(diff, oldObj, newObj, sameForAllAttributeNames || [], variantHashMap);
1856
1924
  }));
@@ -1297,14 +1297,45 @@ function _buildKeyActions(variantDiff, oldVariant) {
1297
1297
  }
1298
1298
  return null;
1299
1299
  }
1300
- function _buildNewSetAttributeAction(id, el, sameForAllAttributeNames) {
1301
- var attributeName = el && el.name;
1300
+ function _buildAttributeValue(diffedValue, oldAttributeValue, newAttributeValue) {
1301
+ var value;
1302
+ if (Array.isArray(diffedValue)) value = getDeltaValue(diffedValue, oldAttributeValue);else if (typeof diffedValue === 'string')
1303
+ // LText: value: {en: "", de: ""}
1304
+ // Enum: value: {key: "foo", label: "Foo"}
1305
+ // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1306
+ // Money: value: {centAmount: 123, currencyCode: ""}
1307
+ // *: value: ""
1308
+
1309
+ // normal
1310
+ value = getDeltaValue(diffedValue, oldAttributeValue);else if (diffedValue.centAmount || diffedValue.currencyCode)
1311
+ // Money
1312
+ value = {
1313
+ centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : newAttributeValue.centAmount,
1314
+ currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : newAttributeValue.currencyCode
1315
+ };else if (diffedValue.key)
1316
+ // Enum / LEnum (use only the key)
1317
+ value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1318
+ // set-typed attribute
1319
+ value = newAttributeValue;
1320
+ } else {
1321
+ // LText
1322
+
1323
+ var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1324
+ var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1325
+ return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1326
+ }, _objectSpread2({}, oldAttributeValue));
1327
+ value = updatedValue;
1328
+ }
1329
+ return value;
1330
+ }
1331
+ function _buildNewSetAttributeAction(variantId, attr, sameForAllAttributeNames) {
1332
+ var attributeName = attr && attr.name;
1302
1333
  if (!attributeName) return undefined;
1303
1334
  var action = {
1304
1335
  action: 'setAttribute',
1305
- variantId: id,
1336
+ variantId: variantId,
1306
1337
  name: attributeName,
1307
- value: el.value
1338
+ value: attr.value
1308
1339
  };
1309
1340
  if (sameForAllAttributeNames.indexOf(attributeName) !== -1) {
1310
1341
  action = _objectSpread2(_objectSpread2({}, action), {}, {
@@ -1332,35 +1363,31 @@ function _buildSetAttributeAction(diffedValue, oldVariant, attribute, sameForAll
1332
1363
  });
1333
1364
  delete action.variantId;
1334
1365
  }
1335
- if (Array.isArray(diffedValue)) action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (typeof diffedValue === 'string')
1336
- // LText: value: {en: "", de: ""}
1337
- // Enum: value: {key: "foo", label: "Foo"}
1338
- // LEnum: value: {key: "foo", label: {en: "Foo", de: "Foo"}}
1339
- // Money: value: {centAmount: 123, currencyCode: ""}
1340
- // *: value: ""
1341
-
1342
- // normal
1343
- action.value = getDeltaValue(diffedValue, oldAttribute.value);else if (diffedValue.centAmount || diffedValue.currencyCode)
1344
- // Money
1345
- action.value = {
1346
- centAmount: diffedValue.centAmount ? getDeltaValue(diffedValue.centAmount) : attribute.value.centAmount,
1347
- currencyCode: diffedValue.currencyCode ? getDeltaValue(diffedValue.currencyCode) : attribute.value.currencyCode
1348
- };else if (diffedValue.key)
1349
- // Enum / LEnum (use only the key)
1350
- action.value = getDeltaValue(diffedValue.key);else if (_typeof(diffedValue) === 'object') if ({}.hasOwnProperty.call(diffedValue, '_t') && diffedValue._t === 'a') {
1351
- // set-typed attribute
1352
- action = _objectSpread2(_objectSpread2({}, action), {}, {
1353
- value: attribute.value
1354
- });
1355
- } else {
1356
- // LText
1366
+ action.value = _buildAttributeValue(diffedValue, oldAttribute.value, attribute.value);
1367
+ return action;
1368
+ }
1369
+ function _buildNewSetProductAttributeAction(attr) {
1370
+ var attributeName = attr && attr.name;
1371
+ if (!attributeName) return undefined;
1372
+ var action = {
1373
+ action: 'setProductAttribute',
1374
+ name: attributeName,
1375
+ value: attr.value
1376
+ };
1377
+ return action;
1378
+ }
1379
+ function _buildSetProductAttributeAction(diffedValue, oldProductData, newAttribute) {
1380
+ if (!newAttribute) return undefined;
1381
+ var action = {
1382
+ action: 'setProductAttribute',
1383
+ name: newAttribute.name
1384
+ };
1357
1385
 
1358
- var updatedValue = Object.keys(diffedValue).reduce(function (acc, lang) {
1359
- var patchedValue = getDeltaValue(diffedValue[lang], acc[lang]);
1360
- return Object.assign(acc, _defineProperty({}, lang, patchedValue));
1361
- }, _objectSpread2({}, oldAttribute.value));
1362
- action.value = updatedValue;
1363
- }
1386
+ // Used as original object for patching long diff text
1387
+ var oldAttribute = oldProductData.attributes.find(function (a) {
1388
+ return a.name === newAttribute.name;
1389
+ }) || {};
1390
+ action.value = _buildAttributeValue(diffedValue, oldAttribute.value, newAttribute.value);
1364
1391
  return action;
1365
1392
  }
1366
1393
  function _buildVariantImagesAction(diffedImages) {
@@ -1471,6 +1498,41 @@ function _buildVariantPricesAction(diffedPrices) {
1471
1498
  });
1472
1499
  return [addPriceActions, changePriceActions, removePriceActions];
1473
1500
  }
1501
+ function _buildProductAttributesActions(diffedAttributes, oldProductData, newProductData) {
1502
+ var actions = [];
1503
+ if (!diffedAttributes) return actions;
1504
+ forEach(diffedAttributes, function (value, key) {
1505
+ if (REGEX_NUMBER$2.test(key)) {
1506
+ if (Array.isArray(value)) {
1507
+ var setAction = _buildNewSetProductAttributeAction(getDeltaValue(value));
1508
+ if (setAction) actions.push(setAction);
1509
+ } else if (newProductData.attributes) {
1510
+ var _setAction = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[key]);
1511
+ if (_setAction) actions.push(_setAction);
1512
+ }
1513
+ } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) {
1514
+ if (Array.isArray(value)) {
1515
+ // Ignore pure array moves!
1516
+ if (value.length === 3 && value[2] === 3) return;
1517
+ var deltaValue = getDeltaValue(value);
1518
+ if (!deltaValue) if (value[0] && value[0].name)
1519
+ // unset attribute if
1520
+ deltaValue = {
1521
+ name: value[0].name
1522
+ };else deltaValue = undefined;
1523
+ var _setAction2 = _buildNewSetProductAttributeAction(deltaValue);
1524
+ if (_setAction2) actions.push(_setAction2);
1525
+ } else {
1526
+ var index = key.substring(1);
1527
+ if (newProductData.attributes) {
1528
+ var _setAction3 = _buildSetProductAttributeAction(value.value, oldProductData, newProductData.attributes[index]);
1529
+ if (_setAction3) actions.push(_setAction3);
1530
+ }
1531
+ }
1532
+ }
1533
+ });
1534
+ return actions;
1535
+ }
1474
1536
  function _buildVariantAttributesActions(attributes, oldVariant, newVariant, sameForAllAttributeNames) {
1475
1537
  var actions = [];
1476
1538
  if (!attributes) return actions;
@@ -1482,8 +1544,8 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1482
1544
  var setAction = _buildNewSetAttributeAction(id, deltaValue, sameForAllAttributeNames);
1483
1545
  if (setAction) actions.push(setAction);
1484
1546
  } else if (newVariant.attributes) {
1485
- var _setAction = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1486
- if (_setAction) actions.push(_setAction);
1547
+ var _setAction4 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[key], sameForAllAttributeNames);
1548
+ if (_setAction4) actions.push(_setAction4);
1487
1549
  }
1488
1550
  } else if (REGEX_UNDERSCORE_NUMBER$2.test(key)) if (Array.isArray(value)) {
1489
1551
  // Ignore pure array moves!
@@ -1495,13 +1557,13 @@ function _buildVariantAttributesActions(attributes, oldVariant, newVariant, same
1495
1557
  _deltaValue = {
1496
1558
  name: value[0].name
1497
1559
  };else _deltaValue = undefined;
1498
- var _setAction2 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1499
- if (_setAction2) actions.push(_setAction2);
1560
+ var _setAction5 = _buildNewSetAttributeAction(_id, _deltaValue, sameForAllAttributeNames);
1561
+ if (_setAction5) actions.push(_setAction5);
1500
1562
  } else {
1501
1563
  var index = key.substring(1);
1502
1564
  if (newVariant.attributes) {
1503
- var _setAction3 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1504
- if (_setAction3) actions.push(_setAction3);
1565
+ var _setAction6 = _buildSetAttributeAction(value.value, oldVariant, newVariant.attributes[index], sameForAllAttributeNames);
1566
+ if (_setAction6) actions.push(_setAction6);
1505
1567
  }
1506
1568
  }
1507
1569
  });
@@ -1716,6 +1778,9 @@ function actionsMapAssets(diff, oldObj, newObj, variantHashMap) {
1716
1778
  });
1717
1779
  return allAssetsActions;
1718
1780
  }
1781
+ function actionsMapProductAttributes(diffedProductData, oldProductData, newProductData) {
1782
+ return _buildProductAttributesActions(diffedProductData.attributes, oldProductData, newProductData);
1783
+ }
1719
1784
  function actionsMapAttributes$1(diff, oldObj, newObj) {
1720
1785
  var sameForAllAttributeNames = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1721
1786
  var variantHashMap = arguments.length > 4 ? arguments[4] : undefined;
@@ -1836,6 +1901,9 @@ function createProductMapActions(mapActionGroup, syncActionConfig) {
1836
1901
  var publish = newObj.publish,
1837
1902
  staged = newObj.staged;
1838
1903
  var variantHashMap = findMatchingPairs(diff.variants, oldObj.variants, newObj.variants);
1904
+ allActions.push(mapActionGroup('productAttributes', function () {
1905
+ return actionsMapProductAttributes(diff, oldObj, newObj);
1906
+ }));
1839
1907
  allActions.push(mapActionGroup('attributes', function () {
1840
1908
  return actionsMapAttributes$1(diff, oldObj, newObj, sameForAllAttributeNames || [], variantHashMap);
1841
1909
  }));
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "node": ">=18"
7
7
  },
8
8
  "name": "@commercetools/sync-actions",
9
- "version": "7.2.0",
9
+ "version": "7.3.0",
10
10
  "description": "Build API update actions for the commercetools platform.",
11
11
  "keywords": [
12
12
  "commercetools",