@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
@@ -9,6 +9,7 @@ import auth from 'basic-auth';
9
9
  import bodyParser from 'body-parser';
10
10
  import { randomBytes } from 'crypto';
11
11
  import { v4 } from 'uuid';
12
+ import { getRepositoryContext } from 'repositories/helpers';
12
13
  import deepEqual from 'deep-equal';
13
14
 
14
15
  const parseExpandClause = clause => {
@@ -982,7 +983,7 @@ class AbstractService {
982
983
 
983
984
  const offset = this._parseParam(request.query.offset);
984
985
 
985
- const result = this.repository.query(request.params.projectKey, {
986
+ const result = this.repository.query(getRepositoryContext(request), {
986
987
  expand: this._parseParam(request.query.expand),
987
988
  where: this._parseParam(request.query.where),
988
989
  limit: limit !== undefined ? Number(limit) : undefined,
@@ -1002,7 +1003,7 @@ class AbstractService {
1002
1003
  }
1003
1004
 
1004
1005
  getWithKey(request, response) {
1005
- const result = this.repository.getByKey(request.params.projectKey, request.params['key'], {
1006
+ const result = this.repository.getByKey(getRepositoryContext(request), request.params['key'], {
1006
1007
  expand: this._parseParam(request.query.expand)
1007
1008
  });
1008
1009
  if (!result) return response.status(404).send();
@@ -1010,7 +1011,7 @@ class AbstractService {
1010
1011
  }
1011
1012
 
1012
1013
  deletewithId(request, response) {
1013
- const result = this.repository.delete(request.params.projectKey, request.params['id'], {
1014
+ const result = this.repository.delete(getRepositoryContext(request), request.params['id'], {
1014
1015
  expand: this._parseParam(request.query.expand)
1015
1016
  });
1016
1017
 
@@ -1027,7 +1028,7 @@ class AbstractService {
1027
1028
 
1028
1029
  post(request, response) {
1029
1030
  const draft = request.body;
1030
- const resource = this.repository.create(request.params.projectKey, draft);
1031
+ const resource = this.repository.create(getRepositoryContext(request), draft);
1031
1032
 
1032
1033
  const result = this._expandWithId(request, resource.id);
1033
1034
 
@@ -1036,7 +1037,7 @@ class AbstractService {
1036
1037
 
1037
1038
  postWithId(request, response) {
1038
1039
  const updateRequest = request.body;
1039
- const resource = this.repository.get(request.params.projectKey, request.params['id']);
1040
+ const resource = this.repository.get(getRepositoryContext(request), request.params['id']);
1040
1041
 
1041
1042
  if (!resource) {
1042
1043
  return response.status(404).send('Not found');
@@ -1046,7 +1047,7 @@ class AbstractService {
1046
1047
  return response.status(409).send('Concurrent modification');
1047
1048
  }
1048
1049
 
1049
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, resource, updateRequest.actions);
1050
+ const updatedResource = this.repository.processUpdateActions(getRepositoryContext(request), resource, updateRequest.actions);
1050
1051
 
1051
1052
  const result = this._expandWithId(request, updatedResource.id);
1052
1053
 
@@ -1058,7 +1059,7 @@ class AbstractService {
1058
1059
  }
1059
1060
 
1060
1061
  _expandWithId(request, resourceId) {
1061
- const result = this.repository.get(request.params.projectKey, resourceId, {
1062
+ const result = this.repository.get(getRepositoryContext(request), resourceId, {
1062
1063
  expand: this._parseParam(request.query.expand)
1063
1064
  });
1064
1065
  return result;
@@ -1094,7 +1095,7 @@ class AbstractRepository {
1094
1095
  this._storage = storage;
1095
1096
  }
1096
1097
 
1097
- processUpdateActions(projectKey, resource, actions) {
1098
+ processUpdateActions(context, resource, actions) {
1098
1099
  // Deep-copy
1099
1100
  const modifiedResource = JSON.parse(JSON.stringify(resource));
1100
1101
  actions.forEach(action => {
@@ -1105,11 +1106,11 @@ class AbstractRepository {
1105
1106
  return;
1106
1107
  }
1107
1108
 
1108
- updateFunc(projectKey, modifiedResource, action);
1109
+ updateFunc(context, modifiedResource, action);
1109
1110
  });
1110
1111
 
1111
1112
  if (!deepEqual(modifiedResource, resource)) {
1112
- this.save(projectKey, modifiedResource);
1113
+ this.save(context, modifiedResource);
1113
1114
  }
1114
1115
 
1115
1116
  return modifiedResource;
@@ -1123,8 +1124,8 @@ class AbstractResourceRepository extends AbstractRepository {
1123
1124
  this._storage.assertStorage(this.getTypeId());
1124
1125
  }
1125
1126
 
1126
- query(projectKey, params = {}) {
1127
- return this._storage.query(projectKey, this.getTypeId(), {
1127
+ query(context, params = {}) {
1128
+ return this._storage.query(context.projectKey, this.getTypeId(), {
1128
1129
  expand: params.expand,
1129
1130
  where: params.where,
1130
1131
  offset: params.offset,
@@ -1132,20 +1133,20 @@ class AbstractResourceRepository extends AbstractRepository {
1132
1133
  });
1133
1134
  }
1134
1135
 
1135
- get(projectKey, id, params = {}) {
1136
- return this._storage.get(projectKey, this.getTypeId(), id, params);
1136
+ get(context, id, params = {}) {
1137
+ return this._storage.get(context.projectKey, this.getTypeId(), id, params);
1137
1138
  }
1138
1139
 
1139
- getByKey(projectKey, key, params = {}) {
1140
- return this._storage.getByKey(projectKey, this.getTypeId(), key, params);
1140
+ getByKey(context, key, params = {}) {
1141
+ return this._storage.getByKey(context.projectKey, this.getTypeId(), key, params);
1141
1142
  }
1142
1143
 
1143
- delete(projectKey, id, params = {}) {
1144
- return this._storage.delete(projectKey, this.getTypeId(), id, params);
1144
+ delete(context, id, params = {}) {
1145
+ return this._storage.delete(context.projectKey, this.getTypeId(), id, params);
1145
1146
  }
1146
1147
 
1147
- save(projectKey, resource) {
1148
- const current = this.get(projectKey, resource.id);
1148
+ save(context, resource) {
1149
+ const current = this.get(context, resource.id);
1149
1150
 
1150
1151
  if (current) {
1151
1152
  checkConcurrentModification(current, resource.version);
@@ -1161,7 +1162,7 @@ class AbstractResourceRepository extends AbstractRepository {
1161
1162
 
1162
1163
  resource.version += 1;
1163
1164
 
1164
- this._storage.add(projectKey, this.getTypeId(), resource);
1165
+ this._storage.add(context.projectKey, this.getTypeId(), resource);
1165
1166
  }
1166
1167
 
1167
1168
  }
@@ -1225,39 +1226,39 @@ class CartDiscountRepository extends AbstractResourceRepository {
1225
1226
  constructor() {
1226
1227
  super(...arguments);
1227
1228
  this.actions = {
1228
- setKey: (projectKey, resource, {
1229
+ setKey: (context, resource, {
1229
1230
  key
1230
1231
  }) => {
1231
1232
  resource.key = key;
1232
1233
  },
1233
- setDescription: (projectKey, resource, {
1234
+ setDescription: (context, resource, {
1234
1235
  description
1235
1236
  }) => {
1236
1237
  resource.description = description;
1237
1238
  },
1238
- setValidFrom: (projectKey, resource, {
1239
+ setValidFrom: (context, resource, {
1239
1240
  validFrom
1240
1241
  }) => {
1241
1242
  resource.validFrom = validFrom;
1242
1243
  },
1243
- setValidUntil: (projectKey, resource, {
1244
+ setValidUntil: (context, resource, {
1244
1245
  validUntil
1245
1246
  }) => {
1246
1247
  resource.validUntil = validUntil;
1247
1248
  },
1248
- setValidFromAndUntil: (projectKey, resource, {
1249
+ setValidFromAndUntil: (context, resource, {
1249
1250
  validFrom,
1250
1251
  validUntil
1251
1252
  }) => {
1252
1253
  resource.validFrom = validFrom;
1253
1254
  resource.validUntil = validUntil;
1254
1255
  },
1255
- changeSortOrder: (projectKey, resource, {
1256
+ changeSortOrder: (context, resource, {
1256
1257
  sortOrder
1257
1258
  }) => {
1258
1259
  resource.sortOrder = sortOrder;
1259
1260
  },
1260
- changeIsActive: (projectKey, resource, {
1261
+ changeIsActive: (context, resource, {
1261
1262
  isActive
1262
1263
  }) => {
1263
1264
  resource.isActive = isActive;
@@ -1269,7 +1270,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1269
1270
  return 'cart-discount';
1270
1271
  }
1271
1272
 
1272
- create(projectKey, draft) {
1273
+ create(context, draft) {
1273
1274
  const resource = { ...getBaseResourceProperties(),
1274
1275
  key: draft.key,
1275
1276
  description: draft.description,
@@ -1285,7 +1286,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1285
1286
  validUntil: draft.validUntil,
1286
1287
  value: this.transformValueDraft(draft.value)
1287
1288
  };
1288
- this.save(projectKey, resource);
1289
+ this.save(context, resource);
1289
1290
  return resource;
1290
1291
  }
1291
1292
 
@@ -1341,7 +1342,7 @@ class CartRepository extends AbstractResourceRepository {
1341
1342
  constructor() {
1342
1343
  super(...arguments);
1343
1344
  this.actions = {
1344
- addLineItem: (projectKey, resource, {
1345
+ addLineItem: (context, resource, {
1345
1346
  productId,
1346
1347
  variantId,
1347
1348
  sku,
@@ -1352,10 +1353,10 @@ class CartRepository extends AbstractResourceRepository {
1352
1353
 
1353
1354
  if (productId && variantId) {
1354
1355
  // Fetch product and variant by ID
1355
- product = this._storage.get(projectKey, 'product', productId, {});
1356
+ product = this._storage.get(context.projectKey, 'product', productId, {});
1356
1357
  } else if (sku) {
1357
1358
  // Fetch product and variant by SKU
1358
- const items = this._storage.query(projectKey, 'product', {
1359
+ const items = this._storage.query(context.projectKey, 'product', {
1359
1360
  where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1360
1361
  });
1361
1362
 
@@ -1450,7 +1451,7 @@ class CartRepository extends AbstractResourceRepository {
1450
1451
 
1451
1452
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1452
1453
  },
1453
- removeLineItem: (projectKey, resource, {
1454
+ removeLineItem: (context, resource, {
1454
1455
  lineItemId,
1455
1456
  quantity
1456
1457
  }) => {
@@ -1484,15 +1485,15 @@ class CartRepository extends AbstractResourceRepository {
1484
1485
 
1485
1486
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1486
1487
  },
1487
- setBillingAddress: (projectKey, resource, {
1488
+ setBillingAddress: (context, resource, {
1488
1489
  address
1489
1490
  }) => {
1490
1491
  resource.billingAddress = address;
1491
1492
  },
1492
- setShippingMethod: (projectKey, resource, {
1493
+ setShippingMethod: (context, resource, {
1493
1494
  shippingMethod
1494
1495
  }) => {
1495
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1496
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1496
1497
  shippingMethod);
1497
1498
 
1498
1499
  if (!resolvedType) {
@@ -1507,17 +1508,17 @@ class CartRepository extends AbstractResourceRepository {
1507
1508
  }
1508
1509
  };
1509
1510
  },
1510
- setCountry: (projectKey, resource, {
1511
+ setCountry: (context, resource, {
1511
1512
  country
1512
1513
  }) => {
1513
1514
  resource.country = country;
1514
1515
  },
1515
- setCustomerEmail: (projectKey, resource, {
1516
+ setCustomerEmail: (context, resource, {
1516
1517
  email
1517
1518
  }) => {
1518
1519
  resource.customerEmail = email;
1519
1520
  },
1520
- setCustomField: (projectKey, resource, {
1521
+ setCustomField: (context, resource, {
1521
1522
  name,
1522
1523
  value
1523
1524
  }) => {
@@ -1527,14 +1528,14 @@ class CartRepository extends AbstractResourceRepository {
1527
1528
 
1528
1529
  resource.custom.fields[name] = value;
1529
1530
  },
1530
- setCustomType: (projectKey, resource, {
1531
+ setCustomType: (context, resource, {
1531
1532
  type,
1532
1533
  fields
1533
1534
  }) => {
1534
1535
  if (!type) {
1535
1536
  resource.custom = undefined;
1536
1537
  } else {
1537
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1538
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1538
1539
 
1539
1540
  if (!resolvedType) {
1540
1541
  throw new Error(`Type ${type} not found`);
@@ -1549,12 +1550,12 @@ class CartRepository extends AbstractResourceRepository {
1549
1550
  };
1550
1551
  }
1551
1552
  },
1552
- setLocale: (projectKey, resource, {
1553
+ setLocale: (context, resource, {
1553
1554
  locale
1554
1555
  }) => {
1555
1556
  resource.locale = locale;
1556
1557
  },
1557
- setShippingAddress: (projectKey, resource, {
1558
+ setShippingAddress: (context, resource, {
1558
1559
  address
1559
1560
  }) => {
1560
1561
  resource.shippingAddress = address;
@@ -1641,10 +1642,10 @@ class CartRepository extends AbstractResourceRepository {
1641
1642
  return 'cart';
1642
1643
  }
1643
1644
 
1644
- create(projectKey, draft) {
1645
+ create(context, draft) {
1645
1646
  var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1646
1647
 
1647
- 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 : [];
1648
+ 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 : [];
1648
1649
  const resource = { ...getBaseResourceProperties(),
1649
1650
  cartState: 'Active',
1650
1651
  lineItems,
@@ -1662,11 +1663,11 @@ class CartRepository extends AbstractResourceRepository {
1662
1663
  locale: draft.locale,
1663
1664
  country: draft.country,
1664
1665
  origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1665
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1666
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1666
1667
  }; // @ts-ignore
1667
1668
 
1668
1669
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1669
- this.save(projectKey, resource);
1670
+ this.save(context, resource);
1670
1671
  return resource;
1671
1672
  }
1672
1673
 
@@ -1712,10 +1713,10 @@ class OrderRepository extends AbstractResourceRepository {
1712
1713
  constructor() {
1713
1714
  super(...arguments);
1714
1715
  this.actions = {
1715
- addPayment: (projectKey, resource, {
1716
+ addPayment: (context, resource, {
1716
1717
  payment
1717
1718
  }) => {
1718
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1719
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1719
1720
 
1720
1721
  if (!resolvedPayment) {
1721
1722
  throw new Error(`Payment ${payment.id} not found`);
@@ -1732,20 +1733,20 @@ class OrderRepository extends AbstractResourceRepository {
1732
1733
  id: payment.id
1733
1734
  });
1734
1735
  },
1735
- changeOrderState: (projectKey, resource, {
1736
+ changeOrderState: (context, resource, {
1736
1737
  orderState
1737
1738
  }) => {
1738
1739
  resource.orderState = orderState;
1739
1740
  },
1740
- changePaymentState: (projectKey, resource, {
1741
+ changePaymentState: (context, resource, {
1741
1742
  paymentState
1742
1743
  }) => {
1743
1744
  resource.paymentState = paymentState;
1744
1745
  },
1745
- transitionState: (projectKey, resource, {
1746
+ transitionState: (context, resource, {
1746
1747
  state
1747
1748
  }) => {
1748
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1749
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1749
1750
 
1750
1751
  if (!resolvedType) {
1751
1752
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1756,17 +1757,17 @@ class OrderRepository extends AbstractResourceRepository {
1756
1757
  id: resolvedType.id
1757
1758
  };
1758
1759
  },
1759
- setBillingAddress: (projectKey, resource, {
1760
+ setBillingAddress: (context, resource, {
1760
1761
  address
1761
1762
  }) => {
1762
1763
  resource.billingAddress = address;
1763
1764
  },
1764
- setCustomerEmail: (projectKey, resource, {
1765
+ setCustomerEmail: (context, resource, {
1765
1766
  email
1766
1767
  }) => {
1767
1768
  resource.customerEmail = email;
1768
1769
  },
1769
- setCustomField: (projectKey, resource, {
1770
+ setCustomField: (context, resource, {
1770
1771
  name,
1771
1772
  value
1772
1773
  }) => {
@@ -1776,14 +1777,14 @@ class OrderRepository extends AbstractResourceRepository {
1776
1777
 
1777
1778
  resource.custom.fields[name] = value;
1778
1779
  },
1779
- setCustomType: (projectKey, resource, {
1780
+ setCustomType: (context, resource, {
1780
1781
  type,
1781
1782
  fields
1782
1783
  }) => {
1783
1784
  if (!type) {
1784
1785
  resource.custom = undefined;
1785
1786
  } else {
1786
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1787
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1787
1788
 
1788
1789
  if (!resolvedType) {
1789
1790
  throw new Error(`Type ${type} not found`);
@@ -1798,27 +1799,27 @@ class OrderRepository extends AbstractResourceRepository {
1798
1799
  };
1799
1800
  }
1800
1801
  },
1801
- setLocale: (projectKey, resource, {
1802
+ setLocale: (context, resource, {
1802
1803
  locale
1803
1804
  }) => {
1804
1805
  resource.locale = locale;
1805
1806
  },
1806
- setOrderNumber: (projectKey, resource, {
1807
+ setOrderNumber: (context, resource, {
1807
1808
  orderNumber
1808
1809
  }) => {
1809
1810
  resource.orderNumber = orderNumber;
1810
1811
  },
1811
- setShippingAddress: (projectKey, resource, {
1812
+ setShippingAddress: (context, resource, {
1812
1813
  address
1813
1814
  }) => {
1814
1815
  resource.shippingAddress = address;
1815
1816
  },
1816
- setStore: (projectKey, resource, {
1817
+ setStore: (context, resource, {
1817
1818
  store
1818
1819
  }) => {
1819
1820
  if (!store) return;
1820
1821
 
1821
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1822
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1822
1823
 
1823
1824
  if (!resolvedType) {
1824
1825
  throw new Error(`No store found with key=${store.key}`);
@@ -1837,17 +1838,24 @@ class OrderRepository extends AbstractResourceRepository {
1837
1838
  return 'order';
1838
1839
  }
1839
1840
 
1840
- create(projectKey, draft) {
1841
+ create(context, draft) {
1841
1842
  assert(draft.cart, 'draft.cart is missing');
1843
+ return this.createFromCart(context, {
1844
+ id: draft.cart.id,
1845
+ typeId: 'cart'
1846
+ }, draft.orderNumber);
1847
+ }
1842
1848
 
1843
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1849
+ createFromCart(context, cartReference, orderNumber) {
1850
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1844
1851
 
1845
1852
  if (!cart) {
1846
1853
  throw new Error('Cannot find cart');
1847
1854
  }
1848
1855
 
1849
1856
  const resource = { ...getBaseResourceProperties(),
1850
- orderNumber: draft.orderNumber,
1857
+ orderNumber,
1858
+ cart: cartReference,
1851
1859
  orderState: 'Open',
1852
1860
  lineItems: [],
1853
1861
  customLineItems: [],
@@ -1855,13 +1863,17 @@ class OrderRepository extends AbstractResourceRepository {
1855
1863
  refusedGifts: [],
1856
1864
  origin: 'Customer',
1857
1865
  syncInfo: [],
1866
+ store: context.storeKey ? {
1867
+ key: context.storeKey,
1868
+ typeId: 'store'
1869
+ } : undefined,
1858
1870
  lastMessageSequenceNumber: 0
1859
1871
  };
1860
- this.save(projectKey, resource);
1872
+ this.save(context, resource);
1861
1873
  return resource;
1862
1874
  }
1863
1875
 
1864
- import(projectKey, draft) {
1876
+ import(context, draft) {
1865
1877
  var _draft$lineItems, _draft$customLineItem;
1866
1878
 
1867
1879
  // TODO: Check if order with given orderNumber already exists
@@ -1869,7 +1881,7 @@ class OrderRepository extends AbstractResourceRepository {
1869
1881
  const resource = { ...getBaseResourceProperties(),
1870
1882
  billingAddress: draft.billingAddress,
1871
1883
  shippingAddress: draft.shippingAddress,
1872
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1884
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1873
1885
  customerEmail: draft.customerEmail,
1874
1886
  lastMessageSequenceNumber: 0,
1875
1887
  orderNumber: draft.orderNumber,
@@ -1877,21 +1889,21 @@ class OrderRepository extends AbstractResourceRepository {
1877
1889
  origin: draft.origin || 'Customer',
1878
1890
  paymentState: draft.paymentState,
1879
1891
  refusedGifts: [],
1880
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1892
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1881
1893
  syncInfo: [],
1882
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1883
- customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1894
+ lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(context, item))) || [],
1895
+ customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(context, item))) || [],
1884
1896
  totalPrice: {
1885
1897
  type: 'centPrecision',
1886
1898
  ...draft.totalPrice,
1887
1899
  fractionDigits: 2
1888
1900
  }
1889
1901
  };
1890
- this.save(projectKey, resource);
1902
+ this.save(context, resource);
1891
1903
  return resource;
1892
1904
  }
1893
1905
 
1894
- lineItemFromImportDraft(projectKey, draft) {
1906
+ lineItemFromImportDraft(context, draft) {
1895
1907
  let product;
1896
1908
  let variant;
1897
1909
 
@@ -1901,7 +1913,7 @@ class OrderRepository extends AbstractResourceRepository {
1901
1913
  sku: draft.variant.sku
1902
1914
  };
1903
1915
 
1904
- var items = this._storage.query(projectKey, 'product', {
1916
+ var items = this._storage.query(context.projectKey, 'product', {
1905
1917
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1906
1918
  });
1907
1919
 
@@ -1928,7 +1940,7 @@ class OrderRepository extends AbstractResourceRepository {
1928
1940
  }
1929
1941
 
1930
1942
  const lineItem = { ...getBaseResourceProperties(),
1931
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1943
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1932
1944
  discountedPricePerQuantity: [],
1933
1945
  lineItemMode: 'Standard',
1934
1946
  name: draft.name,
@@ -1949,9 +1961,9 @@ class OrderRepository extends AbstractResourceRepository {
1949
1961
  return lineItem;
1950
1962
  }
1951
1963
 
1952
- customLineItemFromImportDraft(projectKey, draft) {
1964
+ customLineItemFromImportDraft(context, draft) {
1953
1965
  const lineItem = { ...getBaseResourceProperties(),
1954
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1966
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1955
1967
  discountedPricePerQuantity: [],
1956
1968
  money: createTypedMoney(draft.money),
1957
1969
  name: draft.name,
@@ -1963,8 +1975,8 @@ class OrderRepository extends AbstractResourceRepository {
1963
1975
  return lineItem;
1964
1976
  }
1965
1977
 
1966
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1967
- const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
1978
+ getWithOrderNumber(context, orderNumber, params = {}) {
1979
+ const result = this._storage.query(context.projectKey, this.getTypeId(), { ...params,
1968
1980
  where: [`orderNumber="${orderNumber}"`]
1969
1981
  });
1970
1982
 
@@ -1995,8 +2007,9 @@ class CartService extends AbstractService {
1995
2007
 
1996
2008
  extraRoutes(parent) {
1997
2009
  parent.post('/replicate', (request, response) => {
1998
- // @ts-ignore
1999
- 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);
2010
+ const context = getRepositoryContext(request); // @ts-ignore
2011
+
2012
+ const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(context, request.body.reference.id) : this.repository.get(context, request.body.reference.id);
2000
2013
 
2001
2014
  if (!cartOrOrder) {
2002
2015
  return response.status(400).send();
@@ -2012,7 +2025,7 @@ class CartService extends AbstractService {
2012
2025
  };
2013
2026
  })
2014
2027
  };
2015
- const newCart = this.repository.create(request.params.projectKey, cartDraft);
2028
+ const newCart = this.repository.create(context, cartDraft);
2016
2029
  return response.status(200).send(newCart);
2017
2030
  });
2018
2031
  }
@@ -2023,7 +2036,7 @@ class CategoryRepository extends AbstractResourceRepository {
2023
2036
  constructor() {
2024
2037
  super(...arguments);
2025
2038
  this.actions = {
2026
- changeAssetName: (projectKey, resource, {
2039
+ changeAssetName: (context, resource, {
2027
2040
  assetId,
2028
2041
  assetKey,
2029
2042
  name
@@ -2040,17 +2053,17 @@ class CategoryRepository extends AbstractResourceRepository {
2040
2053
  }
2041
2054
  });
2042
2055
  },
2043
- changeSlug: (projectKey, resource, {
2056
+ changeSlug: (context, resource, {
2044
2057
  slug
2045
2058
  }) => {
2046
2059
  resource.slug = slug;
2047
2060
  },
2048
- setKey: (projectKey, resource, {
2061
+ setKey: (context, resource, {
2049
2062
  key
2050
2063
  }) => {
2051
2064
  resource.key = key;
2052
2065
  },
2053
- setAssetDescription: (projectKey, resource, {
2066
+ setAssetDescription: (context, resource, {
2054
2067
  assetId,
2055
2068
  assetKey,
2056
2069
  description
@@ -2067,7 +2080,7 @@ class CategoryRepository extends AbstractResourceRepository {
2067
2080
  }
2068
2081
  });
2069
2082
  },
2070
- setAssetSources: (projectKey, resource, {
2083
+ setAssetSources: (context, resource, {
2071
2084
  assetId,
2072
2085
  assetKey,
2073
2086
  sources
@@ -2084,22 +2097,22 @@ class CategoryRepository extends AbstractResourceRepository {
2084
2097
  }
2085
2098
  });
2086
2099
  },
2087
- setDescription: (projectKey, resource, {
2100
+ setDescription: (context, resource, {
2088
2101
  description
2089
2102
  }) => {
2090
2103
  resource.description = description;
2091
2104
  },
2092
- setMetaDescription: (projectKey, resource, {
2105
+ setMetaDescription: (context, resource, {
2093
2106
  metaDescription
2094
2107
  }) => {
2095
2108
  resource.metaDescription = metaDescription;
2096
2109
  },
2097
- setMetaKeywords: (projectKey, resource, {
2110
+ setMetaKeywords: (context, resource, {
2098
2111
  metaKeywords
2099
2112
  }) => {
2100
2113
  resource.metaKeywords = metaKeywords;
2101
2114
  },
2102
- setMetaTitle: (projectKey, resource, {
2115
+ setMetaTitle: (context, resource, {
2103
2116
  metaTitle
2104
2117
  }) => {
2105
2118
  resource.metaTitle = metaTitle;
@@ -2111,7 +2124,7 @@ class CategoryRepository extends AbstractResourceRepository {
2111
2124
  return 'category';
2112
2125
  }
2113
2126
 
2114
- create(projectKey, draft) {
2127
+ create(context, draft) {
2115
2128
  var _draft$assets;
2116
2129
 
2117
2130
  const resource = { ...getBaseResourceProperties(),
@@ -2133,11 +2146,11 @@ class CategoryRepository extends AbstractResourceRepository {
2133
2146
  sources: d.sources,
2134
2147
  tags: d.tags,
2135
2148
  key: d.key,
2136
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2149
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2137
2150
  };
2138
2151
  })) || []
2139
2152
  };
2140
- this.save(projectKey, resource);
2153
+ this.save(context, resource);
2141
2154
  return resource;
2142
2155
  }
2143
2156
 
@@ -2160,12 +2173,12 @@ class ChannelRepository extends AbstractResourceRepository {
2160
2173
  return 'channel';
2161
2174
  }
2162
2175
 
2163
- create(projectKey, draft) {
2176
+ create(context, draft) {
2164
2177
  const resource = { ...getBaseResourceProperties(),
2165
2178
  key: draft.key,
2166
2179
  roles: draft.roles || []
2167
2180
  };
2168
- this.save(projectKey, resource);
2181
+ this.save(context, resource);
2169
2182
  return resource;
2170
2183
  }
2171
2184
 
@@ -2187,12 +2200,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2187
2200
  constructor() {
2188
2201
  super(...arguments);
2189
2202
  this.actions = {
2190
- setKey: (projectKey, resource, {
2203
+ setKey: (context, resource, {
2191
2204
  key
2192
2205
  }) => {
2193
2206
  resource.key = key;
2194
2207
  },
2195
- changeName: (projectKey, resource, {
2208
+ changeName: (context, resource, {
2196
2209
  name
2197
2210
  }) => {
2198
2211
  resource.name = name;
@@ -2204,12 +2217,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2204
2217
  return 'customer';
2205
2218
  }
2206
2219
 
2207
- create(projectKey, draft) {
2220
+ create(context, draft) {
2208
2221
  const resource = { ...getBaseResourceProperties(),
2209
2222
  key: draft.key,
2210
2223
  name: draft.groupName
2211
2224
  };
2212
- this.save(projectKey, resource);
2225
+ this.save(context, resource);
2213
2226
  return resource;
2214
2227
  }
2215
2228
 
@@ -2231,7 +2244,7 @@ class CustomerRepository extends AbstractResourceRepository {
2231
2244
  constructor() {
2232
2245
  super(...arguments);
2233
2246
  this.actions = {
2234
- changeEmail: (_projectKey, resource, {
2247
+ changeEmail: (_context, resource, {
2235
2248
  email
2236
2249
  }) => {
2237
2250
  resource.email = email;
@@ -2243,19 +2256,19 @@ class CustomerRepository extends AbstractResourceRepository {
2243
2256
  return 'customer';
2244
2257
  }
2245
2258
 
2246
- create(projectKey, draft) {
2259
+ create(context, draft) {
2247
2260
  const resource = { ...getBaseResourceProperties(),
2248
2261
  email: draft.email,
2249
2262
  password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2250
2263
  isEmailVerified: draft.isEmailVerified || false,
2251
2264
  addresses: []
2252
2265
  };
2253
- this.save(projectKey, resource);
2266
+ this.save(context, resource);
2254
2267
  return resource;
2255
2268
  }
2256
2269
 
2257
- getMe(projectKey) {
2258
- const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
2270
+ getMe(context) {
2271
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {}); // grab the first customer you can find
2259
2272
 
2260
2273
 
2261
2274
  if (results.count > 0) {
@@ -2279,10 +2292,12 @@ class CustomerService extends AbstractService {
2279
2292
 
2280
2293
  extraRoutes(parent) {
2281
2294
  parent.post('/password-token', (request, response) => {
2282
- const customer = this.repository.query(request.params.projectKey, {
2295
+ const customer = this.repository.query(getRepositoryContext(request), {
2283
2296
  where: [`email="${request.body.email}"`]
2284
- });
2285
- const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
2297
+ }); // @ts-ignore
2298
+
2299
+ const ttlMinutes = request.params.ttlMinutes ? // @ts-ignore
2300
+ +request.params.ttlMinutes : 34560;
2286
2301
  const {
2287
2302
  version,
2288
2303
  ...rest
@@ -2302,8 +2317,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2302
2317
  return 'key-value-document';
2303
2318
  }
2304
2319
 
2305
- create(projectKey, draft) {
2306
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2320
+ create(context, draft) {
2321
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2307
2322
  const baseProperties = getBaseResourceProperties();
2308
2323
 
2309
2324
  if (current) {
@@ -2331,12 +2346,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2331
2346
  key: draft.key,
2332
2347
  value: draft.value
2333
2348
  };
2334
- this.save(projectKey, resource);
2349
+ this.save(context, resource);
2335
2350
  return resource;
2336
2351
  }
2337
2352
 
2338
- getWithContainerAndKey(projectKey, container, key) {
2339
- const items = this._storage.all(projectKey, this.getTypeId());
2353
+ getWithContainerAndKey(context, container, key) {
2354
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2340
2355
 
2341
2356
  return items.find(item => item.container === container && item.key === key);
2342
2357
  }
@@ -2360,7 +2375,7 @@ class CustomObjectService extends AbstractService {
2360
2375
  }
2361
2376
 
2362
2377
  getWithContainerAndKey(request, response) {
2363
- const result = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2378
+ const result = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2364
2379
 
2365
2380
  if (!result) {
2366
2381
  return response.status(404).send('Not Found');
@@ -2374,18 +2389,18 @@ class CustomObjectService extends AbstractService {
2374
2389
  key: request.params.key,
2375
2390
  container: request.params.container
2376
2391
  };
2377
- const result = this.repository.create(request.params.projectKey, draft);
2392
+ const result = this.repository.create(getRepositoryContext(request), draft);
2378
2393
  return response.status(200).send(result);
2379
2394
  }
2380
2395
 
2381
2396
  deleteWithContainerAndKey(request, response) {
2382
- const current = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2397
+ const current = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2383
2398
 
2384
2399
  if (!current) {
2385
2400
  return response.status(404).send('Not Found');
2386
2401
  }
2387
2402
 
2388
- const result = this.repository.delete(request.params.projectKey, current.id);
2403
+ const result = this.repository.delete(getRepositoryContext(request), current.id);
2389
2404
  return response.status(200).send(result);
2390
2405
  }
2391
2406
 
@@ -2395,12 +2410,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2395
2410
  constructor() {
2396
2411
  super(...arguments);
2397
2412
  this.actions = {
2398
- changeIsActive: (projectKey, resource, {
2413
+ changeIsActive: (context, resource, {
2399
2414
  isActive
2400
2415
  }) => {
2401
2416
  resource.isActive = isActive;
2402
2417
  },
2403
- changeCartDiscounts: (projectKey, resource, {
2418
+ changeCartDiscounts: (context, resource, {
2404
2419
  cartDiscounts
2405
2420
  }) => {
2406
2421
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2408,42 +2423,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2408
2423
  id: obj.id
2409
2424
  }));
2410
2425
  },
2411
- setDescription: (projectKey, resource, {
2426
+ setDescription: (context, resource, {
2412
2427
  description
2413
2428
  }) => {
2414
2429
  resource.description = description;
2415
2430
  },
2416
- setCartPredicate: (projectKey, resource, {
2431
+ setCartPredicate: (context, resource, {
2417
2432
  cartPredicate
2418
2433
  }) => {
2419
2434
  resource.cartPredicate = cartPredicate;
2420
2435
  },
2421
- setName: (projectKey, resource, {
2436
+ setName: (context, resource, {
2422
2437
  name
2423
2438
  }) => {
2424
2439
  resource.name = name;
2425
2440
  },
2426
- setMaxApplications: (projectKey, resource, {
2441
+ setMaxApplications: (context, resource, {
2427
2442
  maxApplications
2428
2443
  }) => {
2429
2444
  resource.maxApplications = maxApplications;
2430
2445
  },
2431
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2446
+ setMaxApplicationsPerCustomer: (context, resource, {
2432
2447
  maxApplicationsPerCustomer
2433
2448
  }) => {
2434
2449
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2435
2450
  },
2436
- setValidFrom: (projectKey, resource, {
2451
+ setValidFrom: (context, resource, {
2437
2452
  validFrom
2438
2453
  }) => {
2439
2454
  resource.validFrom = validFrom;
2440
2455
  },
2441
- setValidUntil: (projectKey, resource, {
2456
+ setValidUntil: (context, resource, {
2442
2457
  validUntil
2443
2458
  }) => {
2444
2459
  resource.validUntil = validUntil;
2445
2460
  },
2446
- setValidFromAndUntil: (projectKey, resource, {
2461
+ setValidFromAndUntil: (context, resource, {
2447
2462
  validFrom,
2448
2463
  validUntil
2449
2464
  }) => {
@@ -2457,7 +2472,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2457
2472
  return 'cart-discount';
2458
2473
  }
2459
2474
 
2460
- create(projectKey, draft) {
2475
+ create(context, draft) {
2461
2476
  const resource = { ...getBaseResourceProperties(),
2462
2477
  applicationVersion: 1,
2463
2478
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2476,7 +2491,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2476
2491
  maxApplications: draft.maxApplications,
2477
2492
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2478
2493
  };
2479
- this.save(projectKey, resource);
2494
+ this.save(context, resource);
2480
2495
  return resource;
2481
2496
  }
2482
2497
 
@@ -2498,22 +2513,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2498
2513
  constructor() {
2499
2514
  super(...arguments);
2500
2515
  this.actions = {
2501
- setKey: (projectKey, resource, {
2516
+ setKey: (context, resource, {
2502
2517
  key
2503
2518
  }) => {
2504
2519
  resource.key = key;
2505
2520
  },
2506
- setTimeoutInMs: (projectKey, resource, {
2521
+ setTimeoutInMs: (context, resource, {
2507
2522
  timeoutInMs
2508
2523
  }) => {
2509
2524
  resource.timeoutInMs = timeoutInMs;
2510
2525
  },
2511
- changeTriggers: (projectKey, resource, {
2526
+ changeTriggers: (context, resource, {
2512
2527
  triggers
2513
2528
  }) => {
2514
2529
  resource.triggers = triggers;
2515
2530
  },
2516
- changeDestination: (projectKey, resource, {
2531
+ changeDestination: (context, resource, {
2517
2532
  destination
2518
2533
  }) => {
2519
2534
  resource.destination = destination;
@@ -2525,14 +2540,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2525
2540
  return 'extension';
2526
2541
  }
2527
2542
 
2528
- create(projectKey, draft) {
2543
+ create(context, draft) {
2529
2544
  const resource = { ...getBaseResourceProperties(),
2530
2545
  key: draft.key,
2531
2546
  timeoutInMs: draft.timeoutInMs,
2532
2547
  destination: draft.destination,
2533
2548
  triggers: draft.triggers
2534
2549
  };
2535
- this.save(projectKey, resource);
2550
+ this.save(context, resource);
2536
2551
  return resource;
2537
2552
  }
2538
2553
 
@@ -2554,19 +2569,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2554
2569
  constructor() {
2555
2570
  super(...arguments);
2556
2571
  this.actions = {
2557
- changeQuantity: (projectKey, resource, {
2572
+ changeQuantity: (context, resource, {
2558
2573
  quantity
2559
2574
  }) => {
2560
2575
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2561
2576
 
2562
2577
  resource.availableQuantity = quantity;
2563
2578
  },
2564
- setExpectedDelivery: (projectKey, resource, {
2579
+ setExpectedDelivery: (context, resource, {
2565
2580
  expectedDelivery
2566
2581
  }) => {
2567
2582
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2568
2583
  },
2569
- setCustomField: (projectKey, resource, {
2584
+ setCustomField: (context, resource, {
2570
2585
  name,
2571
2586
  value
2572
2587
  }) => {
@@ -2576,14 +2591,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2576
2591
 
2577
2592
  resource.custom.fields[name] = value;
2578
2593
  },
2579
- setCustomType: (projectKey, resource, {
2594
+ setCustomType: (context, resource, {
2580
2595
  type,
2581
2596
  fields
2582
2597
  }) => {
2583
2598
  if (!type) {
2584
2599
  resource.custom = undefined;
2585
2600
  } else {
2586
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2601
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2587
2602
 
2588
2603
  if (!resolvedType) {
2589
2604
  throw new Error(`Type ${type} not found`);
@@ -2598,7 +2613,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2598
2613
  };
2599
2614
  }
2600
2615
  },
2601
- setRestockableInDays: (projectKey, resource, {
2616
+ setRestockableInDays: (context, resource, {
2602
2617
  restockableInDays
2603
2618
  }) => {
2604
2619
  resource.restockableInDays = restockableInDays;
@@ -2610,7 +2625,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2610
2625
  return 'inventory-entry';
2611
2626
  }
2612
2627
 
2613
- create(projectKey, draft) {
2628
+ create(context, draft) {
2614
2629
  var _draft$supplyChannel$, _draft$supplyChannel;
2615
2630
 
2616
2631
  const resource = { ...getBaseResourceProperties(),
@@ -2623,9 +2638,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2623
2638
  typeId: 'channel',
2624
2639
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2625
2640
  },
2626
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2641
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2627
2642
  };
2628
- this.save(projectKey, resource);
2643
+ this.save(context, resource);
2629
2644
  return resource;
2630
2645
  }
2631
2646
 
@@ -2685,14 +2700,14 @@ class PaymentRepository extends AbstractResourceRepository {
2685
2700
  constructor() {
2686
2701
  super(...arguments);
2687
2702
 
2688
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2703
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2689
2704
  id: v4(),
2690
2705
  amount: createTypedMoney(draft.amount),
2691
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2706
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2692
2707
  });
2693
2708
 
2694
2709
  this.actions = {
2695
- setCustomField: (projectKey, resource, {
2710
+ setCustomField: (context, resource, {
2696
2711
  name,
2697
2712
  value
2698
2713
  }) => {
@@ -2702,14 +2717,14 @@ class PaymentRepository extends AbstractResourceRepository {
2702
2717
 
2703
2718
  resource.custom.fields[name] = value;
2704
2719
  },
2705
- setCustomType: (projectKey, resource, {
2720
+ setCustomType: (context, resource, {
2706
2721
  type,
2707
2722
  fields
2708
2723
  }) => {
2709
2724
  if (!type) {
2710
2725
  resource.custom = undefined;
2711
2726
  } else {
2712
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2727
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2713
2728
 
2714
2729
  if (!resolvedType) {
2715
2730
  throw new Error(`Type ${type} not found`);
@@ -2724,12 +2739,12 @@ class PaymentRepository extends AbstractResourceRepository {
2724
2739
  };
2725
2740
  }
2726
2741
  },
2727
- addTransaction: (projectKey, resource, {
2742
+ addTransaction: (context, resource, {
2728
2743
  transaction
2729
2744
  }) => {
2730
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2745
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2731
2746
  },
2732
- changeTransactionState: (_projectKey, resource, {
2747
+ changeTransactionState: (_context, resource, {
2733
2748
  transactionId,
2734
2749
  state
2735
2750
  }) => {
@@ -2739,10 +2754,10 @@ class PaymentRepository extends AbstractResourceRepository {
2739
2754
  };
2740
2755
  resource.transactions[index] = updatedTransaction;
2741
2756
  },
2742
- transitionState: (projectKey, resource, {
2757
+ transitionState: (context, resource, {
2743
2758
  state
2744
2759
  }) => {
2745
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2760
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2746
2761
 
2747
2762
  if (!stateObj) {
2748
2763
  throw new Error(`State ${state} not found`);
@@ -2761,18 +2776,18 @@ class PaymentRepository extends AbstractResourceRepository {
2761
2776
  return 'payment';
2762
2777
  }
2763
2778
 
2764
- create(projectKey, draft) {
2779
+ create(context, draft) {
2765
2780
  const resource = { ...getBaseResourceProperties(),
2766
2781
  amountPlanned: createTypedMoney(draft.amountPlanned),
2767
2782
  paymentMethodInfo: draft.paymentMethodInfo,
2768
2783
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2769
- state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
2784
+ state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, context.projectKey, this._storage) : undefined
2770
2785
  } : {},
2771
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2772
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2773
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2786
+ transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, context)),
2787
+ interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, context.projectKey, this._storage)),
2788
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2774
2789
  };
2775
- this.save(projectKey, resource);
2790
+ this.save(context, resource);
2776
2791
  return resource;
2777
2792
  }
2778
2793
 
@@ -2807,12 +2822,12 @@ class OrderService extends AbstractService {
2807
2822
 
2808
2823
  import(request, response) {
2809
2824
  const importDraft = request.body;
2810
- const resource = this.repository.import(request.params.projectKey, importDraft);
2825
+ const resource = this.repository.import(getRepositoryContext(request), importDraft);
2811
2826
  return response.status(200).send(resource);
2812
2827
  }
2813
2828
 
2814
2829
  getWithOrderNumber(request, response) {
2815
- const resource = this.repository.getWithOrderNumber(request.params.projectKey, request.params.orderNumber, request.query);
2830
+ const resource = this.repository.getWithOrderNumber(getRepositoryContext(request), request.params.orderNumber, request.query);
2816
2831
 
2817
2832
  if (resource) {
2818
2833
  return response.status(200).send(resource);
@@ -2864,7 +2879,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2864
2879
  return 'product-projection';
2865
2880
  }
2866
2881
 
2867
- create(projectKey, draft) {
2882
+ create(context, draft) {
2868
2883
  var _draft$variants$map, _draft$variants;
2869
2884
 
2870
2885
  if (!draft.masterVariant) {
@@ -2889,17 +2904,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2889
2904
  // @ts-ignore
2890
2905
  searchKeywords: draft.searchKeywords
2891
2906
  };
2892
- this.save(projectKey, resource);
2907
+ this.save(context, resource);
2893
2908
  return resource;
2894
2909
  }
2895
2910
 
2896
- search(projectKey, query) {
2911
+ search(context, query) {
2897
2912
  var _query$filterQuery;
2898
2913
 
2899
2914
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2900
2915
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2901
2916
 
2902
- const results = this._storage.query(projectKey, this.getTypeId(), {
2917
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2903
2918
  where: wherePredicate,
2904
2919
  offset: query.offset ? Number(query.offset) : undefined,
2905
2920
  limit: query.limit ? Number(query.limit) : undefined
@@ -2934,7 +2949,7 @@ class ProductProjectionService extends AbstractService {
2934
2949
  }
2935
2950
 
2936
2951
  search(request, response) {
2937
- const resource = this.repository.search(request.params.projectKey, request.query);
2952
+ const resource = this.repository.search(getRepositoryContext(request), request.query);
2938
2953
  return response.status(200).send(resource);
2939
2954
  }
2940
2955
 
@@ -2944,7 +2959,7 @@ class ProductRepository extends AbstractResourceRepository {
2944
2959
  constructor() {
2945
2960
  super(...arguments);
2946
2961
  this.actions = {
2947
- publish: (projectKey, resource, {
2962
+ publish: (context, resource, {
2948
2963
  scope
2949
2964
  }) => {
2950
2965
  if (resource.masterData.staged) {
@@ -2956,7 +2971,7 @@ class ProductRepository extends AbstractResourceRepository {
2956
2971
  resource.masterData.hasStagedChanges = false;
2957
2972
  resource.masterData.published = true;
2958
2973
  },
2959
- setAttribute: (projectKey, resource, {
2974
+ setAttribute: (context, resource, {
2960
2975
  variantId,
2961
2976
  sku,
2962
2977
  name,
@@ -3017,7 +3032,7 @@ class ProductRepository extends AbstractResourceRepository {
3017
3032
  return 'product';
3018
3033
  }
3019
3034
 
3020
- create(projectKey, draft) {
3035
+ create(context, draft) {
3021
3036
  var _draft$publish, _draft$publish2;
3022
3037
 
3023
3038
  const productData = {
@@ -3041,7 +3056,7 @@ class ProductRepository extends AbstractResourceRepository {
3041
3056
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3042
3057
  }
3043
3058
  };
3044
- this.save(projectKey, resource);
3059
+ this.save(context, resource);
3045
3060
  return resource;
3046
3061
  }
3047
3062
 
@@ -3101,7 +3116,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3101
3116
  constructor() {
3102
3117
  super(...arguments);
3103
3118
 
3104
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3119
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3105
3120
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3106
3121
 
3107
3122
  return { ...draft,
@@ -3112,7 +3127,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3112
3127
  };
3113
3128
 
3114
3129
  this.actions = {
3115
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3130
+ changeLocalizedEnumValueLabel: (context, resource, {
3116
3131
  attributeName,
3117
3132
  newValue
3118
3133
  }) => {
@@ -3140,7 +3155,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3140
3155
  }
3141
3156
  });
3142
3157
  },
3143
- changeLabel: (projectKey, resource, {
3158
+ changeLabel: (context, resource, {
3144
3159
  attributeName,
3145
3160
  label
3146
3161
  }) => {
@@ -3159,21 +3174,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3159
3174
  return 'product-type';
3160
3175
  }
3161
3176
 
3162
- create(projectKey, draft) {
3177
+ create(context, draft) {
3163
3178
  var _draft$attributes;
3164
3179
 
3165
3180
  const resource = { ...getBaseResourceProperties(),
3166
3181
  key: draft.key,
3167
3182
  name: draft.name,
3168
3183
  description: draft.description,
3169
- attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(projectKey, a))
3184
+ attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(context, a))
3170
3185
  };
3171
- this.save(projectKey, resource);
3186
+ this.save(context, resource);
3172
3187
  return resource;
3173
3188
  }
3174
3189
 
3175
- getWithKey(projectKey, key) {
3176
- const result = this._storage.query(projectKey, this.getTypeId(), {
3190
+ getWithKey(context, key) {
3191
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3177
3192
  where: [`key="${key}"`]
3178
3193
  });
3179
3194
 
@@ -3206,7 +3221,7 @@ class ProductTypeService extends AbstractService {
3206
3221
  }
3207
3222
 
3208
3223
  getWithKey(request, response) {
3209
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3224
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3210
3225
 
3211
3226
  if (resource) {
3212
3227
  return response.status(200).send(resource);
@@ -3243,32 +3258,32 @@ class ProjectRepository extends AbstractRepository {
3243
3258
  constructor() {
3244
3259
  super(...arguments);
3245
3260
  this.actions = {
3246
- changeName: (projectKey, resource, {
3261
+ changeName: (context, resource, {
3247
3262
  name
3248
3263
  }) => {
3249
3264
  resource.name = name;
3250
3265
  },
3251
- changeCurrencies: (projectKey, resource, {
3266
+ changeCurrencies: (context, resource, {
3252
3267
  currencies
3253
3268
  }) => {
3254
3269
  resource.currencies = currencies;
3255
3270
  },
3256
- changeCountries: (projectKey, resource, {
3271
+ changeCountries: (context, resource, {
3257
3272
  countries
3258
3273
  }) => {
3259
3274
  resource.countries = countries;
3260
3275
  },
3261
- changeLanguages: (projectKey, resource, {
3276
+ changeLanguages: (context, resource, {
3262
3277
  languages
3263
3278
  }) => {
3264
3279
  resource.languages = languages;
3265
3280
  },
3266
- changeMessagesEnabled: (projectKey, resource, {
3281
+ changeMessagesEnabled: (context, resource, {
3267
3282
  messagesEnabled
3268
3283
  }) => {
3269
3284
  resource.messages.enabled = messagesEnabled;
3270
3285
  },
3271
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3286
+ changeProductSearchIndexingEnabled: (context, resource, {
3272
3287
  enabled
3273
3288
  }) => {
3274
3289
  var _resource$searchIndex;
@@ -3280,7 +3295,7 @@ class ProjectRepository extends AbstractRepository {
3280
3295
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3281
3296
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3282
3297
  },
3283
- changeOrderSearchStatus: (projectKey, resource, {
3298
+ changeOrderSearchStatus: (context, resource, {
3284
3299
  status
3285
3300
  }) => {
3286
3301
  var _resource$searchIndex2;
@@ -3292,22 +3307,22 @@ class ProjectRepository extends AbstractRepository {
3292
3307
  resource.searchIndexing.orders.status = status;
3293
3308
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3294
3309
  },
3295
- setShippingRateInputType: (projectKey, resource, {
3310
+ setShippingRateInputType: (context, resource, {
3296
3311
  shippingRateInputType
3297
3312
  }) => {
3298
3313
  resource.shippingRateInputType = shippingRateInputType;
3299
3314
  },
3300
- setExternalOAuth: (projectKey, resource, {
3315
+ setExternalOAuth: (context, resource, {
3301
3316
  externalOAuth
3302
3317
  }) => {
3303
3318
  resource.externalOAuth = externalOAuth;
3304
3319
  },
3305
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3320
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3306
3321
  countryTaxRateFallbackEnabled
3307
3322
  }) => {
3308
3323
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3309
3324
  },
3310
- changeCartsConfiguration: (projectKey, resource, {
3325
+ changeCartsConfiguration: (context, resource, {
3311
3326
  cartsConfiguration
3312
3327
  }) => {
3313
3328
  resource.carts = cartsConfiguration || {
@@ -3318,15 +3333,15 @@ class ProjectRepository extends AbstractRepository {
3318
3333
  };
3319
3334
  }
3320
3335
 
3321
- get(projectKey) {
3322
- const resource = this._storage.getProject(projectKey);
3336
+ get(context) {
3337
+ const resource = this._storage.getProject(context.projectKey);
3323
3338
 
3324
3339
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3325
3340
  return masked;
3326
3341
  }
3327
3342
 
3328
- save(projectKey, resource) {
3329
- const current = this.get(projectKey);
3343
+ save(context, resource) {
3344
+ const current = this.get(context);
3330
3345
 
3331
3346
  if (current) {
3332
3347
  checkConcurrentModification(current, resource.version);
@@ -3359,20 +3374,19 @@ class ProjectService {
3359
3374
  }
3360
3375
 
3361
3376
  get(request, response) {
3362
- const projectKey = request.params.projectKey;
3363
- const project = this.repository.get(projectKey);
3377
+ const project = this.repository.get(getRepositoryContext(request));
3364
3378
  return response.status(200).send(project);
3365
3379
  }
3366
3380
 
3367
3381
  post(request, response) {
3368
3382
  const updateRequest = request.body;
3369
- const project = this.repository.get(request.params.projectKey);
3383
+ const project = this.repository.get(getRepositoryContext(request));
3370
3384
 
3371
3385
  if (!project) {
3372
3386
  return response.status(404).send({});
3373
3387
  }
3374
3388
 
3375
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3389
+ this.repository.processUpdateActions(getRepositoryContext(request), project, updateRequest.actions);
3376
3390
  return response.status(200).send({});
3377
3391
  }
3378
3392
 
@@ -3382,11 +3396,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3382
3396
  constructor() {
3383
3397
  super(...arguments);
3384
3398
 
3385
- this._transformZoneRateDraft = (projectKey, draft) => {
3399
+ this._transformZoneRateDraft = (context, draft) => {
3386
3400
  var _draft$shippingRates;
3387
3401
 
3388
3402
  return { ...draft,
3389
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3403
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3390
3404
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3391
3405
  };
3392
3406
  };
@@ -3400,7 +3414,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3400
3414
  };
3401
3415
 
3402
3416
  this.actions = {
3403
- addShippingRate: (_projectKey, resource, {
3417
+ addShippingRate: (_context, resource, {
3404
3418
  shippingRate,
3405
3419
  zone
3406
3420
  }) => {
@@ -3420,7 +3434,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3420
3434
  shippingRates: [rate]
3421
3435
  });
3422
3436
  },
3423
- removeShippingRate: (_projectKey, resource, {
3437
+ removeShippingRate: (_context, resource, {
3424
3438
  shippingRate,
3425
3439
  zone
3426
3440
  }) => {
@@ -3434,10 +3448,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3434
3448
  }
3435
3449
  });
3436
3450
  },
3437
- addZone: (projectKey, resource, {
3451
+ addZone: (context, resource, {
3438
3452
  zone
3439
3453
  }) => {
3440
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3454
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3441
3455
 
3442
3456
  if (resource.zoneRates === undefined) {
3443
3457
  resource.zoneRates = [];
@@ -3448,39 +3462,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3448
3462
  shippingRates: []
3449
3463
  });
3450
3464
  },
3451
- removeZone: (_projectKey, resource, {
3465
+ removeZone: (_context, resource, {
3452
3466
  zone
3453
3467
  }) => {
3454
3468
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3455
3469
  return zoneRate.zone.id !== zone.id;
3456
3470
  });
3457
3471
  },
3458
- setKey: (_projectKey, resource, {
3472
+ setKey: (_context, resource, {
3459
3473
  key
3460
3474
  }) => {
3461
3475
  resource.key = key;
3462
3476
  },
3463
- setDescription: (_projectKey, resource, {
3477
+ setDescription: (_context, resource, {
3464
3478
  description
3465
3479
  }) => {
3466
3480
  resource.description = description;
3467
3481
  },
3468
- setLocalizedDescription: (_projectKey, resource, {
3482
+ setLocalizedDescription: (_context, resource, {
3469
3483
  localizedDescription
3470
3484
  }) => {
3471
3485
  resource.localizedDescription = localizedDescription;
3472
3486
  },
3473
- setPredicate: (_projectKey, resource, {
3487
+ setPredicate: (_context, resource, {
3474
3488
  predicate
3475
3489
  }) => {
3476
3490
  resource.predicate = predicate;
3477
3491
  },
3478
- changeIsDefault: (_projectKey, resource, {
3492
+ changeIsDefault: (_context, resource, {
3479
3493
  isDefault
3480
3494
  }) => {
3481
3495
  resource.isDefault = isDefault;
3482
3496
  },
3483
- changeName: (_projectKey, resource, {
3497
+ changeName: (_context, resource, {
3484
3498
  name
3485
3499
  }) => {
3486
3500
  resource.name = name;
@@ -3492,16 +3506,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3492
3506
  return 'shipping-method';
3493
3507
  }
3494
3508
 
3495
- create(projectKey, draft) {
3509
+ create(context, draft) {
3496
3510
  var _draft$zoneRates;
3497
3511
 
3498
3512
  const resource = { ...getBaseResourceProperties(),
3499
3513
  ...draft,
3500
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3501
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3502
- custom: createCustomFields(draft.custom, projectKey, this._storage)
3514
+ taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, context.projectKey, this._storage),
3515
+ zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(context, z)),
3516
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
3503
3517
  };
3504
- this.save(projectKey, resource);
3518
+ this.save(context, resource);
3505
3519
  return resource;
3506
3520
  }
3507
3521
 
@@ -3529,13 +3543,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3529
3543
  return 'shopping-list';
3530
3544
  }
3531
3545
 
3532
- create(projectKey, draft) {
3546
+ create(context, draft) {
3533
3547
  var _draft$lineItems, _draft$store;
3534
3548
 
3535
3549
  // const product =
3536
3550
  const resource = { ...getBaseResourceProperties(),
3537
3551
  ...draft,
3538
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3552
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3539
3553
  textLineItems: [],
3540
3554
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3541
3555
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3550,16 +3564,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3550
3564
  typeId: 'product-type',
3551
3565
  id: ''
3552
3566
  },
3553
- custom: createCustomFields(e.custom, projectKey, this._storage)
3567
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3554
3568
  };
3555
3569
  }),
3556
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3570
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3557
3571
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3558
3572
  typeId: 'store',
3559
3573
  key: draft.store.key
3560
3574
  } : undefined
3561
3575
  };
3562
- this.save(projectKey, resource);
3576
+ this.save(context, resource);
3563
3577
  return resource;
3564
3578
  }
3565
3579
 
@@ -3581,22 +3595,22 @@ class StateRepository extends AbstractResourceRepository {
3581
3595
  constructor() {
3582
3596
  super(...arguments);
3583
3597
  this.actions = {
3584
- changeKey: (projectKey, resource, {
3598
+ changeKey: (context, resource, {
3585
3599
  key
3586
3600
  }) => {
3587
3601
  resource.key = key;
3588
3602
  },
3589
- setDescription: (projectKey, resource, {
3603
+ setDescription: (context, resource, {
3590
3604
  description
3591
3605
  }) => {
3592
3606
  resource.description = description;
3593
3607
  },
3594
- setName: (projectKey, resource, {
3608
+ setName: (context, resource, {
3595
3609
  name
3596
3610
  }) => {
3597
3611
  resource.name = name;
3598
3612
  },
3599
- setRoles: (projectKey, resource, {
3613
+ setRoles: (context, resource, {
3600
3614
  roles
3601
3615
  }) => {
3602
3616
  resource.roles = roles;
@@ -3608,14 +3622,14 @@ class StateRepository extends AbstractResourceRepository {
3608
3622
  return 'state';
3609
3623
  }
3610
3624
 
3611
- create(projectKey, draft) {
3625
+ create(context, draft) {
3612
3626
  const resource = { ...getBaseResourceProperties(),
3613
3627
  ...draft,
3614
3628
  builtIn: false,
3615
3629
  initial: draft.initial || false,
3616
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3630
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3617
3631
  };
3618
- this.save(projectKey, resource);
3632
+ this.save(context, resource);
3619
3633
  return resource;
3620
3634
  }
3621
3635
 
@@ -3637,17 +3651,17 @@ class StoreRepository extends AbstractResourceRepository {
3637
3651
  constructor() {
3638
3652
  super(...arguments);
3639
3653
  this.actions = {
3640
- setName: (projectKey, resource, {
3654
+ setName: (context, resource, {
3641
3655
  name
3642
3656
  }) => {
3643
3657
  resource.name = name;
3644
3658
  },
3645
- setDistributionChannels: (projectKey, resource, {
3659
+ setDistributionChannels: (context, resource, {
3646
3660
  distributionChannels
3647
3661
  }) => {
3648
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3662
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3649
3663
  },
3650
- setLanguages: (projectKey, resource, {
3664
+ setLanguages: (context, resource, {
3651
3665
  languages
3652
3666
  }) => {
3653
3667
  resource.languages = languages;
@@ -3659,25 +3673,25 @@ class StoreRepository extends AbstractResourceRepository {
3659
3673
  return 'store';
3660
3674
  }
3661
3675
 
3662
- create(projectKey, draft) {
3676
+ create(context, draft) {
3663
3677
  const resource = { ...getBaseResourceProperties(),
3664
3678
  key: draft.key,
3665
3679
  name: draft.name,
3666
3680
  languages: draft.languages,
3667
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3668
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3681
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3682
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3669
3683
  };
3670
- this.save(projectKey, resource);
3684
+ this.save(context, resource);
3671
3685
  return resource;
3672
3686
  }
3673
3687
 
3674
- transformChannels(projectKey, channels) {
3688
+ transformChannels(context, channels) {
3675
3689
  if (!channels) return [];
3676
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3690
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3677
3691
  }
3678
3692
 
3679
- getWithKey(projectKey, key) {
3680
- const result = this._storage.query(projectKey, this.getTypeId(), {
3693
+ getWithKey(context, key) {
3694
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3681
3695
  where: [`key="${key}"`]
3682
3696
  });
3683
3697
 
@@ -3709,7 +3723,7 @@ class StoreService extends AbstractService {
3709
3723
  }
3710
3724
 
3711
3725
  getWithKey(request, response) {
3712
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3726
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3713
3727
 
3714
3728
  if (resource) {
3715
3729
  return response.status(200).send(resource);
@@ -3725,7 +3739,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3725
3739
  return 'subscription';
3726
3740
  }
3727
3741
 
3728
- create(projectKey, draft) {
3742
+ create(context, draft) {
3729
3743
  // TODO: We could actually test this here by using the aws sdk. For now
3730
3744
  // hardcode a failed check when account id is 0000000000
3731
3745
  if (draft.destination.type === 'SQS') {
@@ -3751,7 +3765,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3751
3765
  messages: draft.messages || [],
3752
3766
  status: 'Healthy'
3753
3767
  };
3754
- this.save(projectKey, resource);
3768
+ this.save(context, resource);
3755
3769
  return resource;
3756
3770
  }
3757
3771
 
@@ -3779,7 +3793,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3779
3793
  });
3780
3794
 
3781
3795
  this.actions = {
3782
- addTaxRate: (projectKey, resource, {
3796
+ addTaxRate: (context, resource, {
3783
3797
  taxRate
3784
3798
  }) => {
3785
3799
  if (resource.rates === undefined) {
@@ -3788,7 +3802,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3788
3802
 
3789
3803
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3790
3804
  },
3791
- removeTaxRate: (projectKey, resource, {
3805
+ removeTaxRate: (context, resource, {
3792
3806
  taxRateId
3793
3807
  }) => {
3794
3808
  if (resource.rates === undefined) {
@@ -3799,7 +3813,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3799
3813
  return taxRate.id !== taxRateId;
3800
3814
  });
3801
3815
  },
3802
- replaceTaxRate: (projectKey, resource, {
3816
+ replaceTaxRate: (context, resource, {
3803
3817
  taxRateId,
3804
3818
  taxRate
3805
3819
  }) => {
@@ -3817,17 +3831,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3817
3831
  }
3818
3832
  }
3819
3833
  },
3820
- setDescription: (projectKey, resource, {
3834
+ setDescription: (context, resource, {
3821
3835
  description
3822
3836
  }) => {
3823
3837
  resource.description = description;
3824
3838
  },
3825
- setKey: (projectKey, resource, {
3839
+ setKey: (context, resource, {
3826
3840
  key
3827
3841
  }) => {
3828
3842
  resource.key = key;
3829
3843
  },
3830
- changeName: (projectKey, resource, {
3844
+ changeName: (context, resource, {
3831
3845
  name
3832
3846
  }) => {
3833
3847
  resource.name = name;
@@ -3839,19 +3853,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3839
3853
  return 'tax-category';
3840
3854
  }
3841
3855
 
3842
- create(projectKey, draft) {
3856
+ create(context, draft) {
3843
3857
  var _draft$rates;
3844
3858
 
3845
3859
  const resource = { ...getBaseResourceProperties(),
3846
3860
  ...draft,
3847
3861
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3848
3862
  };
3849
- this.save(projectKey, resource);
3863
+ this.save(context, resource);
3850
3864
  return resource;
3851
3865
  }
3852
3866
 
3853
- getWithKey(projectKey, key) {
3854
- const result = this._storage.query(projectKey, this.getTypeId(), {
3867
+ getWithKey(context, key) {
3868
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3855
3869
  where: [`key="${key}"`]
3856
3870
  });
3857
3871
 
@@ -3884,7 +3898,7 @@ class TaxCategoryService extends AbstractService {
3884
3898
  }
3885
3899
 
3886
3900
  getWithKey(request, response) {
3887
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3901
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3888
3902
 
3889
3903
  if (resource) {
3890
3904
  return response.status(200).send(resource);
@@ -3899,29 +3913,29 @@ class TypeRepository extends AbstractResourceRepository {
3899
3913
  constructor() {
3900
3914
  super(...arguments);
3901
3915
  this.actions = {
3902
- addFieldDefinition: (projectKey, resource, {
3916
+ addFieldDefinition: (context, resource, {
3903
3917
  fieldDefinition
3904
3918
  }) => {
3905
3919
  resource.fieldDefinitions.push(fieldDefinition);
3906
3920
  },
3907
- removeFieldDefinition: (projectKey, resource, {
3921
+ removeFieldDefinition: (context, resource, {
3908
3922
  fieldName
3909
3923
  }) => {
3910
3924
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3911
3925
  return f.name !== fieldName;
3912
3926
  });
3913
3927
  },
3914
- setDescription: (projectKey, resource, {
3928
+ setDescription: (context, resource, {
3915
3929
  description
3916
3930
  }) => {
3917
3931
  resource.description = description;
3918
3932
  },
3919
- changeName: (projectKey, resource, {
3933
+ changeName: (context, resource, {
3920
3934
  name
3921
3935
  }) => {
3922
3936
  resource.name = name;
3923
3937
  },
3924
- changeFieldDefinitionOrder: (projectKey, resource, {
3938
+ changeFieldDefinitionOrder: (context, resource, {
3925
3939
  fieldNames
3926
3940
  }) => {
3927
3941
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3945,7 +3959,7 @@ class TypeRepository extends AbstractResourceRepository {
3945
3959
 
3946
3960
  resource.fieldDefinitions.push(...current);
3947
3961
  },
3948
- addEnumValue: (projectKey, resource, {
3962
+ addEnumValue: (context, resource, {
3949
3963
  fieldName,
3950
3964
  value
3951
3965
  }) => {
@@ -3962,7 +3976,7 @@ class TypeRepository extends AbstractResourceRepository {
3962
3976
  }
3963
3977
  });
3964
3978
  },
3965
- changeEnumValueLabel: (projectKey, resource, {
3979
+ changeEnumValueLabel: (context, resource, {
3966
3980
  fieldName,
3967
3981
  value
3968
3982
  }) => {
@@ -3994,7 +4008,7 @@ class TypeRepository extends AbstractResourceRepository {
3994
4008
  return 'type';
3995
4009
  }
3996
4010
 
3997
- create(projectKey, draft) {
4011
+ create(context, draft) {
3998
4012
  const resource = { ...getBaseResourceProperties(),
3999
4013
  key: draft.key,
4000
4014
  name: draft.name,
@@ -4002,7 +4016,7 @@ class TypeRepository extends AbstractResourceRepository {
4002
4016
  fieldDefinitions: draft.fieldDefinitions || [],
4003
4017
  description: draft.description
4004
4018
  };
4005
- this.save(projectKey, resource);
4019
+ this.save(context, resource);
4006
4020
  return resource;
4007
4021
  }
4008
4022
 
@@ -4024,29 +4038,29 @@ class ZoneRepository extends AbstractResourceRepository {
4024
4038
  constructor() {
4025
4039
  super(...arguments);
4026
4040
  this.actions = {
4027
- addLocation: (projectKey, resource, {
4041
+ addLocation: (context, resource, {
4028
4042
  location
4029
4043
  }) => {
4030
4044
  resource.locations.push(location);
4031
4045
  },
4032
- removeLocation: (projectKey, resource, {
4046
+ removeLocation: (context, resource, {
4033
4047
  location
4034
4048
  }) => {
4035
4049
  resource.locations = resource.locations.filter(loc => {
4036
4050
  return !(loc.country === location.country && loc.state === location.state);
4037
4051
  });
4038
4052
  },
4039
- changeName: (projectKey, resource, {
4053
+ changeName: (context, resource, {
4040
4054
  name
4041
4055
  }) => {
4042
4056
  resource.name = name;
4043
4057
  },
4044
- setDescription: (projectKey, resource, {
4058
+ setDescription: (context, resource, {
4045
4059
  description
4046
4060
  }) => {
4047
4061
  resource.description = description;
4048
4062
  },
4049
- setKey: (projectKey, resource, {
4063
+ setKey: (context, resource, {
4050
4064
  key
4051
4065
  }) => {
4052
4066
  resource.key = key;
@@ -4058,14 +4072,14 @@ class ZoneRepository extends AbstractResourceRepository {
4058
4072
  return 'zone';
4059
4073
  }
4060
4074
 
4061
- create(projectKey, draft) {
4075
+ create(context, draft) {
4062
4076
  const resource = { ...getBaseResourceProperties(),
4063
4077
  key: draft.key,
4064
4078
  locations: draft.locations || [],
4065
4079
  name: draft.name,
4066
4080
  description: draft.description
4067
4081
  };
4068
- this.save(projectKey, resource);
4082
+ this.save(context, resource);
4069
4083
  return resource;
4070
4084
  }
4071
4085
 
@@ -4107,7 +4121,7 @@ class MyCustomerService extends AbstractService {
4107
4121
  }
4108
4122
 
4109
4123
  getMe(request, response) {
4110
- const resource = this.repository.getMe(request.params.projectKey);
4124
+ const resource = this.repository.getMe(getRepositoryContext(request));
4111
4125
 
4112
4126
  if (!resource) {
4113
4127
  return response.status(404).send('Not found');
@@ -4118,7 +4132,7 @@ class MyCustomerService extends AbstractService {
4118
4132
 
4119
4133
  signUp(request, response) {
4120
4134
  const draft = request.body;
4121
- const resource = this.repository.create(request.params.projectKey, draft);
4135
+ const resource = this.repository.create(getRepositoryContext(request), draft);
4122
4136
 
4123
4137
  const result = this._expandWithId(request, resource.id);
4124
4138
 
@@ -4133,7 +4147,7 @@ class MyCustomerService extends AbstractService {
4133
4147
  password
4134
4148
  } = request.body;
4135
4149
  const encodedPassword = Buffer.from(password).toString('base64');
4136
- const result = this.repository.query(request.params.projectKey, {
4150
+ const result = this.repository.query(getRepositoryContext(request), {
4137
4151
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4138
4152
  });
4139
4153
 
@@ -4154,10 +4168,22 @@ class MyCustomerService extends AbstractService {
4154
4168
 
4155
4169
  }
4156
4170
 
4171
+ class MyOrderRepository extends OrderRepository {
4172
+ create(context, draft) {
4173
+ assert(draft.id, 'draft.id is missing');
4174
+ const cartIdentifier = {
4175
+ id: draft.id,
4176
+ typeId: 'cart'
4177
+ };
4178
+ return this.createFromCart(context, cartIdentifier);
4179
+ }
4180
+
4181
+ }
4182
+
4157
4183
  class MyOrderService extends AbstractService {
4158
4184
  constructor(parent, storage) {
4159
4185
  super(parent);
4160
- this.repository = new OrderRepository(storage);
4186
+ this.repository = new MyOrderRepository(storage);
4161
4187
  }
4162
4188
 
4163
4189
  getBasePath() {
@@ -4260,8 +4286,10 @@ class CommercetoolsMock {
4260
4286
 
4261
4287
  if (this.options.enableAuthentication) {
4262
4288
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4289
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4263
4290
  } else {
4264
4291
  app.use('/:projectKey', projectRouter);
4292
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4265
4293
  }
4266
4294
 
4267
4295
  this._projectService = new ProjectService(projectRouter, this._storage);