@labdigital/commercetools-mock 0.5.23 → 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 (77) hide show
  1. package/dist/commercetools-mock.cjs.development.js +364 -298
  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 +364 -298
  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 +13 -13
  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 +5 -2
  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 +88 -36
  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 +13 -7
  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.test.ts +48 -8
  67. package/src/services/cart.ts +17 -11
  68. package/src/services/custom-object.ts +8 -4
  69. package/src/services/customer.ts +5 -2
  70. package/src/services/my-customer.ts +7 -3
  71. package/src/services/my-order.ts +3 -3
  72. package/src/services/order.ts +3 -2
  73. package/src/services/product-projection.ts +2 -1
  74. package/src/services/product-type.ts +2 -1
  75. package/src/services/project.ts +4 -3
  76. package/src/services/store.ts +2 -1
  77. 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
 
@@ -1423,7 +1424,17 @@ class CartRepository extends AbstractResourceRepository {
1423
1424
  });
1424
1425
  }
1425
1426
 
1426
- const price = variant.prices[0];
1427
+ const currency = resource.totalPrice.currencyCode;
1428
+ const price = selectPrice({
1429
+ prices: variant.prices,
1430
+ currency,
1431
+ country: resource.country
1432
+ });
1433
+
1434
+ if (!price) {
1435
+ throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
1436
+ }
1437
+
1427
1438
  resource.lineItems.push({
1428
1439
  id: uuid.v4(),
1429
1440
  productId: product.id,
@@ -1447,7 +1458,7 @@ class CartRepository extends AbstractResourceRepository {
1447
1458
 
1448
1459
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1449
1460
  },
1450
- removeLineItem: (projectKey, resource, {
1461
+ removeLineItem: (context, resource, {
1451
1462
  lineItemId,
1452
1463
  quantity
1453
1464
  }) => {
@@ -1481,15 +1492,15 @@ class CartRepository extends AbstractResourceRepository {
1481
1492
 
1482
1493
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1483
1494
  },
1484
- setBillingAddress: (projectKey, resource, {
1495
+ setBillingAddress: (context, resource, {
1485
1496
  address
1486
1497
  }) => {
1487
1498
  resource.billingAddress = address;
1488
1499
  },
1489
- setShippingMethod: (projectKey, resource, {
1500
+ setShippingMethod: (context, resource, {
1490
1501
  shippingMethod
1491
1502
  }) => {
1492
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1503
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1493
1504
  shippingMethod);
1494
1505
 
1495
1506
  if (!resolvedType) {
@@ -1504,17 +1515,17 @@ class CartRepository extends AbstractResourceRepository {
1504
1515
  }
1505
1516
  };
1506
1517
  },
1507
- setCountry: (projectKey, resource, {
1518
+ setCountry: (context, resource, {
1508
1519
  country
1509
1520
  }) => {
1510
1521
  resource.country = country;
1511
1522
  },
1512
- setCustomerEmail: (projectKey, resource, {
1523
+ setCustomerEmail: (context, resource, {
1513
1524
  email
1514
1525
  }) => {
1515
1526
  resource.customerEmail = email;
1516
1527
  },
1517
- setCustomField: (projectKey, resource, {
1528
+ setCustomField: (context, resource, {
1518
1529
  name,
1519
1530
  value
1520
1531
  }) => {
@@ -1524,14 +1535,14 @@ class CartRepository extends AbstractResourceRepository {
1524
1535
 
1525
1536
  resource.custom.fields[name] = value;
1526
1537
  },
1527
- setCustomType: (projectKey, resource, {
1538
+ setCustomType: (context, resource, {
1528
1539
  type,
1529
1540
  fields
1530
1541
  }) => {
1531
1542
  if (!type) {
1532
1543
  resource.custom = undefined;
1533
1544
  } else {
1534
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1545
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1535
1546
 
1536
1547
  if (!resolvedType) {
1537
1548
  throw new Error(`Type ${type} not found`);
@@ -1546,29 +1557,25 @@ class CartRepository extends AbstractResourceRepository {
1546
1557
  };
1547
1558
  }
1548
1559
  },
1549
- setLocale: (projectKey, resource, {
1560
+ setLocale: (context, resource, {
1550
1561
  locale
1551
1562
  }) => {
1552
1563
  resource.locale = locale;
1553
1564
  },
1554
- setShippingAddress: (projectKey, resource, {
1565
+ setShippingAddress: (context, resource, {
1555
1566
  address
1556
1567
  }) => {
1557
1568
  resource.shippingAddress = address;
1558
1569
  }
1559
1570
  };
1560
1571
 
1561
- this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1562
- var _variant$prices2;
1563
-
1572
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
1564
1573
  const {
1565
1574
  productId,
1566
- quantity
1567
- } = draftLineItem; // @ts-ignore
1568
-
1569
- let variantId = draftLineItem.variant.id; // @ts-ignore
1570
-
1571
- let sku = draftLineItem.variant.sku;
1575
+ quantity,
1576
+ variantId,
1577
+ sku
1578
+ } = draftLineItem;
1572
1579
  let product = null;
1573
1580
  let variant;
1574
1581
 
@@ -1606,11 +1613,15 @@ class CartRepository extends AbstractResourceRepository {
1606
1613
  throw new Error(sku ? `A variant with SKU '${sku}' for product '${product.id}' not found.` : `A variant with ID '${variantId}' for product '${product.id}' not found.`);
1607
1614
  }
1608
1615
 
1609
- const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1610
1616
  const quant = quantity != null ? quantity : 1;
1617
+ const price = selectPrice({
1618
+ prices: variant.prices,
1619
+ currency,
1620
+ country
1621
+ });
1611
1622
 
1612
1623
  if (!price) {
1613
- throw new Error(`Price not set on ${productId}`);
1624
+ throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1614
1625
  }
1615
1626
 
1616
1627
  return {
@@ -1638,13 +1649,13 @@ class CartRepository extends AbstractResourceRepository {
1638
1649
  return 'cart';
1639
1650
  }
1640
1651
 
1641
- create(projectKey, draft) {
1642
- var _draft$lineItems;
1652
+ create(context, draft) {
1653
+ var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1643
1654
 
1644
- const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
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 : [];
1645
1656
  const resource = { ...getBaseResourceProperties(),
1646
1657
  cartState: 'Active',
1647
- lineItems: lineItems != null ? lineItems : [],
1658
+ lineItems,
1648
1659
  customLineItems: [],
1649
1660
  totalPrice: {
1650
1661
  type: 'centPrecision',
@@ -1652,16 +1663,18 @@ class CartRepository extends AbstractResourceRepository {
1652
1663
  currencyCode: draft.currency,
1653
1664
  fractionDigits: 0
1654
1665
  },
1655
- taxMode: 'Platform',
1656
- taxRoundingMode: 'HalfEven',
1657
- taxCalculationMode: 'LineItemLevel',
1666
+ taxMode: (_draft$taxMode = draft.taxMode) != null ? _draft$taxMode : 'Platform',
1667
+ taxRoundingMode: (_draft$taxRoundingMod = draft.taxRoundingMode) != null ? _draft$taxRoundingMod : 'HalfEven',
1668
+ taxCalculationMode: (_draft$taxCalculation = draft.taxCalculationMode) != null ? _draft$taxCalculation : 'LineItemLevel',
1658
1669
  refusedGifts: [],
1659
- origin: 'Customer',
1660
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1670
+ locale: draft.locale,
1671
+ country: draft.country,
1672
+ origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1673
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1661
1674
  }; // @ts-ignore
1662
1675
 
1663
1676
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1664
- this.save(projectKey, resource);
1677
+ this.save(context, resource);
1665
1678
  return resource;
1666
1679
  }
1667
1680
 
@@ -1680,6 +1693,25 @@ class CartRepository extends AbstractResourceRepository {
1680
1693
 
1681
1694
  }
1682
1695
 
1696
+ const selectPrice = ({
1697
+ prices,
1698
+ currency,
1699
+ country
1700
+ }) => {
1701
+ if (!prices) {
1702
+ return undefined;
1703
+ } // Quick-and-dirty way of selecting price based on the given currency and country.
1704
+ // Can be improved later to give more priority to exact matches over
1705
+ // 'all country' matches, and include customer groups in the mix as well
1706
+
1707
+
1708
+ return prices.find(price => {
1709
+ const countryMatch = !price.country || price.country === country;
1710
+ const currencyMatch = price.value.currencyCode === currency;
1711
+ return countryMatch && currencyMatch;
1712
+ });
1713
+ };
1714
+
1683
1715
  const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
1684
1716
 
1685
1717
  const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
@@ -1688,10 +1720,10 @@ class OrderRepository extends AbstractResourceRepository {
1688
1720
  constructor() {
1689
1721
  super(...arguments);
1690
1722
  this.actions = {
1691
- addPayment: (projectKey, resource, {
1723
+ addPayment: (context, resource, {
1692
1724
  payment
1693
1725
  }) => {
1694
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1726
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1695
1727
 
1696
1728
  if (!resolvedPayment) {
1697
1729
  throw new Error(`Payment ${payment.id} not found`);
@@ -1708,20 +1740,20 @@ class OrderRepository extends AbstractResourceRepository {
1708
1740
  id: payment.id
1709
1741
  });
1710
1742
  },
1711
- changeOrderState: (projectKey, resource, {
1743
+ changeOrderState: (context, resource, {
1712
1744
  orderState
1713
1745
  }) => {
1714
1746
  resource.orderState = orderState;
1715
1747
  },
1716
- changePaymentState: (projectKey, resource, {
1748
+ changePaymentState: (context, resource, {
1717
1749
  paymentState
1718
1750
  }) => {
1719
1751
  resource.paymentState = paymentState;
1720
1752
  },
1721
- transitionState: (projectKey, resource, {
1753
+ transitionState: (context, resource, {
1722
1754
  state
1723
1755
  }) => {
1724
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1756
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1725
1757
 
1726
1758
  if (!resolvedType) {
1727
1759
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1732,17 +1764,17 @@ class OrderRepository extends AbstractResourceRepository {
1732
1764
  id: resolvedType.id
1733
1765
  };
1734
1766
  },
1735
- setBillingAddress: (projectKey, resource, {
1767
+ setBillingAddress: (context, resource, {
1736
1768
  address
1737
1769
  }) => {
1738
1770
  resource.billingAddress = address;
1739
1771
  },
1740
- setCustomerEmail: (projectKey, resource, {
1772
+ setCustomerEmail: (context, resource, {
1741
1773
  email
1742
1774
  }) => {
1743
1775
  resource.customerEmail = email;
1744
1776
  },
1745
- setCustomField: (projectKey, resource, {
1777
+ setCustomField: (context, resource, {
1746
1778
  name,
1747
1779
  value
1748
1780
  }) => {
@@ -1752,14 +1784,14 @@ class OrderRepository extends AbstractResourceRepository {
1752
1784
 
1753
1785
  resource.custom.fields[name] = value;
1754
1786
  },
1755
- setCustomType: (projectKey, resource, {
1787
+ setCustomType: (context, resource, {
1756
1788
  type,
1757
1789
  fields
1758
1790
  }) => {
1759
1791
  if (!type) {
1760
1792
  resource.custom = undefined;
1761
1793
  } else {
1762
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1794
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1763
1795
 
1764
1796
  if (!resolvedType) {
1765
1797
  throw new Error(`Type ${type} not found`);
@@ -1774,27 +1806,27 @@ class OrderRepository extends AbstractResourceRepository {
1774
1806
  };
1775
1807
  }
1776
1808
  },
1777
- setLocale: (projectKey, resource, {
1809
+ setLocale: (context, resource, {
1778
1810
  locale
1779
1811
  }) => {
1780
1812
  resource.locale = locale;
1781
1813
  },
1782
- setOrderNumber: (projectKey, resource, {
1814
+ setOrderNumber: (context, resource, {
1783
1815
  orderNumber
1784
1816
  }) => {
1785
1817
  resource.orderNumber = orderNumber;
1786
1818
  },
1787
- setShippingAddress: (projectKey, resource, {
1819
+ setShippingAddress: (context, resource, {
1788
1820
  address
1789
1821
  }) => {
1790
1822
  resource.shippingAddress = address;
1791
1823
  },
1792
- setStore: (projectKey, resource, {
1824
+ setStore: (context, resource, {
1793
1825
  store
1794
1826
  }) => {
1795
1827
  if (!store) return;
1796
1828
 
1797
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1829
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1798
1830
 
1799
1831
  if (!resolvedType) {
1800
1832
  throw new Error(`No store found with key=${store.key}`);
@@ -1813,17 +1845,24 @@ class OrderRepository extends AbstractResourceRepository {
1813
1845
  return 'order';
1814
1846
  }
1815
1847
 
1816
- create(projectKey, draft) {
1848
+ create(context, draft) {
1817
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
+ }
1818
1855
 
1819
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1856
+ createFromCart(context, cartReference, orderNumber) {
1857
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1820
1858
 
1821
1859
  if (!cart) {
1822
1860
  throw new Error('Cannot find cart');
1823
1861
  }
1824
1862
 
1825
1863
  const resource = { ...getBaseResourceProperties(),
1826
- orderNumber: draft.orderNumber,
1864
+ orderNumber,
1865
+ cart: cartReference,
1827
1866
  orderState: 'Open',
1828
1867
  lineItems: [],
1829
1868
  customLineItems: [],
@@ -1831,13 +1870,17 @@ class OrderRepository extends AbstractResourceRepository {
1831
1870
  refusedGifts: [],
1832
1871
  origin: 'Customer',
1833
1872
  syncInfo: [],
1873
+ store: context.storeKey ? {
1874
+ key: context.storeKey,
1875
+ typeId: 'store'
1876
+ } : undefined,
1834
1877
  lastMessageSequenceNumber: 0
1835
1878
  };
1836
- this.save(projectKey, resource);
1879
+ this.save(context, resource);
1837
1880
  return resource;
1838
1881
  }
1839
1882
 
1840
- import(projectKey, draft) {
1883
+ import(context, draft) {
1841
1884
  var _draft$lineItems, _draft$customLineItem;
1842
1885
 
1843
1886
  // TODO: Check if order with given orderNumber already exists
@@ -1845,7 +1888,7 @@ class OrderRepository extends AbstractResourceRepository {
1845
1888
  const resource = { ...getBaseResourceProperties(),
1846
1889
  billingAddress: draft.billingAddress,
1847
1890
  shippingAddress: draft.shippingAddress,
1848
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1891
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1849
1892
  customerEmail: draft.customerEmail,
1850
1893
  lastMessageSequenceNumber: 0,
1851
1894
  orderNumber: draft.orderNumber,
@@ -1853,21 +1896,21 @@ class OrderRepository extends AbstractResourceRepository {
1853
1896
  origin: draft.origin || 'Customer',
1854
1897
  paymentState: draft.paymentState,
1855
1898
  refusedGifts: [],
1856
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1899
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1857
1900
  syncInfo: [],
1858
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1859
- 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))) || [],
1860
1903
  totalPrice: {
1861
1904
  type: 'centPrecision',
1862
1905
  ...draft.totalPrice,
1863
1906
  fractionDigits: 2
1864
1907
  }
1865
1908
  };
1866
- this.save(projectKey, resource);
1909
+ this.save(context, resource);
1867
1910
  return resource;
1868
1911
  }
1869
1912
 
1870
- lineItemFromImportDraft(projectKey, draft) {
1913
+ lineItemFromImportDraft(context, draft) {
1871
1914
  let product;
1872
1915
  let variant;
1873
1916
 
@@ -1877,7 +1920,7 @@ class OrderRepository extends AbstractResourceRepository {
1877
1920
  sku: draft.variant.sku
1878
1921
  };
1879
1922
 
1880
- var items = this._storage.query(projectKey, 'product', {
1923
+ var items = this._storage.query(context.projectKey, 'product', {
1881
1924
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1882
1925
  });
1883
1926
 
@@ -1904,7 +1947,7 @@ class OrderRepository extends AbstractResourceRepository {
1904
1947
  }
1905
1948
 
1906
1949
  const lineItem = { ...getBaseResourceProperties(),
1907
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1950
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1908
1951
  discountedPricePerQuantity: [],
1909
1952
  lineItemMode: 'Standard',
1910
1953
  name: draft.name,
@@ -1925,9 +1968,9 @@ class OrderRepository extends AbstractResourceRepository {
1925
1968
  return lineItem;
1926
1969
  }
1927
1970
 
1928
- customLineItemFromImportDraft(projectKey, draft) {
1971
+ customLineItemFromImportDraft(context, draft) {
1929
1972
  const lineItem = { ...getBaseResourceProperties(),
1930
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1973
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1931
1974
  discountedPricePerQuantity: [],
1932
1975
  money: createTypedMoney(draft.money),
1933
1976
  name: draft.name,
@@ -1939,8 +1982,8 @@ class OrderRepository extends AbstractResourceRepository {
1939
1982
  return lineItem;
1940
1983
  }
1941
1984
 
1942
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1943
- 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,
1944
1987
  where: [`orderNumber="${orderNumber}"`]
1945
1988
  });
1946
1989
 
@@ -1971,17 +2014,25 @@ class CartService extends AbstractService {
1971
2014
 
1972
2015
  extraRoutes(parent) {
1973
2016
  parent.post('/replicate', (request, response) => {
1974
- // @ts-ignore
1975
- 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);
1976
2020
 
1977
2021
  if (!cartOrOrder) {
1978
2022
  return response.status(400).send();
1979
2023
  }
1980
2024
 
1981
- const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
2025
+ const cartDraft = { ...cartOrOrder,
1982
2026
  currency: cartOrOrder.totalPrice.currencyCode,
1983
- discountCodes: []
1984
- });
2027
+ discountCodes: [],
2028
+ lineItems: cartOrOrder.lineItems.map(lineItem => {
2029
+ return { ...lineItem,
2030
+ variantId: lineItem.variant.id,
2031
+ sku: lineItem.variant.sku
2032
+ };
2033
+ })
2034
+ };
2035
+ const newCart = this.repository.create(context, cartDraft);
1985
2036
  return response.status(200).send(newCart);
1986
2037
  });
1987
2038
  }
@@ -1992,7 +2043,7 @@ class CategoryRepository extends AbstractResourceRepository {
1992
2043
  constructor() {
1993
2044
  super(...arguments);
1994
2045
  this.actions = {
1995
- changeAssetName: (projectKey, resource, {
2046
+ changeAssetName: (context, resource, {
1996
2047
  assetId,
1997
2048
  assetKey,
1998
2049
  name
@@ -2009,17 +2060,17 @@ class CategoryRepository extends AbstractResourceRepository {
2009
2060
  }
2010
2061
  });
2011
2062
  },
2012
- changeSlug: (projectKey, resource, {
2063
+ changeSlug: (context, resource, {
2013
2064
  slug
2014
2065
  }) => {
2015
2066
  resource.slug = slug;
2016
2067
  },
2017
- setKey: (projectKey, resource, {
2068
+ setKey: (context, resource, {
2018
2069
  key
2019
2070
  }) => {
2020
2071
  resource.key = key;
2021
2072
  },
2022
- setAssetDescription: (projectKey, resource, {
2073
+ setAssetDescription: (context, resource, {
2023
2074
  assetId,
2024
2075
  assetKey,
2025
2076
  description
@@ -2036,7 +2087,7 @@ class CategoryRepository extends AbstractResourceRepository {
2036
2087
  }
2037
2088
  });
2038
2089
  },
2039
- setAssetSources: (projectKey, resource, {
2090
+ setAssetSources: (context, resource, {
2040
2091
  assetId,
2041
2092
  assetKey,
2042
2093
  sources
@@ -2053,22 +2104,22 @@ class CategoryRepository extends AbstractResourceRepository {
2053
2104
  }
2054
2105
  });
2055
2106
  },
2056
- setDescription: (projectKey, resource, {
2107
+ setDescription: (context, resource, {
2057
2108
  description
2058
2109
  }) => {
2059
2110
  resource.description = description;
2060
2111
  },
2061
- setMetaDescription: (projectKey, resource, {
2112
+ setMetaDescription: (context, resource, {
2062
2113
  metaDescription
2063
2114
  }) => {
2064
2115
  resource.metaDescription = metaDescription;
2065
2116
  },
2066
- setMetaKeywords: (projectKey, resource, {
2117
+ setMetaKeywords: (context, resource, {
2067
2118
  metaKeywords
2068
2119
  }) => {
2069
2120
  resource.metaKeywords = metaKeywords;
2070
2121
  },
2071
- setMetaTitle: (projectKey, resource, {
2122
+ setMetaTitle: (context, resource, {
2072
2123
  metaTitle
2073
2124
  }) => {
2074
2125
  resource.metaTitle = metaTitle;
@@ -2080,7 +2131,7 @@ class CategoryRepository extends AbstractResourceRepository {
2080
2131
  return 'category';
2081
2132
  }
2082
2133
 
2083
- create(projectKey, draft) {
2134
+ create(context, draft) {
2084
2135
  var _draft$assets;
2085
2136
 
2086
2137
  const resource = { ...getBaseResourceProperties(),
@@ -2102,11 +2153,11 @@ class CategoryRepository extends AbstractResourceRepository {
2102
2153
  sources: d.sources,
2103
2154
  tags: d.tags,
2104
2155
  key: d.key,
2105
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2156
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2106
2157
  };
2107
2158
  })) || []
2108
2159
  };
2109
- this.save(projectKey, resource);
2160
+ this.save(context, resource);
2110
2161
  return resource;
2111
2162
  }
2112
2163
 
@@ -2129,12 +2180,12 @@ class ChannelRepository extends AbstractResourceRepository {
2129
2180
  return 'channel';
2130
2181
  }
2131
2182
 
2132
- create(projectKey, draft) {
2183
+ create(context, draft) {
2133
2184
  const resource = { ...getBaseResourceProperties(),
2134
2185
  key: draft.key,
2135
2186
  roles: draft.roles || []
2136
2187
  };
2137
- this.save(projectKey, resource);
2188
+ this.save(context, resource);
2138
2189
  return resource;
2139
2190
  }
2140
2191
 
@@ -2156,12 +2207,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2156
2207
  constructor() {
2157
2208
  super(...arguments);
2158
2209
  this.actions = {
2159
- setKey: (projectKey, resource, {
2210
+ setKey: (context, resource, {
2160
2211
  key
2161
2212
  }) => {
2162
2213
  resource.key = key;
2163
2214
  },
2164
- changeName: (projectKey, resource, {
2215
+ changeName: (context, resource, {
2165
2216
  name
2166
2217
  }) => {
2167
2218
  resource.name = name;
@@ -2173,12 +2224,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2173
2224
  return 'customer';
2174
2225
  }
2175
2226
 
2176
- create(projectKey, draft) {
2227
+ create(context, draft) {
2177
2228
  const resource = { ...getBaseResourceProperties(),
2178
2229
  key: draft.key,
2179
2230
  name: draft.groupName
2180
2231
  };
2181
- this.save(projectKey, resource);
2232
+ this.save(context, resource);
2182
2233
  return resource;
2183
2234
  }
2184
2235
 
@@ -2200,7 +2251,7 @@ class CustomerRepository extends AbstractResourceRepository {
2200
2251
  constructor() {
2201
2252
  super(...arguments);
2202
2253
  this.actions = {
2203
- changeEmail: (_projectKey, resource, {
2254
+ changeEmail: (_context, resource, {
2204
2255
  email
2205
2256
  }) => {
2206
2257
  resource.email = email;
@@ -2212,19 +2263,19 @@ class CustomerRepository extends AbstractResourceRepository {
2212
2263
  return 'customer';
2213
2264
  }
2214
2265
 
2215
- create(projectKey, draft) {
2266
+ create(context, draft) {
2216
2267
  const resource = { ...getBaseResourceProperties(),
2217
2268
  email: draft.email,
2218
- password: Buffer.from(draft.password).toString('base64'),
2269
+ password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2219
2270
  isEmailVerified: draft.isEmailVerified || false,
2220
2271
  addresses: []
2221
2272
  };
2222
- this.save(projectKey, resource);
2273
+ this.save(context, resource);
2223
2274
  return resource;
2224
2275
  }
2225
2276
 
2226
- getMe(projectKey) {
2227
- 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
2228
2279
 
2229
2280
 
2230
2281
  if (results.count > 0) {
@@ -2248,10 +2299,12 @@ class CustomerService extends AbstractService {
2248
2299
 
2249
2300
  extraRoutes(parent) {
2250
2301
  parent.post('/password-token', (request, response) => {
2251
- const customer = this.repository.query(request.params.projectKey, {
2302
+ const customer = this.repository.query(helpers.getRepositoryContext(request), {
2252
2303
  where: [`email="${request.body.email}"`]
2253
- });
2254
- 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;
2255
2308
  const {
2256
2309
  version,
2257
2310
  ...rest
@@ -2271,8 +2324,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2271
2324
  return 'key-value-document';
2272
2325
  }
2273
2326
 
2274
- create(projectKey, draft) {
2275
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2327
+ create(context, draft) {
2328
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2276
2329
  const baseProperties = getBaseResourceProperties();
2277
2330
 
2278
2331
  if (current) {
@@ -2300,12 +2353,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2300
2353
  key: draft.key,
2301
2354
  value: draft.value
2302
2355
  };
2303
- this.save(projectKey, resource);
2356
+ this.save(context, resource);
2304
2357
  return resource;
2305
2358
  }
2306
2359
 
2307
- getWithContainerAndKey(projectKey, container, key) {
2308
- const items = this._storage.all(projectKey, this.getTypeId());
2360
+ getWithContainerAndKey(context, container, key) {
2361
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2309
2362
 
2310
2363
  return items.find(item => item.container === container && item.key === key);
2311
2364
  }
@@ -2329,7 +2382,7 @@ class CustomObjectService extends AbstractService {
2329
2382
  }
2330
2383
 
2331
2384
  getWithContainerAndKey(request, response) {
2332
- 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);
2333
2386
 
2334
2387
  if (!result) {
2335
2388
  return response.status(404).send('Not Found');
@@ -2343,18 +2396,18 @@ class CustomObjectService extends AbstractService {
2343
2396
  key: request.params.key,
2344
2397
  container: request.params.container
2345
2398
  };
2346
- const result = this.repository.create(request.params.projectKey, draft);
2399
+ const result = this.repository.create(helpers.getRepositoryContext(request), draft);
2347
2400
  return response.status(200).send(result);
2348
2401
  }
2349
2402
 
2350
2403
  deleteWithContainerAndKey(request, response) {
2351
- 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);
2352
2405
 
2353
2406
  if (!current) {
2354
2407
  return response.status(404).send('Not Found');
2355
2408
  }
2356
2409
 
2357
- const result = this.repository.delete(request.params.projectKey, current.id);
2410
+ const result = this.repository.delete(helpers.getRepositoryContext(request), current.id);
2358
2411
  return response.status(200).send(result);
2359
2412
  }
2360
2413
 
@@ -2364,12 +2417,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2364
2417
  constructor() {
2365
2418
  super(...arguments);
2366
2419
  this.actions = {
2367
- changeIsActive: (projectKey, resource, {
2420
+ changeIsActive: (context, resource, {
2368
2421
  isActive
2369
2422
  }) => {
2370
2423
  resource.isActive = isActive;
2371
2424
  },
2372
- changeCartDiscounts: (projectKey, resource, {
2425
+ changeCartDiscounts: (context, resource, {
2373
2426
  cartDiscounts
2374
2427
  }) => {
2375
2428
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2377,42 +2430,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2377
2430
  id: obj.id
2378
2431
  }));
2379
2432
  },
2380
- setDescription: (projectKey, resource, {
2433
+ setDescription: (context, resource, {
2381
2434
  description
2382
2435
  }) => {
2383
2436
  resource.description = description;
2384
2437
  },
2385
- setCartPredicate: (projectKey, resource, {
2438
+ setCartPredicate: (context, resource, {
2386
2439
  cartPredicate
2387
2440
  }) => {
2388
2441
  resource.cartPredicate = cartPredicate;
2389
2442
  },
2390
- setName: (projectKey, resource, {
2443
+ setName: (context, resource, {
2391
2444
  name
2392
2445
  }) => {
2393
2446
  resource.name = name;
2394
2447
  },
2395
- setMaxApplications: (projectKey, resource, {
2448
+ setMaxApplications: (context, resource, {
2396
2449
  maxApplications
2397
2450
  }) => {
2398
2451
  resource.maxApplications = maxApplications;
2399
2452
  },
2400
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2453
+ setMaxApplicationsPerCustomer: (context, resource, {
2401
2454
  maxApplicationsPerCustomer
2402
2455
  }) => {
2403
2456
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2404
2457
  },
2405
- setValidFrom: (projectKey, resource, {
2458
+ setValidFrom: (context, resource, {
2406
2459
  validFrom
2407
2460
  }) => {
2408
2461
  resource.validFrom = validFrom;
2409
2462
  },
2410
- setValidUntil: (projectKey, resource, {
2463
+ setValidUntil: (context, resource, {
2411
2464
  validUntil
2412
2465
  }) => {
2413
2466
  resource.validUntil = validUntil;
2414
2467
  },
2415
- setValidFromAndUntil: (projectKey, resource, {
2468
+ setValidFromAndUntil: (context, resource, {
2416
2469
  validFrom,
2417
2470
  validUntil
2418
2471
  }) => {
@@ -2426,7 +2479,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2426
2479
  return 'cart-discount';
2427
2480
  }
2428
2481
 
2429
- create(projectKey, draft) {
2482
+ create(context, draft) {
2430
2483
  const resource = { ...getBaseResourceProperties(),
2431
2484
  applicationVersion: 1,
2432
2485
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2445,7 +2498,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2445
2498
  maxApplications: draft.maxApplications,
2446
2499
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2447
2500
  };
2448
- this.save(projectKey, resource);
2501
+ this.save(context, resource);
2449
2502
  return resource;
2450
2503
  }
2451
2504
 
@@ -2467,22 +2520,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2467
2520
  constructor() {
2468
2521
  super(...arguments);
2469
2522
  this.actions = {
2470
- setKey: (projectKey, resource, {
2523
+ setKey: (context, resource, {
2471
2524
  key
2472
2525
  }) => {
2473
2526
  resource.key = key;
2474
2527
  },
2475
- setTimeoutInMs: (projectKey, resource, {
2528
+ setTimeoutInMs: (context, resource, {
2476
2529
  timeoutInMs
2477
2530
  }) => {
2478
2531
  resource.timeoutInMs = timeoutInMs;
2479
2532
  },
2480
- changeTriggers: (projectKey, resource, {
2533
+ changeTriggers: (context, resource, {
2481
2534
  triggers
2482
2535
  }) => {
2483
2536
  resource.triggers = triggers;
2484
2537
  },
2485
- changeDestination: (projectKey, resource, {
2538
+ changeDestination: (context, resource, {
2486
2539
  destination
2487
2540
  }) => {
2488
2541
  resource.destination = destination;
@@ -2494,14 +2547,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2494
2547
  return 'extension';
2495
2548
  }
2496
2549
 
2497
- create(projectKey, draft) {
2550
+ create(context, draft) {
2498
2551
  const resource = { ...getBaseResourceProperties(),
2499
2552
  key: draft.key,
2500
2553
  timeoutInMs: draft.timeoutInMs,
2501
2554
  destination: draft.destination,
2502
2555
  triggers: draft.triggers
2503
2556
  };
2504
- this.save(projectKey, resource);
2557
+ this.save(context, resource);
2505
2558
  return resource;
2506
2559
  }
2507
2560
 
@@ -2523,19 +2576,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2523
2576
  constructor() {
2524
2577
  super(...arguments);
2525
2578
  this.actions = {
2526
- changeQuantity: (projectKey, resource, {
2579
+ changeQuantity: (context, resource, {
2527
2580
  quantity
2528
2581
  }) => {
2529
2582
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2530
2583
 
2531
2584
  resource.availableQuantity = quantity;
2532
2585
  },
2533
- setExpectedDelivery: (projectKey, resource, {
2586
+ setExpectedDelivery: (context, resource, {
2534
2587
  expectedDelivery
2535
2588
  }) => {
2536
2589
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2537
2590
  },
2538
- setCustomField: (projectKey, resource, {
2591
+ setCustomField: (context, resource, {
2539
2592
  name,
2540
2593
  value
2541
2594
  }) => {
@@ -2545,14 +2598,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2545
2598
 
2546
2599
  resource.custom.fields[name] = value;
2547
2600
  },
2548
- setCustomType: (projectKey, resource, {
2601
+ setCustomType: (context, resource, {
2549
2602
  type,
2550
2603
  fields
2551
2604
  }) => {
2552
2605
  if (!type) {
2553
2606
  resource.custom = undefined;
2554
2607
  } else {
2555
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2608
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2556
2609
 
2557
2610
  if (!resolvedType) {
2558
2611
  throw new Error(`Type ${type} not found`);
@@ -2567,7 +2620,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2567
2620
  };
2568
2621
  }
2569
2622
  },
2570
- setRestockableInDays: (projectKey, resource, {
2623
+ setRestockableInDays: (context, resource, {
2571
2624
  restockableInDays
2572
2625
  }) => {
2573
2626
  resource.restockableInDays = restockableInDays;
@@ -2579,7 +2632,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2579
2632
  return 'inventory-entry';
2580
2633
  }
2581
2634
 
2582
- create(projectKey, draft) {
2635
+ create(context, draft) {
2583
2636
  var _draft$supplyChannel$, _draft$supplyChannel;
2584
2637
 
2585
2638
  const resource = { ...getBaseResourceProperties(),
@@ -2592,9 +2645,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2592
2645
  typeId: 'channel',
2593
2646
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2594
2647
  },
2595
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2648
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2596
2649
  };
2597
- this.save(projectKey, resource);
2650
+ this.save(context, resource);
2598
2651
  return resource;
2599
2652
  }
2600
2653
 
@@ -2654,14 +2707,14 @@ class PaymentRepository extends AbstractResourceRepository {
2654
2707
  constructor() {
2655
2708
  super(...arguments);
2656
2709
 
2657
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2710
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2658
2711
  id: uuid.v4(),
2659
2712
  amount: createTypedMoney(draft.amount),
2660
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2713
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2661
2714
  });
2662
2715
 
2663
2716
  this.actions = {
2664
- setCustomField: (projectKey, resource, {
2717
+ setCustomField: (context, resource, {
2665
2718
  name,
2666
2719
  value
2667
2720
  }) => {
@@ -2671,14 +2724,14 @@ class PaymentRepository extends AbstractResourceRepository {
2671
2724
 
2672
2725
  resource.custom.fields[name] = value;
2673
2726
  },
2674
- setCustomType: (projectKey, resource, {
2727
+ setCustomType: (context, resource, {
2675
2728
  type,
2676
2729
  fields
2677
2730
  }) => {
2678
2731
  if (!type) {
2679
2732
  resource.custom = undefined;
2680
2733
  } else {
2681
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2734
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2682
2735
 
2683
2736
  if (!resolvedType) {
2684
2737
  throw new Error(`Type ${type} not found`);
@@ -2693,12 +2746,12 @@ class PaymentRepository extends AbstractResourceRepository {
2693
2746
  };
2694
2747
  }
2695
2748
  },
2696
- addTransaction: (projectKey, resource, {
2749
+ addTransaction: (context, resource, {
2697
2750
  transaction
2698
2751
  }) => {
2699
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2752
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2700
2753
  },
2701
- changeTransactionState: (_projectKey, resource, {
2754
+ changeTransactionState: (_context, resource, {
2702
2755
  transactionId,
2703
2756
  state
2704
2757
  }) => {
@@ -2708,10 +2761,10 @@ class PaymentRepository extends AbstractResourceRepository {
2708
2761
  };
2709
2762
  resource.transactions[index] = updatedTransaction;
2710
2763
  },
2711
- transitionState: (projectKey, resource, {
2764
+ transitionState: (context, resource, {
2712
2765
  state
2713
2766
  }) => {
2714
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2767
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2715
2768
 
2716
2769
  if (!stateObj) {
2717
2770
  throw new Error(`State ${state} not found`);
@@ -2730,18 +2783,18 @@ class PaymentRepository extends AbstractResourceRepository {
2730
2783
  return 'payment';
2731
2784
  }
2732
2785
 
2733
- create(projectKey, draft) {
2786
+ create(context, draft) {
2734
2787
  const resource = { ...getBaseResourceProperties(),
2735
2788
  amountPlanned: createTypedMoney(draft.amountPlanned),
2736
2789
  paymentMethodInfo: draft.paymentMethodInfo,
2737
2790
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2738
- 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
2739
2792
  } : {},
2740
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2741
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2742
- 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)
2743
2796
  };
2744
- this.save(projectKey, resource);
2797
+ this.save(context, resource);
2745
2798
  return resource;
2746
2799
  }
2747
2800
 
@@ -2776,12 +2829,12 @@ class OrderService extends AbstractService {
2776
2829
 
2777
2830
  import(request, response) {
2778
2831
  const importDraft = request.body;
2779
- const resource = this.repository.import(request.params.projectKey, importDraft);
2832
+ const resource = this.repository.import(helpers.getRepositoryContext(request), importDraft);
2780
2833
  return response.status(200).send(resource);
2781
2834
  }
2782
2835
 
2783
2836
  getWithOrderNumber(request, response) {
2784
- 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);
2785
2838
 
2786
2839
  if (resource) {
2787
2840
  return response.status(200).send(resource);
@@ -2833,7 +2886,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2833
2886
  return 'product-projection';
2834
2887
  }
2835
2888
 
2836
- create(projectKey, draft) {
2889
+ create(context, draft) {
2837
2890
  var _draft$variants$map, _draft$variants;
2838
2891
 
2839
2892
  if (!draft.masterVariant) {
@@ -2858,17 +2911,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2858
2911
  // @ts-ignore
2859
2912
  searchKeywords: draft.searchKeywords
2860
2913
  };
2861
- this.save(projectKey, resource);
2914
+ this.save(context, resource);
2862
2915
  return resource;
2863
2916
  }
2864
2917
 
2865
- search(projectKey, query) {
2918
+ search(context, query) {
2866
2919
  var _query$filterQuery;
2867
2920
 
2868
2921
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2869
2922
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2870
2923
 
2871
- const results = this._storage.query(projectKey, this.getTypeId(), {
2924
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2872
2925
  where: wherePredicate,
2873
2926
  offset: query.offset ? Number(query.offset) : undefined,
2874
2927
  limit: query.limit ? Number(query.limit) : undefined
@@ -2903,7 +2956,7 @@ class ProductProjectionService extends AbstractService {
2903
2956
  }
2904
2957
 
2905
2958
  search(request, response) {
2906
- const resource = this.repository.search(request.params.projectKey, request.query);
2959
+ const resource = this.repository.search(helpers.getRepositoryContext(request), request.query);
2907
2960
  return response.status(200).send(resource);
2908
2961
  }
2909
2962
 
@@ -2913,7 +2966,7 @@ class ProductRepository extends AbstractResourceRepository {
2913
2966
  constructor() {
2914
2967
  super(...arguments);
2915
2968
  this.actions = {
2916
- publish: (projectKey, resource, {
2969
+ publish: (context, resource, {
2917
2970
  scope
2918
2971
  }) => {
2919
2972
  if (resource.masterData.staged) {
@@ -2925,7 +2978,7 @@ class ProductRepository extends AbstractResourceRepository {
2925
2978
  resource.masterData.hasStagedChanges = false;
2926
2979
  resource.masterData.published = true;
2927
2980
  },
2928
- setAttribute: (projectKey, resource, {
2981
+ setAttribute: (context, resource, {
2929
2982
  variantId,
2930
2983
  sku,
2931
2984
  name,
@@ -2986,7 +3039,7 @@ class ProductRepository extends AbstractResourceRepository {
2986
3039
  return 'product';
2987
3040
  }
2988
3041
 
2989
- create(projectKey, draft) {
3042
+ create(context, draft) {
2990
3043
  var _draft$publish, _draft$publish2;
2991
3044
 
2992
3045
  const productData = {
@@ -3010,7 +3063,7 @@ class ProductRepository extends AbstractResourceRepository {
3010
3063
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3011
3064
  }
3012
3065
  };
3013
- this.save(projectKey, resource);
3066
+ this.save(context, resource);
3014
3067
  return resource;
3015
3068
  }
3016
3069
 
@@ -3070,7 +3123,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3070
3123
  constructor() {
3071
3124
  super(...arguments);
3072
3125
 
3073
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3126
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3074
3127
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3075
3128
 
3076
3129
  return { ...draft,
@@ -3081,7 +3134,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3081
3134
  };
3082
3135
 
3083
3136
  this.actions = {
3084
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3137
+ changeLocalizedEnumValueLabel: (context, resource, {
3085
3138
  attributeName,
3086
3139
  newValue
3087
3140
  }) => {
@@ -3109,7 +3162,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3109
3162
  }
3110
3163
  });
3111
3164
  },
3112
- changeLabel: (projectKey, resource, {
3165
+ changeLabel: (context, resource, {
3113
3166
  attributeName,
3114
3167
  label
3115
3168
  }) => {
@@ -3128,21 +3181,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3128
3181
  return 'product-type';
3129
3182
  }
3130
3183
 
3131
- create(projectKey, draft) {
3184
+ create(context, draft) {
3132
3185
  var _draft$attributes;
3133
3186
 
3134
3187
  const resource = { ...getBaseResourceProperties(),
3135
3188
  key: draft.key,
3136
3189
  name: draft.name,
3137
3190
  description: draft.description,
3138
- 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))
3139
3192
  };
3140
- this.save(projectKey, resource);
3193
+ this.save(context, resource);
3141
3194
  return resource;
3142
3195
  }
3143
3196
 
3144
- getWithKey(projectKey, key) {
3145
- const result = this._storage.query(projectKey, this.getTypeId(), {
3197
+ getWithKey(context, key) {
3198
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3146
3199
  where: [`key="${key}"`]
3147
3200
  });
3148
3201
 
@@ -3175,7 +3228,7 @@ class ProductTypeService extends AbstractService {
3175
3228
  }
3176
3229
 
3177
3230
  getWithKey(request, response) {
3178
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3231
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3179
3232
 
3180
3233
  if (resource) {
3181
3234
  return response.status(200).send(resource);
@@ -3212,32 +3265,32 @@ class ProjectRepository extends AbstractRepository {
3212
3265
  constructor() {
3213
3266
  super(...arguments);
3214
3267
  this.actions = {
3215
- changeName: (projectKey, resource, {
3268
+ changeName: (context, resource, {
3216
3269
  name
3217
3270
  }) => {
3218
3271
  resource.name = name;
3219
3272
  },
3220
- changeCurrencies: (projectKey, resource, {
3273
+ changeCurrencies: (context, resource, {
3221
3274
  currencies
3222
3275
  }) => {
3223
3276
  resource.currencies = currencies;
3224
3277
  },
3225
- changeCountries: (projectKey, resource, {
3278
+ changeCountries: (context, resource, {
3226
3279
  countries
3227
3280
  }) => {
3228
3281
  resource.countries = countries;
3229
3282
  },
3230
- changeLanguages: (projectKey, resource, {
3283
+ changeLanguages: (context, resource, {
3231
3284
  languages
3232
3285
  }) => {
3233
3286
  resource.languages = languages;
3234
3287
  },
3235
- changeMessagesEnabled: (projectKey, resource, {
3288
+ changeMessagesEnabled: (context, resource, {
3236
3289
  messagesEnabled
3237
3290
  }) => {
3238
3291
  resource.messages.enabled = messagesEnabled;
3239
3292
  },
3240
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3293
+ changeProductSearchIndexingEnabled: (context, resource, {
3241
3294
  enabled
3242
3295
  }) => {
3243
3296
  var _resource$searchIndex;
@@ -3249,7 +3302,7 @@ class ProjectRepository extends AbstractRepository {
3249
3302
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3250
3303
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3251
3304
  },
3252
- changeOrderSearchStatus: (projectKey, resource, {
3305
+ changeOrderSearchStatus: (context, resource, {
3253
3306
  status
3254
3307
  }) => {
3255
3308
  var _resource$searchIndex2;
@@ -3261,22 +3314,22 @@ class ProjectRepository extends AbstractRepository {
3261
3314
  resource.searchIndexing.orders.status = status;
3262
3315
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3263
3316
  },
3264
- setShippingRateInputType: (projectKey, resource, {
3317
+ setShippingRateInputType: (context, resource, {
3265
3318
  shippingRateInputType
3266
3319
  }) => {
3267
3320
  resource.shippingRateInputType = shippingRateInputType;
3268
3321
  },
3269
- setExternalOAuth: (projectKey, resource, {
3322
+ setExternalOAuth: (context, resource, {
3270
3323
  externalOAuth
3271
3324
  }) => {
3272
3325
  resource.externalOAuth = externalOAuth;
3273
3326
  },
3274
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3327
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3275
3328
  countryTaxRateFallbackEnabled
3276
3329
  }) => {
3277
3330
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3278
3331
  },
3279
- changeCartsConfiguration: (projectKey, resource, {
3332
+ changeCartsConfiguration: (context, resource, {
3280
3333
  cartsConfiguration
3281
3334
  }) => {
3282
3335
  resource.carts = cartsConfiguration || {
@@ -3287,15 +3340,15 @@ class ProjectRepository extends AbstractRepository {
3287
3340
  };
3288
3341
  }
3289
3342
 
3290
- get(projectKey) {
3291
- const resource = this._storage.getProject(projectKey);
3343
+ get(context) {
3344
+ const resource = this._storage.getProject(context.projectKey);
3292
3345
 
3293
3346
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3294
3347
  return masked;
3295
3348
  }
3296
3349
 
3297
- save(projectKey, resource) {
3298
- const current = this.get(projectKey);
3350
+ save(context, resource) {
3351
+ const current = this.get(context);
3299
3352
 
3300
3353
  if (current) {
3301
3354
  checkConcurrentModification(current, resource.version);
@@ -3328,20 +3381,19 @@ class ProjectService {
3328
3381
  }
3329
3382
 
3330
3383
  get(request, response) {
3331
- const projectKey = request.params.projectKey;
3332
- const project = this.repository.get(projectKey);
3384
+ const project = this.repository.get(helpers.getRepositoryContext(request));
3333
3385
  return response.status(200).send(project);
3334
3386
  }
3335
3387
 
3336
3388
  post(request, response) {
3337
3389
  const updateRequest = request.body;
3338
- const project = this.repository.get(request.params.projectKey);
3390
+ const project = this.repository.get(helpers.getRepositoryContext(request));
3339
3391
 
3340
3392
  if (!project) {
3341
3393
  return response.status(404).send({});
3342
3394
  }
3343
3395
 
3344
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3396
+ this.repository.processUpdateActions(helpers.getRepositoryContext(request), project, updateRequest.actions);
3345
3397
  return response.status(200).send({});
3346
3398
  }
3347
3399
 
@@ -3351,11 +3403,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3351
3403
  constructor() {
3352
3404
  super(...arguments);
3353
3405
 
3354
- this._transformZoneRateDraft = (projectKey, draft) => {
3406
+ this._transformZoneRateDraft = (context, draft) => {
3355
3407
  var _draft$shippingRates;
3356
3408
 
3357
3409
  return { ...draft,
3358
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3410
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3359
3411
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3360
3412
  };
3361
3413
  };
@@ -3369,7 +3421,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3369
3421
  };
3370
3422
 
3371
3423
  this.actions = {
3372
- addShippingRate: (_projectKey, resource, {
3424
+ addShippingRate: (_context, resource, {
3373
3425
  shippingRate,
3374
3426
  zone
3375
3427
  }) => {
@@ -3389,7 +3441,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3389
3441
  shippingRates: [rate]
3390
3442
  });
3391
3443
  },
3392
- removeShippingRate: (_projectKey, resource, {
3444
+ removeShippingRate: (_context, resource, {
3393
3445
  shippingRate,
3394
3446
  zone
3395
3447
  }) => {
@@ -3403,10 +3455,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3403
3455
  }
3404
3456
  });
3405
3457
  },
3406
- addZone: (projectKey, resource, {
3458
+ addZone: (context, resource, {
3407
3459
  zone
3408
3460
  }) => {
3409
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3461
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3410
3462
 
3411
3463
  if (resource.zoneRates === undefined) {
3412
3464
  resource.zoneRates = [];
@@ -3417,39 +3469,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3417
3469
  shippingRates: []
3418
3470
  });
3419
3471
  },
3420
- removeZone: (_projectKey, resource, {
3472
+ removeZone: (_context, resource, {
3421
3473
  zone
3422
3474
  }) => {
3423
3475
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3424
3476
  return zoneRate.zone.id !== zone.id;
3425
3477
  });
3426
3478
  },
3427
- setKey: (_projectKey, resource, {
3479
+ setKey: (_context, resource, {
3428
3480
  key
3429
3481
  }) => {
3430
3482
  resource.key = key;
3431
3483
  },
3432
- setDescription: (_projectKey, resource, {
3484
+ setDescription: (_context, resource, {
3433
3485
  description
3434
3486
  }) => {
3435
3487
  resource.description = description;
3436
3488
  },
3437
- setLocalizedDescription: (_projectKey, resource, {
3489
+ setLocalizedDescription: (_context, resource, {
3438
3490
  localizedDescription
3439
3491
  }) => {
3440
3492
  resource.localizedDescription = localizedDescription;
3441
3493
  },
3442
- setPredicate: (_projectKey, resource, {
3494
+ setPredicate: (_context, resource, {
3443
3495
  predicate
3444
3496
  }) => {
3445
3497
  resource.predicate = predicate;
3446
3498
  },
3447
- changeIsDefault: (_projectKey, resource, {
3499
+ changeIsDefault: (_context, resource, {
3448
3500
  isDefault
3449
3501
  }) => {
3450
3502
  resource.isDefault = isDefault;
3451
3503
  },
3452
- changeName: (_projectKey, resource, {
3504
+ changeName: (_context, resource, {
3453
3505
  name
3454
3506
  }) => {
3455
3507
  resource.name = name;
@@ -3461,16 +3513,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3461
3513
  return 'shipping-method';
3462
3514
  }
3463
3515
 
3464
- create(projectKey, draft) {
3516
+ create(context, draft) {
3465
3517
  var _draft$zoneRates;
3466
3518
 
3467
3519
  const resource = { ...getBaseResourceProperties(),
3468
3520
  ...draft,
3469
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3470
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3471
- 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)
3472
3524
  };
3473
- this.save(projectKey, resource);
3525
+ this.save(context, resource);
3474
3526
  return resource;
3475
3527
  }
3476
3528
 
@@ -3498,13 +3550,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3498
3550
  return 'shopping-list';
3499
3551
  }
3500
3552
 
3501
- create(projectKey, draft) {
3553
+ create(context, draft) {
3502
3554
  var _draft$lineItems, _draft$store;
3503
3555
 
3504
3556
  // const product =
3505
3557
  const resource = { ...getBaseResourceProperties(),
3506
3558
  ...draft,
3507
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3559
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3508
3560
  textLineItems: [],
3509
3561
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3510
3562
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3519,16 +3571,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3519
3571
  typeId: 'product-type',
3520
3572
  id: ''
3521
3573
  },
3522
- custom: createCustomFields(e.custom, projectKey, this._storage)
3574
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3523
3575
  };
3524
3576
  }),
3525
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3577
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3526
3578
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3527
3579
  typeId: 'store',
3528
3580
  key: draft.store.key
3529
3581
  } : undefined
3530
3582
  };
3531
- this.save(projectKey, resource);
3583
+ this.save(context, resource);
3532
3584
  return resource;
3533
3585
  }
3534
3586
 
@@ -3550,22 +3602,22 @@ class StateRepository extends AbstractResourceRepository {
3550
3602
  constructor() {
3551
3603
  super(...arguments);
3552
3604
  this.actions = {
3553
- changeKey: (projectKey, resource, {
3605
+ changeKey: (context, resource, {
3554
3606
  key
3555
3607
  }) => {
3556
3608
  resource.key = key;
3557
3609
  },
3558
- setDescription: (projectKey, resource, {
3610
+ setDescription: (context, resource, {
3559
3611
  description
3560
3612
  }) => {
3561
3613
  resource.description = description;
3562
3614
  },
3563
- setName: (projectKey, resource, {
3615
+ setName: (context, resource, {
3564
3616
  name
3565
3617
  }) => {
3566
3618
  resource.name = name;
3567
3619
  },
3568
- setRoles: (projectKey, resource, {
3620
+ setRoles: (context, resource, {
3569
3621
  roles
3570
3622
  }) => {
3571
3623
  resource.roles = roles;
@@ -3577,14 +3629,14 @@ class StateRepository extends AbstractResourceRepository {
3577
3629
  return 'state';
3578
3630
  }
3579
3631
 
3580
- create(projectKey, draft) {
3632
+ create(context, draft) {
3581
3633
  const resource = { ...getBaseResourceProperties(),
3582
3634
  ...draft,
3583
3635
  builtIn: false,
3584
3636
  initial: draft.initial || false,
3585
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3637
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3586
3638
  };
3587
- this.save(projectKey, resource);
3639
+ this.save(context, resource);
3588
3640
  return resource;
3589
3641
  }
3590
3642
 
@@ -3606,17 +3658,17 @@ class StoreRepository extends AbstractResourceRepository {
3606
3658
  constructor() {
3607
3659
  super(...arguments);
3608
3660
  this.actions = {
3609
- setName: (projectKey, resource, {
3661
+ setName: (context, resource, {
3610
3662
  name
3611
3663
  }) => {
3612
3664
  resource.name = name;
3613
3665
  },
3614
- setDistributionChannels: (projectKey, resource, {
3666
+ setDistributionChannels: (context, resource, {
3615
3667
  distributionChannels
3616
3668
  }) => {
3617
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3669
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3618
3670
  },
3619
- setLanguages: (projectKey, resource, {
3671
+ setLanguages: (context, resource, {
3620
3672
  languages
3621
3673
  }) => {
3622
3674
  resource.languages = languages;
@@ -3628,25 +3680,25 @@ class StoreRepository extends AbstractResourceRepository {
3628
3680
  return 'store';
3629
3681
  }
3630
3682
 
3631
- create(projectKey, draft) {
3683
+ create(context, draft) {
3632
3684
  const resource = { ...getBaseResourceProperties(),
3633
3685
  key: draft.key,
3634
3686
  name: draft.name,
3635
3687
  languages: draft.languages,
3636
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3637
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3688
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3689
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3638
3690
  };
3639
- this.save(projectKey, resource);
3691
+ this.save(context, resource);
3640
3692
  return resource;
3641
3693
  }
3642
3694
 
3643
- transformChannels(projectKey, channels) {
3695
+ transformChannels(context, channels) {
3644
3696
  if (!channels) return [];
3645
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3697
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3646
3698
  }
3647
3699
 
3648
- getWithKey(projectKey, key) {
3649
- const result = this._storage.query(projectKey, this.getTypeId(), {
3700
+ getWithKey(context, key) {
3701
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3650
3702
  where: [`key="${key}"`]
3651
3703
  });
3652
3704
 
@@ -3678,7 +3730,7 @@ class StoreService extends AbstractService {
3678
3730
  }
3679
3731
 
3680
3732
  getWithKey(request, response) {
3681
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3733
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3682
3734
 
3683
3735
  if (resource) {
3684
3736
  return response.status(200).send(resource);
@@ -3694,7 +3746,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3694
3746
  return 'subscription';
3695
3747
  }
3696
3748
 
3697
- create(projectKey, draft) {
3749
+ create(context, draft) {
3698
3750
  // TODO: We could actually test this here by using the aws sdk. For now
3699
3751
  // hardcode a failed check when account id is 0000000000
3700
3752
  if (draft.destination.type === 'SQS') {
@@ -3720,7 +3772,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3720
3772
  messages: draft.messages || [],
3721
3773
  status: 'Healthy'
3722
3774
  };
3723
- this.save(projectKey, resource);
3775
+ this.save(context, resource);
3724
3776
  return resource;
3725
3777
  }
3726
3778
 
@@ -3748,7 +3800,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3748
3800
  });
3749
3801
 
3750
3802
  this.actions = {
3751
- addTaxRate: (projectKey, resource, {
3803
+ addTaxRate: (context, resource, {
3752
3804
  taxRate
3753
3805
  }) => {
3754
3806
  if (resource.rates === undefined) {
@@ -3757,7 +3809,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3757
3809
 
3758
3810
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3759
3811
  },
3760
- removeTaxRate: (projectKey, resource, {
3812
+ removeTaxRate: (context, resource, {
3761
3813
  taxRateId
3762
3814
  }) => {
3763
3815
  if (resource.rates === undefined) {
@@ -3768,7 +3820,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3768
3820
  return taxRate.id !== taxRateId;
3769
3821
  });
3770
3822
  },
3771
- replaceTaxRate: (projectKey, resource, {
3823
+ replaceTaxRate: (context, resource, {
3772
3824
  taxRateId,
3773
3825
  taxRate
3774
3826
  }) => {
@@ -3786,17 +3838,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3786
3838
  }
3787
3839
  }
3788
3840
  },
3789
- setDescription: (projectKey, resource, {
3841
+ setDescription: (context, resource, {
3790
3842
  description
3791
3843
  }) => {
3792
3844
  resource.description = description;
3793
3845
  },
3794
- setKey: (projectKey, resource, {
3846
+ setKey: (context, resource, {
3795
3847
  key
3796
3848
  }) => {
3797
3849
  resource.key = key;
3798
3850
  },
3799
- changeName: (projectKey, resource, {
3851
+ changeName: (context, resource, {
3800
3852
  name
3801
3853
  }) => {
3802
3854
  resource.name = name;
@@ -3808,19 +3860,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3808
3860
  return 'tax-category';
3809
3861
  }
3810
3862
 
3811
- create(projectKey, draft) {
3863
+ create(context, draft) {
3812
3864
  var _draft$rates;
3813
3865
 
3814
3866
  const resource = { ...getBaseResourceProperties(),
3815
3867
  ...draft,
3816
3868
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3817
3869
  };
3818
- this.save(projectKey, resource);
3870
+ this.save(context, resource);
3819
3871
  return resource;
3820
3872
  }
3821
3873
 
3822
- getWithKey(projectKey, key) {
3823
- const result = this._storage.query(projectKey, this.getTypeId(), {
3874
+ getWithKey(context, key) {
3875
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3824
3876
  where: [`key="${key}"`]
3825
3877
  });
3826
3878
 
@@ -3853,7 +3905,7 @@ class TaxCategoryService extends AbstractService {
3853
3905
  }
3854
3906
 
3855
3907
  getWithKey(request, response) {
3856
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3908
+ const resource = this.repository.getWithKey(helpers.getRepositoryContext(request), request.params.key);
3857
3909
 
3858
3910
  if (resource) {
3859
3911
  return response.status(200).send(resource);
@@ -3868,29 +3920,29 @@ class TypeRepository extends AbstractResourceRepository {
3868
3920
  constructor() {
3869
3921
  super(...arguments);
3870
3922
  this.actions = {
3871
- addFieldDefinition: (projectKey, resource, {
3923
+ addFieldDefinition: (context, resource, {
3872
3924
  fieldDefinition
3873
3925
  }) => {
3874
3926
  resource.fieldDefinitions.push(fieldDefinition);
3875
3927
  },
3876
- removeFieldDefinition: (projectKey, resource, {
3928
+ removeFieldDefinition: (context, resource, {
3877
3929
  fieldName
3878
3930
  }) => {
3879
3931
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3880
3932
  return f.name !== fieldName;
3881
3933
  });
3882
3934
  },
3883
- setDescription: (projectKey, resource, {
3935
+ setDescription: (context, resource, {
3884
3936
  description
3885
3937
  }) => {
3886
3938
  resource.description = description;
3887
3939
  },
3888
- changeName: (projectKey, resource, {
3940
+ changeName: (context, resource, {
3889
3941
  name
3890
3942
  }) => {
3891
3943
  resource.name = name;
3892
3944
  },
3893
- changeFieldDefinitionOrder: (projectKey, resource, {
3945
+ changeFieldDefinitionOrder: (context, resource, {
3894
3946
  fieldNames
3895
3947
  }) => {
3896
3948
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3914,7 +3966,7 @@ class TypeRepository extends AbstractResourceRepository {
3914
3966
 
3915
3967
  resource.fieldDefinitions.push(...current);
3916
3968
  },
3917
- addEnumValue: (projectKey, resource, {
3969
+ addEnumValue: (context, resource, {
3918
3970
  fieldName,
3919
3971
  value
3920
3972
  }) => {
@@ -3931,7 +3983,7 @@ class TypeRepository extends AbstractResourceRepository {
3931
3983
  }
3932
3984
  });
3933
3985
  },
3934
- changeEnumValueLabel: (projectKey, resource, {
3986
+ changeEnumValueLabel: (context, resource, {
3935
3987
  fieldName,
3936
3988
  value
3937
3989
  }) => {
@@ -3963,7 +4015,7 @@ class TypeRepository extends AbstractResourceRepository {
3963
4015
  return 'type';
3964
4016
  }
3965
4017
 
3966
- create(projectKey, draft) {
4018
+ create(context, draft) {
3967
4019
  const resource = { ...getBaseResourceProperties(),
3968
4020
  key: draft.key,
3969
4021
  name: draft.name,
@@ -3971,7 +4023,7 @@ class TypeRepository extends AbstractResourceRepository {
3971
4023
  fieldDefinitions: draft.fieldDefinitions || [],
3972
4024
  description: draft.description
3973
4025
  };
3974
- this.save(projectKey, resource);
4026
+ this.save(context, resource);
3975
4027
  return resource;
3976
4028
  }
3977
4029
 
@@ -3993,29 +4045,29 @@ class ZoneRepository extends AbstractResourceRepository {
3993
4045
  constructor() {
3994
4046
  super(...arguments);
3995
4047
  this.actions = {
3996
- addLocation: (projectKey, resource, {
4048
+ addLocation: (context, resource, {
3997
4049
  location
3998
4050
  }) => {
3999
4051
  resource.locations.push(location);
4000
4052
  },
4001
- removeLocation: (projectKey, resource, {
4053
+ removeLocation: (context, resource, {
4002
4054
  location
4003
4055
  }) => {
4004
4056
  resource.locations = resource.locations.filter(loc => {
4005
4057
  return !(loc.country === location.country && loc.state === location.state);
4006
4058
  });
4007
4059
  },
4008
- changeName: (projectKey, resource, {
4060
+ changeName: (context, resource, {
4009
4061
  name
4010
4062
  }) => {
4011
4063
  resource.name = name;
4012
4064
  },
4013
- setDescription: (projectKey, resource, {
4065
+ setDescription: (context, resource, {
4014
4066
  description
4015
4067
  }) => {
4016
4068
  resource.description = description;
4017
4069
  },
4018
- setKey: (projectKey, resource, {
4070
+ setKey: (context, resource, {
4019
4071
  key
4020
4072
  }) => {
4021
4073
  resource.key = key;
@@ -4027,14 +4079,14 @@ class ZoneRepository extends AbstractResourceRepository {
4027
4079
  return 'zone';
4028
4080
  }
4029
4081
 
4030
- create(projectKey, draft) {
4082
+ create(context, draft) {
4031
4083
  const resource = { ...getBaseResourceProperties(),
4032
4084
  key: draft.key,
4033
4085
  locations: draft.locations || [],
4034
4086
  name: draft.name,
4035
4087
  description: draft.description
4036
4088
  };
4037
- this.save(projectKey, resource);
4089
+ this.save(context, resource);
4038
4090
  return resource;
4039
4091
  }
4040
4092
 
@@ -4076,7 +4128,7 @@ class MyCustomerService extends AbstractService {
4076
4128
  }
4077
4129
 
4078
4130
  getMe(request, response) {
4079
- const resource = this.repository.getMe(request.params.projectKey);
4131
+ const resource = this.repository.getMe(helpers.getRepositoryContext(request));
4080
4132
 
4081
4133
  if (!resource) {
4082
4134
  return response.status(404).send('Not found');
@@ -4087,7 +4139,7 @@ class MyCustomerService extends AbstractService {
4087
4139
 
4088
4140
  signUp(request, response) {
4089
4141
  const draft = request.body;
4090
- const resource = this.repository.create(request.params.projectKey, draft);
4142
+ const resource = this.repository.create(helpers.getRepositoryContext(request), draft);
4091
4143
 
4092
4144
  const result = this._expandWithId(request, resource.id);
4093
4145
 
@@ -4102,7 +4154,7 @@ class MyCustomerService extends AbstractService {
4102
4154
  password
4103
4155
  } = request.body;
4104
4156
  const encodedPassword = Buffer.from(password).toString('base64');
4105
- const result = this.repository.query(request.params.projectKey, {
4157
+ const result = this.repository.query(helpers.getRepositoryContext(request), {
4106
4158
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4107
4159
  });
4108
4160
 
@@ -4123,10 +4175,22 @@ class MyCustomerService extends AbstractService {
4123
4175
 
4124
4176
  }
4125
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
+
4126
4190
  class MyOrderService extends AbstractService {
4127
4191
  constructor(parent, storage) {
4128
4192
  super(parent);
4129
- this.repository = new OrderRepository(storage);
4193
+ this.repository = new MyOrderRepository(storage);
4130
4194
  }
4131
4195
 
4132
4196
  getBasePath() {
@@ -4229,8 +4293,10 @@ class CommercetoolsMock {
4229
4293
 
4230
4294
  if (this.options.enableAuthentication) {
4231
4295
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4296
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4232
4297
  } else {
4233
4298
  app.use('/:projectKey', projectRouter);
4299
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4234
4300
  }
4235
4301
 
4236
4302
  this._projectService = new ProjectService(projectRouter, this._storage);