@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
@@ -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
 
@@ -1416,7 +1417,17 @@ class CartRepository extends AbstractResourceRepository {
1416
1417
  });
1417
1418
  }
1418
1419
 
1419
- const price = variant.prices[0];
1420
+ const currency = resource.totalPrice.currencyCode;
1421
+ const price = selectPrice({
1422
+ prices: variant.prices,
1423
+ currency,
1424
+ country: resource.country
1425
+ });
1426
+
1427
+ if (!price) {
1428
+ throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
1429
+ }
1430
+
1420
1431
  resource.lineItems.push({
1421
1432
  id: v4(),
1422
1433
  productId: product.id,
@@ -1440,7 +1451,7 @@ class CartRepository extends AbstractResourceRepository {
1440
1451
 
1441
1452
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1442
1453
  },
1443
- removeLineItem: (projectKey, resource, {
1454
+ removeLineItem: (context, resource, {
1444
1455
  lineItemId,
1445
1456
  quantity
1446
1457
  }) => {
@@ -1474,15 +1485,15 @@ class CartRepository extends AbstractResourceRepository {
1474
1485
 
1475
1486
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1476
1487
  },
1477
- setBillingAddress: (projectKey, resource, {
1488
+ setBillingAddress: (context, resource, {
1478
1489
  address
1479
1490
  }) => {
1480
1491
  resource.billingAddress = address;
1481
1492
  },
1482
- setShippingMethod: (projectKey, resource, {
1493
+ setShippingMethod: (context, resource, {
1483
1494
  shippingMethod
1484
1495
  }) => {
1485
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1496
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1486
1497
  shippingMethod);
1487
1498
 
1488
1499
  if (!resolvedType) {
@@ -1497,17 +1508,17 @@ class CartRepository extends AbstractResourceRepository {
1497
1508
  }
1498
1509
  };
1499
1510
  },
1500
- setCountry: (projectKey, resource, {
1511
+ setCountry: (context, resource, {
1501
1512
  country
1502
1513
  }) => {
1503
1514
  resource.country = country;
1504
1515
  },
1505
- setCustomerEmail: (projectKey, resource, {
1516
+ setCustomerEmail: (context, resource, {
1506
1517
  email
1507
1518
  }) => {
1508
1519
  resource.customerEmail = email;
1509
1520
  },
1510
- setCustomField: (projectKey, resource, {
1521
+ setCustomField: (context, resource, {
1511
1522
  name,
1512
1523
  value
1513
1524
  }) => {
@@ -1517,14 +1528,14 @@ class CartRepository extends AbstractResourceRepository {
1517
1528
 
1518
1529
  resource.custom.fields[name] = value;
1519
1530
  },
1520
- setCustomType: (projectKey, resource, {
1531
+ setCustomType: (context, resource, {
1521
1532
  type,
1522
1533
  fields
1523
1534
  }) => {
1524
1535
  if (!type) {
1525
1536
  resource.custom = undefined;
1526
1537
  } else {
1527
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1538
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1528
1539
 
1529
1540
  if (!resolvedType) {
1530
1541
  throw new Error(`Type ${type} not found`);
@@ -1539,29 +1550,25 @@ class CartRepository extends AbstractResourceRepository {
1539
1550
  };
1540
1551
  }
1541
1552
  },
1542
- setLocale: (projectKey, resource, {
1553
+ setLocale: (context, resource, {
1543
1554
  locale
1544
1555
  }) => {
1545
1556
  resource.locale = locale;
1546
1557
  },
1547
- setShippingAddress: (projectKey, resource, {
1558
+ setShippingAddress: (context, resource, {
1548
1559
  address
1549
1560
  }) => {
1550
1561
  resource.shippingAddress = address;
1551
1562
  }
1552
1563
  };
1553
1564
 
1554
- this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
1555
- var _variant$prices2;
1556
-
1565
+ this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
1557
1566
  const {
1558
1567
  productId,
1559
- quantity
1560
- } = draftLineItem; // @ts-ignore
1561
-
1562
- let variantId = draftLineItem.variant.id; // @ts-ignore
1563
-
1564
- let sku = draftLineItem.variant.sku;
1568
+ quantity,
1569
+ variantId,
1570
+ sku
1571
+ } = draftLineItem;
1565
1572
  let product = null;
1566
1573
  let variant;
1567
1574
 
@@ -1599,11 +1606,15 @@ class CartRepository extends AbstractResourceRepository {
1599
1606
  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.`);
1600
1607
  }
1601
1608
 
1602
- const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
1603
1609
  const quant = quantity != null ? quantity : 1;
1610
+ const price = selectPrice({
1611
+ prices: variant.prices,
1612
+ currency,
1613
+ country
1614
+ });
1604
1615
 
1605
1616
  if (!price) {
1606
- throw new Error(`Price not set on ${productId}`);
1617
+ throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1607
1618
  }
1608
1619
 
1609
1620
  return {
@@ -1631,13 +1642,13 @@ class CartRepository extends AbstractResourceRepository {
1631
1642
  return 'cart';
1632
1643
  }
1633
1644
 
1634
- create(projectKey, draft) {
1635
- var _draft$lineItems;
1645
+ create(context, draft) {
1646
+ var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1636
1647
 
1637
- const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
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 : [];
1638
1649
  const resource = { ...getBaseResourceProperties(),
1639
1650
  cartState: 'Active',
1640
- lineItems: lineItems != null ? lineItems : [],
1651
+ lineItems,
1641
1652
  customLineItems: [],
1642
1653
  totalPrice: {
1643
1654
  type: 'centPrecision',
@@ -1645,16 +1656,18 @@ class CartRepository extends AbstractResourceRepository {
1645
1656
  currencyCode: draft.currency,
1646
1657
  fractionDigits: 0
1647
1658
  },
1648
- taxMode: 'Platform',
1649
- taxRoundingMode: 'HalfEven',
1650
- taxCalculationMode: 'LineItemLevel',
1659
+ taxMode: (_draft$taxMode = draft.taxMode) != null ? _draft$taxMode : 'Platform',
1660
+ taxRoundingMode: (_draft$taxRoundingMod = draft.taxRoundingMode) != null ? _draft$taxRoundingMod : 'HalfEven',
1661
+ taxCalculationMode: (_draft$taxCalculation = draft.taxCalculationMode) != null ? _draft$taxCalculation : 'LineItemLevel',
1651
1662
  refusedGifts: [],
1652
- origin: 'Customer',
1653
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1663
+ locale: draft.locale,
1664
+ country: draft.country,
1665
+ origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1666
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1654
1667
  }; // @ts-ignore
1655
1668
 
1656
1669
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1657
- this.save(projectKey, resource);
1670
+ this.save(context, resource);
1658
1671
  return resource;
1659
1672
  }
1660
1673
 
@@ -1673,6 +1686,25 @@ class CartRepository extends AbstractResourceRepository {
1673
1686
 
1674
1687
  }
1675
1688
 
1689
+ const selectPrice = ({
1690
+ prices,
1691
+ currency,
1692
+ country
1693
+ }) => {
1694
+ if (!prices) {
1695
+ return undefined;
1696
+ } // Quick-and-dirty way of selecting price based on the given currency and country.
1697
+ // Can be improved later to give more priority to exact matches over
1698
+ // 'all country' matches, and include customer groups in the mix as well
1699
+
1700
+
1701
+ return prices.find(price => {
1702
+ const countryMatch = !price.country || price.country === country;
1703
+ const currencyMatch = price.value.currencyCode === currency;
1704
+ return countryMatch && currencyMatch;
1705
+ });
1706
+ };
1707
+
1676
1708
  const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
1677
1709
 
1678
1710
  const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
@@ -1681,10 +1713,10 @@ class OrderRepository extends AbstractResourceRepository {
1681
1713
  constructor() {
1682
1714
  super(...arguments);
1683
1715
  this.actions = {
1684
- addPayment: (projectKey, resource, {
1716
+ addPayment: (context, resource, {
1685
1717
  payment
1686
1718
  }) => {
1687
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1719
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1688
1720
 
1689
1721
  if (!resolvedPayment) {
1690
1722
  throw new Error(`Payment ${payment.id} not found`);
@@ -1701,20 +1733,20 @@ class OrderRepository extends AbstractResourceRepository {
1701
1733
  id: payment.id
1702
1734
  });
1703
1735
  },
1704
- changeOrderState: (projectKey, resource, {
1736
+ changeOrderState: (context, resource, {
1705
1737
  orderState
1706
1738
  }) => {
1707
1739
  resource.orderState = orderState;
1708
1740
  },
1709
- changePaymentState: (projectKey, resource, {
1741
+ changePaymentState: (context, resource, {
1710
1742
  paymentState
1711
1743
  }) => {
1712
1744
  resource.paymentState = paymentState;
1713
1745
  },
1714
- transitionState: (projectKey, resource, {
1746
+ transitionState: (context, resource, {
1715
1747
  state
1716
1748
  }) => {
1717
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1749
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1718
1750
 
1719
1751
  if (!resolvedType) {
1720
1752
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1725,17 +1757,17 @@ class OrderRepository extends AbstractResourceRepository {
1725
1757
  id: resolvedType.id
1726
1758
  };
1727
1759
  },
1728
- setBillingAddress: (projectKey, resource, {
1760
+ setBillingAddress: (context, resource, {
1729
1761
  address
1730
1762
  }) => {
1731
1763
  resource.billingAddress = address;
1732
1764
  },
1733
- setCustomerEmail: (projectKey, resource, {
1765
+ setCustomerEmail: (context, resource, {
1734
1766
  email
1735
1767
  }) => {
1736
1768
  resource.customerEmail = email;
1737
1769
  },
1738
- setCustomField: (projectKey, resource, {
1770
+ setCustomField: (context, resource, {
1739
1771
  name,
1740
1772
  value
1741
1773
  }) => {
@@ -1745,14 +1777,14 @@ class OrderRepository extends AbstractResourceRepository {
1745
1777
 
1746
1778
  resource.custom.fields[name] = value;
1747
1779
  },
1748
- setCustomType: (projectKey, resource, {
1780
+ setCustomType: (context, resource, {
1749
1781
  type,
1750
1782
  fields
1751
1783
  }) => {
1752
1784
  if (!type) {
1753
1785
  resource.custom = undefined;
1754
1786
  } else {
1755
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1787
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1756
1788
 
1757
1789
  if (!resolvedType) {
1758
1790
  throw new Error(`Type ${type} not found`);
@@ -1767,27 +1799,27 @@ class OrderRepository extends AbstractResourceRepository {
1767
1799
  };
1768
1800
  }
1769
1801
  },
1770
- setLocale: (projectKey, resource, {
1802
+ setLocale: (context, resource, {
1771
1803
  locale
1772
1804
  }) => {
1773
1805
  resource.locale = locale;
1774
1806
  },
1775
- setOrderNumber: (projectKey, resource, {
1807
+ setOrderNumber: (context, resource, {
1776
1808
  orderNumber
1777
1809
  }) => {
1778
1810
  resource.orderNumber = orderNumber;
1779
1811
  },
1780
- setShippingAddress: (projectKey, resource, {
1812
+ setShippingAddress: (context, resource, {
1781
1813
  address
1782
1814
  }) => {
1783
1815
  resource.shippingAddress = address;
1784
1816
  },
1785
- setStore: (projectKey, resource, {
1817
+ setStore: (context, resource, {
1786
1818
  store
1787
1819
  }) => {
1788
1820
  if (!store) return;
1789
1821
 
1790
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1822
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1791
1823
 
1792
1824
  if (!resolvedType) {
1793
1825
  throw new Error(`No store found with key=${store.key}`);
@@ -1806,17 +1838,24 @@ class OrderRepository extends AbstractResourceRepository {
1806
1838
  return 'order';
1807
1839
  }
1808
1840
 
1809
- create(projectKey, draft) {
1841
+ create(context, draft) {
1810
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
+ }
1811
1848
 
1812
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1849
+ createFromCart(context, cartReference, orderNumber) {
1850
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1813
1851
 
1814
1852
  if (!cart) {
1815
1853
  throw new Error('Cannot find cart');
1816
1854
  }
1817
1855
 
1818
1856
  const resource = { ...getBaseResourceProperties(),
1819
- orderNumber: draft.orderNumber,
1857
+ orderNumber,
1858
+ cart: cartReference,
1820
1859
  orderState: 'Open',
1821
1860
  lineItems: [],
1822
1861
  customLineItems: [],
@@ -1824,13 +1863,17 @@ class OrderRepository extends AbstractResourceRepository {
1824
1863
  refusedGifts: [],
1825
1864
  origin: 'Customer',
1826
1865
  syncInfo: [],
1866
+ store: context.storeKey ? {
1867
+ key: context.storeKey,
1868
+ typeId: 'store'
1869
+ } : undefined,
1827
1870
  lastMessageSequenceNumber: 0
1828
1871
  };
1829
- this.save(projectKey, resource);
1872
+ this.save(context, resource);
1830
1873
  return resource;
1831
1874
  }
1832
1875
 
1833
- import(projectKey, draft) {
1876
+ import(context, draft) {
1834
1877
  var _draft$lineItems, _draft$customLineItem;
1835
1878
 
1836
1879
  // TODO: Check if order with given orderNumber already exists
@@ -1838,7 +1881,7 @@ class OrderRepository extends AbstractResourceRepository {
1838
1881
  const resource = { ...getBaseResourceProperties(),
1839
1882
  billingAddress: draft.billingAddress,
1840
1883
  shippingAddress: draft.shippingAddress,
1841
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1884
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1842
1885
  customerEmail: draft.customerEmail,
1843
1886
  lastMessageSequenceNumber: 0,
1844
1887
  orderNumber: draft.orderNumber,
@@ -1846,21 +1889,21 @@ class OrderRepository extends AbstractResourceRepository {
1846
1889
  origin: draft.origin || 'Customer',
1847
1890
  paymentState: draft.paymentState,
1848
1891
  refusedGifts: [],
1849
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1892
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1850
1893
  syncInfo: [],
1851
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1852
- 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))) || [],
1853
1896
  totalPrice: {
1854
1897
  type: 'centPrecision',
1855
1898
  ...draft.totalPrice,
1856
1899
  fractionDigits: 2
1857
1900
  }
1858
1901
  };
1859
- this.save(projectKey, resource);
1902
+ this.save(context, resource);
1860
1903
  return resource;
1861
1904
  }
1862
1905
 
1863
- lineItemFromImportDraft(projectKey, draft) {
1906
+ lineItemFromImportDraft(context, draft) {
1864
1907
  let product;
1865
1908
  let variant;
1866
1909
 
@@ -1870,7 +1913,7 @@ class OrderRepository extends AbstractResourceRepository {
1870
1913
  sku: draft.variant.sku
1871
1914
  };
1872
1915
 
1873
- var items = this._storage.query(projectKey, 'product', {
1916
+ var items = this._storage.query(context.projectKey, 'product', {
1874
1917
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1875
1918
  });
1876
1919
 
@@ -1897,7 +1940,7 @@ class OrderRepository extends AbstractResourceRepository {
1897
1940
  }
1898
1941
 
1899
1942
  const lineItem = { ...getBaseResourceProperties(),
1900
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1943
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1901
1944
  discountedPricePerQuantity: [],
1902
1945
  lineItemMode: 'Standard',
1903
1946
  name: draft.name,
@@ -1918,9 +1961,9 @@ class OrderRepository extends AbstractResourceRepository {
1918
1961
  return lineItem;
1919
1962
  }
1920
1963
 
1921
- customLineItemFromImportDraft(projectKey, draft) {
1964
+ customLineItemFromImportDraft(context, draft) {
1922
1965
  const lineItem = { ...getBaseResourceProperties(),
1923
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1966
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1924
1967
  discountedPricePerQuantity: [],
1925
1968
  money: createTypedMoney(draft.money),
1926
1969
  name: draft.name,
@@ -1932,8 +1975,8 @@ class OrderRepository extends AbstractResourceRepository {
1932
1975
  return lineItem;
1933
1976
  }
1934
1977
 
1935
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1936
- 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,
1937
1980
  where: [`orderNumber="${orderNumber}"`]
1938
1981
  });
1939
1982
 
@@ -1964,17 +2007,25 @@ class CartService extends AbstractService {
1964
2007
 
1965
2008
  extraRoutes(parent) {
1966
2009
  parent.post('/replicate', (request, response) => {
1967
- // @ts-ignore
1968
- 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);
1969
2013
 
1970
2014
  if (!cartOrOrder) {
1971
2015
  return response.status(400).send();
1972
2016
  }
1973
2017
 
1974
- const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
2018
+ const cartDraft = { ...cartOrOrder,
1975
2019
  currency: cartOrOrder.totalPrice.currencyCode,
1976
- discountCodes: []
1977
- });
2020
+ discountCodes: [],
2021
+ lineItems: cartOrOrder.lineItems.map(lineItem => {
2022
+ return { ...lineItem,
2023
+ variantId: lineItem.variant.id,
2024
+ sku: lineItem.variant.sku
2025
+ };
2026
+ })
2027
+ };
2028
+ const newCart = this.repository.create(context, cartDraft);
1978
2029
  return response.status(200).send(newCart);
1979
2030
  });
1980
2031
  }
@@ -1985,7 +2036,7 @@ class CategoryRepository extends AbstractResourceRepository {
1985
2036
  constructor() {
1986
2037
  super(...arguments);
1987
2038
  this.actions = {
1988
- changeAssetName: (projectKey, resource, {
2039
+ changeAssetName: (context, resource, {
1989
2040
  assetId,
1990
2041
  assetKey,
1991
2042
  name
@@ -2002,17 +2053,17 @@ class CategoryRepository extends AbstractResourceRepository {
2002
2053
  }
2003
2054
  });
2004
2055
  },
2005
- changeSlug: (projectKey, resource, {
2056
+ changeSlug: (context, resource, {
2006
2057
  slug
2007
2058
  }) => {
2008
2059
  resource.slug = slug;
2009
2060
  },
2010
- setKey: (projectKey, resource, {
2061
+ setKey: (context, resource, {
2011
2062
  key
2012
2063
  }) => {
2013
2064
  resource.key = key;
2014
2065
  },
2015
- setAssetDescription: (projectKey, resource, {
2066
+ setAssetDescription: (context, resource, {
2016
2067
  assetId,
2017
2068
  assetKey,
2018
2069
  description
@@ -2029,7 +2080,7 @@ class CategoryRepository extends AbstractResourceRepository {
2029
2080
  }
2030
2081
  });
2031
2082
  },
2032
- setAssetSources: (projectKey, resource, {
2083
+ setAssetSources: (context, resource, {
2033
2084
  assetId,
2034
2085
  assetKey,
2035
2086
  sources
@@ -2046,22 +2097,22 @@ class CategoryRepository extends AbstractResourceRepository {
2046
2097
  }
2047
2098
  });
2048
2099
  },
2049
- setDescription: (projectKey, resource, {
2100
+ setDescription: (context, resource, {
2050
2101
  description
2051
2102
  }) => {
2052
2103
  resource.description = description;
2053
2104
  },
2054
- setMetaDescription: (projectKey, resource, {
2105
+ setMetaDescription: (context, resource, {
2055
2106
  metaDescription
2056
2107
  }) => {
2057
2108
  resource.metaDescription = metaDescription;
2058
2109
  },
2059
- setMetaKeywords: (projectKey, resource, {
2110
+ setMetaKeywords: (context, resource, {
2060
2111
  metaKeywords
2061
2112
  }) => {
2062
2113
  resource.metaKeywords = metaKeywords;
2063
2114
  },
2064
- setMetaTitle: (projectKey, resource, {
2115
+ setMetaTitle: (context, resource, {
2065
2116
  metaTitle
2066
2117
  }) => {
2067
2118
  resource.metaTitle = metaTitle;
@@ -2073,7 +2124,7 @@ class CategoryRepository extends AbstractResourceRepository {
2073
2124
  return 'category';
2074
2125
  }
2075
2126
 
2076
- create(projectKey, draft) {
2127
+ create(context, draft) {
2077
2128
  var _draft$assets;
2078
2129
 
2079
2130
  const resource = { ...getBaseResourceProperties(),
@@ -2095,11 +2146,11 @@ class CategoryRepository extends AbstractResourceRepository {
2095
2146
  sources: d.sources,
2096
2147
  tags: d.tags,
2097
2148
  key: d.key,
2098
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2149
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2099
2150
  };
2100
2151
  })) || []
2101
2152
  };
2102
- this.save(projectKey, resource);
2153
+ this.save(context, resource);
2103
2154
  return resource;
2104
2155
  }
2105
2156
 
@@ -2122,12 +2173,12 @@ class ChannelRepository extends AbstractResourceRepository {
2122
2173
  return 'channel';
2123
2174
  }
2124
2175
 
2125
- create(projectKey, draft) {
2176
+ create(context, draft) {
2126
2177
  const resource = { ...getBaseResourceProperties(),
2127
2178
  key: draft.key,
2128
2179
  roles: draft.roles || []
2129
2180
  };
2130
- this.save(projectKey, resource);
2181
+ this.save(context, resource);
2131
2182
  return resource;
2132
2183
  }
2133
2184
 
@@ -2149,12 +2200,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2149
2200
  constructor() {
2150
2201
  super(...arguments);
2151
2202
  this.actions = {
2152
- setKey: (projectKey, resource, {
2203
+ setKey: (context, resource, {
2153
2204
  key
2154
2205
  }) => {
2155
2206
  resource.key = key;
2156
2207
  },
2157
- changeName: (projectKey, resource, {
2208
+ changeName: (context, resource, {
2158
2209
  name
2159
2210
  }) => {
2160
2211
  resource.name = name;
@@ -2166,12 +2217,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2166
2217
  return 'customer';
2167
2218
  }
2168
2219
 
2169
- create(projectKey, draft) {
2220
+ create(context, draft) {
2170
2221
  const resource = { ...getBaseResourceProperties(),
2171
2222
  key: draft.key,
2172
2223
  name: draft.groupName
2173
2224
  };
2174
- this.save(projectKey, resource);
2225
+ this.save(context, resource);
2175
2226
  return resource;
2176
2227
  }
2177
2228
 
@@ -2193,7 +2244,7 @@ class CustomerRepository extends AbstractResourceRepository {
2193
2244
  constructor() {
2194
2245
  super(...arguments);
2195
2246
  this.actions = {
2196
- changeEmail: (_projectKey, resource, {
2247
+ changeEmail: (_context, resource, {
2197
2248
  email
2198
2249
  }) => {
2199
2250
  resource.email = email;
@@ -2205,19 +2256,19 @@ class CustomerRepository extends AbstractResourceRepository {
2205
2256
  return 'customer';
2206
2257
  }
2207
2258
 
2208
- create(projectKey, draft) {
2259
+ create(context, draft) {
2209
2260
  const resource = { ...getBaseResourceProperties(),
2210
2261
  email: draft.email,
2211
- password: Buffer.from(draft.password).toString('base64'),
2262
+ password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2212
2263
  isEmailVerified: draft.isEmailVerified || false,
2213
2264
  addresses: []
2214
2265
  };
2215
- this.save(projectKey, resource);
2266
+ this.save(context, resource);
2216
2267
  return resource;
2217
2268
  }
2218
2269
 
2219
- getMe(projectKey) {
2220
- 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
2221
2272
 
2222
2273
 
2223
2274
  if (results.count > 0) {
@@ -2241,10 +2292,12 @@ class CustomerService extends AbstractService {
2241
2292
 
2242
2293
  extraRoutes(parent) {
2243
2294
  parent.post('/password-token', (request, response) => {
2244
- const customer = this.repository.query(request.params.projectKey, {
2295
+ const customer = this.repository.query(getRepositoryContext(request), {
2245
2296
  where: [`email="${request.body.email}"`]
2246
- });
2247
- 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;
2248
2301
  const {
2249
2302
  version,
2250
2303
  ...rest
@@ -2264,8 +2317,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2264
2317
  return 'key-value-document';
2265
2318
  }
2266
2319
 
2267
- create(projectKey, draft) {
2268
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2320
+ create(context, draft) {
2321
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2269
2322
  const baseProperties = getBaseResourceProperties();
2270
2323
 
2271
2324
  if (current) {
@@ -2293,12 +2346,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2293
2346
  key: draft.key,
2294
2347
  value: draft.value
2295
2348
  };
2296
- this.save(projectKey, resource);
2349
+ this.save(context, resource);
2297
2350
  return resource;
2298
2351
  }
2299
2352
 
2300
- getWithContainerAndKey(projectKey, container, key) {
2301
- const items = this._storage.all(projectKey, this.getTypeId());
2353
+ getWithContainerAndKey(context, container, key) {
2354
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2302
2355
 
2303
2356
  return items.find(item => item.container === container && item.key === key);
2304
2357
  }
@@ -2322,7 +2375,7 @@ class CustomObjectService extends AbstractService {
2322
2375
  }
2323
2376
 
2324
2377
  getWithContainerAndKey(request, response) {
2325
- 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);
2326
2379
 
2327
2380
  if (!result) {
2328
2381
  return response.status(404).send('Not Found');
@@ -2336,18 +2389,18 @@ class CustomObjectService extends AbstractService {
2336
2389
  key: request.params.key,
2337
2390
  container: request.params.container
2338
2391
  };
2339
- const result = this.repository.create(request.params.projectKey, draft);
2392
+ const result = this.repository.create(getRepositoryContext(request), draft);
2340
2393
  return response.status(200).send(result);
2341
2394
  }
2342
2395
 
2343
2396
  deleteWithContainerAndKey(request, response) {
2344
- 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);
2345
2398
 
2346
2399
  if (!current) {
2347
2400
  return response.status(404).send('Not Found');
2348
2401
  }
2349
2402
 
2350
- const result = this.repository.delete(request.params.projectKey, current.id);
2403
+ const result = this.repository.delete(getRepositoryContext(request), current.id);
2351
2404
  return response.status(200).send(result);
2352
2405
  }
2353
2406
 
@@ -2357,12 +2410,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2357
2410
  constructor() {
2358
2411
  super(...arguments);
2359
2412
  this.actions = {
2360
- changeIsActive: (projectKey, resource, {
2413
+ changeIsActive: (context, resource, {
2361
2414
  isActive
2362
2415
  }) => {
2363
2416
  resource.isActive = isActive;
2364
2417
  },
2365
- changeCartDiscounts: (projectKey, resource, {
2418
+ changeCartDiscounts: (context, resource, {
2366
2419
  cartDiscounts
2367
2420
  }) => {
2368
2421
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2370,42 +2423,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2370
2423
  id: obj.id
2371
2424
  }));
2372
2425
  },
2373
- setDescription: (projectKey, resource, {
2426
+ setDescription: (context, resource, {
2374
2427
  description
2375
2428
  }) => {
2376
2429
  resource.description = description;
2377
2430
  },
2378
- setCartPredicate: (projectKey, resource, {
2431
+ setCartPredicate: (context, resource, {
2379
2432
  cartPredicate
2380
2433
  }) => {
2381
2434
  resource.cartPredicate = cartPredicate;
2382
2435
  },
2383
- setName: (projectKey, resource, {
2436
+ setName: (context, resource, {
2384
2437
  name
2385
2438
  }) => {
2386
2439
  resource.name = name;
2387
2440
  },
2388
- setMaxApplications: (projectKey, resource, {
2441
+ setMaxApplications: (context, resource, {
2389
2442
  maxApplications
2390
2443
  }) => {
2391
2444
  resource.maxApplications = maxApplications;
2392
2445
  },
2393
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2446
+ setMaxApplicationsPerCustomer: (context, resource, {
2394
2447
  maxApplicationsPerCustomer
2395
2448
  }) => {
2396
2449
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2397
2450
  },
2398
- setValidFrom: (projectKey, resource, {
2451
+ setValidFrom: (context, resource, {
2399
2452
  validFrom
2400
2453
  }) => {
2401
2454
  resource.validFrom = validFrom;
2402
2455
  },
2403
- setValidUntil: (projectKey, resource, {
2456
+ setValidUntil: (context, resource, {
2404
2457
  validUntil
2405
2458
  }) => {
2406
2459
  resource.validUntil = validUntil;
2407
2460
  },
2408
- setValidFromAndUntil: (projectKey, resource, {
2461
+ setValidFromAndUntil: (context, resource, {
2409
2462
  validFrom,
2410
2463
  validUntil
2411
2464
  }) => {
@@ -2419,7 +2472,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2419
2472
  return 'cart-discount';
2420
2473
  }
2421
2474
 
2422
- create(projectKey, draft) {
2475
+ create(context, draft) {
2423
2476
  const resource = { ...getBaseResourceProperties(),
2424
2477
  applicationVersion: 1,
2425
2478
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2438,7 +2491,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2438
2491
  maxApplications: draft.maxApplications,
2439
2492
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2440
2493
  };
2441
- this.save(projectKey, resource);
2494
+ this.save(context, resource);
2442
2495
  return resource;
2443
2496
  }
2444
2497
 
@@ -2460,22 +2513,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2460
2513
  constructor() {
2461
2514
  super(...arguments);
2462
2515
  this.actions = {
2463
- setKey: (projectKey, resource, {
2516
+ setKey: (context, resource, {
2464
2517
  key
2465
2518
  }) => {
2466
2519
  resource.key = key;
2467
2520
  },
2468
- setTimeoutInMs: (projectKey, resource, {
2521
+ setTimeoutInMs: (context, resource, {
2469
2522
  timeoutInMs
2470
2523
  }) => {
2471
2524
  resource.timeoutInMs = timeoutInMs;
2472
2525
  },
2473
- changeTriggers: (projectKey, resource, {
2526
+ changeTriggers: (context, resource, {
2474
2527
  triggers
2475
2528
  }) => {
2476
2529
  resource.triggers = triggers;
2477
2530
  },
2478
- changeDestination: (projectKey, resource, {
2531
+ changeDestination: (context, resource, {
2479
2532
  destination
2480
2533
  }) => {
2481
2534
  resource.destination = destination;
@@ -2487,14 +2540,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2487
2540
  return 'extension';
2488
2541
  }
2489
2542
 
2490
- create(projectKey, draft) {
2543
+ create(context, draft) {
2491
2544
  const resource = { ...getBaseResourceProperties(),
2492
2545
  key: draft.key,
2493
2546
  timeoutInMs: draft.timeoutInMs,
2494
2547
  destination: draft.destination,
2495
2548
  triggers: draft.triggers
2496
2549
  };
2497
- this.save(projectKey, resource);
2550
+ this.save(context, resource);
2498
2551
  return resource;
2499
2552
  }
2500
2553
 
@@ -2516,19 +2569,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2516
2569
  constructor() {
2517
2570
  super(...arguments);
2518
2571
  this.actions = {
2519
- changeQuantity: (projectKey, resource, {
2572
+ changeQuantity: (context, resource, {
2520
2573
  quantity
2521
2574
  }) => {
2522
2575
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2523
2576
 
2524
2577
  resource.availableQuantity = quantity;
2525
2578
  },
2526
- setExpectedDelivery: (projectKey, resource, {
2579
+ setExpectedDelivery: (context, resource, {
2527
2580
  expectedDelivery
2528
2581
  }) => {
2529
2582
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2530
2583
  },
2531
- setCustomField: (projectKey, resource, {
2584
+ setCustomField: (context, resource, {
2532
2585
  name,
2533
2586
  value
2534
2587
  }) => {
@@ -2538,14 +2591,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2538
2591
 
2539
2592
  resource.custom.fields[name] = value;
2540
2593
  },
2541
- setCustomType: (projectKey, resource, {
2594
+ setCustomType: (context, resource, {
2542
2595
  type,
2543
2596
  fields
2544
2597
  }) => {
2545
2598
  if (!type) {
2546
2599
  resource.custom = undefined;
2547
2600
  } else {
2548
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2601
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2549
2602
 
2550
2603
  if (!resolvedType) {
2551
2604
  throw new Error(`Type ${type} not found`);
@@ -2560,7 +2613,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2560
2613
  };
2561
2614
  }
2562
2615
  },
2563
- setRestockableInDays: (projectKey, resource, {
2616
+ setRestockableInDays: (context, resource, {
2564
2617
  restockableInDays
2565
2618
  }) => {
2566
2619
  resource.restockableInDays = restockableInDays;
@@ -2572,7 +2625,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2572
2625
  return 'inventory-entry';
2573
2626
  }
2574
2627
 
2575
- create(projectKey, draft) {
2628
+ create(context, draft) {
2576
2629
  var _draft$supplyChannel$, _draft$supplyChannel;
2577
2630
 
2578
2631
  const resource = { ...getBaseResourceProperties(),
@@ -2585,9 +2638,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2585
2638
  typeId: 'channel',
2586
2639
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2587
2640
  },
2588
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2641
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2589
2642
  };
2590
- this.save(projectKey, resource);
2643
+ this.save(context, resource);
2591
2644
  return resource;
2592
2645
  }
2593
2646
 
@@ -2647,14 +2700,14 @@ class PaymentRepository extends AbstractResourceRepository {
2647
2700
  constructor() {
2648
2701
  super(...arguments);
2649
2702
 
2650
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2703
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2651
2704
  id: v4(),
2652
2705
  amount: createTypedMoney(draft.amount),
2653
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2706
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2654
2707
  });
2655
2708
 
2656
2709
  this.actions = {
2657
- setCustomField: (projectKey, resource, {
2710
+ setCustomField: (context, resource, {
2658
2711
  name,
2659
2712
  value
2660
2713
  }) => {
@@ -2664,14 +2717,14 @@ class PaymentRepository extends AbstractResourceRepository {
2664
2717
 
2665
2718
  resource.custom.fields[name] = value;
2666
2719
  },
2667
- setCustomType: (projectKey, resource, {
2720
+ setCustomType: (context, resource, {
2668
2721
  type,
2669
2722
  fields
2670
2723
  }) => {
2671
2724
  if (!type) {
2672
2725
  resource.custom = undefined;
2673
2726
  } else {
2674
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2727
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2675
2728
 
2676
2729
  if (!resolvedType) {
2677
2730
  throw new Error(`Type ${type} not found`);
@@ -2686,12 +2739,12 @@ class PaymentRepository extends AbstractResourceRepository {
2686
2739
  };
2687
2740
  }
2688
2741
  },
2689
- addTransaction: (projectKey, resource, {
2742
+ addTransaction: (context, resource, {
2690
2743
  transaction
2691
2744
  }) => {
2692
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2745
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2693
2746
  },
2694
- changeTransactionState: (_projectKey, resource, {
2747
+ changeTransactionState: (_context, resource, {
2695
2748
  transactionId,
2696
2749
  state
2697
2750
  }) => {
@@ -2701,10 +2754,10 @@ class PaymentRepository extends AbstractResourceRepository {
2701
2754
  };
2702
2755
  resource.transactions[index] = updatedTransaction;
2703
2756
  },
2704
- transitionState: (projectKey, resource, {
2757
+ transitionState: (context, resource, {
2705
2758
  state
2706
2759
  }) => {
2707
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2760
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2708
2761
 
2709
2762
  if (!stateObj) {
2710
2763
  throw new Error(`State ${state} not found`);
@@ -2723,18 +2776,18 @@ class PaymentRepository extends AbstractResourceRepository {
2723
2776
  return 'payment';
2724
2777
  }
2725
2778
 
2726
- create(projectKey, draft) {
2779
+ create(context, draft) {
2727
2780
  const resource = { ...getBaseResourceProperties(),
2728
2781
  amountPlanned: createTypedMoney(draft.amountPlanned),
2729
2782
  paymentMethodInfo: draft.paymentMethodInfo,
2730
2783
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2731
- 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
2732
2785
  } : {},
2733
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2734
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2735
- 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)
2736
2789
  };
2737
- this.save(projectKey, resource);
2790
+ this.save(context, resource);
2738
2791
  return resource;
2739
2792
  }
2740
2793
 
@@ -2769,12 +2822,12 @@ class OrderService extends AbstractService {
2769
2822
 
2770
2823
  import(request, response) {
2771
2824
  const importDraft = request.body;
2772
- const resource = this.repository.import(request.params.projectKey, importDraft);
2825
+ const resource = this.repository.import(getRepositoryContext(request), importDraft);
2773
2826
  return response.status(200).send(resource);
2774
2827
  }
2775
2828
 
2776
2829
  getWithOrderNumber(request, response) {
2777
- 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);
2778
2831
 
2779
2832
  if (resource) {
2780
2833
  return response.status(200).send(resource);
@@ -2826,7 +2879,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2826
2879
  return 'product-projection';
2827
2880
  }
2828
2881
 
2829
- create(projectKey, draft) {
2882
+ create(context, draft) {
2830
2883
  var _draft$variants$map, _draft$variants;
2831
2884
 
2832
2885
  if (!draft.masterVariant) {
@@ -2851,17 +2904,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2851
2904
  // @ts-ignore
2852
2905
  searchKeywords: draft.searchKeywords
2853
2906
  };
2854
- this.save(projectKey, resource);
2907
+ this.save(context, resource);
2855
2908
  return resource;
2856
2909
  }
2857
2910
 
2858
- search(projectKey, query) {
2911
+ search(context, query) {
2859
2912
  var _query$filterQuery;
2860
2913
 
2861
2914
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2862
2915
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2863
2916
 
2864
- const results = this._storage.query(projectKey, this.getTypeId(), {
2917
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2865
2918
  where: wherePredicate,
2866
2919
  offset: query.offset ? Number(query.offset) : undefined,
2867
2920
  limit: query.limit ? Number(query.limit) : undefined
@@ -2896,7 +2949,7 @@ class ProductProjectionService extends AbstractService {
2896
2949
  }
2897
2950
 
2898
2951
  search(request, response) {
2899
- const resource = this.repository.search(request.params.projectKey, request.query);
2952
+ const resource = this.repository.search(getRepositoryContext(request), request.query);
2900
2953
  return response.status(200).send(resource);
2901
2954
  }
2902
2955
 
@@ -2906,7 +2959,7 @@ class ProductRepository extends AbstractResourceRepository {
2906
2959
  constructor() {
2907
2960
  super(...arguments);
2908
2961
  this.actions = {
2909
- publish: (projectKey, resource, {
2962
+ publish: (context, resource, {
2910
2963
  scope
2911
2964
  }) => {
2912
2965
  if (resource.masterData.staged) {
@@ -2918,7 +2971,7 @@ class ProductRepository extends AbstractResourceRepository {
2918
2971
  resource.masterData.hasStagedChanges = false;
2919
2972
  resource.masterData.published = true;
2920
2973
  },
2921
- setAttribute: (projectKey, resource, {
2974
+ setAttribute: (context, resource, {
2922
2975
  variantId,
2923
2976
  sku,
2924
2977
  name,
@@ -2979,7 +3032,7 @@ class ProductRepository extends AbstractResourceRepository {
2979
3032
  return 'product';
2980
3033
  }
2981
3034
 
2982
- create(projectKey, draft) {
3035
+ create(context, draft) {
2983
3036
  var _draft$publish, _draft$publish2;
2984
3037
 
2985
3038
  const productData = {
@@ -3003,7 +3056,7 @@ class ProductRepository extends AbstractResourceRepository {
3003
3056
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3004
3057
  }
3005
3058
  };
3006
- this.save(projectKey, resource);
3059
+ this.save(context, resource);
3007
3060
  return resource;
3008
3061
  }
3009
3062
 
@@ -3063,7 +3116,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3063
3116
  constructor() {
3064
3117
  super(...arguments);
3065
3118
 
3066
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3119
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3067
3120
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3068
3121
 
3069
3122
  return { ...draft,
@@ -3074,7 +3127,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3074
3127
  };
3075
3128
 
3076
3129
  this.actions = {
3077
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3130
+ changeLocalizedEnumValueLabel: (context, resource, {
3078
3131
  attributeName,
3079
3132
  newValue
3080
3133
  }) => {
@@ -3102,7 +3155,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3102
3155
  }
3103
3156
  });
3104
3157
  },
3105
- changeLabel: (projectKey, resource, {
3158
+ changeLabel: (context, resource, {
3106
3159
  attributeName,
3107
3160
  label
3108
3161
  }) => {
@@ -3121,21 +3174,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3121
3174
  return 'product-type';
3122
3175
  }
3123
3176
 
3124
- create(projectKey, draft) {
3177
+ create(context, draft) {
3125
3178
  var _draft$attributes;
3126
3179
 
3127
3180
  const resource = { ...getBaseResourceProperties(),
3128
3181
  key: draft.key,
3129
3182
  name: draft.name,
3130
3183
  description: draft.description,
3131
- 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))
3132
3185
  };
3133
- this.save(projectKey, resource);
3186
+ this.save(context, resource);
3134
3187
  return resource;
3135
3188
  }
3136
3189
 
3137
- getWithKey(projectKey, key) {
3138
- const result = this._storage.query(projectKey, this.getTypeId(), {
3190
+ getWithKey(context, key) {
3191
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3139
3192
  where: [`key="${key}"`]
3140
3193
  });
3141
3194
 
@@ -3168,7 +3221,7 @@ class ProductTypeService extends AbstractService {
3168
3221
  }
3169
3222
 
3170
3223
  getWithKey(request, response) {
3171
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3224
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3172
3225
 
3173
3226
  if (resource) {
3174
3227
  return response.status(200).send(resource);
@@ -3205,32 +3258,32 @@ class ProjectRepository extends AbstractRepository {
3205
3258
  constructor() {
3206
3259
  super(...arguments);
3207
3260
  this.actions = {
3208
- changeName: (projectKey, resource, {
3261
+ changeName: (context, resource, {
3209
3262
  name
3210
3263
  }) => {
3211
3264
  resource.name = name;
3212
3265
  },
3213
- changeCurrencies: (projectKey, resource, {
3266
+ changeCurrencies: (context, resource, {
3214
3267
  currencies
3215
3268
  }) => {
3216
3269
  resource.currencies = currencies;
3217
3270
  },
3218
- changeCountries: (projectKey, resource, {
3271
+ changeCountries: (context, resource, {
3219
3272
  countries
3220
3273
  }) => {
3221
3274
  resource.countries = countries;
3222
3275
  },
3223
- changeLanguages: (projectKey, resource, {
3276
+ changeLanguages: (context, resource, {
3224
3277
  languages
3225
3278
  }) => {
3226
3279
  resource.languages = languages;
3227
3280
  },
3228
- changeMessagesEnabled: (projectKey, resource, {
3281
+ changeMessagesEnabled: (context, resource, {
3229
3282
  messagesEnabled
3230
3283
  }) => {
3231
3284
  resource.messages.enabled = messagesEnabled;
3232
3285
  },
3233
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3286
+ changeProductSearchIndexingEnabled: (context, resource, {
3234
3287
  enabled
3235
3288
  }) => {
3236
3289
  var _resource$searchIndex;
@@ -3242,7 +3295,7 @@ class ProjectRepository extends AbstractRepository {
3242
3295
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3243
3296
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3244
3297
  },
3245
- changeOrderSearchStatus: (projectKey, resource, {
3298
+ changeOrderSearchStatus: (context, resource, {
3246
3299
  status
3247
3300
  }) => {
3248
3301
  var _resource$searchIndex2;
@@ -3254,22 +3307,22 @@ class ProjectRepository extends AbstractRepository {
3254
3307
  resource.searchIndexing.orders.status = status;
3255
3308
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3256
3309
  },
3257
- setShippingRateInputType: (projectKey, resource, {
3310
+ setShippingRateInputType: (context, resource, {
3258
3311
  shippingRateInputType
3259
3312
  }) => {
3260
3313
  resource.shippingRateInputType = shippingRateInputType;
3261
3314
  },
3262
- setExternalOAuth: (projectKey, resource, {
3315
+ setExternalOAuth: (context, resource, {
3263
3316
  externalOAuth
3264
3317
  }) => {
3265
3318
  resource.externalOAuth = externalOAuth;
3266
3319
  },
3267
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3320
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3268
3321
  countryTaxRateFallbackEnabled
3269
3322
  }) => {
3270
3323
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3271
3324
  },
3272
- changeCartsConfiguration: (projectKey, resource, {
3325
+ changeCartsConfiguration: (context, resource, {
3273
3326
  cartsConfiguration
3274
3327
  }) => {
3275
3328
  resource.carts = cartsConfiguration || {
@@ -3280,15 +3333,15 @@ class ProjectRepository extends AbstractRepository {
3280
3333
  };
3281
3334
  }
3282
3335
 
3283
- get(projectKey) {
3284
- const resource = this._storage.getProject(projectKey);
3336
+ get(context) {
3337
+ const resource = this._storage.getProject(context.projectKey);
3285
3338
 
3286
3339
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3287
3340
  return masked;
3288
3341
  }
3289
3342
 
3290
- save(projectKey, resource) {
3291
- const current = this.get(projectKey);
3343
+ save(context, resource) {
3344
+ const current = this.get(context);
3292
3345
 
3293
3346
  if (current) {
3294
3347
  checkConcurrentModification(current, resource.version);
@@ -3321,20 +3374,19 @@ class ProjectService {
3321
3374
  }
3322
3375
 
3323
3376
  get(request, response) {
3324
- const projectKey = request.params.projectKey;
3325
- const project = this.repository.get(projectKey);
3377
+ const project = this.repository.get(getRepositoryContext(request));
3326
3378
  return response.status(200).send(project);
3327
3379
  }
3328
3380
 
3329
3381
  post(request, response) {
3330
3382
  const updateRequest = request.body;
3331
- const project = this.repository.get(request.params.projectKey);
3383
+ const project = this.repository.get(getRepositoryContext(request));
3332
3384
 
3333
3385
  if (!project) {
3334
3386
  return response.status(404).send({});
3335
3387
  }
3336
3388
 
3337
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3389
+ this.repository.processUpdateActions(getRepositoryContext(request), project, updateRequest.actions);
3338
3390
  return response.status(200).send({});
3339
3391
  }
3340
3392
 
@@ -3344,11 +3396,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3344
3396
  constructor() {
3345
3397
  super(...arguments);
3346
3398
 
3347
- this._transformZoneRateDraft = (projectKey, draft) => {
3399
+ this._transformZoneRateDraft = (context, draft) => {
3348
3400
  var _draft$shippingRates;
3349
3401
 
3350
3402
  return { ...draft,
3351
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3403
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3352
3404
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3353
3405
  };
3354
3406
  };
@@ -3362,7 +3414,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3362
3414
  };
3363
3415
 
3364
3416
  this.actions = {
3365
- addShippingRate: (_projectKey, resource, {
3417
+ addShippingRate: (_context, resource, {
3366
3418
  shippingRate,
3367
3419
  zone
3368
3420
  }) => {
@@ -3382,7 +3434,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3382
3434
  shippingRates: [rate]
3383
3435
  });
3384
3436
  },
3385
- removeShippingRate: (_projectKey, resource, {
3437
+ removeShippingRate: (_context, resource, {
3386
3438
  shippingRate,
3387
3439
  zone
3388
3440
  }) => {
@@ -3396,10 +3448,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3396
3448
  }
3397
3449
  });
3398
3450
  },
3399
- addZone: (projectKey, resource, {
3451
+ addZone: (context, resource, {
3400
3452
  zone
3401
3453
  }) => {
3402
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3454
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3403
3455
 
3404
3456
  if (resource.zoneRates === undefined) {
3405
3457
  resource.zoneRates = [];
@@ -3410,39 +3462,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3410
3462
  shippingRates: []
3411
3463
  });
3412
3464
  },
3413
- removeZone: (_projectKey, resource, {
3465
+ removeZone: (_context, resource, {
3414
3466
  zone
3415
3467
  }) => {
3416
3468
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3417
3469
  return zoneRate.zone.id !== zone.id;
3418
3470
  });
3419
3471
  },
3420
- setKey: (_projectKey, resource, {
3472
+ setKey: (_context, resource, {
3421
3473
  key
3422
3474
  }) => {
3423
3475
  resource.key = key;
3424
3476
  },
3425
- setDescription: (_projectKey, resource, {
3477
+ setDescription: (_context, resource, {
3426
3478
  description
3427
3479
  }) => {
3428
3480
  resource.description = description;
3429
3481
  },
3430
- setLocalizedDescription: (_projectKey, resource, {
3482
+ setLocalizedDescription: (_context, resource, {
3431
3483
  localizedDescription
3432
3484
  }) => {
3433
3485
  resource.localizedDescription = localizedDescription;
3434
3486
  },
3435
- setPredicate: (_projectKey, resource, {
3487
+ setPredicate: (_context, resource, {
3436
3488
  predicate
3437
3489
  }) => {
3438
3490
  resource.predicate = predicate;
3439
3491
  },
3440
- changeIsDefault: (_projectKey, resource, {
3492
+ changeIsDefault: (_context, resource, {
3441
3493
  isDefault
3442
3494
  }) => {
3443
3495
  resource.isDefault = isDefault;
3444
3496
  },
3445
- changeName: (_projectKey, resource, {
3497
+ changeName: (_context, resource, {
3446
3498
  name
3447
3499
  }) => {
3448
3500
  resource.name = name;
@@ -3454,16 +3506,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3454
3506
  return 'shipping-method';
3455
3507
  }
3456
3508
 
3457
- create(projectKey, draft) {
3509
+ create(context, draft) {
3458
3510
  var _draft$zoneRates;
3459
3511
 
3460
3512
  const resource = { ...getBaseResourceProperties(),
3461
3513
  ...draft,
3462
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3463
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3464
- 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)
3465
3517
  };
3466
- this.save(projectKey, resource);
3518
+ this.save(context, resource);
3467
3519
  return resource;
3468
3520
  }
3469
3521
 
@@ -3491,13 +3543,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3491
3543
  return 'shopping-list';
3492
3544
  }
3493
3545
 
3494
- create(projectKey, draft) {
3546
+ create(context, draft) {
3495
3547
  var _draft$lineItems, _draft$store;
3496
3548
 
3497
3549
  // const product =
3498
3550
  const resource = { ...getBaseResourceProperties(),
3499
3551
  ...draft,
3500
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3552
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3501
3553
  textLineItems: [],
3502
3554
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3503
3555
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3512,16 +3564,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3512
3564
  typeId: 'product-type',
3513
3565
  id: ''
3514
3566
  },
3515
- custom: createCustomFields(e.custom, projectKey, this._storage)
3567
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3516
3568
  };
3517
3569
  }),
3518
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3570
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3519
3571
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3520
3572
  typeId: 'store',
3521
3573
  key: draft.store.key
3522
3574
  } : undefined
3523
3575
  };
3524
- this.save(projectKey, resource);
3576
+ this.save(context, resource);
3525
3577
  return resource;
3526
3578
  }
3527
3579
 
@@ -3543,22 +3595,22 @@ class StateRepository extends AbstractResourceRepository {
3543
3595
  constructor() {
3544
3596
  super(...arguments);
3545
3597
  this.actions = {
3546
- changeKey: (projectKey, resource, {
3598
+ changeKey: (context, resource, {
3547
3599
  key
3548
3600
  }) => {
3549
3601
  resource.key = key;
3550
3602
  },
3551
- setDescription: (projectKey, resource, {
3603
+ setDescription: (context, resource, {
3552
3604
  description
3553
3605
  }) => {
3554
3606
  resource.description = description;
3555
3607
  },
3556
- setName: (projectKey, resource, {
3608
+ setName: (context, resource, {
3557
3609
  name
3558
3610
  }) => {
3559
3611
  resource.name = name;
3560
3612
  },
3561
- setRoles: (projectKey, resource, {
3613
+ setRoles: (context, resource, {
3562
3614
  roles
3563
3615
  }) => {
3564
3616
  resource.roles = roles;
@@ -3570,14 +3622,14 @@ class StateRepository extends AbstractResourceRepository {
3570
3622
  return 'state';
3571
3623
  }
3572
3624
 
3573
- create(projectKey, draft) {
3625
+ create(context, draft) {
3574
3626
  const resource = { ...getBaseResourceProperties(),
3575
3627
  ...draft,
3576
3628
  builtIn: false,
3577
3629
  initial: draft.initial || false,
3578
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3630
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3579
3631
  };
3580
- this.save(projectKey, resource);
3632
+ this.save(context, resource);
3581
3633
  return resource;
3582
3634
  }
3583
3635
 
@@ -3599,17 +3651,17 @@ class StoreRepository extends AbstractResourceRepository {
3599
3651
  constructor() {
3600
3652
  super(...arguments);
3601
3653
  this.actions = {
3602
- setName: (projectKey, resource, {
3654
+ setName: (context, resource, {
3603
3655
  name
3604
3656
  }) => {
3605
3657
  resource.name = name;
3606
3658
  },
3607
- setDistributionChannels: (projectKey, resource, {
3659
+ setDistributionChannels: (context, resource, {
3608
3660
  distributionChannels
3609
3661
  }) => {
3610
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3662
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3611
3663
  },
3612
- setLanguages: (projectKey, resource, {
3664
+ setLanguages: (context, resource, {
3613
3665
  languages
3614
3666
  }) => {
3615
3667
  resource.languages = languages;
@@ -3621,25 +3673,25 @@ class StoreRepository extends AbstractResourceRepository {
3621
3673
  return 'store';
3622
3674
  }
3623
3675
 
3624
- create(projectKey, draft) {
3676
+ create(context, draft) {
3625
3677
  const resource = { ...getBaseResourceProperties(),
3626
3678
  key: draft.key,
3627
3679
  name: draft.name,
3628
3680
  languages: draft.languages,
3629
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3630
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3681
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3682
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3631
3683
  };
3632
- this.save(projectKey, resource);
3684
+ this.save(context, resource);
3633
3685
  return resource;
3634
3686
  }
3635
3687
 
3636
- transformChannels(projectKey, channels) {
3688
+ transformChannels(context, channels) {
3637
3689
  if (!channels) return [];
3638
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3690
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3639
3691
  }
3640
3692
 
3641
- getWithKey(projectKey, key) {
3642
- const result = this._storage.query(projectKey, this.getTypeId(), {
3693
+ getWithKey(context, key) {
3694
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3643
3695
  where: [`key="${key}"`]
3644
3696
  });
3645
3697
 
@@ -3671,7 +3723,7 @@ class StoreService extends AbstractService {
3671
3723
  }
3672
3724
 
3673
3725
  getWithKey(request, response) {
3674
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3726
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3675
3727
 
3676
3728
  if (resource) {
3677
3729
  return response.status(200).send(resource);
@@ -3687,7 +3739,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3687
3739
  return 'subscription';
3688
3740
  }
3689
3741
 
3690
- create(projectKey, draft) {
3742
+ create(context, draft) {
3691
3743
  // TODO: We could actually test this here by using the aws sdk. For now
3692
3744
  // hardcode a failed check when account id is 0000000000
3693
3745
  if (draft.destination.type === 'SQS') {
@@ -3713,7 +3765,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3713
3765
  messages: draft.messages || [],
3714
3766
  status: 'Healthy'
3715
3767
  };
3716
- this.save(projectKey, resource);
3768
+ this.save(context, resource);
3717
3769
  return resource;
3718
3770
  }
3719
3771
 
@@ -3741,7 +3793,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3741
3793
  });
3742
3794
 
3743
3795
  this.actions = {
3744
- addTaxRate: (projectKey, resource, {
3796
+ addTaxRate: (context, resource, {
3745
3797
  taxRate
3746
3798
  }) => {
3747
3799
  if (resource.rates === undefined) {
@@ -3750,7 +3802,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3750
3802
 
3751
3803
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3752
3804
  },
3753
- removeTaxRate: (projectKey, resource, {
3805
+ removeTaxRate: (context, resource, {
3754
3806
  taxRateId
3755
3807
  }) => {
3756
3808
  if (resource.rates === undefined) {
@@ -3761,7 +3813,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3761
3813
  return taxRate.id !== taxRateId;
3762
3814
  });
3763
3815
  },
3764
- replaceTaxRate: (projectKey, resource, {
3816
+ replaceTaxRate: (context, resource, {
3765
3817
  taxRateId,
3766
3818
  taxRate
3767
3819
  }) => {
@@ -3779,17 +3831,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3779
3831
  }
3780
3832
  }
3781
3833
  },
3782
- setDescription: (projectKey, resource, {
3834
+ setDescription: (context, resource, {
3783
3835
  description
3784
3836
  }) => {
3785
3837
  resource.description = description;
3786
3838
  },
3787
- setKey: (projectKey, resource, {
3839
+ setKey: (context, resource, {
3788
3840
  key
3789
3841
  }) => {
3790
3842
  resource.key = key;
3791
3843
  },
3792
- changeName: (projectKey, resource, {
3844
+ changeName: (context, resource, {
3793
3845
  name
3794
3846
  }) => {
3795
3847
  resource.name = name;
@@ -3801,19 +3853,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3801
3853
  return 'tax-category';
3802
3854
  }
3803
3855
 
3804
- create(projectKey, draft) {
3856
+ create(context, draft) {
3805
3857
  var _draft$rates;
3806
3858
 
3807
3859
  const resource = { ...getBaseResourceProperties(),
3808
3860
  ...draft,
3809
3861
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3810
3862
  };
3811
- this.save(projectKey, resource);
3863
+ this.save(context, resource);
3812
3864
  return resource;
3813
3865
  }
3814
3866
 
3815
- getWithKey(projectKey, key) {
3816
- const result = this._storage.query(projectKey, this.getTypeId(), {
3867
+ getWithKey(context, key) {
3868
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3817
3869
  where: [`key="${key}"`]
3818
3870
  });
3819
3871
 
@@ -3846,7 +3898,7 @@ class TaxCategoryService extends AbstractService {
3846
3898
  }
3847
3899
 
3848
3900
  getWithKey(request, response) {
3849
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3901
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3850
3902
 
3851
3903
  if (resource) {
3852
3904
  return response.status(200).send(resource);
@@ -3861,29 +3913,29 @@ class TypeRepository extends AbstractResourceRepository {
3861
3913
  constructor() {
3862
3914
  super(...arguments);
3863
3915
  this.actions = {
3864
- addFieldDefinition: (projectKey, resource, {
3916
+ addFieldDefinition: (context, resource, {
3865
3917
  fieldDefinition
3866
3918
  }) => {
3867
3919
  resource.fieldDefinitions.push(fieldDefinition);
3868
3920
  },
3869
- removeFieldDefinition: (projectKey, resource, {
3921
+ removeFieldDefinition: (context, resource, {
3870
3922
  fieldName
3871
3923
  }) => {
3872
3924
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3873
3925
  return f.name !== fieldName;
3874
3926
  });
3875
3927
  },
3876
- setDescription: (projectKey, resource, {
3928
+ setDescription: (context, resource, {
3877
3929
  description
3878
3930
  }) => {
3879
3931
  resource.description = description;
3880
3932
  },
3881
- changeName: (projectKey, resource, {
3933
+ changeName: (context, resource, {
3882
3934
  name
3883
3935
  }) => {
3884
3936
  resource.name = name;
3885
3937
  },
3886
- changeFieldDefinitionOrder: (projectKey, resource, {
3938
+ changeFieldDefinitionOrder: (context, resource, {
3887
3939
  fieldNames
3888
3940
  }) => {
3889
3941
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3907,7 +3959,7 @@ class TypeRepository extends AbstractResourceRepository {
3907
3959
 
3908
3960
  resource.fieldDefinitions.push(...current);
3909
3961
  },
3910
- addEnumValue: (projectKey, resource, {
3962
+ addEnumValue: (context, resource, {
3911
3963
  fieldName,
3912
3964
  value
3913
3965
  }) => {
@@ -3924,7 +3976,7 @@ class TypeRepository extends AbstractResourceRepository {
3924
3976
  }
3925
3977
  });
3926
3978
  },
3927
- changeEnumValueLabel: (projectKey, resource, {
3979
+ changeEnumValueLabel: (context, resource, {
3928
3980
  fieldName,
3929
3981
  value
3930
3982
  }) => {
@@ -3956,7 +4008,7 @@ class TypeRepository extends AbstractResourceRepository {
3956
4008
  return 'type';
3957
4009
  }
3958
4010
 
3959
- create(projectKey, draft) {
4011
+ create(context, draft) {
3960
4012
  const resource = { ...getBaseResourceProperties(),
3961
4013
  key: draft.key,
3962
4014
  name: draft.name,
@@ -3964,7 +4016,7 @@ class TypeRepository extends AbstractResourceRepository {
3964
4016
  fieldDefinitions: draft.fieldDefinitions || [],
3965
4017
  description: draft.description
3966
4018
  };
3967
- this.save(projectKey, resource);
4019
+ this.save(context, resource);
3968
4020
  return resource;
3969
4021
  }
3970
4022
 
@@ -3986,29 +4038,29 @@ class ZoneRepository extends AbstractResourceRepository {
3986
4038
  constructor() {
3987
4039
  super(...arguments);
3988
4040
  this.actions = {
3989
- addLocation: (projectKey, resource, {
4041
+ addLocation: (context, resource, {
3990
4042
  location
3991
4043
  }) => {
3992
4044
  resource.locations.push(location);
3993
4045
  },
3994
- removeLocation: (projectKey, resource, {
4046
+ removeLocation: (context, resource, {
3995
4047
  location
3996
4048
  }) => {
3997
4049
  resource.locations = resource.locations.filter(loc => {
3998
4050
  return !(loc.country === location.country && loc.state === location.state);
3999
4051
  });
4000
4052
  },
4001
- changeName: (projectKey, resource, {
4053
+ changeName: (context, resource, {
4002
4054
  name
4003
4055
  }) => {
4004
4056
  resource.name = name;
4005
4057
  },
4006
- setDescription: (projectKey, resource, {
4058
+ setDescription: (context, resource, {
4007
4059
  description
4008
4060
  }) => {
4009
4061
  resource.description = description;
4010
4062
  },
4011
- setKey: (projectKey, resource, {
4063
+ setKey: (context, resource, {
4012
4064
  key
4013
4065
  }) => {
4014
4066
  resource.key = key;
@@ -4020,14 +4072,14 @@ class ZoneRepository extends AbstractResourceRepository {
4020
4072
  return 'zone';
4021
4073
  }
4022
4074
 
4023
- create(projectKey, draft) {
4075
+ create(context, draft) {
4024
4076
  const resource = { ...getBaseResourceProperties(),
4025
4077
  key: draft.key,
4026
4078
  locations: draft.locations || [],
4027
4079
  name: draft.name,
4028
4080
  description: draft.description
4029
4081
  };
4030
- this.save(projectKey, resource);
4082
+ this.save(context, resource);
4031
4083
  return resource;
4032
4084
  }
4033
4085
 
@@ -4069,7 +4121,7 @@ class MyCustomerService extends AbstractService {
4069
4121
  }
4070
4122
 
4071
4123
  getMe(request, response) {
4072
- const resource = this.repository.getMe(request.params.projectKey);
4124
+ const resource = this.repository.getMe(getRepositoryContext(request));
4073
4125
 
4074
4126
  if (!resource) {
4075
4127
  return response.status(404).send('Not found');
@@ -4080,7 +4132,7 @@ class MyCustomerService extends AbstractService {
4080
4132
 
4081
4133
  signUp(request, response) {
4082
4134
  const draft = request.body;
4083
- const resource = this.repository.create(request.params.projectKey, draft);
4135
+ const resource = this.repository.create(getRepositoryContext(request), draft);
4084
4136
 
4085
4137
  const result = this._expandWithId(request, resource.id);
4086
4138
 
@@ -4095,7 +4147,7 @@ class MyCustomerService extends AbstractService {
4095
4147
  password
4096
4148
  } = request.body;
4097
4149
  const encodedPassword = Buffer.from(password).toString('base64');
4098
- const result = this.repository.query(request.params.projectKey, {
4150
+ const result = this.repository.query(getRepositoryContext(request), {
4099
4151
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4100
4152
  });
4101
4153
 
@@ -4116,10 +4168,22 @@ class MyCustomerService extends AbstractService {
4116
4168
 
4117
4169
  }
4118
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
+
4119
4183
  class MyOrderService extends AbstractService {
4120
4184
  constructor(parent, storage) {
4121
4185
  super(parent);
4122
- this.repository = new OrderRepository(storage);
4186
+ this.repository = new MyOrderRepository(storage);
4123
4187
  }
4124
4188
 
4125
4189
  getBasePath() {
@@ -4222,8 +4286,10 @@ class CommercetoolsMock {
4222
4286
 
4223
4287
  if (this.options.enableAuthentication) {
4224
4288
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4289
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4225
4290
  } else {
4226
4291
  app.use('/:projectKey', projectRouter);
4292
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4227
4293
  }
4228
4294
 
4229
4295
  this._projectService = new ProjectService(projectRouter, this._storage);