@labdigital/commercetools-mock 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/commercetools-mock.cjs.development.js +305 -277
  2. package/dist/commercetools-mock.cjs.development.js.map +1 -1
  3. package/dist/commercetools-mock.cjs.production.min.js +1 -1
  4. package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
  5. package/dist/commercetools-mock.esm.js +305 -277
  6. package/dist/commercetools-mock.esm.js.map +1 -1
  7. package/dist/repositories/abstract.d.ts +13 -9
  8. package/dist/repositories/cart-discount.d.ts +3 -3
  9. package/dist/repositories/cart.d.ts +12 -12
  10. package/dist/repositories/category.d.ts +11 -11
  11. package/dist/repositories/channel.d.ts +2 -2
  12. package/dist/repositories/custom-object.d.ts +3 -3
  13. package/dist/repositories/customer-group.d.ts +4 -4
  14. package/dist/repositories/customer.d.ts +4 -4
  15. package/dist/repositories/discount-code.d.ts +3 -3
  16. package/dist/repositories/extension.d.ts +3 -3
  17. package/dist/repositories/helpers.d.ts +3 -0
  18. package/dist/repositories/inventory-entry.d.ts +7 -7
  19. package/dist/repositories/my-order.d.ts +6 -0
  20. package/dist/repositories/order.d.ts +18 -17
  21. package/dist/repositories/payment.d.ts +8 -8
  22. package/dist/repositories/product-projection.d.ts +3 -3
  23. package/dist/repositories/product-type.d.ts +5 -5
  24. package/dist/repositories/product.d.ts +4 -4
  25. package/dist/repositories/project.d.ts +4 -4
  26. package/dist/repositories/shipping-method.d.ts +3 -3
  27. package/dist/repositories/shopping-list.d.ts +2 -2
  28. package/dist/repositories/state.d.ts +3 -3
  29. package/dist/repositories/store.d.ts +4 -4
  30. package/dist/repositories/subscription.d.ts +2 -2
  31. package/dist/repositories/tax-category.d.ts +4 -4
  32. package/dist/repositories/type.d.ts +3 -3
  33. package/dist/repositories/zone.d.ts +3 -3
  34. package/dist/services/my-order.d.ts +2 -2
  35. package/package.json +1 -1
  36. package/src/ctMock.ts +6 -0
  37. package/src/repositories/abstract.ts +37 -17
  38. package/src/repositories/cart-discount.ts +11 -11
  39. package/src/repositories/cart.ts +28 -19
  40. package/src/repositories/category.ts +17 -13
  41. package/src/repositories/channel.ts +3 -3
  42. package/src/repositories/custom-object.ts +16 -8
  43. package/src/repositories/customer-group.ts +5 -5
  44. package/src/repositories/customer.ts +10 -6
  45. package/src/repositories/discount-code.ts +14 -14
  46. package/src/repositories/extension.ts +12 -8
  47. package/src/repositories/helpers.ts +9 -0
  48. package/src/repositories/inventory-entry.ts +17 -10
  49. package/src/repositories/my-order.ts +19 -0
  50. package/src/repositories/order.test.ts +79 -3
  51. package/src/repositories/order.ts +77 -37
  52. package/src/repositories/payment.ts +21 -17
  53. package/src/repositories/product-projection.ts +5 -5
  54. package/src/repositories/product-type.ts +14 -10
  55. package/src/repositories/product.ts +5 -5
  56. package/src/repositories/project.ts +21 -17
  57. package/src/repositories/shipping-method.ts +27 -20
  58. package/src/repositories/shopping-list.ts +10 -6
  59. package/src/repositories/state.ts +13 -9
  60. package/src/repositories/store.ts +18 -14
  61. package/src/repositories/subscription.ts +3 -3
  62. package/src/repositories/tax-category.ts +16 -12
  63. package/src/repositories/type.ts +15 -11
  64. package/src/repositories/zone.ts +13 -9
  65. package/src/services/abstract.ts +21 -10
  66. package/src/services/cart.ts +6 -12
  67. package/src/services/custom-object.ts +8 -4
  68. package/src/services/customer.ts +5 -2
  69. package/src/services/my-customer.ts +7 -3
  70. package/src/services/my-order.ts +3 -3
  71. package/src/services/order.ts +3 -2
  72. package/src/services/product-projection.ts +2 -1
  73. package/src/services/product-type.ts +2 -1
  74. package/src/services/project.ts +4 -3
  75. package/src/services/store.ts +2 -1
  76. package/src/services/tax-category.ts +2 -1
@@ -16,6 +16,7 @@ var auth = _interopDefault(require('basic-auth'));
16
16
  var bodyParser = _interopDefault(require('body-parser'));
17
17
  var crypto = require('crypto');
18
18
  var uuid = require('uuid');
19
+ var helpers = require('repositories/helpers');
19
20
  var deepEqual = _interopDefault(require('deep-equal'));
20
21
 
21
22
  const parseExpandClause = clause => {
@@ -989,7 +990,7 @@ class AbstractService {
989
990
 
990
991
  const offset = this._parseParam(request.query.offset);
991
992
 
992
- const result = this.repository.query(request.params.projectKey, {
993
+ const result = this.repository.query(helpers.getRepositoryContext(request), {
993
994
  expand: this._parseParam(request.query.expand),
994
995
  where: this._parseParam(request.query.where),
995
996
  limit: limit !== undefined ? Number(limit) : undefined,
@@ -1009,7 +1010,7 @@ class AbstractService {
1009
1010
  }
1010
1011
 
1011
1012
  getWithKey(request, response) {
1012
- const result = this.repository.getByKey(request.params.projectKey, request.params['key'], {
1013
+ const result = this.repository.getByKey(helpers.getRepositoryContext(request), request.params['key'], {
1013
1014
  expand: this._parseParam(request.query.expand)
1014
1015
  });
1015
1016
  if (!result) return response.status(404).send();
@@ -1017,7 +1018,7 @@ class AbstractService {
1017
1018
  }
1018
1019
 
1019
1020
  deletewithId(request, response) {
1020
- const result = this.repository.delete(request.params.projectKey, request.params['id'], {
1021
+ const result = this.repository.delete(helpers.getRepositoryContext(request), request.params['id'], {
1021
1022
  expand: this._parseParam(request.query.expand)
1022
1023
  });
1023
1024
 
@@ -1034,7 +1035,7 @@ class AbstractService {
1034
1035
 
1035
1036
  post(request, response) {
1036
1037
  const draft = request.body;
1037
- const resource = this.repository.create(request.params.projectKey, draft);
1038
+ const resource = this.repository.create(helpers.getRepositoryContext(request), draft);
1038
1039
 
1039
1040
  const result = this._expandWithId(request, resource.id);
1040
1041
 
@@ -1043,7 +1044,7 @@ class AbstractService {
1043
1044
 
1044
1045
  postWithId(request, response) {
1045
1046
  const updateRequest = request.body;
1046
- const resource = this.repository.get(request.params.projectKey, request.params['id']);
1047
+ const resource = this.repository.get(helpers.getRepositoryContext(request), request.params['id']);
1047
1048
 
1048
1049
  if (!resource) {
1049
1050
  return response.status(404).send('Not found');
@@ -1053,7 +1054,7 @@ class AbstractService {
1053
1054
  return response.status(409).send('Concurrent modification');
1054
1055
  }
1055
1056
 
1056
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, resource, updateRequest.actions);
1057
+ const updatedResource = this.repository.processUpdateActions(helpers.getRepositoryContext(request), resource, updateRequest.actions);
1057
1058
 
1058
1059
  const result = this._expandWithId(request, updatedResource.id);
1059
1060
 
@@ -1065,7 +1066,7 @@ class AbstractService {
1065
1066
  }
1066
1067
 
1067
1068
  _expandWithId(request, resourceId) {
1068
- const result = this.repository.get(request.params.projectKey, resourceId, {
1069
+ const result = this.repository.get(helpers.getRepositoryContext(request), resourceId, {
1069
1070
  expand: this._parseParam(request.query.expand)
1070
1071
  });
1071
1072
  return result;
@@ -1101,7 +1102,7 @@ class AbstractRepository {
1101
1102
  this._storage = storage;
1102
1103
  }
1103
1104
 
1104
- processUpdateActions(projectKey, resource, actions) {
1105
+ processUpdateActions(context, resource, actions) {
1105
1106
  // Deep-copy
1106
1107
  const modifiedResource = JSON.parse(JSON.stringify(resource));
1107
1108
  actions.forEach(action => {
@@ -1112,11 +1113,11 @@ class AbstractRepository {
1112
1113
  return;
1113
1114
  }
1114
1115
 
1115
- updateFunc(projectKey, modifiedResource, action);
1116
+ updateFunc(context, modifiedResource, action);
1116
1117
  });
1117
1118
 
1118
1119
  if (!deepEqual(modifiedResource, resource)) {
1119
- this.save(projectKey, modifiedResource);
1120
+ this.save(context, modifiedResource);
1120
1121
  }
1121
1122
 
1122
1123
  return modifiedResource;
@@ -1130,8 +1131,8 @@ class AbstractResourceRepository extends AbstractRepository {
1130
1131
  this._storage.assertStorage(this.getTypeId());
1131
1132
  }
1132
1133
 
1133
- query(projectKey, params = {}) {
1134
- return this._storage.query(projectKey, this.getTypeId(), {
1134
+ query(context, params = {}) {
1135
+ return this._storage.query(context.projectKey, this.getTypeId(), {
1135
1136
  expand: params.expand,
1136
1137
  where: params.where,
1137
1138
  offset: params.offset,
@@ -1139,20 +1140,20 @@ class AbstractResourceRepository extends AbstractRepository {
1139
1140
  });
1140
1141
  }
1141
1142
 
1142
- get(projectKey, id, params = {}) {
1143
- return this._storage.get(projectKey, this.getTypeId(), id, params);
1143
+ get(context, id, params = {}) {
1144
+ return this._storage.get(context.projectKey, this.getTypeId(), id, params);
1144
1145
  }
1145
1146
 
1146
- getByKey(projectKey, key, params = {}) {
1147
- return this._storage.getByKey(projectKey, this.getTypeId(), key, params);
1147
+ getByKey(context, key, params = {}) {
1148
+ return this._storage.getByKey(context.projectKey, this.getTypeId(), key, params);
1148
1149
  }
1149
1150
 
1150
- delete(projectKey, id, params = {}) {
1151
- return this._storage.delete(projectKey, this.getTypeId(), id, params);
1151
+ delete(context, id, params = {}) {
1152
+ return this._storage.delete(context.projectKey, this.getTypeId(), id, params);
1152
1153
  }
1153
1154
 
1154
- save(projectKey, resource) {
1155
- const current = this.get(projectKey, resource.id);
1155
+ save(context, resource) {
1156
+ const current = this.get(context, resource.id);
1156
1157
 
1157
1158
  if (current) {
1158
1159
  checkConcurrentModification(current, resource.version);
@@ -1168,7 +1169,7 @@ class AbstractResourceRepository extends AbstractRepository {
1168
1169
 
1169
1170
  resource.version += 1;
1170
1171
 
1171
- this._storage.add(projectKey, this.getTypeId(), resource);
1172
+ this._storage.add(context.projectKey, this.getTypeId(), resource);
1172
1173
  }
1173
1174
 
1174
1175
  }
@@ -1232,39 +1233,39 @@ class CartDiscountRepository extends AbstractResourceRepository {
1232
1233
  constructor() {
1233
1234
  super(...arguments);
1234
1235
  this.actions = {
1235
- setKey: (projectKey, resource, {
1236
+ setKey: (context, resource, {
1236
1237
  key
1237
1238
  }) => {
1238
1239
  resource.key = key;
1239
1240
  },
1240
- setDescription: (projectKey, resource, {
1241
+ setDescription: (context, resource, {
1241
1242
  description
1242
1243
  }) => {
1243
1244
  resource.description = description;
1244
1245
  },
1245
- setValidFrom: (projectKey, resource, {
1246
+ setValidFrom: (context, resource, {
1246
1247
  validFrom
1247
1248
  }) => {
1248
1249
  resource.validFrom = validFrom;
1249
1250
  },
1250
- setValidUntil: (projectKey, resource, {
1251
+ setValidUntil: (context, resource, {
1251
1252
  validUntil
1252
1253
  }) => {
1253
1254
  resource.validUntil = validUntil;
1254
1255
  },
1255
- setValidFromAndUntil: (projectKey, resource, {
1256
+ setValidFromAndUntil: (context, resource, {
1256
1257
  validFrom,
1257
1258
  validUntil
1258
1259
  }) => {
1259
1260
  resource.validFrom = validFrom;
1260
1261
  resource.validUntil = validUntil;
1261
1262
  },
1262
- changeSortOrder: (projectKey, resource, {
1263
+ changeSortOrder: (context, resource, {
1263
1264
  sortOrder
1264
1265
  }) => {
1265
1266
  resource.sortOrder = sortOrder;
1266
1267
  },
1267
- changeIsActive: (projectKey, resource, {
1268
+ changeIsActive: (context, resource, {
1268
1269
  isActive
1269
1270
  }) => {
1270
1271
  resource.isActive = isActive;
@@ -1276,7 +1277,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1276
1277
  return 'cart-discount';
1277
1278
  }
1278
1279
 
1279
- create(projectKey, draft) {
1280
+ create(context, draft) {
1280
1281
  const resource = { ...getBaseResourceProperties(),
1281
1282
  key: draft.key,
1282
1283
  description: draft.description,
@@ -1292,7 +1293,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1292
1293
  validUntil: draft.validUntil,
1293
1294
  value: this.transformValueDraft(draft.value)
1294
1295
  };
1295
- this.save(projectKey, resource);
1296
+ this.save(context, resource);
1296
1297
  return resource;
1297
1298
  }
1298
1299
 
@@ -1348,7 +1349,7 @@ class CartRepository extends AbstractResourceRepository {
1348
1349
  constructor() {
1349
1350
  super(...arguments);
1350
1351
  this.actions = {
1351
- addLineItem: (projectKey, resource, {
1352
+ addLineItem: (context, resource, {
1352
1353
  productId,
1353
1354
  variantId,
1354
1355
  sku,
@@ -1359,10 +1360,10 @@ class CartRepository extends AbstractResourceRepository {
1359
1360
 
1360
1361
  if (productId && variantId) {
1361
1362
  // Fetch product and variant by ID
1362
- product = this._storage.get(projectKey, 'product', productId, {});
1363
+ product = this._storage.get(context.projectKey, 'product', productId, {});
1363
1364
  } else if (sku) {
1364
1365
  // Fetch product and variant by SKU
1365
- const items = this._storage.query(projectKey, 'product', {
1366
+ const items = this._storage.query(context.projectKey, 'product', {
1366
1367
  where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1367
1368
  });
1368
1369
 
@@ -1457,7 +1458,7 @@ class CartRepository extends AbstractResourceRepository {
1457
1458
 
1458
1459
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1459
1460
  },
1460
- removeLineItem: (projectKey, resource, {
1461
+ removeLineItem: (context, resource, {
1461
1462
  lineItemId,
1462
1463
  quantity
1463
1464
  }) => {
@@ -1491,15 +1492,15 @@ class CartRepository extends AbstractResourceRepository {
1491
1492
 
1492
1493
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1493
1494
  },
1494
- setBillingAddress: (projectKey, resource, {
1495
+ setBillingAddress: (context, resource, {
1495
1496
  address
1496
1497
  }) => {
1497
1498
  resource.billingAddress = address;
1498
1499
  },
1499
- setShippingMethod: (projectKey, resource, {
1500
+ setShippingMethod: (context, resource, {
1500
1501
  shippingMethod
1501
1502
  }) => {
1502
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1503
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1503
1504
  shippingMethod);
1504
1505
 
1505
1506
  if (!resolvedType) {
@@ -1514,17 +1515,17 @@ class CartRepository extends AbstractResourceRepository {
1514
1515
  }
1515
1516
  };
1516
1517
  },
1517
- setCountry: (projectKey, resource, {
1518
+ setCountry: (context, resource, {
1518
1519
  country
1519
1520
  }) => {
1520
1521
  resource.country = country;
1521
1522
  },
1522
- setCustomerEmail: (projectKey, resource, {
1523
+ setCustomerEmail: (context, resource, {
1523
1524
  email
1524
1525
  }) => {
1525
1526
  resource.customerEmail = email;
1526
1527
  },
1527
- setCustomField: (projectKey, resource, {
1528
+ setCustomField: (context, resource, {
1528
1529
  name,
1529
1530
  value
1530
1531
  }) => {
@@ -1534,14 +1535,14 @@ class CartRepository extends AbstractResourceRepository {
1534
1535
 
1535
1536
  resource.custom.fields[name] = value;
1536
1537
  },
1537
- setCustomType: (projectKey, resource, {
1538
+ setCustomType: (context, resource, {
1538
1539
  type,
1539
1540
  fields
1540
1541
  }) => {
1541
1542
  if (!type) {
1542
1543
  resource.custom = undefined;
1543
1544
  } else {
1544
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1545
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1545
1546
 
1546
1547
  if (!resolvedType) {
1547
1548
  throw new Error(`Type ${type} not found`);
@@ -1556,12 +1557,12 @@ class CartRepository extends AbstractResourceRepository {
1556
1557
  };
1557
1558
  }
1558
1559
  },
1559
- setLocale: (projectKey, resource, {
1560
+ setLocale: (context, resource, {
1560
1561
  locale
1561
1562
  }) => {
1562
1563
  resource.locale = locale;
1563
1564
  },
1564
- setShippingAddress: (projectKey, resource, {
1565
+ setShippingAddress: (context, resource, {
1565
1566
  address
1566
1567
  }) => {
1567
1568
  resource.shippingAddress = address;
@@ -1648,10 +1649,10 @@ class CartRepository extends AbstractResourceRepository {
1648
1649
  return 'cart';
1649
1650
  }
1650
1651
 
1651
- create(projectKey, draft) {
1652
+ create(context, draft) {
1652
1653
  var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1653
1654
 
1654
- const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1655
+ const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1655
1656
  const resource = { ...getBaseResourceProperties(),
1656
1657
  cartState: 'Active',
1657
1658
  lineItems,
@@ -1669,11 +1670,11 @@ class CartRepository extends AbstractResourceRepository {
1669
1670
  locale: draft.locale,
1670
1671
  country: draft.country,
1671
1672
  origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1672
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1673
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1673
1674
  }; // @ts-ignore
1674
1675
 
1675
1676
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1676
- this.save(projectKey, resource);
1677
+ this.save(context, resource);
1677
1678
  return resource;
1678
1679
  }
1679
1680
 
@@ -1719,10 +1720,10 @@ class OrderRepository extends AbstractResourceRepository {
1719
1720
  constructor() {
1720
1721
  super(...arguments);
1721
1722
  this.actions = {
1722
- addPayment: (projectKey, resource, {
1723
+ addPayment: (context, resource, {
1723
1724
  payment
1724
1725
  }) => {
1725
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1726
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1726
1727
 
1727
1728
  if (!resolvedPayment) {
1728
1729
  throw new Error(`Payment ${payment.id} not found`);
@@ -1739,20 +1740,20 @@ class OrderRepository extends AbstractResourceRepository {
1739
1740
  id: payment.id
1740
1741
  });
1741
1742
  },
1742
- changeOrderState: (projectKey, resource, {
1743
+ changeOrderState: (context, resource, {
1743
1744
  orderState
1744
1745
  }) => {
1745
1746
  resource.orderState = orderState;
1746
1747
  },
1747
- changePaymentState: (projectKey, resource, {
1748
+ changePaymentState: (context, resource, {
1748
1749
  paymentState
1749
1750
  }) => {
1750
1751
  resource.paymentState = paymentState;
1751
1752
  },
1752
- transitionState: (projectKey, resource, {
1753
+ transitionState: (context, resource, {
1753
1754
  state
1754
1755
  }) => {
1755
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1756
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1756
1757
 
1757
1758
  if (!resolvedType) {
1758
1759
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1763,17 +1764,17 @@ class OrderRepository extends AbstractResourceRepository {
1763
1764
  id: resolvedType.id
1764
1765
  };
1765
1766
  },
1766
- setBillingAddress: (projectKey, resource, {
1767
+ setBillingAddress: (context, resource, {
1767
1768
  address
1768
1769
  }) => {
1769
1770
  resource.billingAddress = address;
1770
1771
  },
1771
- setCustomerEmail: (projectKey, resource, {
1772
+ setCustomerEmail: (context, resource, {
1772
1773
  email
1773
1774
  }) => {
1774
1775
  resource.customerEmail = email;
1775
1776
  },
1776
- setCustomField: (projectKey, resource, {
1777
+ setCustomField: (context, resource, {
1777
1778
  name,
1778
1779
  value
1779
1780
  }) => {
@@ -1783,14 +1784,14 @@ class OrderRepository extends AbstractResourceRepository {
1783
1784
 
1784
1785
  resource.custom.fields[name] = value;
1785
1786
  },
1786
- setCustomType: (projectKey, resource, {
1787
+ setCustomType: (context, resource, {
1787
1788
  type,
1788
1789
  fields
1789
1790
  }) => {
1790
1791
  if (!type) {
1791
1792
  resource.custom = undefined;
1792
1793
  } else {
1793
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1794
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1794
1795
 
1795
1796
  if (!resolvedType) {
1796
1797
  throw new Error(`Type ${type} not found`);
@@ -1805,27 +1806,27 @@ class OrderRepository extends AbstractResourceRepository {
1805
1806
  };
1806
1807
  }
1807
1808
  },
1808
- setLocale: (projectKey, resource, {
1809
+ setLocale: (context, resource, {
1809
1810
  locale
1810
1811
  }) => {
1811
1812
  resource.locale = locale;
1812
1813
  },
1813
- setOrderNumber: (projectKey, resource, {
1814
+ setOrderNumber: (context, resource, {
1814
1815
  orderNumber
1815
1816
  }) => {
1816
1817
  resource.orderNumber = orderNumber;
1817
1818
  },
1818
- setShippingAddress: (projectKey, resource, {
1819
+ setShippingAddress: (context, resource, {
1819
1820
  address
1820
1821
  }) => {
1821
1822
  resource.shippingAddress = address;
1822
1823
  },
1823
- setStore: (projectKey, resource, {
1824
+ setStore: (context, resource, {
1824
1825
  store
1825
1826
  }) => {
1826
1827
  if (!store) return;
1827
1828
 
1828
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1829
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1829
1830
 
1830
1831
  if (!resolvedType) {
1831
1832
  throw new Error(`No store found with key=${store.key}`);
@@ -1844,17 +1845,24 @@ class OrderRepository extends AbstractResourceRepository {
1844
1845
  return 'order';
1845
1846
  }
1846
1847
 
1847
- create(projectKey, draft) {
1848
+ create(context, draft) {
1848
1849
  assert(draft.cart, 'draft.cart is missing');
1850
+ return this.createFromCart(context, {
1851
+ id: draft.cart.id,
1852
+ typeId: 'cart'
1853
+ }, draft.orderNumber);
1854
+ }
1849
1855
 
1850
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1856
+ createFromCart(context, cartReference, orderNumber) {
1857
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1851
1858
 
1852
1859
  if (!cart) {
1853
1860
  throw new Error('Cannot find cart');
1854
1861
  }
1855
1862
 
1856
1863
  const resource = { ...getBaseResourceProperties(),
1857
- orderNumber: draft.orderNumber,
1864
+ orderNumber,
1865
+ cart: cartReference,
1858
1866
  orderState: 'Open',
1859
1867
  lineItems: [],
1860
1868
  customLineItems: [],
@@ -1862,13 +1870,17 @@ class OrderRepository extends AbstractResourceRepository {
1862
1870
  refusedGifts: [],
1863
1871
  origin: 'Customer',
1864
1872
  syncInfo: [],
1873
+ store: context.storeKey ? {
1874
+ key: context.storeKey,
1875
+ typeId: 'store'
1876
+ } : undefined,
1865
1877
  lastMessageSequenceNumber: 0
1866
1878
  };
1867
- this.save(projectKey, resource);
1879
+ this.save(context, resource);
1868
1880
  return resource;
1869
1881
  }
1870
1882
 
1871
- import(projectKey, draft) {
1883
+ import(context, draft) {
1872
1884
  var _draft$lineItems, _draft$customLineItem;
1873
1885
 
1874
1886
  // TODO: Check if order with given orderNumber already exists
@@ -1876,7 +1888,7 @@ class OrderRepository extends AbstractResourceRepository {
1876
1888
  const resource = { ...getBaseResourceProperties(),
1877
1889
  billingAddress: draft.billingAddress,
1878
1890
  shippingAddress: draft.shippingAddress,
1879
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1891
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1880
1892
  customerEmail: draft.customerEmail,
1881
1893
  lastMessageSequenceNumber: 0,
1882
1894
  orderNumber: draft.orderNumber,
@@ -1884,21 +1896,21 @@ class OrderRepository extends AbstractResourceRepository {
1884
1896
  origin: draft.origin || 'Customer',
1885
1897
  paymentState: draft.paymentState,
1886
1898
  refusedGifts: [],
1887
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1899
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1888
1900
  syncInfo: [],
1889
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1890
- customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1901
+ lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(context, item))) || [],
1902
+ customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(context, item))) || [],
1891
1903
  totalPrice: {
1892
1904
  type: 'centPrecision',
1893
1905
  ...draft.totalPrice,
1894
1906
  fractionDigits: 2
1895
1907
  }
1896
1908
  };
1897
- this.save(projectKey, resource);
1909
+ this.save(context, resource);
1898
1910
  return resource;
1899
1911
  }
1900
1912
 
1901
- lineItemFromImportDraft(projectKey, draft) {
1913
+ lineItemFromImportDraft(context, draft) {
1902
1914
  let product;
1903
1915
  let variant;
1904
1916
 
@@ -1908,7 +1920,7 @@ class OrderRepository extends AbstractResourceRepository {
1908
1920
  sku: draft.variant.sku
1909
1921
  };
1910
1922
 
1911
- var items = this._storage.query(projectKey, 'product', {
1923
+ var items = this._storage.query(context.projectKey, 'product', {
1912
1924
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1913
1925
  });
1914
1926
 
@@ -1935,7 +1947,7 @@ class OrderRepository extends AbstractResourceRepository {
1935
1947
  }
1936
1948
 
1937
1949
  const lineItem = { ...getBaseResourceProperties(),
1938
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1950
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1939
1951
  discountedPricePerQuantity: [],
1940
1952
  lineItemMode: 'Standard',
1941
1953
  name: draft.name,
@@ -1956,9 +1968,9 @@ class OrderRepository extends AbstractResourceRepository {
1956
1968
  return lineItem;
1957
1969
  }
1958
1970
 
1959
- customLineItemFromImportDraft(projectKey, draft) {
1971
+ customLineItemFromImportDraft(context, draft) {
1960
1972
  const lineItem = { ...getBaseResourceProperties(),
1961
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1973
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1962
1974
  discountedPricePerQuantity: [],
1963
1975
  money: createTypedMoney(draft.money),
1964
1976
  name: draft.name,
@@ -1970,8 +1982,8 @@ class OrderRepository extends AbstractResourceRepository {
1970
1982
  return lineItem;
1971
1983
  }
1972
1984
 
1973
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1974
- const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
1985
+ getWithOrderNumber(context, orderNumber, params = {}) {
1986
+ const result = this._storage.query(context.projectKey, this.getTypeId(), { ...params,
1975
1987
  where: [`orderNumber="${orderNumber}"`]
1976
1988
  });
1977
1989
 
@@ -2002,8 +2014,9 @@ class CartService extends AbstractService {
2002
2014
 
2003
2015
  extraRoutes(parent) {
2004
2016
  parent.post('/replicate', (request, response) => {
2005
- // @ts-ignore
2006
- const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(request.params.projectKey, request.body.reference.id) : this.repository.get(request.params.projectKey, request.body.reference.id);
2017
+ const context = helpers.getRepositoryContext(request); // @ts-ignore
2018
+
2019
+ const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(context, request.body.reference.id) : this.repository.get(context, request.body.reference.id);
2007
2020
 
2008
2021
  if (!cartOrOrder) {
2009
2022
  return response.status(400).send();
@@ -2019,7 +2032,7 @@ class CartService extends AbstractService {
2019
2032
  };
2020
2033
  })
2021
2034
  };
2022
- const newCart = this.repository.create(request.params.projectKey, cartDraft);
2035
+ const newCart = this.repository.create(context, cartDraft);
2023
2036
  return response.status(200).send(newCart);
2024
2037
  });
2025
2038
  }
@@ -2030,7 +2043,7 @@ class CategoryRepository extends AbstractResourceRepository {
2030
2043
  constructor() {
2031
2044
  super(...arguments);
2032
2045
  this.actions = {
2033
- changeAssetName: (projectKey, resource, {
2046
+ changeAssetName: (context, resource, {
2034
2047
  assetId,
2035
2048
  assetKey,
2036
2049
  name
@@ -2047,17 +2060,17 @@ class CategoryRepository extends AbstractResourceRepository {
2047
2060
  }
2048
2061
  });
2049
2062
  },
2050
- changeSlug: (projectKey, resource, {
2063
+ changeSlug: (context, resource, {
2051
2064
  slug
2052
2065
  }) => {
2053
2066
  resource.slug = slug;
2054
2067
  },
2055
- setKey: (projectKey, resource, {
2068
+ setKey: (context, resource, {
2056
2069
  key
2057
2070
  }) => {
2058
2071
  resource.key = key;
2059
2072
  },
2060
- setAssetDescription: (projectKey, resource, {
2073
+ setAssetDescription: (context, resource, {
2061
2074
  assetId,
2062
2075
  assetKey,
2063
2076
  description
@@ -2074,7 +2087,7 @@ class CategoryRepository extends AbstractResourceRepository {
2074
2087
  }
2075
2088
  });
2076
2089
  },
2077
- setAssetSources: (projectKey, resource, {
2090
+ setAssetSources: (context, resource, {
2078
2091
  assetId,
2079
2092
  assetKey,
2080
2093
  sources
@@ -2091,22 +2104,22 @@ class CategoryRepository extends AbstractResourceRepository {
2091
2104
  }
2092
2105
  });
2093
2106
  },
2094
- setDescription: (projectKey, resource, {
2107
+ setDescription: (context, resource, {
2095
2108
  description
2096
2109
  }) => {
2097
2110
  resource.description = description;
2098
2111
  },
2099
- setMetaDescription: (projectKey, resource, {
2112
+ setMetaDescription: (context, resource, {
2100
2113
  metaDescription
2101
2114
  }) => {
2102
2115
  resource.metaDescription = metaDescription;
2103
2116
  },
2104
- setMetaKeywords: (projectKey, resource, {
2117
+ setMetaKeywords: (context, resource, {
2105
2118
  metaKeywords
2106
2119
  }) => {
2107
2120
  resource.metaKeywords = metaKeywords;
2108
2121
  },
2109
- setMetaTitle: (projectKey, resource, {
2122
+ setMetaTitle: (context, resource, {
2110
2123
  metaTitle
2111
2124
  }) => {
2112
2125
  resource.metaTitle = metaTitle;
@@ -2118,7 +2131,7 @@ class CategoryRepository extends AbstractResourceRepository {
2118
2131
  return 'category';
2119
2132
  }
2120
2133
 
2121
- create(projectKey, draft) {
2134
+ create(context, draft) {
2122
2135
  var _draft$assets;
2123
2136
 
2124
2137
  const resource = { ...getBaseResourceProperties(),
@@ -2140,11 +2153,11 @@ class CategoryRepository extends AbstractResourceRepository {
2140
2153
  sources: d.sources,
2141
2154
  tags: d.tags,
2142
2155
  key: d.key,
2143
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2156
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2144
2157
  };
2145
2158
  })) || []
2146
2159
  };
2147
- this.save(projectKey, resource);
2160
+ this.save(context, resource);
2148
2161
  return resource;
2149
2162
  }
2150
2163
 
@@ -2167,12 +2180,12 @@ class ChannelRepository extends AbstractResourceRepository {
2167
2180
  return 'channel';
2168
2181
  }
2169
2182
 
2170
- create(projectKey, draft) {
2183
+ create(context, draft) {
2171
2184
  const resource = { ...getBaseResourceProperties(),
2172
2185
  key: draft.key,
2173
2186
  roles: draft.roles || []
2174
2187
  };
2175
- this.save(projectKey, resource);
2188
+ this.save(context, resource);
2176
2189
  return resource;
2177
2190
  }
2178
2191
 
@@ -2194,12 +2207,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2194
2207
  constructor() {
2195
2208
  super(...arguments);
2196
2209
  this.actions = {
2197
- setKey: (projectKey, resource, {
2210
+ setKey: (context, resource, {
2198
2211
  key
2199
2212
  }) => {
2200
2213
  resource.key = key;
2201
2214
  },
2202
- changeName: (projectKey, resource, {
2215
+ changeName: (context, resource, {
2203
2216
  name
2204
2217
  }) => {
2205
2218
  resource.name = name;
@@ -2211,12 +2224,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2211
2224
  return 'customer';
2212
2225
  }
2213
2226
 
2214
- create(projectKey, draft) {
2227
+ create(context, draft) {
2215
2228
  const resource = { ...getBaseResourceProperties(),
2216
2229
  key: draft.key,
2217
2230
  name: draft.groupName
2218
2231
  };
2219
- this.save(projectKey, resource);
2232
+ this.save(context, resource);
2220
2233
  return resource;
2221
2234
  }
2222
2235
 
@@ -2238,7 +2251,7 @@ class CustomerRepository extends AbstractResourceRepository {
2238
2251
  constructor() {
2239
2252
  super(...arguments);
2240
2253
  this.actions = {
2241
- changeEmail: (_projectKey, resource, {
2254
+ changeEmail: (_context, resource, {
2242
2255
  email
2243
2256
  }) => {
2244
2257
  resource.email = email;
@@ -2250,19 +2263,19 @@ class CustomerRepository extends AbstractResourceRepository {
2250
2263
  return 'customer';
2251
2264
  }
2252
2265
 
2253
- create(projectKey, draft) {
2266
+ create(context, draft) {
2254
2267
  const resource = { ...getBaseResourceProperties(),
2255
2268
  email: draft.email,
2256
2269
  password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2257
2270
  isEmailVerified: draft.isEmailVerified || false,
2258
2271
  addresses: []
2259
2272
  };
2260
- this.save(projectKey, resource);
2273
+ this.save(context, resource);
2261
2274
  return resource;
2262
2275
  }
2263
2276
 
2264
- getMe(projectKey) {
2265
- const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
2277
+ getMe(context) {
2278
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {}); // grab the first customer you can find
2266
2279
 
2267
2280
 
2268
2281
  if (results.count > 0) {
@@ -2286,10 +2299,12 @@ class CustomerService extends AbstractService {
2286
2299
 
2287
2300
  extraRoutes(parent) {
2288
2301
  parent.post('/password-token', (request, response) => {
2289
- const customer = this.repository.query(request.params.projectKey, {
2302
+ const customer = this.repository.query(helpers.getRepositoryContext(request), {
2290
2303
  where: [`email="${request.body.email}"`]
2291
- });
2292
- const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
2304
+ }); // @ts-ignore
2305
+
2306
+ const ttlMinutes = request.params.ttlMinutes ? // @ts-ignore
2307
+ +request.params.ttlMinutes : 34560;
2293
2308
  const {
2294
2309
  version,
2295
2310
  ...rest
@@ -2309,8 +2324,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2309
2324
  return 'key-value-document';
2310
2325
  }
2311
2326
 
2312
- create(projectKey, draft) {
2313
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2327
+ create(context, draft) {
2328
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2314
2329
  const baseProperties = getBaseResourceProperties();
2315
2330
 
2316
2331
  if (current) {
@@ -2338,12 +2353,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2338
2353
  key: draft.key,
2339
2354
  value: draft.value
2340
2355
  };
2341
- this.save(projectKey, resource);
2356
+ this.save(context, resource);
2342
2357
  return resource;
2343
2358
  }
2344
2359
 
2345
- getWithContainerAndKey(projectKey, container, key) {
2346
- const items = this._storage.all(projectKey, this.getTypeId());
2360
+ getWithContainerAndKey(context, container, key) {
2361
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2347
2362
 
2348
2363
  return items.find(item => item.container === container && item.key === key);
2349
2364
  }
@@ -2367,7 +2382,7 @@ class CustomObjectService extends AbstractService {
2367
2382
  }
2368
2383
 
2369
2384
  getWithContainerAndKey(request, response) {
2370
- const result = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2385
+ const result = this.repository.getWithContainerAndKey(helpers.getRepositoryContext(request), request.params.container, request.params.key);
2371
2386
 
2372
2387
  if (!result) {
2373
2388
  return response.status(404).send('Not Found');
@@ -2381,18 +2396,18 @@ class CustomObjectService extends AbstractService {
2381
2396
  key: request.params.key,
2382
2397
  container: request.params.container
2383
2398
  };
2384
- const result = this.repository.create(request.params.projectKey, draft);
2399
+ const result = this.repository.create(helpers.getRepositoryContext(request), draft);
2385
2400
  return response.status(200).send(result);
2386
2401
  }
2387
2402
 
2388
2403
  deleteWithContainerAndKey(request, response) {
2389
- const current = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2404
+ const current = this.repository.getWithContainerAndKey(helpers.getRepositoryContext(request), request.params.container, request.params.key);
2390
2405
 
2391
2406
  if (!current) {
2392
2407
  return response.status(404).send('Not Found');
2393
2408
  }
2394
2409
 
2395
- const result = this.repository.delete(request.params.projectKey, current.id);
2410
+ const result = this.repository.delete(helpers.getRepositoryContext(request), current.id);
2396
2411
  return response.status(200).send(result);
2397
2412
  }
2398
2413
 
@@ -2402,12 +2417,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2402
2417
  constructor() {
2403
2418
  super(...arguments);
2404
2419
  this.actions = {
2405
- changeIsActive: (projectKey, resource, {
2420
+ changeIsActive: (context, resource, {
2406
2421
  isActive
2407
2422
  }) => {
2408
2423
  resource.isActive = isActive;
2409
2424
  },
2410
- changeCartDiscounts: (projectKey, resource, {
2425
+ changeCartDiscounts: (context, resource, {
2411
2426
  cartDiscounts
2412
2427
  }) => {
2413
2428
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2415,42 +2430,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2415
2430
  id: obj.id
2416
2431
  }));
2417
2432
  },
2418
- setDescription: (projectKey, resource, {
2433
+ setDescription: (context, resource, {
2419
2434
  description
2420
2435
  }) => {
2421
2436
  resource.description = description;
2422
2437
  },
2423
- setCartPredicate: (projectKey, resource, {
2438
+ setCartPredicate: (context, resource, {
2424
2439
  cartPredicate
2425
2440
  }) => {
2426
2441
  resource.cartPredicate = cartPredicate;
2427
2442
  },
2428
- setName: (projectKey, resource, {
2443
+ setName: (context, resource, {
2429
2444
  name
2430
2445
  }) => {
2431
2446
  resource.name = name;
2432
2447
  },
2433
- setMaxApplications: (projectKey, resource, {
2448
+ setMaxApplications: (context, resource, {
2434
2449
  maxApplications
2435
2450
  }) => {
2436
2451
  resource.maxApplications = maxApplications;
2437
2452
  },
2438
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2453
+ setMaxApplicationsPerCustomer: (context, resource, {
2439
2454
  maxApplicationsPerCustomer
2440
2455
  }) => {
2441
2456
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2442
2457
  },
2443
- setValidFrom: (projectKey, resource, {
2458
+ setValidFrom: (context, resource, {
2444
2459
  validFrom
2445
2460
  }) => {
2446
2461
  resource.validFrom = validFrom;
2447
2462
  },
2448
- setValidUntil: (projectKey, resource, {
2463
+ setValidUntil: (context, resource, {
2449
2464
  validUntil
2450
2465
  }) => {
2451
2466
  resource.validUntil = validUntil;
2452
2467
  },
2453
- setValidFromAndUntil: (projectKey, resource, {
2468
+ setValidFromAndUntil: (context, resource, {
2454
2469
  validFrom,
2455
2470
  validUntil
2456
2471
  }) => {
@@ -2464,7 +2479,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2464
2479
  return 'cart-discount';
2465
2480
  }
2466
2481
 
2467
- create(projectKey, draft) {
2482
+ create(context, draft) {
2468
2483
  const resource = { ...getBaseResourceProperties(),
2469
2484
  applicationVersion: 1,
2470
2485
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2483,7 +2498,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2483
2498
  maxApplications: draft.maxApplications,
2484
2499
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2485
2500
  };
2486
- this.save(projectKey, resource);
2501
+ this.save(context, resource);
2487
2502
  return resource;
2488
2503
  }
2489
2504
 
@@ -2505,22 +2520,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2505
2520
  constructor() {
2506
2521
  super(...arguments);
2507
2522
  this.actions = {
2508
- setKey: (projectKey, resource, {
2523
+ setKey: (context, resource, {
2509
2524
  key
2510
2525
  }) => {
2511
2526
  resource.key = key;
2512
2527
  },
2513
- setTimeoutInMs: (projectKey, resource, {
2528
+ setTimeoutInMs: (context, resource, {
2514
2529
  timeoutInMs
2515
2530
  }) => {
2516
2531
  resource.timeoutInMs = timeoutInMs;
2517
2532
  },
2518
- changeTriggers: (projectKey, resource, {
2533
+ changeTriggers: (context, resource, {
2519
2534
  triggers
2520
2535
  }) => {
2521
2536
  resource.triggers = triggers;
2522
2537
  },
2523
- changeDestination: (projectKey, resource, {
2538
+ changeDestination: (context, resource, {
2524
2539
  destination
2525
2540
  }) => {
2526
2541
  resource.destination = destination;
@@ -2532,14 +2547,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2532
2547
  return 'extension';
2533
2548
  }
2534
2549
 
2535
- create(projectKey, draft) {
2550
+ create(context, draft) {
2536
2551
  const resource = { ...getBaseResourceProperties(),
2537
2552
  key: draft.key,
2538
2553
  timeoutInMs: draft.timeoutInMs,
2539
2554
  destination: draft.destination,
2540
2555
  triggers: draft.triggers
2541
2556
  };
2542
- this.save(projectKey, resource);
2557
+ this.save(context, resource);
2543
2558
  return resource;
2544
2559
  }
2545
2560
 
@@ -2561,19 +2576,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2561
2576
  constructor() {
2562
2577
  super(...arguments);
2563
2578
  this.actions = {
2564
- changeQuantity: (projectKey, resource, {
2579
+ changeQuantity: (context, resource, {
2565
2580
  quantity
2566
2581
  }) => {
2567
2582
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2568
2583
 
2569
2584
  resource.availableQuantity = quantity;
2570
2585
  },
2571
- setExpectedDelivery: (projectKey, resource, {
2586
+ setExpectedDelivery: (context, resource, {
2572
2587
  expectedDelivery
2573
2588
  }) => {
2574
2589
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2575
2590
  },
2576
- setCustomField: (projectKey, resource, {
2591
+ setCustomField: (context, resource, {
2577
2592
  name,
2578
2593
  value
2579
2594
  }) => {
@@ -2583,14 +2598,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2583
2598
 
2584
2599
  resource.custom.fields[name] = value;
2585
2600
  },
2586
- setCustomType: (projectKey, resource, {
2601
+ setCustomType: (context, resource, {
2587
2602
  type,
2588
2603
  fields
2589
2604
  }) => {
2590
2605
  if (!type) {
2591
2606
  resource.custom = undefined;
2592
2607
  } else {
2593
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2608
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2594
2609
 
2595
2610
  if (!resolvedType) {
2596
2611
  throw new Error(`Type ${type} not found`);
@@ -2605,7 +2620,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2605
2620
  };
2606
2621
  }
2607
2622
  },
2608
- setRestockableInDays: (projectKey, resource, {
2623
+ setRestockableInDays: (context, resource, {
2609
2624
  restockableInDays
2610
2625
  }) => {
2611
2626
  resource.restockableInDays = restockableInDays;
@@ -2617,7 +2632,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2617
2632
  return 'inventory-entry';
2618
2633
  }
2619
2634
 
2620
- create(projectKey, draft) {
2635
+ create(context, draft) {
2621
2636
  var _draft$supplyChannel$, _draft$supplyChannel;
2622
2637
 
2623
2638
  const resource = { ...getBaseResourceProperties(),
@@ -2630,9 +2645,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2630
2645
  typeId: 'channel',
2631
2646
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2632
2647
  },
2633
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2648
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2634
2649
  };
2635
- this.save(projectKey, resource);
2650
+ this.save(context, resource);
2636
2651
  return resource;
2637
2652
  }
2638
2653
 
@@ -2692,14 +2707,14 @@ class PaymentRepository extends AbstractResourceRepository {
2692
2707
  constructor() {
2693
2708
  super(...arguments);
2694
2709
 
2695
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2710
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2696
2711
  id: uuid.v4(),
2697
2712
  amount: createTypedMoney(draft.amount),
2698
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2713
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2699
2714
  });
2700
2715
 
2701
2716
  this.actions = {
2702
- setCustomField: (projectKey, resource, {
2717
+ setCustomField: (context, resource, {
2703
2718
  name,
2704
2719
  value
2705
2720
  }) => {
@@ -2709,14 +2724,14 @@ class PaymentRepository extends AbstractResourceRepository {
2709
2724
 
2710
2725
  resource.custom.fields[name] = value;
2711
2726
  },
2712
- setCustomType: (projectKey, resource, {
2727
+ setCustomType: (context, resource, {
2713
2728
  type,
2714
2729
  fields
2715
2730
  }) => {
2716
2731
  if (!type) {
2717
2732
  resource.custom = undefined;
2718
2733
  } else {
2719
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2734
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2720
2735
 
2721
2736
  if (!resolvedType) {
2722
2737
  throw new Error(`Type ${type} not found`);
@@ -2731,12 +2746,12 @@ class PaymentRepository extends AbstractResourceRepository {
2731
2746
  };
2732
2747
  }
2733
2748
  },
2734
- addTransaction: (projectKey, resource, {
2749
+ addTransaction: (context, resource, {
2735
2750
  transaction
2736
2751
  }) => {
2737
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2752
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2738
2753
  },
2739
- changeTransactionState: (_projectKey, resource, {
2754
+ changeTransactionState: (_context, resource, {
2740
2755
  transactionId,
2741
2756
  state
2742
2757
  }) => {
@@ -2746,10 +2761,10 @@ class PaymentRepository extends AbstractResourceRepository {
2746
2761
  };
2747
2762
  resource.transactions[index] = updatedTransaction;
2748
2763
  },
2749
- transitionState: (projectKey, resource, {
2764
+ transitionState: (context, resource, {
2750
2765
  state
2751
2766
  }) => {
2752
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2767
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2753
2768
 
2754
2769
  if (!stateObj) {
2755
2770
  throw new Error(`State ${state} not found`);
@@ -2768,18 +2783,18 @@ class PaymentRepository extends AbstractResourceRepository {
2768
2783
  return 'payment';
2769
2784
  }
2770
2785
 
2771
- create(projectKey, draft) {
2786
+ create(context, draft) {
2772
2787
  const resource = { ...getBaseResourceProperties(),
2773
2788
  amountPlanned: createTypedMoney(draft.amountPlanned),
2774
2789
  paymentMethodInfo: draft.paymentMethodInfo,
2775
2790
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2776
- state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
2791
+ state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, context.projectKey, this._storage) : undefined
2777
2792
  } : {},
2778
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2779
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2780
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2793
+ transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, context)),
2794
+ interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, context.projectKey, this._storage)),
2795
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2781
2796
  };
2782
- this.save(projectKey, resource);
2797
+ this.save(context, resource);
2783
2798
  return resource;
2784
2799
  }
2785
2800
 
@@ -2814,12 +2829,12 @@ class OrderService extends AbstractService {
2814
2829
 
2815
2830
  import(request, response) {
2816
2831
  const importDraft = request.body;
2817
- const resource = this.repository.import(request.params.projectKey, importDraft);
2832
+ const resource = this.repository.import(helpers.getRepositoryContext(request), importDraft);
2818
2833
  return response.status(200).send(resource);
2819
2834
  }
2820
2835
 
2821
2836
  getWithOrderNumber(request, response) {
2822
- const resource = this.repository.getWithOrderNumber(request.params.projectKey, request.params.orderNumber, request.query);
2837
+ const resource = this.repository.getWithOrderNumber(helpers.getRepositoryContext(request), request.params.orderNumber, request.query);
2823
2838
 
2824
2839
  if (resource) {
2825
2840
  return response.status(200).send(resource);
@@ -2871,7 +2886,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2871
2886
  return 'product-projection';
2872
2887
  }
2873
2888
 
2874
- create(projectKey, draft) {
2889
+ create(context, draft) {
2875
2890
  var _draft$variants$map, _draft$variants;
2876
2891
 
2877
2892
  if (!draft.masterVariant) {
@@ -2896,17 +2911,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2896
2911
  // @ts-ignore
2897
2912
  searchKeywords: draft.searchKeywords
2898
2913
  };
2899
- this.save(projectKey, resource);
2914
+ this.save(context, resource);
2900
2915
  return resource;
2901
2916
  }
2902
2917
 
2903
- search(projectKey, query) {
2918
+ search(context, query) {
2904
2919
  var _query$filterQuery;
2905
2920
 
2906
2921
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2907
2922
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2908
2923
 
2909
- const results = this._storage.query(projectKey, this.getTypeId(), {
2924
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2910
2925
  where: wherePredicate,
2911
2926
  offset: query.offset ? Number(query.offset) : undefined,
2912
2927
  limit: query.limit ? Number(query.limit) : undefined
@@ -2941,7 +2956,7 @@ class ProductProjectionService extends AbstractService {
2941
2956
  }
2942
2957
 
2943
2958
  search(request, response) {
2944
- const resource = this.repository.search(request.params.projectKey, request.query);
2959
+ const resource = this.repository.search(helpers.getRepositoryContext(request), request.query);
2945
2960
  return response.status(200).send(resource);
2946
2961
  }
2947
2962
 
@@ -2951,7 +2966,7 @@ class ProductRepository extends AbstractResourceRepository {
2951
2966
  constructor() {
2952
2967
  super(...arguments);
2953
2968
  this.actions = {
2954
- publish: (projectKey, resource, {
2969
+ publish: (context, resource, {
2955
2970
  scope
2956
2971
  }) => {
2957
2972
  if (resource.masterData.staged) {
@@ -2963,7 +2978,7 @@ class ProductRepository extends AbstractResourceRepository {
2963
2978
  resource.masterData.hasStagedChanges = false;
2964
2979
  resource.masterData.published = true;
2965
2980
  },
2966
- setAttribute: (projectKey, resource, {
2981
+ setAttribute: (context, resource, {
2967
2982
  variantId,
2968
2983
  sku,
2969
2984
  name,
@@ -3024,7 +3039,7 @@ class ProductRepository extends AbstractResourceRepository {
3024
3039
  return 'product';
3025
3040
  }
3026
3041
 
3027
- create(projectKey, draft) {
3042
+ create(context, draft) {
3028
3043
  var _draft$publish, _draft$publish2;
3029
3044
 
3030
3045
  const productData = {
@@ -3048,7 +3063,7 @@ class ProductRepository extends AbstractResourceRepository {
3048
3063
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3049
3064
  }
3050
3065
  };
3051
- this.save(projectKey, resource);
3066
+ this.save(context, resource);
3052
3067
  return resource;
3053
3068
  }
3054
3069
 
@@ -3108,7 +3123,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3108
3123
  constructor() {
3109
3124
  super(...arguments);
3110
3125
 
3111
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3126
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3112
3127
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3113
3128
 
3114
3129
  return { ...draft,
@@ -3119,7 +3134,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3119
3134
  };
3120
3135
 
3121
3136
  this.actions = {
3122
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3137
+ changeLocalizedEnumValueLabel: (context, resource, {
3123
3138
  attributeName,
3124
3139
  newValue
3125
3140
  }) => {
@@ -3147,7 +3162,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3147
3162
  }
3148
3163
  });
3149
3164
  },
3150
- changeLabel: (projectKey, resource, {
3165
+ changeLabel: (context, resource, {
3151
3166
  attributeName,
3152
3167
  label
3153
3168
  }) => {
@@ -3166,21 +3181,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3166
3181
  return 'product-type';
3167
3182
  }
3168
3183
 
3169
- create(projectKey, draft) {
3184
+ create(context, draft) {
3170
3185
  var _draft$attributes;
3171
3186
 
3172
3187
  const resource = { ...getBaseResourceProperties(),
3173
3188
  key: draft.key,
3174
3189
  name: draft.name,
3175
3190
  description: draft.description,
3176
- attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(projectKey, a))
3191
+ attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(context, a))
3177
3192
  };
3178
- this.save(projectKey, resource);
3193
+ this.save(context, resource);
3179
3194
  return resource;
3180
3195
  }
3181
3196
 
3182
- getWithKey(projectKey, key) {
3183
- const result = this._storage.query(projectKey, this.getTypeId(), {
3197
+ getWithKey(context, key) {
3198
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3184
3199
  where: [`key="${key}"`]
3185
3200
  });
3186
3201
 
@@ -3213,7 +3228,7 @@ class ProductTypeService extends AbstractService {
3213
3228
  }
3214
3229
 
3215
3230
  getWithKey(request, response) {
3216
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3231
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3217
3232
 
3218
3233
  if (resource) {
3219
3234
  return response.status(200).send(resource);
@@ -3250,32 +3265,32 @@ class ProjectRepository extends AbstractRepository {
3250
3265
  constructor() {
3251
3266
  super(...arguments);
3252
3267
  this.actions = {
3253
- changeName: (projectKey, resource, {
3268
+ changeName: (context, resource, {
3254
3269
  name
3255
3270
  }) => {
3256
3271
  resource.name = name;
3257
3272
  },
3258
- changeCurrencies: (projectKey, resource, {
3273
+ changeCurrencies: (context, resource, {
3259
3274
  currencies
3260
3275
  }) => {
3261
3276
  resource.currencies = currencies;
3262
3277
  },
3263
- changeCountries: (projectKey, resource, {
3278
+ changeCountries: (context, resource, {
3264
3279
  countries
3265
3280
  }) => {
3266
3281
  resource.countries = countries;
3267
3282
  },
3268
- changeLanguages: (projectKey, resource, {
3283
+ changeLanguages: (context, resource, {
3269
3284
  languages
3270
3285
  }) => {
3271
3286
  resource.languages = languages;
3272
3287
  },
3273
- changeMessagesEnabled: (projectKey, resource, {
3288
+ changeMessagesEnabled: (context, resource, {
3274
3289
  messagesEnabled
3275
3290
  }) => {
3276
3291
  resource.messages.enabled = messagesEnabled;
3277
3292
  },
3278
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3293
+ changeProductSearchIndexingEnabled: (context, resource, {
3279
3294
  enabled
3280
3295
  }) => {
3281
3296
  var _resource$searchIndex;
@@ -3287,7 +3302,7 @@ class ProjectRepository extends AbstractRepository {
3287
3302
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3288
3303
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3289
3304
  },
3290
- changeOrderSearchStatus: (projectKey, resource, {
3305
+ changeOrderSearchStatus: (context, resource, {
3291
3306
  status
3292
3307
  }) => {
3293
3308
  var _resource$searchIndex2;
@@ -3299,22 +3314,22 @@ class ProjectRepository extends AbstractRepository {
3299
3314
  resource.searchIndexing.orders.status = status;
3300
3315
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3301
3316
  },
3302
- setShippingRateInputType: (projectKey, resource, {
3317
+ setShippingRateInputType: (context, resource, {
3303
3318
  shippingRateInputType
3304
3319
  }) => {
3305
3320
  resource.shippingRateInputType = shippingRateInputType;
3306
3321
  },
3307
- setExternalOAuth: (projectKey, resource, {
3322
+ setExternalOAuth: (context, resource, {
3308
3323
  externalOAuth
3309
3324
  }) => {
3310
3325
  resource.externalOAuth = externalOAuth;
3311
3326
  },
3312
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3327
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3313
3328
  countryTaxRateFallbackEnabled
3314
3329
  }) => {
3315
3330
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3316
3331
  },
3317
- changeCartsConfiguration: (projectKey, resource, {
3332
+ changeCartsConfiguration: (context, resource, {
3318
3333
  cartsConfiguration
3319
3334
  }) => {
3320
3335
  resource.carts = cartsConfiguration || {
@@ -3325,15 +3340,15 @@ class ProjectRepository extends AbstractRepository {
3325
3340
  };
3326
3341
  }
3327
3342
 
3328
- get(projectKey) {
3329
- const resource = this._storage.getProject(projectKey);
3343
+ get(context) {
3344
+ const resource = this._storage.getProject(context.projectKey);
3330
3345
 
3331
3346
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3332
3347
  return masked;
3333
3348
  }
3334
3349
 
3335
- save(projectKey, resource) {
3336
- const current = this.get(projectKey);
3350
+ save(context, resource) {
3351
+ const current = this.get(context);
3337
3352
 
3338
3353
  if (current) {
3339
3354
  checkConcurrentModification(current, resource.version);
@@ -3366,20 +3381,19 @@ class ProjectService {
3366
3381
  }
3367
3382
 
3368
3383
  get(request, response) {
3369
- const projectKey = request.params.projectKey;
3370
- const project = this.repository.get(projectKey);
3384
+ const project = this.repository.get(helpers.getRepositoryContext(request));
3371
3385
  return response.status(200).send(project);
3372
3386
  }
3373
3387
 
3374
3388
  post(request, response) {
3375
3389
  const updateRequest = request.body;
3376
- const project = this.repository.get(request.params.projectKey);
3390
+ const project = this.repository.get(helpers.getRepositoryContext(request));
3377
3391
 
3378
3392
  if (!project) {
3379
3393
  return response.status(404).send({});
3380
3394
  }
3381
3395
 
3382
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3396
+ this.repository.processUpdateActions(helpers.getRepositoryContext(request), project, updateRequest.actions);
3383
3397
  return response.status(200).send({});
3384
3398
  }
3385
3399
 
@@ -3389,11 +3403,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3389
3403
  constructor() {
3390
3404
  super(...arguments);
3391
3405
 
3392
- this._transformZoneRateDraft = (projectKey, draft) => {
3406
+ this._transformZoneRateDraft = (context, draft) => {
3393
3407
  var _draft$shippingRates;
3394
3408
 
3395
3409
  return { ...draft,
3396
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3410
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3397
3411
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3398
3412
  };
3399
3413
  };
@@ -3407,7 +3421,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3407
3421
  };
3408
3422
 
3409
3423
  this.actions = {
3410
- addShippingRate: (_projectKey, resource, {
3424
+ addShippingRate: (_context, resource, {
3411
3425
  shippingRate,
3412
3426
  zone
3413
3427
  }) => {
@@ -3427,7 +3441,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3427
3441
  shippingRates: [rate]
3428
3442
  });
3429
3443
  },
3430
- removeShippingRate: (_projectKey, resource, {
3444
+ removeShippingRate: (_context, resource, {
3431
3445
  shippingRate,
3432
3446
  zone
3433
3447
  }) => {
@@ -3441,10 +3455,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3441
3455
  }
3442
3456
  });
3443
3457
  },
3444
- addZone: (projectKey, resource, {
3458
+ addZone: (context, resource, {
3445
3459
  zone
3446
3460
  }) => {
3447
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3461
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3448
3462
 
3449
3463
  if (resource.zoneRates === undefined) {
3450
3464
  resource.zoneRates = [];
@@ -3455,39 +3469,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3455
3469
  shippingRates: []
3456
3470
  });
3457
3471
  },
3458
- removeZone: (_projectKey, resource, {
3472
+ removeZone: (_context, resource, {
3459
3473
  zone
3460
3474
  }) => {
3461
3475
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3462
3476
  return zoneRate.zone.id !== zone.id;
3463
3477
  });
3464
3478
  },
3465
- setKey: (_projectKey, resource, {
3479
+ setKey: (_context, resource, {
3466
3480
  key
3467
3481
  }) => {
3468
3482
  resource.key = key;
3469
3483
  },
3470
- setDescription: (_projectKey, resource, {
3484
+ setDescription: (_context, resource, {
3471
3485
  description
3472
3486
  }) => {
3473
3487
  resource.description = description;
3474
3488
  },
3475
- setLocalizedDescription: (_projectKey, resource, {
3489
+ setLocalizedDescription: (_context, resource, {
3476
3490
  localizedDescription
3477
3491
  }) => {
3478
3492
  resource.localizedDescription = localizedDescription;
3479
3493
  },
3480
- setPredicate: (_projectKey, resource, {
3494
+ setPredicate: (_context, resource, {
3481
3495
  predicate
3482
3496
  }) => {
3483
3497
  resource.predicate = predicate;
3484
3498
  },
3485
- changeIsDefault: (_projectKey, resource, {
3499
+ changeIsDefault: (_context, resource, {
3486
3500
  isDefault
3487
3501
  }) => {
3488
3502
  resource.isDefault = isDefault;
3489
3503
  },
3490
- changeName: (_projectKey, resource, {
3504
+ changeName: (_context, resource, {
3491
3505
  name
3492
3506
  }) => {
3493
3507
  resource.name = name;
@@ -3499,16 +3513,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3499
3513
  return 'shipping-method';
3500
3514
  }
3501
3515
 
3502
- create(projectKey, draft) {
3516
+ create(context, draft) {
3503
3517
  var _draft$zoneRates;
3504
3518
 
3505
3519
  const resource = { ...getBaseResourceProperties(),
3506
3520
  ...draft,
3507
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3508
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3509
- custom: createCustomFields(draft.custom, projectKey, this._storage)
3521
+ taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, context.projectKey, this._storage),
3522
+ zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(context, z)),
3523
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
3510
3524
  };
3511
- this.save(projectKey, resource);
3525
+ this.save(context, resource);
3512
3526
  return resource;
3513
3527
  }
3514
3528
 
@@ -3536,13 +3550,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3536
3550
  return 'shopping-list';
3537
3551
  }
3538
3552
 
3539
- create(projectKey, draft) {
3553
+ create(context, draft) {
3540
3554
  var _draft$lineItems, _draft$store;
3541
3555
 
3542
3556
  // const product =
3543
3557
  const resource = { ...getBaseResourceProperties(),
3544
3558
  ...draft,
3545
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3559
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3546
3560
  textLineItems: [],
3547
3561
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3548
3562
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3557,16 +3571,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3557
3571
  typeId: 'product-type',
3558
3572
  id: ''
3559
3573
  },
3560
- custom: createCustomFields(e.custom, projectKey, this._storage)
3574
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3561
3575
  };
3562
3576
  }),
3563
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3577
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3564
3578
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3565
3579
  typeId: 'store',
3566
3580
  key: draft.store.key
3567
3581
  } : undefined
3568
3582
  };
3569
- this.save(projectKey, resource);
3583
+ this.save(context, resource);
3570
3584
  return resource;
3571
3585
  }
3572
3586
 
@@ -3588,22 +3602,22 @@ class StateRepository extends AbstractResourceRepository {
3588
3602
  constructor() {
3589
3603
  super(...arguments);
3590
3604
  this.actions = {
3591
- changeKey: (projectKey, resource, {
3605
+ changeKey: (context, resource, {
3592
3606
  key
3593
3607
  }) => {
3594
3608
  resource.key = key;
3595
3609
  },
3596
- setDescription: (projectKey, resource, {
3610
+ setDescription: (context, resource, {
3597
3611
  description
3598
3612
  }) => {
3599
3613
  resource.description = description;
3600
3614
  },
3601
- setName: (projectKey, resource, {
3615
+ setName: (context, resource, {
3602
3616
  name
3603
3617
  }) => {
3604
3618
  resource.name = name;
3605
3619
  },
3606
- setRoles: (projectKey, resource, {
3620
+ setRoles: (context, resource, {
3607
3621
  roles
3608
3622
  }) => {
3609
3623
  resource.roles = roles;
@@ -3615,14 +3629,14 @@ class StateRepository extends AbstractResourceRepository {
3615
3629
  return 'state';
3616
3630
  }
3617
3631
 
3618
- create(projectKey, draft) {
3632
+ create(context, draft) {
3619
3633
  const resource = { ...getBaseResourceProperties(),
3620
3634
  ...draft,
3621
3635
  builtIn: false,
3622
3636
  initial: draft.initial || false,
3623
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3637
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3624
3638
  };
3625
- this.save(projectKey, resource);
3639
+ this.save(context, resource);
3626
3640
  return resource;
3627
3641
  }
3628
3642
 
@@ -3644,17 +3658,17 @@ class StoreRepository extends AbstractResourceRepository {
3644
3658
  constructor() {
3645
3659
  super(...arguments);
3646
3660
  this.actions = {
3647
- setName: (projectKey, resource, {
3661
+ setName: (context, resource, {
3648
3662
  name
3649
3663
  }) => {
3650
3664
  resource.name = name;
3651
3665
  },
3652
- setDistributionChannels: (projectKey, resource, {
3666
+ setDistributionChannels: (context, resource, {
3653
3667
  distributionChannels
3654
3668
  }) => {
3655
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3669
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3656
3670
  },
3657
- setLanguages: (projectKey, resource, {
3671
+ setLanguages: (context, resource, {
3658
3672
  languages
3659
3673
  }) => {
3660
3674
  resource.languages = languages;
@@ -3666,25 +3680,25 @@ class StoreRepository extends AbstractResourceRepository {
3666
3680
  return 'store';
3667
3681
  }
3668
3682
 
3669
- create(projectKey, draft) {
3683
+ create(context, draft) {
3670
3684
  const resource = { ...getBaseResourceProperties(),
3671
3685
  key: draft.key,
3672
3686
  name: draft.name,
3673
3687
  languages: draft.languages,
3674
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3675
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3688
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3689
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3676
3690
  };
3677
- this.save(projectKey, resource);
3691
+ this.save(context, resource);
3678
3692
  return resource;
3679
3693
  }
3680
3694
 
3681
- transformChannels(projectKey, channels) {
3695
+ transformChannels(context, channels) {
3682
3696
  if (!channels) return [];
3683
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3697
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3684
3698
  }
3685
3699
 
3686
- getWithKey(projectKey, key) {
3687
- const result = this._storage.query(projectKey, this.getTypeId(), {
3700
+ getWithKey(context, key) {
3701
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3688
3702
  where: [`key="${key}"`]
3689
3703
  });
3690
3704
 
@@ -3716,7 +3730,7 @@ class StoreService extends AbstractService {
3716
3730
  }
3717
3731
 
3718
3732
  getWithKey(request, response) {
3719
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3733
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3720
3734
 
3721
3735
  if (resource) {
3722
3736
  return response.status(200).send(resource);
@@ -3732,7 +3746,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3732
3746
  return 'subscription';
3733
3747
  }
3734
3748
 
3735
- create(projectKey, draft) {
3749
+ create(context, draft) {
3736
3750
  // TODO: We could actually test this here by using the aws sdk. For now
3737
3751
  // hardcode a failed check when account id is 0000000000
3738
3752
  if (draft.destination.type === 'SQS') {
@@ -3758,7 +3772,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3758
3772
  messages: draft.messages || [],
3759
3773
  status: 'Healthy'
3760
3774
  };
3761
- this.save(projectKey, resource);
3775
+ this.save(context, resource);
3762
3776
  return resource;
3763
3777
  }
3764
3778
 
@@ -3786,7 +3800,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3786
3800
  });
3787
3801
 
3788
3802
  this.actions = {
3789
- addTaxRate: (projectKey, resource, {
3803
+ addTaxRate: (context, resource, {
3790
3804
  taxRate
3791
3805
  }) => {
3792
3806
  if (resource.rates === undefined) {
@@ -3795,7 +3809,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3795
3809
 
3796
3810
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3797
3811
  },
3798
- removeTaxRate: (projectKey, resource, {
3812
+ removeTaxRate: (context, resource, {
3799
3813
  taxRateId
3800
3814
  }) => {
3801
3815
  if (resource.rates === undefined) {
@@ -3806,7 +3820,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3806
3820
  return taxRate.id !== taxRateId;
3807
3821
  });
3808
3822
  },
3809
- replaceTaxRate: (projectKey, resource, {
3823
+ replaceTaxRate: (context, resource, {
3810
3824
  taxRateId,
3811
3825
  taxRate
3812
3826
  }) => {
@@ -3824,17 +3838,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3824
3838
  }
3825
3839
  }
3826
3840
  },
3827
- setDescription: (projectKey, resource, {
3841
+ setDescription: (context, resource, {
3828
3842
  description
3829
3843
  }) => {
3830
3844
  resource.description = description;
3831
3845
  },
3832
- setKey: (projectKey, resource, {
3846
+ setKey: (context, resource, {
3833
3847
  key
3834
3848
  }) => {
3835
3849
  resource.key = key;
3836
3850
  },
3837
- changeName: (projectKey, resource, {
3851
+ changeName: (context, resource, {
3838
3852
  name
3839
3853
  }) => {
3840
3854
  resource.name = name;
@@ -3846,19 +3860,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3846
3860
  return 'tax-category';
3847
3861
  }
3848
3862
 
3849
- create(projectKey, draft) {
3863
+ create(context, draft) {
3850
3864
  var _draft$rates;
3851
3865
 
3852
3866
  const resource = { ...getBaseResourceProperties(),
3853
3867
  ...draft,
3854
3868
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3855
3869
  };
3856
- this.save(projectKey, resource);
3870
+ this.save(context, resource);
3857
3871
  return resource;
3858
3872
  }
3859
3873
 
3860
- getWithKey(projectKey, key) {
3861
- const result = this._storage.query(projectKey, this.getTypeId(), {
3874
+ getWithKey(context, key) {
3875
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3862
3876
  where: [`key="${key}"`]
3863
3877
  });
3864
3878
 
@@ -3891,7 +3905,7 @@ class TaxCategoryService extends AbstractService {
3891
3905
  }
3892
3906
 
3893
3907
  getWithKey(request, response) {
3894
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3908
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3895
3909
 
3896
3910
  if (resource) {
3897
3911
  return response.status(200).send(resource);
@@ -3906,29 +3920,29 @@ class TypeRepository extends AbstractResourceRepository {
3906
3920
  constructor() {
3907
3921
  super(...arguments);
3908
3922
  this.actions = {
3909
- addFieldDefinition: (projectKey, resource, {
3923
+ addFieldDefinition: (context, resource, {
3910
3924
  fieldDefinition
3911
3925
  }) => {
3912
3926
  resource.fieldDefinitions.push(fieldDefinition);
3913
3927
  },
3914
- removeFieldDefinition: (projectKey, resource, {
3928
+ removeFieldDefinition: (context, resource, {
3915
3929
  fieldName
3916
3930
  }) => {
3917
3931
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3918
3932
  return f.name !== fieldName;
3919
3933
  });
3920
3934
  },
3921
- setDescription: (projectKey, resource, {
3935
+ setDescription: (context, resource, {
3922
3936
  description
3923
3937
  }) => {
3924
3938
  resource.description = description;
3925
3939
  },
3926
- changeName: (projectKey, resource, {
3940
+ changeName: (context, resource, {
3927
3941
  name
3928
3942
  }) => {
3929
3943
  resource.name = name;
3930
3944
  },
3931
- changeFieldDefinitionOrder: (projectKey, resource, {
3945
+ changeFieldDefinitionOrder: (context, resource, {
3932
3946
  fieldNames
3933
3947
  }) => {
3934
3948
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3952,7 +3966,7 @@ class TypeRepository extends AbstractResourceRepository {
3952
3966
 
3953
3967
  resource.fieldDefinitions.push(...current);
3954
3968
  },
3955
- addEnumValue: (projectKey, resource, {
3969
+ addEnumValue: (context, resource, {
3956
3970
  fieldName,
3957
3971
  value
3958
3972
  }) => {
@@ -3969,7 +3983,7 @@ class TypeRepository extends AbstractResourceRepository {
3969
3983
  }
3970
3984
  });
3971
3985
  },
3972
- changeEnumValueLabel: (projectKey, resource, {
3986
+ changeEnumValueLabel: (context, resource, {
3973
3987
  fieldName,
3974
3988
  value
3975
3989
  }) => {
@@ -4001,7 +4015,7 @@ class TypeRepository extends AbstractResourceRepository {
4001
4015
  return 'type';
4002
4016
  }
4003
4017
 
4004
- create(projectKey, draft) {
4018
+ create(context, draft) {
4005
4019
  const resource = { ...getBaseResourceProperties(),
4006
4020
  key: draft.key,
4007
4021
  name: draft.name,
@@ -4009,7 +4023,7 @@ class TypeRepository extends AbstractResourceRepository {
4009
4023
  fieldDefinitions: draft.fieldDefinitions || [],
4010
4024
  description: draft.description
4011
4025
  };
4012
- this.save(projectKey, resource);
4026
+ this.save(context, resource);
4013
4027
  return resource;
4014
4028
  }
4015
4029
 
@@ -4031,29 +4045,29 @@ class ZoneRepository extends AbstractResourceRepository {
4031
4045
  constructor() {
4032
4046
  super(...arguments);
4033
4047
  this.actions = {
4034
- addLocation: (projectKey, resource, {
4048
+ addLocation: (context, resource, {
4035
4049
  location
4036
4050
  }) => {
4037
4051
  resource.locations.push(location);
4038
4052
  },
4039
- removeLocation: (projectKey, resource, {
4053
+ removeLocation: (context, resource, {
4040
4054
  location
4041
4055
  }) => {
4042
4056
  resource.locations = resource.locations.filter(loc => {
4043
4057
  return !(loc.country === location.country && loc.state === location.state);
4044
4058
  });
4045
4059
  },
4046
- changeName: (projectKey, resource, {
4060
+ changeName: (context, resource, {
4047
4061
  name
4048
4062
  }) => {
4049
4063
  resource.name = name;
4050
4064
  },
4051
- setDescription: (projectKey, resource, {
4065
+ setDescription: (context, resource, {
4052
4066
  description
4053
4067
  }) => {
4054
4068
  resource.description = description;
4055
4069
  },
4056
- setKey: (projectKey, resource, {
4070
+ setKey: (context, resource, {
4057
4071
  key
4058
4072
  }) => {
4059
4073
  resource.key = key;
@@ -4065,14 +4079,14 @@ class ZoneRepository extends AbstractResourceRepository {
4065
4079
  return 'zone';
4066
4080
  }
4067
4081
 
4068
- create(projectKey, draft) {
4082
+ create(context, draft) {
4069
4083
  const resource = { ...getBaseResourceProperties(),
4070
4084
  key: draft.key,
4071
4085
  locations: draft.locations || [],
4072
4086
  name: draft.name,
4073
4087
  description: draft.description
4074
4088
  };
4075
- this.save(projectKey, resource);
4089
+ this.save(context, resource);
4076
4090
  return resource;
4077
4091
  }
4078
4092
 
@@ -4114,7 +4128,7 @@ class MyCustomerService extends AbstractService {
4114
4128
  }
4115
4129
 
4116
4130
  getMe(request, response) {
4117
- const resource = this.repository.getMe(request.params.projectKey);
4131
+ const resource = this.repository.getMe(helpers.getRepositoryContext(request));
4118
4132
 
4119
4133
  if (!resource) {
4120
4134
  return response.status(404).send('Not found');
@@ -4125,7 +4139,7 @@ class MyCustomerService extends AbstractService {
4125
4139
 
4126
4140
  signUp(request, response) {
4127
4141
  const draft = request.body;
4128
- const resource = this.repository.create(request.params.projectKey, draft);
4142
+ const resource = this.repository.create(helpers.getRepositoryContext(request), draft);
4129
4143
 
4130
4144
  const result = this._expandWithId(request, resource.id);
4131
4145
 
@@ -4140,7 +4154,7 @@ class MyCustomerService extends AbstractService {
4140
4154
  password
4141
4155
  } = request.body;
4142
4156
  const encodedPassword = Buffer.from(password).toString('base64');
4143
- const result = this.repository.query(request.params.projectKey, {
4157
+ const result = this.repository.query(helpers.getRepositoryContext(request), {
4144
4158
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4145
4159
  });
4146
4160
 
@@ -4161,10 +4175,22 @@ class MyCustomerService extends AbstractService {
4161
4175
 
4162
4176
  }
4163
4177
 
4178
+ class MyOrderRepository extends OrderRepository {
4179
+ create(context, draft) {
4180
+ assert(draft.id, 'draft.id is missing');
4181
+ const cartIdentifier = {
4182
+ id: draft.id,
4183
+ typeId: 'cart'
4184
+ };
4185
+ return this.createFromCart(context, cartIdentifier);
4186
+ }
4187
+
4188
+ }
4189
+
4164
4190
  class MyOrderService extends AbstractService {
4165
4191
  constructor(parent, storage) {
4166
4192
  super(parent);
4167
- this.repository = new OrderRepository(storage);
4193
+ this.repository = new MyOrderRepository(storage);
4168
4194
  }
4169
4195
 
4170
4196
  getBasePath() {
@@ -4267,8 +4293,10 @@ class CommercetoolsMock {
4267
4293
 
4268
4294
  if (this.options.enableAuthentication) {
4269
4295
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4296
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4270
4297
  } else {
4271
4298
  app.use('/:projectKey', projectRouter);
4299
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4272
4300
  }
4273
4301
 
4274
4302
  this._projectService = new ProjectService(projectRouter, this._storage);