@labdigital/commercetools-mock 0.6.1 → 0.6.4

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 (81) hide show
  1. package/dist/commercetools-mock.cjs.development.js +365 -332
  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 +365 -332
  6. package/dist/commercetools-mock.esm.js.map +1 -1
  7. package/dist/projectAPI.d.ts +1 -1
  8. package/dist/repositories/abstract.d.ts +13 -9
  9. package/dist/repositories/cart-discount.d.ts +3 -3
  10. package/dist/repositories/cart.d.ts +12 -12
  11. package/dist/repositories/category.d.ts +11 -11
  12. package/dist/repositories/channel.d.ts +2 -2
  13. package/dist/repositories/custom-object.d.ts +3 -3
  14. package/dist/repositories/customer-group.d.ts +4 -4
  15. package/dist/repositories/customer.d.ts +4 -4
  16. package/dist/repositories/discount-code.d.ts +3 -3
  17. package/dist/repositories/extension.d.ts +3 -3
  18. package/dist/repositories/helpers.d.ts +3 -0
  19. package/dist/repositories/inventory-entry.d.ts +7 -7
  20. package/dist/repositories/my-order.d.ts +6 -0
  21. package/dist/repositories/order.d.ts +18 -17
  22. package/dist/repositories/payment.d.ts +8 -8
  23. package/dist/repositories/product-projection.d.ts +3 -3
  24. package/dist/repositories/product-type.d.ts +5 -5
  25. package/dist/repositories/product.d.ts +4 -4
  26. package/dist/repositories/project.d.ts +4 -4
  27. package/dist/repositories/shipping-method.d.ts +3 -3
  28. package/dist/repositories/shopping-list.d.ts +2 -2
  29. package/dist/repositories/state.d.ts +3 -3
  30. package/dist/repositories/store.d.ts +4 -4
  31. package/dist/repositories/subscription.d.ts +2 -2
  32. package/dist/repositories/tax-category.d.ts +4 -4
  33. package/dist/repositories/type.d.ts +3 -3
  34. package/dist/repositories/zone.d.ts +3 -3
  35. package/dist/services/my-order.d.ts +2 -2
  36. package/dist/storage.d.ts +1 -1
  37. package/package.json +1 -1
  38. package/src/ctMock.ts +6 -0
  39. package/src/projectAPI.ts +1 -1
  40. package/src/repositories/abstract.ts +37 -17
  41. package/src/repositories/cart-discount.ts +11 -11
  42. package/src/repositories/cart.ts +28 -19
  43. package/src/repositories/category.ts +17 -13
  44. package/src/repositories/channel.ts +3 -3
  45. package/src/repositories/custom-object.ts +16 -8
  46. package/src/repositories/customer-group.ts +5 -5
  47. package/src/repositories/customer.ts +10 -6
  48. package/src/repositories/discount-code.ts +14 -14
  49. package/src/repositories/errors.ts +3 -3
  50. package/src/repositories/extension.ts +12 -8
  51. package/src/repositories/helpers.ts +9 -0
  52. package/src/repositories/inventory-entry.ts +17 -10
  53. package/src/repositories/my-order.ts +19 -0
  54. package/src/repositories/order.test.ts +79 -3
  55. package/src/repositories/order.ts +77 -37
  56. package/src/repositories/payment.ts +21 -17
  57. package/src/repositories/product-projection.ts +5 -5
  58. package/src/repositories/product-type.ts +14 -10
  59. package/src/repositories/product.ts +5 -5
  60. package/src/repositories/project.ts +21 -17
  61. package/src/repositories/shipping-method.ts +27 -20
  62. package/src/repositories/shopping-list.ts +10 -6
  63. package/src/repositories/state.ts +13 -9
  64. package/src/repositories/store.ts +18 -14
  65. package/src/repositories/subscription.ts +3 -3
  66. package/src/repositories/tax-category.ts +16 -12
  67. package/src/repositories/type.ts +15 -11
  68. package/src/repositories/zone.ts +13 -9
  69. package/src/services/abstract.ts +17 -9
  70. package/src/services/cart.ts +6 -12
  71. package/src/services/custom-object.ts +8 -4
  72. package/src/services/customer.ts +5 -2
  73. package/src/services/my-customer.ts +7 -3
  74. package/src/services/my-order.ts +3 -3
  75. package/src/services/order.ts +3 -2
  76. package/src/services/product-projection.ts +2 -1
  77. package/src/services/product-type.ts +2 -1
  78. package/src/services/project.ts +4 -4
  79. package/src/services/store.ts +2 -1
  80. package/src/services/tax-category.ts +2 -1
  81. package/src/storage.ts +1 -1
@@ -950,6 +950,67 @@ const copyHeaders = headers => {
950
950
  const DEFAULT_API_HOSTNAME = /^https:\/\/api\..*?\.commercetools.com:443$/;
951
951
  const DEFAULT_AUTH_HOSTNAME = /^https:\/\/auth\..*?\.commercetools.com:443$/;
952
952
 
953
+ const createCustomFields = (draft, projectKey, storage) => {
954
+ if (!draft) return undefined;
955
+ if (!draft.type) return undefined;
956
+ if (!draft.type.typeId) return undefined;
957
+ if (!draft.fields) return undefined;
958
+ const typeResource = storage.getByResourceIdentifier(projectKey, draft.type);
959
+
960
+ if (!typeResource) {
961
+ throw new Error(`No type '${draft.type.typeId}' with id=${draft.type.id} or key=${draft.type.key}`);
962
+ }
963
+
964
+ return {
965
+ type: {
966
+ typeId: draft.type.typeId,
967
+ id: typeResource.id
968
+ },
969
+ fields: draft.fields
970
+ };
971
+ };
972
+ const createPrice = draft => {
973
+ return {
974
+ id: v4(),
975
+ value: createTypedMoney(draft.value)
976
+ };
977
+ };
978
+ const createTypedMoney = value => {
979
+ return {
980
+ type: 'centPrecision',
981
+ fractionDigits: 2,
982
+ ...value
983
+ };
984
+ };
985
+ const resolveStoreReference = (ref, projectKey, storage) => {
986
+ if (!ref) return undefined;
987
+ const resource = storage.getByResourceIdentifier(projectKey, ref);
988
+
989
+ if (!resource) {
990
+ throw new Error('No such store');
991
+ }
992
+
993
+ const store = resource;
994
+ return {
995
+ typeId: 'store',
996
+ key: store.key
997
+ };
998
+ };
999
+ const getReferenceFromResourceIdentifier = (resourceIdentifier, projectKey, storage) => {
1000
+ const resource = storage.getByResourceIdentifier(projectKey, resourceIdentifier);
1001
+ if (!resource) throw new Error(`resource type ${resourceIdentifier.typeId} with id ${resourceIdentifier.id} and key ${resourceIdentifier.key} not found`);
1002
+ return {
1003
+ typeId: resourceIdentifier.typeId,
1004
+ id: resource == null ? void 0 : resource.id
1005
+ };
1006
+ };
1007
+ const getRepositoryContext = request => {
1008
+ return {
1009
+ projectKey: request.params.projectKey,
1010
+ storeKey: request.params.storeKey
1011
+ };
1012
+ };
1013
+
953
1014
  class AbstractService {
954
1015
  constructor(parent) {
955
1016
  this.createStatusCode = 201;
@@ -982,7 +1043,7 @@ class AbstractService {
982
1043
 
983
1044
  const offset = this._parseParam(request.query.offset);
984
1045
 
985
- const result = this.repository.query(request.params.projectKey, {
1046
+ const result = this.repository.query(getRepositoryContext(request), {
986
1047
  expand: this._parseParam(request.query.expand),
987
1048
  where: this._parseParam(request.query.where),
988
1049
  limit: limit !== undefined ? Number(limit) : undefined,
@@ -1002,7 +1063,7 @@ class AbstractService {
1002
1063
  }
1003
1064
 
1004
1065
  getWithKey(request, response) {
1005
- const result = this.repository.getByKey(request.params.projectKey, request.params['key'], {
1066
+ const result = this.repository.getByKey(getRepositoryContext(request), request.params['key'], {
1006
1067
  expand: this._parseParam(request.query.expand)
1007
1068
  });
1008
1069
  if (!result) return response.status(404).send();
@@ -1010,7 +1071,7 @@ class AbstractService {
1010
1071
  }
1011
1072
 
1012
1073
  deletewithId(request, response) {
1013
- const result = this.repository.delete(request.params.projectKey, request.params['id'], {
1074
+ const result = this.repository.delete(getRepositoryContext(request), request.params['id'], {
1014
1075
  expand: this._parseParam(request.query.expand)
1015
1076
  });
1016
1077
 
@@ -1027,7 +1088,7 @@ class AbstractService {
1027
1088
 
1028
1089
  post(request, response) {
1029
1090
  const draft = request.body;
1030
- const resource = this.repository.create(request.params.projectKey, draft);
1091
+ const resource = this.repository.create(getRepositoryContext(request), draft);
1031
1092
 
1032
1093
  const result = this._expandWithId(request, resource.id);
1033
1094
 
@@ -1036,7 +1097,7 @@ class AbstractService {
1036
1097
 
1037
1098
  postWithId(request, response) {
1038
1099
  const updateRequest = request.body;
1039
- const resource = this.repository.get(request.params.projectKey, request.params['id']);
1100
+ const resource = this.repository.get(getRepositoryContext(request), request.params['id']);
1040
1101
 
1041
1102
  if (!resource) {
1042
1103
  return response.status(404).send('Not found');
@@ -1046,7 +1107,7 @@ class AbstractService {
1046
1107
  return response.status(409).send('Concurrent modification');
1047
1108
  }
1048
1109
 
1049
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, resource, updateRequest.actions);
1110
+ const updatedResource = this.repository.processUpdateActions(getRepositoryContext(request), resource, updateRequest.actions);
1050
1111
 
1051
1112
  const result = this._expandWithId(request, updatedResource.id);
1052
1113
 
@@ -1058,7 +1119,7 @@ class AbstractService {
1058
1119
  }
1059
1120
 
1060
1121
  _expandWithId(request, resourceId) {
1061
- const result = this.repository.get(request.params.projectKey, resourceId, {
1122
+ const result = this.repository.get(getRepositoryContext(request), resourceId, {
1062
1123
  expand: this._parseParam(request.query.expand)
1063
1124
  });
1064
1125
  return result;
@@ -1094,7 +1155,7 @@ class AbstractRepository {
1094
1155
  this._storage = storage;
1095
1156
  }
1096
1157
 
1097
- processUpdateActions(projectKey, resource, actions) {
1158
+ processUpdateActions(context, resource, actions) {
1098
1159
  // Deep-copy
1099
1160
  const modifiedResource = JSON.parse(JSON.stringify(resource));
1100
1161
  actions.forEach(action => {
@@ -1105,11 +1166,11 @@ class AbstractRepository {
1105
1166
  return;
1106
1167
  }
1107
1168
 
1108
- updateFunc(projectKey, modifiedResource, action);
1169
+ updateFunc(context, modifiedResource, action);
1109
1170
  });
1110
1171
 
1111
1172
  if (!deepEqual(modifiedResource, resource)) {
1112
- this.save(projectKey, modifiedResource);
1173
+ this.save(context, modifiedResource);
1113
1174
  }
1114
1175
 
1115
1176
  return modifiedResource;
@@ -1123,8 +1184,8 @@ class AbstractResourceRepository extends AbstractRepository {
1123
1184
  this._storage.assertStorage(this.getTypeId());
1124
1185
  }
1125
1186
 
1126
- query(projectKey, params = {}) {
1127
- return this._storage.query(projectKey, this.getTypeId(), {
1187
+ query(context, params = {}) {
1188
+ return this._storage.query(context.projectKey, this.getTypeId(), {
1128
1189
  expand: params.expand,
1129
1190
  where: params.where,
1130
1191
  offset: params.offset,
@@ -1132,20 +1193,20 @@ class AbstractResourceRepository extends AbstractRepository {
1132
1193
  });
1133
1194
  }
1134
1195
 
1135
- get(projectKey, id, params = {}) {
1136
- return this._storage.get(projectKey, this.getTypeId(), id, params);
1196
+ get(context, id, params = {}) {
1197
+ return this._storage.get(context.projectKey, this.getTypeId(), id, params);
1137
1198
  }
1138
1199
 
1139
- getByKey(projectKey, key, params = {}) {
1140
- return this._storage.getByKey(projectKey, this.getTypeId(), key, params);
1200
+ getByKey(context, key, params = {}) {
1201
+ return this._storage.getByKey(context.projectKey, this.getTypeId(), key, params);
1141
1202
  }
1142
1203
 
1143
- delete(projectKey, id, params = {}) {
1144
- return this._storage.delete(projectKey, this.getTypeId(), id, params);
1204
+ delete(context, id, params = {}) {
1205
+ return this._storage.delete(context.projectKey, this.getTypeId(), id, params);
1145
1206
  }
1146
1207
 
1147
- save(projectKey, resource) {
1148
- const current = this.get(projectKey, resource.id);
1208
+ save(context, resource) {
1209
+ const current = this.get(context, resource.id);
1149
1210
 
1150
1211
  if (current) {
1151
1212
  checkConcurrentModification(current, resource.version);
@@ -1161,103 +1222,48 @@ class AbstractResourceRepository extends AbstractRepository {
1161
1222
 
1162
1223
  resource.version += 1;
1163
1224
 
1164
- this._storage.add(projectKey, this.getTypeId(), resource);
1225
+ this._storage.add(context.projectKey, this.getTypeId(), resource);
1165
1226
  }
1166
1227
 
1167
1228
  }
1168
1229
 
1169
- const createCustomFields = (draft, projectKey, storage) => {
1170
- if (!draft) return undefined;
1171
- if (!draft.type) return undefined;
1172
- if (!draft.type.typeId) return undefined;
1173
- if (!draft.fields) return undefined;
1174
- const typeResource = storage.getByResourceIdentifier(projectKey, draft.type);
1175
-
1176
- if (!typeResource) {
1177
- throw new Error(`No type '${draft.type.typeId}' with id=${draft.type.id} or key=${draft.type.key}`);
1178
- }
1179
-
1180
- return {
1181
- type: {
1182
- typeId: draft.type.typeId,
1183
- id: typeResource.id
1184
- },
1185
- fields: draft.fields
1186
- };
1187
- };
1188
- const createPrice = draft => {
1189
- return {
1190
- id: v4(),
1191
- value: createTypedMoney(draft.value)
1192
- };
1193
- };
1194
- const createTypedMoney = value => {
1195
- return {
1196
- type: 'centPrecision',
1197
- fractionDigits: 2,
1198
- ...value
1199
- };
1200
- };
1201
- const resolveStoreReference = (ref, projectKey, storage) => {
1202
- if (!ref) return undefined;
1203
- const resource = storage.getByResourceIdentifier(projectKey, ref);
1204
-
1205
- if (!resource) {
1206
- throw new Error('No such store');
1207
- }
1208
-
1209
- const store = resource;
1210
- return {
1211
- typeId: 'store',
1212
- key: store.key
1213
- };
1214
- };
1215
- const getReferenceFromResourceIdentifier = (resourceIdentifier, projectKey, storage) => {
1216
- const resource = storage.getByResourceIdentifier(projectKey, resourceIdentifier);
1217
- if (!resource) throw new Error(`resource type ${resourceIdentifier.typeId} with id ${resourceIdentifier.id} and key ${resourceIdentifier.key} not found`);
1218
- return {
1219
- typeId: resourceIdentifier.typeId,
1220
- id: resource == null ? void 0 : resource.id
1221
- };
1222
- };
1223
-
1224
1230
  class CartDiscountRepository extends AbstractResourceRepository {
1225
1231
  constructor() {
1226
1232
  super(...arguments);
1227
1233
  this.actions = {
1228
- setKey: (projectKey, resource, {
1234
+ setKey: (context, resource, {
1229
1235
  key
1230
1236
  }) => {
1231
1237
  resource.key = key;
1232
1238
  },
1233
- setDescription: (projectKey, resource, {
1239
+ setDescription: (context, resource, {
1234
1240
  description
1235
1241
  }) => {
1236
1242
  resource.description = description;
1237
1243
  },
1238
- setValidFrom: (projectKey, resource, {
1244
+ setValidFrom: (context, resource, {
1239
1245
  validFrom
1240
1246
  }) => {
1241
1247
  resource.validFrom = validFrom;
1242
1248
  },
1243
- setValidUntil: (projectKey, resource, {
1249
+ setValidUntil: (context, resource, {
1244
1250
  validUntil
1245
1251
  }) => {
1246
1252
  resource.validUntil = validUntil;
1247
1253
  },
1248
- setValidFromAndUntil: (projectKey, resource, {
1254
+ setValidFromAndUntil: (context, resource, {
1249
1255
  validFrom,
1250
1256
  validUntil
1251
1257
  }) => {
1252
1258
  resource.validFrom = validFrom;
1253
1259
  resource.validUntil = validUntil;
1254
1260
  },
1255
- changeSortOrder: (projectKey, resource, {
1261
+ changeSortOrder: (context, resource, {
1256
1262
  sortOrder
1257
1263
  }) => {
1258
1264
  resource.sortOrder = sortOrder;
1259
1265
  },
1260
- changeIsActive: (projectKey, resource, {
1266
+ changeIsActive: (context, resource, {
1261
1267
  isActive
1262
1268
  }) => {
1263
1269
  resource.isActive = isActive;
@@ -1269,7 +1275,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1269
1275
  return 'cart-discount';
1270
1276
  }
1271
1277
 
1272
- create(projectKey, draft) {
1278
+ create(context, draft) {
1273
1279
  const resource = { ...getBaseResourceProperties(),
1274
1280
  key: draft.key,
1275
1281
  description: draft.description,
@@ -1285,7 +1291,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1285
1291
  validUntil: draft.validUntil,
1286
1292
  value: this.transformValueDraft(draft.value)
1287
1293
  };
1288
- this.save(projectKey, resource);
1294
+ this.save(context, resource);
1289
1295
  return resource;
1290
1296
  }
1291
1297
 
@@ -1341,7 +1347,7 @@ class CartRepository extends AbstractResourceRepository {
1341
1347
  constructor() {
1342
1348
  super(...arguments);
1343
1349
  this.actions = {
1344
- addLineItem: (projectKey, resource, {
1350
+ addLineItem: (context, resource, {
1345
1351
  productId,
1346
1352
  variantId,
1347
1353
  sku,
@@ -1352,10 +1358,10 @@ class CartRepository extends AbstractResourceRepository {
1352
1358
 
1353
1359
  if (productId && variantId) {
1354
1360
  // Fetch product and variant by ID
1355
- product = this._storage.get(projectKey, 'product', productId, {});
1361
+ product = this._storage.get(context.projectKey, 'product', productId, {});
1356
1362
  } else if (sku) {
1357
1363
  // Fetch product and variant by SKU
1358
- const items = this._storage.query(projectKey, 'product', {
1364
+ const items = this._storage.query(context.projectKey, 'product', {
1359
1365
  where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1360
1366
  });
1361
1367
 
@@ -1450,7 +1456,7 @@ class CartRepository extends AbstractResourceRepository {
1450
1456
 
1451
1457
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1452
1458
  },
1453
- removeLineItem: (projectKey, resource, {
1459
+ removeLineItem: (context, resource, {
1454
1460
  lineItemId,
1455
1461
  quantity
1456
1462
  }) => {
@@ -1484,15 +1490,15 @@ class CartRepository extends AbstractResourceRepository {
1484
1490
 
1485
1491
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1486
1492
  },
1487
- setBillingAddress: (projectKey, resource, {
1493
+ setBillingAddress: (context, resource, {
1488
1494
  address
1489
1495
  }) => {
1490
1496
  resource.billingAddress = address;
1491
1497
  },
1492
- setShippingMethod: (projectKey, resource, {
1498
+ setShippingMethod: (context, resource, {
1493
1499
  shippingMethod
1494
1500
  }) => {
1495
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1501
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1496
1502
  shippingMethod);
1497
1503
 
1498
1504
  if (!resolvedType) {
@@ -1507,17 +1513,17 @@ class CartRepository extends AbstractResourceRepository {
1507
1513
  }
1508
1514
  };
1509
1515
  },
1510
- setCountry: (projectKey, resource, {
1516
+ setCountry: (context, resource, {
1511
1517
  country
1512
1518
  }) => {
1513
1519
  resource.country = country;
1514
1520
  },
1515
- setCustomerEmail: (projectKey, resource, {
1521
+ setCustomerEmail: (context, resource, {
1516
1522
  email
1517
1523
  }) => {
1518
1524
  resource.customerEmail = email;
1519
1525
  },
1520
- setCustomField: (projectKey, resource, {
1526
+ setCustomField: (context, resource, {
1521
1527
  name,
1522
1528
  value
1523
1529
  }) => {
@@ -1527,14 +1533,14 @@ class CartRepository extends AbstractResourceRepository {
1527
1533
 
1528
1534
  resource.custom.fields[name] = value;
1529
1535
  },
1530
- setCustomType: (projectKey, resource, {
1536
+ setCustomType: (context, resource, {
1531
1537
  type,
1532
1538
  fields
1533
1539
  }) => {
1534
1540
  if (!type) {
1535
1541
  resource.custom = undefined;
1536
1542
  } else {
1537
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1543
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1538
1544
 
1539
1545
  if (!resolvedType) {
1540
1546
  throw new Error(`Type ${type} not found`);
@@ -1549,12 +1555,12 @@ class CartRepository extends AbstractResourceRepository {
1549
1555
  };
1550
1556
  }
1551
1557
  },
1552
- setLocale: (projectKey, resource, {
1558
+ setLocale: (context, resource, {
1553
1559
  locale
1554
1560
  }) => {
1555
1561
  resource.locale = locale;
1556
1562
  },
1557
- setShippingAddress: (projectKey, resource, {
1563
+ setShippingAddress: (context, resource, {
1558
1564
  address
1559
1565
  }) => {
1560
1566
  resource.shippingAddress = address;
@@ -1641,10 +1647,10 @@ class CartRepository extends AbstractResourceRepository {
1641
1647
  return 'cart';
1642
1648
  }
1643
1649
 
1644
- create(projectKey, draft) {
1650
+ create(context, draft) {
1645
1651
  var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1646
1652
 
1647
- const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1653
+ const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1648
1654
  const resource = { ...getBaseResourceProperties(),
1649
1655
  cartState: 'Active',
1650
1656
  lineItems,
@@ -1662,11 +1668,11 @@ class CartRepository extends AbstractResourceRepository {
1662
1668
  locale: draft.locale,
1663
1669
  country: draft.country,
1664
1670
  origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1665
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1671
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1666
1672
  }; // @ts-ignore
1667
1673
 
1668
1674
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1669
- this.save(projectKey, resource);
1675
+ this.save(context, resource);
1670
1676
  return resource;
1671
1677
  }
1672
1678
 
@@ -1712,10 +1718,10 @@ class OrderRepository extends AbstractResourceRepository {
1712
1718
  constructor() {
1713
1719
  super(...arguments);
1714
1720
  this.actions = {
1715
- addPayment: (projectKey, resource, {
1721
+ addPayment: (context, resource, {
1716
1722
  payment
1717
1723
  }) => {
1718
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1724
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1719
1725
 
1720
1726
  if (!resolvedPayment) {
1721
1727
  throw new Error(`Payment ${payment.id} not found`);
@@ -1732,20 +1738,20 @@ class OrderRepository extends AbstractResourceRepository {
1732
1738
  id: payment.id
1733
1739
  });
1734
1740
  },
1735
- changeOrderState: (projectKey, resource, {
1741
+ changeOrderState: (context, resource, {
1736
1742
  orderState
1737
1743
  }) => {
1738
1744
  resource.orderState = orderState;
1739
1745
  },
1740
- changePaymentState: (projectKey, resource, {
1746
+ changePaymentState: (context, resource, {
1741
1747
  paymentState
1742
1748
  }) => {
1743
1749
  resource.paymentState = paymentState;
1744
1750
  },
1745
- transitionState: (projectKey, resource, {
1751
+ transitionState: (context, resource, {
1746
1752
  state
1747
1753
  }) => {
1748
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1754
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1749
1755
 
1750
1756
  if (!resolvedType) {
1751
1757
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1756,17 +1762,17 @@ class OrderRepository extends AbstractResourceRepository {
1756
1762
  id: resolvedType.id
1757
1763
  };
1758
1764
  },
1759
- setBillingAddress: (projectKey, resource, {
1765
+ setBillingAddress: (context, resource, {
1760
1766
  address
1761
1767
  }) => {
1762
1768
  resource.billingAddress = address;
1763
1769
  },
1764
- setCustomerEmail: (projectKey, resource, {
1770
+ setCustomerEmail: (context, resource, {
1765
1771
  email
1766
1772
  }) => {
1767
1773
  resource.customerEmail = email;
1768
1774
  },
1769
- setCustomField: (projectKey, resource, {
1775
+ setCustomField: (context, resource, {
1770
1776
  name,
1771
1777
  value
1772
1778
  }) => {
@@ -1776,14 +1782,14 @@ class OrderRepository extends AbstractResourceRepository {
1776
1782
 
1777
1783
  resource.custom.fields[name] = value;
1778
1784
  },
1779
- setCustomType: (projectKey, resource, {
1785
+ setCustomType: (context, resource, {
1780
1786
  type,
1781
1787
  fields
1782
1788
  }) => {
1783
1789
  if (!type) {
1784
1790
  resource.custom = undefined;
1785
1791
  } else {
1786
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1792
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1787
1793
 
1788
1794
  if (!resolvedType) {
1789
1795
  throw new Error(`Type ${type} not found`);
@@ -1798,27 +1804,27 @@ class OrderRepository extends AbstractResourceRepository {
1798
1804
  };
1799
1805
  }
1800
1806
  },
1801
- setLocale: (projectKey, resource, {
1807
+ setLocale: (context, resource, {
1802
1808
  locale
1803
1809
  }) => {
1804
1810
  resource.locale = locale;
1805
1811
  },
1806
- setOrderNumber: (projectKey, resource, {
1812
+ setOrderNumber: (context, resource, {
1807
1813
  orderNumber
1808
1814
  }) => {
1809
1815
  resource.orderNumber = orderNumber;
1810
1816
  },
1811
- setShippingAddress: (projectKey, resource, {
1817
+ setShippingAddress: (context, resource, {
1812
1818
  address
1813
1819
  }) => {
1814
1820
  resource.shippingAddress = address;
1815
1821
  },
1816
- setStore: (projectKey, resource, {
1822
+ setStore: (context, resource, {
1817
1823
  store
1818
1824
  }) => {
1819
1825
  if (!store) return;
1820
1826
 
1821
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1827
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1822
1828
 
1823
1829
  if (!resolvedType) {
1824
1830
  throw new Error(`No store found with key=${store.key}`);
@@ -1837,17 +1843,24 @@ class OrderRepository extends AbstractResourceRepository {
1837
1843
  return 'order';
1838
1844
  }
1839
1845
 
1840
- create(projectKey, draft) {
1846
+ create(context, draft) {
1841
1847
  assert(draft.cart, 'draft.cart is missing');
1848
+ return this.createFromCart(context, {
1849
+ id: draft.cart.id,
1850
+ typeId: 'cart'
1851
+ }, draft.orderNumber);
1852
+ }
1842
1853
 
1843
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1854
+ createFromCart(context, cartReference, orderNumber) {
1855
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1844
1856
 
1845
1857
  if (!cart) {
1846
1858
  throw new Error('Cannot find cart');
1847
1859
  }
1848
1860
 
1849
1861
  const resource = { ...getBaseResourceProperties(),
1850
- orderNumber: draft.orderNumber,
1862
+ orderNumber,
1863
+ cart: cartReference,
1851
1864
  orderState: 'Open',
1852
1865
  lineItems: [],
1853
1866
  customLineItems: [],
@@ -1855,13 +1868,17 @@ class OrderRepository extends AbstractResourceRepository {
1855
1868
  refusedGifts: [],
1856
1869
  origin: 'Customer',
1857
1870
  syncInfo: [],
1871
+ store: context.storeKey ? {
1872
+ key: context.storeKey,
1873
+ typeId: 'store'
1874
+ } : undefined,
1858
1875
  lastMessageSequenceNumber: 0
1859
1876
  };
1860
- this.save(projectKey, resource);
1877
+ this.save(context, resource);
1861
1878
  return resource;
1862
1879
  }
1863
1880
 
1864
- import(projectKey, draft) {
1881
+ import(context, draft) {
1865
1882
  var _draft$lineItems, _draft$customLineItem;
1866
1883
 
1867
1884
  // TODO: Check if order with given orderNumber already exists
@@ -1869,7 +1886,7 @@ class OrderRepository extends AbstractResourceRepository {
1869
1886
  const resource = { ...getBaseResourceProperties(),
1870
1887
  billingAddress: draft.billingAddress,
1871
1888
  shippingAddress: draft.shippingAddress,
1872
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1889
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1873
1890
  customerEmail: draft.customerEmail,
1874
1891
  lastMessageSequenceNumber: 0,
1875
1892
  orderNumber: draft.orderNumber,
@@ -1877,21 +1894,21 @@ class OrderRepository extends AbstractResourceRepository {
1877
1894
  origin: draft.origin || 'Customer',
1878
1895
  paymentState: draft.paymentState,
1879
1896
  refusedGifts: [],
1880
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1897
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1881
1898
  syncInfo: [],
1882
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1883
- customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1899
+ lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(context, item))) || [],
1900
+ customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(context, item))) || [],
1884
1901
  totalPrice: {
1885
1902
  type: 'centPrecision',
1886
1903
  ...draft.totalPrice,
1887
1904
  fractionDigits: 2
1888
1905
  }
1889
1906
  };
1890
- this.save(projectKey, resource);
1907
+ this.save(context, resource);
1891
1908
  return resource;
1892
1909
  }
1893
1910
 
1894
- lineItemFromImportDraft(projectKey, draft) {
1911
+ lineItemFromImportDraft(context, draft) {
1895
1912
  let product;
1896
1913
  let variant;
1897
1914
 
@@ -1901,7 +1918,7 @@ class OrderRepository extends AbstractResourceRepository {
1901
1918
  sku: draft.variant.sku
1902
1919
  };
1903
1920
 
1904
- var items = this._storage.query(projectKey, 'product', {
1921
+ var items = this._storage.query(context.projectKey, 'product', {
1905
1922
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1906
1923
  });
1907
1924
 
@@ -1928,7 +1945,7 @@ class OrderRepository extends AbstractResourceRepository {
1928
1945
  }
1929
1946
 
1930
1947
  const lineItem = { ...getBaseResourceProperties(),
1931
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1948
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1932
1949
  discountedPricePerQuantity: [],
1933
1950
  lineItemMode: 'Standard',
1934
1951
  name: draft.name,
@@ -1949,9 +1966,9 @@ class OrderRepository extends AbstractResourceRepository {
1949
1966
  return lineItem;
1950
1967
  }
1951
1968
 
1952
- customLineItemFromImportDraft(projectKey, draft) {
1969
+ customLineItemFromImportDraft(context, draft) {
1953
1970
  const lineItem = { ...getBaseResourceProperties(),
1954
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1971
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1955
1972
  discountedPricePerQuantity: [],
1956
1973
  money: createTypedMoney(draft.money),
1957
1974
  name: draft.name,
@@ -1963,8 +1980,8 @@ class OrderRepository extends AbstractResourceRepository {
1963
1980
  return lineItem;
1964
1981
  }
1965
1982
 
1966
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1967
- const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
1983
+ getWithOrderNumber(context, orderNumber, params = {}) {
1984
+ const result = this._storage.query(context.projectKey, this.getTypeId(), { ...params,
1968
1985
  where: [`orderNumber="${orderNumber}"`]
1969
1986
  });
1970
1987
 
@@ -1995,8 +2012,9 @@ class CartService extends AbstractService {
1995
2012
 
1996
2013
  extraRoutes(parent) {
1997
2014
  parent.post('/replicate', (request, response) => {
1998
- // @ts-ignore
1999
- const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(request.params.projectKey, request.body.reference.id) : this.repository.get(request.params.projectKey, request.body.reference.id);
2015
+ const context = getRepositoryContext(request); // @ts-ignore
2016
+
2017
+ const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(context, request.body.reference.id) : this.repository.get(context, request.body.reference.id);
2000
2018
 
2001
2019
  if (!cartOrOrder) {
2002
2020
  return response.status(400).send();
@@ -2012,7 +2030,7 @@ class CartService extends AbstractService {
2012
2030
  };
2013
2031
  })
2014
2032
  };
2015
- const newCart = this.repository.create(request.params.projectKey, cartDraft);
2033
+ const newCart = this.repository.create(context, cartDraft);
2016
2034
  return response.status(200).send(newCart);
2017
2035
  });
2018
2036
  }
@@ -2023,7 +2041,7 @@ class CategoryRepository extends AbstractResourceRepository {
2023
2041
  constructor() {
2024
2042
  super(...arguments);
2025
2043
  this.actions = {
2026
- changeAssetName: (projectKey, resource, {
2044
+ changeAssetName: (context, resource, {
2027
2045
  assetId,
2028
2046
  assetKey,
2029
2047
  name
@@ -2040,17 +2058,17 @@ class CategoryRepository extends AbstractResourceRepository {
2040
2058
  }
2041
2059
  });
2042
2060
  },
2043
- changeSlug: (projectKey, resource, {
2061
+ changeSlug: (context, resource, {
2044
2062
  slug
2045
2063
  }) => {
2046
2064
  resource.slug = slug;
2047
2065
  },
2048
- setKey: (projectKey, resource, {
2066
+ setKey: (context, resource, {
2049
2067
  key
2050
2068
  }) => {
2051
2069
  resource.key = key;
2052
2070
  },
2053
- setAssetDescription: (projectKey, resource, {
2071
+ setAssetDescription: (context, resource, {
2054
2072
  assetId,
2055
2073
  assetKey,
2056
2074
  description
@@ -2067,7 +2085,7 @@ class CategoryRepository extends AbstractResourceRepository {
2067
2085
  }
2068
2086
  });
2069
2087
  },
2070
- setAssetSources: (projectKey, resource, {
2088
+ setAssetSources: (context, resource, {
2071
2089
  assetId,
2072
2090
  assetKey,
2073
2091
  sources
@@ -2084,22 +2102,22 @@ class CategoryRepository extends AbstractResourceRepository {
2084
2102
  }
2085
2103
  });
2086
2104
  },
2087
- setDescription: (projectKey, resource, {
2105
+ setDescription: (context, resource, {
2088
2106
  description
2089
2107
  }) => {
2090
2108
  resource.description = description;
2091
2109
  },
2092
- setMetaDescription: (projectKey, resource, {
2110
+ setMetaDescription: (context, resource, {
2093
2111
  metaDescription
2094
2112
  }) => {
2095
2113
  resource.metaDescription = metaDescription;
2096
2114
  },
2097
- setMetaKeywords: (projectKey, resource, {
2115
+ setMetaKeywords: (context, resource, {
2098
2116
  metaKeywords
2099
2117
  }) => {
2100
2118
  resource.metaKeywords = metaKeywords;
2101
2119
  },
2102
- setMetaTitle: (projectKey, resource, {
2120
+ setMetaTitle: (context, resource, {
2103
2121
  metaTitle
2104
2122
  }) => {
2105
2123
  resource.metaTitle = metaTitle;
@@ -2111,7 +2129,7 @@ class CategoryRepository extends AbstractResourceRepository {
2111
2129
  return 'category';
2112
2130
  }
2113
2131
 
2114
- create(projectKey, draft) {
2132
+ create(context, draft) {
2115
2133
  var _draft$assets;
2116
2134
 
2117
2135
  const resource = { ...getBaseResourceProperties(),
@@ -2133,11 +2151,11 @@ class CategoryRepository extends AbstractResourceRepository {
2133
2151
  sources: d.sources,
2134
2152
  tags: d.tags,
2135
2153
  key: d.key,
2136
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2154
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2137
2155
  };
2138
2156
  })) || []
2139
2157
  };
2140
- this.save(projectKey, resource);
2158
+ this.save(context, resource);
2141
2159
  return resource;
2142
2160
  }
2143
2161
 
@@ -2160,12 +2178,12 @@ class ChannelRepository extends AbstractResourceRepository {
2160
2178
  return 'channel';
2161
2179
  }
2162
2180
 
2163
- create(projectKey, draft) {
2181
+ create(context, draft) {
2164
2182
  const resource = { ...getBaseResourceProperties(),
2165
2183
  key: draft.key,
2166
2184
  roles: draft.roles || []
2167
2185
  };
2168
- this.save(projectKey, resource);
2186
+ this.save(context, resource);
2169
2187
  return resource;
2170
2188
  }
2171
2189
 
@@ -2187,12 +2205,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2187
2205
  constructor() {
2188
2206
  super(...arguments);
2189
2207
  this.actions = {
2190
- setKey: (projectKey, resource, {
2208
+ setKey: (context, resource, {
2191
2209
  key
2192
2210
  }) => {
2193
2211
  resource.key = key;
2194
2212
  },
2195
- changeName: (projectKey, resource, {
2213
+ changeName: (context, resource, {
2196
2214
  name
2197
2215
  }) => {
2198
2216
  resource.name = name;
@@ -2204,12 +2222,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2204
2222
  return 'customer';
2205
2223
  }
2206
2224
 
2207
- create(projectKey, draft) {
2225
+ create(context, draft) {
2208
2226
  const resource = { ...getBaseResourceProperties(),
2209
2227
  key: draft.key,
2210
2228
  name: draft.groupName
2211
2229
  };
2212
- this.save(projectKey, resource);
2230
+ this.save(context, resource);
2213
2231
  return resource;
2214
2232
  }
2215
2233
 
@@ -2231,7 +2249,7 @@ class CustomerRepository extends AbstractResourceRepository {
2231
2249
  constructor() {
2232
2250
  super(...arguments);
2233
2251
  this.actions = {
2234
- changeEmail: (_projectKey, resource, {
2252
+ changeEmail: (_context, resource, {
2235
2253
  email
2236
2254
  }) => {
2237
2255
  resource.email = email;
@@ -2243,19 +2261,19 @@ class CustomerRepository extends AbstractResourceRepository {
2243
2261
  return 'customer';
2244
2262
  }
2245
2263
 
2246
- create(projectKey, draft) {
2264
+ create(context, draft) {
2247
2265
  const resource = { ...getBaseResourceProperties(),
2248
2266
  email: draft.email,
2249
2267
  password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2250
2268
  isEmailVerified: draft.isEmailVerified || false,
2251
2269
  addresses: []
2252
2270
  };
2253
- this.save(projectKey, resource);
2271
+ this.save(context, resource);
2254
2272
  return resource;
2255
2273
  }
2256
2274
 
2257
- getMe(projectKey) {
2258
- const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
2275
+ getMe(context) {
2276
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {}); // grab the first customer you can find
2259
2277
 
2260
2278
 
2261
2279
  if (results.count > 0) {
@@ -2279,10 +2297,12 @@ class CustomerService extends AbstractService {
2279
2297
 
2280
2298
  extraRoutes(parent) {
2281
2299
  parent.post('/password-token', (request, response) => {
2282
- const customer = this.repository.query(request.params.projectKey, {
2300
+ const customer = this.repository.query(getRepositoryContext(request), {
2283
2301
  where: [`email="${request.body.email}"`]
2284
- });
2285
- const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
2302
+ }); // @ts-ignore
2303
+
2304
+ const ttlMinutes = request.params.ttlMinutes ? // @ts-ignore
2305
+ +request.params.ttlMinutes : 34560;
2286
2306
  const {
2287
2307
  version,
2288
2308
  ...rest
@@ -2302,8 +2322,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2302
2322
  return 'key-value-document';
2303
2323
  }
2304
2324
 
2305
- create(projectKey, draft) {
2306
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2325
+ create(context, draft) {
2326
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2307
2327
  const baseProperties = getBaseResourceProperties();
2308
2328
 
2309
2329
  if (current) {
@@ -2331,12 +2351,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2331
2351
  key: draft.key,
2332
2352
  value: draft.value
2333
2353
  };
2334
- this.save(projectKey, resource);
2354
+ this.save(context, resource);
2335
2355
  return resource;
2336
2356
  }
2337
2357
 
2338
- getWithContainerAndKey(projectKey, container, key) {
2339
- const items = this._storage.all(projectKey, this.getTypeId());
2358
+ getWithContainerAndKey(context, container, key) {
2359
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2340
2360
 
2341
2361
  return items.find(item => item.container === container && item.key === key);
2342
2362
  }
@@ -2360,7 +2380,7 @@ class CustomObjectService extends AbstractService {
2360
2380
  }
2361
2381
 
2362
2382
  getWithContainerAndKey(request, response) {
2363
- const result = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2383
+ const result = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2364
2384
 
2365
2385
  if (!result) {
2366
2386
  return response.status(404).send('Not Found');
@@ -2374,18 +2394,18 @@ class CustomObjectService extends AbstractService {
2374
2394
  key: request.params.key,
2375
2395
  container: request.params.container
2376
2396
  };
2377
- const result = this.repository.create(request.params.projectKey, draft);
2397
+ const result = this.repository.create(getRepositoryContext(request), draft);
2378
2398
  return response.status(200).send(result);
2379
2399
  }
2380
2400
 
2381
2401
  deleteWithContainerAndKey(request, response) {
2382
- const current = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2402
+ const current = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2383
2403
 
2384
2404
  if (!current) {
2385
2405
  return response.status(404).send('Not Found');
2386
2406
  }
2387
2407
 
2388
- const result = this.repository.delete(request.params.projectKey, current.id);
2408
+ const result = this.repository.delete(getRepositoryContext(request), current.id);
2389
2409
  return response.status(200).send(result);
2390
2410
  }
2391
2411
 
@@ -2395,12 +2415,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2395
2415
  constructor() {
2396
2416
  super(...arguments);
2397
2417
  this.actions = {
2398
- changeIsActive: (projectKey, resource, {
2418
+ changeIsActive: (context, resource, {
2399
2419
  isActive
2400
2420
  }) => {
2401
2421
  resource.isActive = isActive;
2402
2422
  },
2403
- changeCartDiscounts: (projectKey, resource, {
2423
+ changeCartDiscounts: (context, resource, {
2404
2424
  cartDiscounts
2405
2425
  }) => {
2406
2426
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2408,42 +2428,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2408
2428
  id: obj.id
2409
2429
  }));
2410
2430
  },
2411
- setDescription: (projectKey, resource, {
2431
+ setDescription: (context, resource, {
2412
2432
  description
2413
2433
  }) => {
2414
2434
  resource.description = description;
2415
2435
  },
2416
- setCartPredicate: (projectKey, resource, {
2436
+ setCartPredicate: (context, resource, {
2417
2437
  cartPredicate
2418
2438
  }) => {
2419
2439
  resource.cartPredicate = cartPredicate;
2420
2440
  },
2421
- setName: (projectKey, resource, {
2441
+ setName: (context, resource, {
2422
2442
  name
2423
2443
  }) => {
2424
2444
  resource.name = name;
2425
2445
  },
2426
- setMaxApplications: (projectKey, resource, {
2446
+ setMaxApplications: (context, resource, {
2427
2447
  maxApplications
2428
2448
  }) => {
2429
2449
  resource.maxApplications = maxApplications;
2430
2450
  },
2431
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2451
+ setMaxApplicationsPerCustomer: (context, resource, {
2432
2452
  maxApplicationsPerCustomer
2433
2453
  }) => {
2434
2454
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2435
2455
  },
2436
- setValidFrom: (projectKey, resource, {
2456
+ setValidFrom: (context, resource, {
2437
2457
  validFrom
2438
2458
  }) => {
2439
2459
  resource.validFrom = validFrom;
2440
2460
  },
2441
- setValidUntil: (projectKey, resource, {
2461
+ setValidUntil: (context, resource, {
2442
2462
  validUntil
2443
2463
  }) => {
2444
2464
  resource.validUntil = validUntil;
2445
2465
  },
2446
- setValidFromAndUntil: (projectKey, resource, {
2466
+ setValidFromAndUntil: (context, resource, {
2447
2467
  validFrom,
2448
2468
  validUntil
2449
2469
  }) => {
@@ -2457,7 +2477,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2457
2477
  return 'cart-discount';
2458
2478
  }
2459
2479
 
2460
- create(projectKey, draft) {
2480
+ create(context, draft) {
2461
2481
  const resource = { ...getBaseResourceProperties(),
2462
2482
  applicationVersion: 1,
2463
2483
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2476,7 +2496,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2476
2496
  maxApplications: draft.maxApplications,
2477
2497
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2478
2498
  };
2479
- this.save(projectKey, resource);
2499
+ this.save(context, resource);
2480
2500
  return resource;
2481
2501
  }
2482
2502
 
@@ -2498,22 +2518,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2498
2518
  constructor() {
2499
2519
  super(...arguments);
2500
2520
  this.actions = {
2501
- setKey: (projectKey, resource, {
2521
+ setKey: (context, resource, {
2502
2522
  key
2503
2523
  }) => {
2504
2524
  resource.key = key;
2505
2525
  },
2506
- setTimeoutInMs: (projectKey, resource, {
2526
+ setTimeoutInMs: (context, resource, {
2507
2527
  timeoutInMs
2508
2528
  }) => {
2509
2529
  resource.timeoutInMs = timeoutInMs;
2510
2530
  },
2511
- changeTriggers: (projectKey, resource, {
2531
+ changeTriggers: (context, resource, {
2512
2532
  triggers
2513
2533
  }) => {
2514
2534
  resource.triggers = triggers;
2515
2535
  },
2516
- changeDestination: (projectKey, resource, {
2536
+ changeDestination: (context, resource, {
2517
2537
  destination
2518
2538
  }) => {
2519
2539
  resource.destination = destination;
@@ -2525,14 +2545,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2525
2545
  return 'extension';
2526
2546
  }
2527
2547
 
2528
- create(projectKey, draft) {
2548
+ create(context, draft) {
2529
2549
  const resource = { ...getBaseResourceProperties(),
2530
2550
  key: draft.key,
2531
2551
  timeoutInMs: draft.timeoutInMs,
2532
2552
  destination: draft.destination,
2533
2553
  triggers: draft.triggers
2534
2554
  };
2535
- this.save(projectKey, resource);
2555
+ this.save(context, resource);
2536
2556
  return resource;
2537
2557
  }
2538
2558
 
@@ -2554,19 +2574,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2554
2574
  constructor() {
2555
2575
  super(...arguments);
2556
2576
  this.actions = {
2557
- changeQuantity: (projectKey, resource, {
2577
+ changeQuantity: (context, resource, {
2558
2578
  quantity
2559
2579
  }) => {
2560
2580
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2561
2581
 
2562
2582
  resource.availableQuantity = quantity;
2563
2583
  },
2564
- setExpectedDelivery: (projectKey, resource, {
2584
+ setExpectedDelivery: (context, resource, {
2565
2585
  expectedDelivery
2566
2586
  }) => {
2567
2587
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2568
2588
  },
2569
- setCustomField: (projectKey, resource, {
2589
+ setCustomField: (context, resource, {
2570
2590
  name,
2571
2591
  value
2572
2592
  }) => {
@@ -2576,14 +2596,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2576
2596
 
2577
2597
  resource.custom.fields[name] = value;
2578
2598
  },
2579
- setCustomType: (projectKey, resource, {
2599
+ setCustomType: (context, resource, {
2580
2600
  type,
2581
2601
  fields
2582
2602
  }) => {
2583
2603
  if (!type) {
2584
2604
  resource.custom = undefined;
2585
2605
  } else {
2586
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2606
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2587
2607
 
2588
2608
  if (!resolvedType) {
2589
2609
  throw new Error(`Type ${type} not found`);
@@ -2598,7 +2618,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2598
2618
  };
2599
2619
  }
2600
2620
  },
2601
- setRestockableInDays: (projectKey, resource, {
2621
+ setRestockableInDays: (context, resource, {
2602
2622
  restockableInDays
2603
2623
  }) => {
2604
2624
  resource.restockableInDays = restockableInDays;
@@ -2610,7 +2630,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2610
2630
  return 'inventory-entry';
2611
2631
  }
2612
2632
 
2613
- create(projectKey, draft) {
2633
+ create(context, draft) {
2614
2634
  var _draft$supplyChannel$, _draft$supplyChannel;
2615
2635
 
2616
2636
  const resource = { ...getBaseResourceProperties(),
@@ -2623,9 +2643,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2623
2643
  typeId: 'channel',
2624
2644
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2625
2645
  },
2626
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2646
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2627
2647
  };
2628
- this.save(projectKey, resource);
2648
+ this.save(context, resource);
2629
2649
  return resource;
2630
2650
  }
2631
2651
 
@@ -2685,14 +2705,14 @@ class PaymentRepository extends AbstractResourceRepository {
2685
2705
  constructor() {
2686
2706
  super(...arguments);
2687
2707
 
2688
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2708
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2689
2709
  id: v4(),
2690
2710
  amount: createTypedMoney(draft.amount),
2691
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2711
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2692
2712
  });
2693
2713
 
2694
2714
  this.actions = {
2695
- setCustomField: (projectKey, resource, {
2715
+ setCustomField: (context, resource, {
2696
2716
  name,
2697
2717
  value
2698
2718
  }) => {
@@ -2702,14 +2722,14 @@ class PaymentRepository extends AbstractResourceRepository {
2702
2722
 
2703
2723
  resource.custom.fields[name] = value;
2704
2724
  },
2705
- setCustomType: (projectKey, resource, {
2725
+ setCustomType: (context, resource, {
2706
2726
  type,
2707
2727
  fields
2708
2728
  }) => {
2709
2729
  if (!type) {
2710
2730
  resource.custom = undefined;
2711
2731
  } else {
2712
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2732
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2713
2733
 
2714
2734
  if (!resolvedType) {
2715
2735
  throw new Error(`Type ${type} not found`);
@@ -2724,12 +2744,12 @@ class PaymentRepository extends AbstractResourceRepository {
2724
2744
  };
2725
2745
  }
2726
2746
  },
2727
- addTransaction: (projectKey, resource, {
2747
+ addTransaction: (context, resource, {
2728
2748
  transaction
2729
2749
  }) => {
2730
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2750
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2731
2751
  },
2732
- changeTransactionState: (_projectKey, resource, {
2752
+ changeTransactionState: (_context, resource, {
2733
2753
  transactionId,
2734
2754
  state
2735
2755
  }) => {
@@ -2739,10 +2759,10 @@ class PaymentRepository extends AbstractResourceRepository {
2739
2759
  };
2740
2760
  resource.transactions[index] = updatedTransaction;
2741
2761
  },
2742
- transitionState: (projectKey, resource, {
2762
+ transitionState: (context, resource, {
2743
2763
  state
2744
2764
  }) => {
2745
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2765
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2746
2766
 
2747
2767
  if (!stateObj) {
2748
2768
  throw new Error(`State ${state} not found`);
@@ -2761,18 +2781,18 @@ class PaymentRepository extends AbstractResourceRepository {
2761
2781
  return 'payment';
2762
2782
  }
2763
2783
 
2764
- create(projectKey, draft) {
2784
+ create(context, draft) {
2765
2785
  const resource = { ...getBaseResourceProperties(),
2766
2786
  amountPlanned: createTypedMoney(draft.amountPlanned),
2767
2787
  paymentMethodInfo: draft.paymentMethodInfo,
2768
2788
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2769
- state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
2789
+ state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, context.projectKey, this._storage) : undefined
2770
2790
  } : {},
2771
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2772
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2773
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2791
+ transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, context)),
2792
+ interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, context.projectKey, this._storage)),
2793
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2774
2794
  };
2775
- this.save(projectKey, resource);
2795
+ this.save(context, resource);
2776
2796
  return resource;
2777
2797
  }
2778
2798
 
@@ -2807,12 +2827,12 @@ class OrderService extends AbstractService {
2807
2827
 
2808
2828
  import(request, response) {
2809
2829
  const importDraft = request.body;
2810
- const resource = this.repository.import(request.params.projectKey, importDraft);
2830
+ const resource = this.repository.import(getRepositoryContext(request), importDraft);
2811
2831
  return response.status(200).send(resource);
2812
2832
  }
2813
2833
 
2814
2834
  getWithOrderNumber(request, response) {
2815
- const resource = this.repository.getWithOrderNumber(request.params.projectKey, request.params.orderNumber, request.query);
2835
+ const resource = this.repository.getWithOrderNumber(getRepositoryContext(request), request.params.orderNumber, request.query);
2816
2836
 
2817
2837
  if (resource) {
2818
2838
  return response.status(200).send(resource);
@@ -2864,7 +2884,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2864
2884
  return 'product-projection';
2865
2885
  }
2866
2886
 
2867
- create(projectKey, draft) {
2887
+ create(context, draft) {
2868
2888
  var _draft$variants$map, _draft$variants;
2869
2889
 
2870
2890
  if (!draft.masterVariant) {
@@ -2889,17 +2909,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2889
2909
  // @ts-ignore
2890
2910
  searchKeywords: draft.searchKeywords
2891
2911
  };
2892
- this.save(projectKey, resource);
2912
+ this.save(context, resource);
2893
2913
  return resource;
2894
2914
  }
2895
2915
 
2896
- search(projectKey, query) {
2916
+ search(context, query) {
2897
2917
  var _query$filterQuery;
2898
2918
 
2899
2919
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2900
2920
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2901
2921
 
2902
- const results = this._storage.query(projectKey, this.getTypeId(), {
2922
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2903
2923
  where: wherePredicate,
2904
2924
  offset: query.offset ? Number(query.offset) : undefined,
2905
2925
  limit: query.limit ? Number(query.limit) : undefined
@@ -2934,7 +2954,7 @@ class ProductProjectionService extends AbstractService {
2934
2954
  }
2935
2955
 
2936
2956
  search(request, response) {
2937
- const resource = this.repository.search(request.params.projectKey, request.query);
2957
+ const resource = this.repository.search(getRepositoryContext(request), request.query);
2938
2958
  return response.status(200).send(resource);
2939
2959
  }
2940
2960
 
@@ -2944,7 +2964,7 @@ class ProductRepository extends AbstractResourceRepository {
2944
2964
  constructor() {
2945
2965
  super(...arguments);
2946
2966
  this.actions = {
2947
- publish: (projectKey, resource, {
2967
+ publish: (context, resource, {
2948
2968
  scope
2949
2969
  }) => {
2950
2970
  if (resource.masterData.staged) {
@@ -2956,7 +2976,7 @@ class ProductRepository extends AbstractResourceRepository {
2956
2976
  resource.masterData.hasStagedChanges = false;
2957
2977
  resource.masterData.published = true;
2958
2978
  },
2959
- setAttribute: (projectKey, resource, {
2979
+ setAttribute: (context, resource, {
2960
2980
  variantId,
2961
2981
  sku,
2962
2982
  name,
@@ -3017,7 +3037,7 @@ class ProductRepository extends AbstractResourceRepository {
3017
3037
  return 'product';
3018
3038
  }
3019
3039
 
3020
- create(projectKey, draft) {
3040
+ create(context, draft) {
3021
3041
  var _draft$publish, _draft$publish2;
3022
3042
 
3023
3043
  const productData = {
@@ -3041,7 +3061,7 @@ class ProductRepository extends AbstractResourceRepository {
3041
3061
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3042
3062
  }
3043
3063
  };
3044
- this.save(projectKey, resource);
3064
+ this.save(context, resource);
3045
3065
  return resource;
3046
3066
  }
3047
3067
 
@@ -3101,7 +3121,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3101
3121
  constructor() {
3102
3122
  super(...arguments);
3103
3123
 
3104
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3124
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3105
3125
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3106
3126
 
3107
3127
  return { ...draft,
@@ -3112,7 +3132,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3112
3132
  };
3113
3133
 
3114
3134
  this.actions = {
3115
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3135
+ changeLocalizedEnumValueLabel: (context, resource, {
3116
3136
  attributeName,
3117
3137
  newValue
3118
3138
  }) => {
@@ -3140,7 +3160,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3140
3160
  }
3141
3161
  });
3142
3162
  },
3143
- changeLabel: (projectKey, resource, {
3163
+ changeLabel: (context, resource, {
3144
3164
  attributeName,
3145
3165
  label
3146
3166
  }) => {
@@ -3159,21 +3179,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3159
3179
  return 'product-type';
3160
3180
  }
3161
3181
 
3162
- create(projectKey, draft) {
3182
+ create(context, draft) {
3163
3183
  var _draft$attributes;
3164
3184
 
3165
3185
  const resource = { ...getBaseResourceProperties(),
3166
3186
  key: draft.key,
3167
3187
  name: draft.name,
3168
3188
  description: draft.description,
3169
- attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(projectKey, a))
3189
+ attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(context, a))
3170
3190
  };
3171
- this.save(projectKey, resource);
3191
+ this.save(context, resource);
3172
3192
  return resource;
3173
3193
  }
3174
3194
 
3175
- getWithKey(projectKey, key) {
3176
- const result = this._storage.query(projectKey, this.getTypeId(), {
3195
+ getWithKey(context, key) {
3196
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3177
3197
  where: [`key="${key}"`]
3178
3198
  });
3179
3199
 
@@ -3206,7 +3226,7 @@ class ProductTypeService extends AbstractService {
3206
3226
  }
3207
3227
 
3208
3228
  getWithKey(request, response) {
3209
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3229
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3210
3230
 
3211
3231
  if (resource) {
3212
3232
  return response.status(200).send(resource);
@@ -3243,32 +3263,32 @@ class ProjectRepository extends AbstractRepository {
3243
3263
  constructor() {
3244
3264
  super(...arguments);
3245
3265
  this.actions = {
3246
- changeName: (projectKey, resource, {
3266
+ changeName: (context, resource, {
3247
3267
  name
3248
3268
  }) => {
3249
3269
  resource.name = name;
3250
3270
  },
3251
- changeCurrencies: (projectKey, resource, {
3271
+ changeCurrencies: (context, resource, {
3252
3272
  currencies
3253
3273
  }) => {
3254
3274
  resource.currencies = currencies;
3255
3275
  },
3256
- changeCountries: (projectKey, resource, {
3276
+ changeCountries: (context, resource, {
3257
3277
  countries
3258
3278
  }) => {
3259
3279
  resource.countries = countries;
3260
3280
  },
3261
- changeLanguages: (projectKey, resource, {
3281
+ changeLanguages: (context, resource, {
3262
3282
  languages
3263
3283
  }) => {
3264
3284
  resource.languages = languages;
3265
3285
  },
3266
- changeMessagesEnabled: (projectKey, resource, {
3286
+ changeMessagesEnabled: (context, resource, {
3267
3287
  messagesEnabled
3268
3288
  }) => {
3269
3289
  resource.messages.enabled = messagesEnabled;
3270
3290
  },
3271
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3291
+ changeProductSearchIndexingEnabled: (context, resource, {
3272
3292
  enabled
3273
3293
  }) => {
3274
3294
  var _resource$searchIndex;
@@ -3280,7 +3300,7 @@ class ProjectRepository extends AbstractRepository {
3280
3300
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3281
3301
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3282
3302
  },
3283
- changeOrderSearchStatus: (projectKey, resource, {
3303
+ changeOrderSearchStatus: (context, resource, {
3284
3304
  status
3285
3305
  }) => {
3286
3306
  var _resource$searchIndex2;
@@ -3292,22 +3312,22 @@ class ProjectRepository extends AbstractRepository {
3292
3312
  resource.searchIndexing.orders.status = status;
3293
3313
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3294
3314
  },
3295
- setShippingRateInputType: (projectKey, resource, {
3315
+ setShippingRateInputType: (context, resource, {
3296
3316
  shippingRateInputType
3297
3317
  }) => {
3298
3318
  resource.shippingRateInputType = shippingRateInputType;
3299
3319
  },
3300
- setExternalOAuth: (projectKey, resource, {
3320
+ setExternalOAuth: (context, resource, {
3301
3321
  externalOAuth
3302
3322
  }) => {
3303
3323
  resource.externalOAuth = externalOAuth;
3304
3324
  },
3305
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3325
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3306
3326
  countryTaxRateFallbackEnabled
3307
3327
  }) => {
3308
3328
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3309
3329
  },
3310
- changeCartsConfiguration: (projectKey, resource, {
3330
+ changeCartsConfiguration: (context, resource, {
3311
3331
  cartsConfiguration
3312
3332
  }) => {
3313
3333
  resource.carts = cartsConfiguration || {
@@ -3318,15 +3338,15 @@ class ProjectRepository extends AbstractRepository {
3318
3338
  };
3319
3339
  }
3320
3340
 
3321
- get(projectKey) {
3322
- const resource = this._storage.getProject(projectKey);
3341
+ get(context) {
3342
+ const resource = this._storage.getProject(context.projectKey);
3323
3343
 
3324
3344
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3325
3345
  return masked;
3326
3346
  }
3327
3347
 
3328
- save(projectKey, resource) {
3329
- const current = this.get(projectKey);
3348
+ save(context, resource) {
3349
+ const current = this.get(context);
3330
3350
 
3331
3351
  if (current) {
3332
3352
  checkConcurrentModification(current, resource.version);
@@ -3359,20 +3379,19 @@ class ProjectService {
3359
3379
  }
3360
3380
 
3361
3381
  get(request, response) {
3362
- const projectKey = request.params.projectKey;
3363
- const project = this.repository.get(projectKey);
3382
+ const project = this.repository.get(getRepositoryContext(request));
3364
3383
  return response.status(200).send(project);
3365
3384
  }
3366
3385
 
3367
3386
  post(request, response) {
3368
3387
  const updateRequest = request.body;
3369
- const project = this.repository.get(request.params.projectKey);
3388
+ const project = this.repository.get(getRepositoryContext(request));
3370
3389
 
3371
3390
  if (!project) {
3372
3391
  return response.status(404).send({});
3373
3392
  }
3374
3393
 
3375
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3394
+ this.repository.processUpdateActions(getRepositoryContext(request), project, updateRequest.actions);
3376
3395
  return response.status(200).send({});
3377
3396
  }
3378
3397
 
@@ -3382,11 +3401,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3382
3401
  constructor() {
3383
3402
  super(...arguments);
3384
3403
 
3385
- this._transformZoneRateDraft = (projectKey, draft) => {
3404
+ this._transformZoneRateDraft = (context, draft) => {
3386
3405
  var _draft$shippingRates;
3387
3406
 
3388
3407
  return { ...draft,
3389
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3408
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3390
3409
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3391
3410
  };
3392
3411
  };
@@ -3400,7 +3419,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3400
3419
  };
3401
3420
 
3402
3421
  this.actions = {
3403
- addShippingRate: (_projectKey, resource, {
3422
+ addShippingRate: (_context, resource, {
3404
3423
  shippingRate,
3405
3424
  zone
3406
3425
  }) => {
@@ -3420,7 +3439,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3420
3439
  shippingRates: [rate]
3421
3440
  });
3422
3441
  },
3423
- removeShippingRate: (_projectKey, resource, {
3442
+ removeShippingRate: (_context, resource, {
3424
3443
  shippingRate,
3425
3444
  zone
3426
3445
  }) => {
@@ -3434,10 +3453,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3434
3453
  }
3435
3454
  });
3436
3455
  },
3437
- addZone: (projectKey, resource, {
3456
+ addZone: (context, resource, {
3438
3457
  zone
3439
3458
  }) => {
3440
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3459
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3441
3460
 
3442
3461
  if (resource.zoneRates === undefined) {
3443
3462
  resource.zoneRates = [];
@@ -3448,39 +3467,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3448
3467
  shippingRates: []
3449
3468
  });
3450
3469
  },
3451
- removeZone: (_projectKey, resource, {
3470
+ removeZone: (_context, resource, {
3452
3471
  zone
3453
3472
  }) => {
3454
3473
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3455
3474
  return zoneRate.zone.id !== zone.id;
3456
3475
  });
3457
3476
  },
3458
- setKey: (_projectKey, resource, {
3477
+ setKey: (_context, resource, {
3459
3478
  key
3460
3479
  }) => {
3461
3480
  resource.key = key;
3462
3481
  },
3463
- setDescription: (_projectKey, resource, {
3482
+ setDescription: (_context, resource, {
3464
3483
  description
3465
3484
  }) => {
3466
3485
  resource.description = description;
3467
3486
  },
3468
- setLocalizedDescription: (_projectKey, resource, {
3487
+ setLocalizedDescription: (_context, resource, {
3469
3488
  localizedDescription
3470
3489
  }) => {
3471
3490
  resource.localizedDescription = localizedDescription;
3472
3491
  },
3473
- setPredicate: (_projectKey, resource, {
3492
+ setPredicate: (_context, resource, {
3474
3493
  predicate
3475
3494
  }) => {
3476
3495
  resource.predicate = predicate;
3477
3496
  },
3478
- changeIsDefault: (_projectKey, resource, {
3497
+ changeIsDefault: (_context, resource, {
3479
3498
  isDefault
3480
3499
  }) => {
3481
3500
  resource.isDefault = isDefault;
3482
3501
  },
3483
- changeName: (_projectKey, resource, {
3502
+ changeName: (_context, resource, {
3484
3503
  name
3485
3504
  }) => {
3486
3505
  resource.name = name;
@@ -3492,16 +3511,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3492
3511
  return 'shipping-method';
3493
3512
  }
3494
3513
 
3495
- create(projectKey, draft) {
3514
+ create(context, draft) {
3496
3515
  var _draft$zoneRates;
3497
3516
 
3498
3517
  const resource = { ...getBaseResourceProperties(),
3499
3518
  ...draft,
3500
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3501
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3502
- custom: createCustomFields(draft.custom, projectKey, this._storage)
3519
+ taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, context.projectKey, this._storage),
3520
+ zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(context, z)),
3521
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
3503
3522
  };
3504
- this.save(projectKey, resource);
3523
+ this.save(context, resource);
3505
3524
  return resource;
3506
3525
  }
3507
3526
 
@@ -3529,13 +3548,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3529
3548
  return 'shopping-list';
3530
3549
  }
3531
3550
 
3532
- create(projectKey, draft) {
3551
+ create(context, draft) {
3533
3552
  var _draft$lineItems, _draft$store;
3534
3553
 
3535
3554
  // const product =
3536
3555
  const resource = { ...getBaseResourceProperties(),
3537
3556
  ...draft,
3538
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3557
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3539
3558
  textLineItems: [],
3540
3559
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3541
3560
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3550,16 +3569,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3550
3569
  typeId: 'product-type',
3551
3570
  id: ''
3552
3571
  },
3553
- custom: createCustomFields(e.custom, projectKey, this._storage)
3572
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3554
3573
  };
3555
3574
  }),
3556
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3575
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3557
3576
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3558
3577
  typeId: 'store',
3559
3578
  key: draft.store.key
3560
3579
  } : undefined
3561
3580
  };
3562
- this.save(projectKey, resource);
3581
+ this.save(context, resource);
3563
3582
  return resource;
3564
3583
  }
3565
3584
 
@@ -3581,22 +3600,22 @@ class StateRepository extends AbstractResourceRepository {
3581
3600
  constructor() {
3582
3601
  super(...arguments);
3583
3602
  this.actions = {
3584
- changeKey: (projectKey, resource, {
3603
+ changeKey: (context, resource, {
3585
3604
  key
3586
3605
  }) => {
3587
3606
  resource.key = key;
3588
3607
  },
3589
- setDescription: (projectKey, resource, {
3608
+ setDescription: (context, resource, {
3590
3609
  description
3591
3610
  }) => {
3592
3611
  resource.description = description;
3593
3612
  },
3594
- setName: (projectKey, resource, {
3613
+ setName: (context, resource, {
3595
3614
  name
3596
3615
  }) => {
3597
3616
  resource.name = name;
3598
3617
  },
3599
- setRoles: (projectKey, resource, {
3618
+ setRoles: (context, resource, {
3600
3619
  roles
3601
3620
  }) => {
3602
3621
  resource.roles = roles;
@@ -3608,14 +3627,14 @@ class StateRepository extends AbstractResourceRepository {
3608
3627
  return 'state';
3609
3628
  }
3610
3629
 
3611
- create(projectKey, draft) {
3630
+ create(context, draft) {
3612
3631
  const resource = { ...getBaseResourceProperties(),
3613
3632
  ...draft,
3614
3633
  builtIn: false,
3615
3634
  initial: draft.initial || false,
3616
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3635
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3617
3636
  };
3618
- this.save(projectKey, resource);
3637
+ this.save(context, resource);
3619
3638
  return resource;
3620
3639
  }
3621
3640
 
@@ -3637,17 +3656,17 @@ class StoreRepository extends AbstractResourceRepository {
3637
3656
  constructor() {
3638
3657
  super(...arguments);
3639
3658
  this.actions = {
3640
- setName: (projectKey, resource, {
3659
+ setName: (context, resource, {
3641
3660
  name
3642
3661
  }) => {
3643
3662
  resource.name = name;
3644
3663
  },
3645
- setDistributionChannels: (projectKey, resource, {
3664
+ setDistributionChannels: (context, resource, {
3646
3665
  distributionChannels
3647
3666
  }) => {
3648
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3667
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3649
3668
  },
3650
- setLanguages: (projectKey, resource, {
3669
+ setLanguages: (context, resource, {
3651
3670
  languages
3652
3671
  }) => {
3653
3672
  resource.languages = languages;
@@ -3659,25 +3678,25 @@ class StoreRepository extends AbstractResourceRepository {
3659
3678
  return 'store';
3660
3679
  }
3661
3680
 
3662
- create(projectKey, draft) {
3681
+ create(context, draft) {
3663
3682
  const resource = { ...getBaseResourceProperties(),
3664
3683
  key: draft.key,
3665
3684
  name: draft.name,
3666
3685
  languages: draft.languages,
3667
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3668
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3686
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3687
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3669
3688
  };
3670
- this.save(projectKey, resource);
3689
+ this.save(context, resource);
3671
3690
  return resource;
3672
3691
  }
3673
3692
 
3674
- transformChannels(projectKey, channels) {
3693
+ transformChannels(context, channels) {
3675
3694
  if (!channels) return [];
3676
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3695
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3677
3696
  }
3678
3697
 
3679
- getWithKey(projectKey, key) {
3680
- const result = this._storage.query(projectKey, this.getTypeId(), {
3698
+ getWithKey(context, key) {
3699
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3681
3700
  where: [`key="${key}"`]
3682
3701
  });
3683
3702
 
@@ -3709,7 +3728,7 @@ class StoreService extends AbstractService {
3709
3728
  }
3710
3729
 
3711
3730
  getWithKey(request, response) {
3712
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3731
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3713
3732
 
3714
3733
  if (resource) {
3715
3734
  return response.status(200).send(resource);
@@ -3725,7 +3744,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3725
3744
  return 'subscription';
3726
3745
  }
3727
3746
 
3728
- create(projectKey, draft) {
3747
+ create(context, draft) {
3729
3748
  // TODO: We could actually test this here by using the aws sdk. For now
3730
3749
  // hardcode a failed check when account id is 0000000000
3731
3750
  if (draft.destination.type === 'SQS') {
@@ -3751,7 +3770,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3751
3770
  messages: draft.messages || [],
3752
3771
  status: 'Healthy'
3753
3772
  };
3754
- this.save(projectKey, resource);
3773
+ this.save(context, resource);
3755
3774
  return resource;
3756
3775
  }
3757
3776
 
@@ -3779,7 +3798,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3779
3798
  });
3780
3799
 
3781
3800
  this.actions = {
3782
- addTaxRate: (projectKey, resource, {
3801
+ addTaxRate: (context, resource, {
3783
3802
  taxRate
3784
3803
  }) => {
3785
3804
  if (resource.rates === undefined) {
@@ -3788,7 +3807,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3788
3807
 
3789
3808
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3790
3809
  },
3791
- removeTaxRate: (projectKey, resource, {
3810
+ removeTaxRate: (context, resource, {
3792
3811
  taxRateId
3793
3812
  }) => {
3794
3813
  if (resource.rates === undefined) {
@@ -3799,7 +3818,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3799
3818
  return taxRate.id !== taxRateId;
3800
3819
  });
3801
3820
  },
3802
- replaceTaxRate: (projectKey, resource, {
3821
+ replaceTaxRate: (context, resource, {
3803
3822
  taxRateId,
3804
3823
  taxRate
3805
3824
  }) => {
@@ -3817,17 +3836,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3817
3836
  }
3818
3837
  }
3819
3838
  },
3820
- setDescription: (projectKey, resource, {
3839
+ setDescription: (context, resource, {
3821
3840
  description
3822
3841
  }) => {
3823
3842
  resource.description = description;
3824
3843
  },
3825
- setKey: (projectKey, resource, {
3844
+ setKey: (context, resource, {
3826
3845
  key
3827
3846
  }) => {
3828
3847
  resource.key = key;
3829
3848
  },
3830
- changeName: (projectKey, resource, {
3849
+ changeName: (context, resource, {
3831
3850
  name
3832
3851
  }) => {
3833
3852
  resource.name = name;
@@ -3839,19 +3858,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3839
3858
  return 'tax-category';
3840
3859
  }
3841
3860
 
3842
- create(projectKey, draft) {
3861
+ create(context, draft) {
3843
3862
  var _draft$rates;
3844
3863
 
3845
3864
  const resource = { ...getBaseResourceProperties(),
3846
3865
  ...draft,
3847
3866
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3848
3867
  };
3849
- this.save(projectKey, resource);
3868
+ this.save(context, resource);
3850
3869
  return resource;
3851
3870
  }
3852
3871
 
3853
- getWithKey(projectKey, key) {
3854
- const result = this._storage.query(projectKey, this.getTypeId(), {
3872
+ getWithKey(context, key) {
3873
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3855
3874
  where: [`key="${key}"`]
3856
3875
  });
3857
3876
 
@@ -3884,7 +3903,7 @@ class TaxCategoryService extends AbstractService {
3884
3903
  }
3885
3904
 
3886
3905
  getWithKey(request, response) {
3887
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3906
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3888
3907
 
3889
3908
  if (resource) {
3890
3909
  return response.status(200).send(resource);
@@ -3899,29 +3918,29 @@ class TypeRepository extends AbstractResourceRepository {
3899
3918
  constructor() {
3900
3919
  super(...arguments);
3901
3920
  this.actions = {
3902
- addFieldDefinition: (projectKey, resource, {
3921
+ addFieldDefinition: (context, resource, {
3903
3922
  fieldDefinition
3904
3923
  }) => {
3905
3924
  resource.fieldDefinitions.push(fieldDefinition);
3906
3925
  },
3907
- removeFieldDefinition: (projectKey, resource, {
3926
+ removeFieldDefinition: (context, resource, {
3908
3927
  fieldName
3909
3928
  }) => {
3910
3929
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3911
3930
  return f.name !== fieldName;
3912
3931
  });
3913
3932
  },
3914
- setDescription: (projectKey, resource, {
3933
+ setDescription: (context, resource, {
3915
3934
  description
3916
3935
  }) => {
3917
3936
  resource.description = description;
3918
3937
  },
3919
- changeName: (projectKey, resource, {
3938
+ changeName: (context, resource, {
3920
3939
  name
3921
3940
  }) => {
3922
3941
  resource.name = name;
3923
3942
  },
3924
- changeFieldDefinitionOrder: (projectKey, resource, {
3943
+ changeFieldDefinitionOrder: (context, resource, {
3925
3944
  fieldNames
3926
3945
  }) => {
3927
3946
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3945,7 +3964,7 @@ class TypeRepository extends AbstractResourceRepository {
3945
3964
 
3946
3965
  resource.fieldDefinitions.push(...current);
3947
3966
  },
3948
- addEnumValue: (projectKey, resource, {
3967
+ addEnumValue: (context, resource, {
3949
3968
  fieldName,
3950
3969
  value
3951
3970
  }) => {
@@ -3962,7 +3981,7 @@ class TypeRepository extends AbstractResourceRepository {
3962
3981
  }
3963
3982
  });
3964
3983
  },
3965
- changeEnumValueLabel: (projectKey, resource, {
3984
+ changeEnumValueLabel: (context, resource, {
3966
3985
  fieldName,
3967
3986
  value
3968
3987
  }) => {
@@ -3994,7 +4013,7 @@ class TypeRepository extends AbstractResourceRepository {
3994
4013
  return 'type';
3995
4014
  }
3996
4015
 
3997
- create(projectKey, draft) {
4016
+ create(context, draft) {
3998
4017
  const resource = { ...getBaseResourceProperties(),
3999
4018
  key: draft.key,
4000
4019
  name: draft.name,
@@ -4002,7 +4021,7 @@ class TypeRepository extends AbstractResourceRepository {
4002
4021
  fieldDefinitions: draft.fieldDefinitions || [],
4003
4022
  description: draft.description
4004
4023
  };
4005
- this.save(projectKey, resource);
4024
+ this.save(context, resource);
4006
4025
  return resource;
4007
4026
  }
4008
4027
 
@@ -4024,29 +4043,29 @@ class ZoneRepository extends AbstractResourceRepository {
4024
4043
  constructor() {
4025
4044
  super(...arguments);
4026
4045
  this.actions = {
4027
- addLocation: (projectKey, resource, {
4046
+ addLocation: (context, resource, {
4028
4047
  location
4029
4048
  }) => {
4030
4049
  resource.locations.push(location);
4031
4050
  },
4032
- removeLocation: (projectKey, resource, {
4051
+ removeLocation: (context, resource, {
4033
4052
  location
4034
4053
  }) => {
4035
4054
  resource.locations = resource.locations.filter(loc => {
4036
4055
  return !(loc.country === location.country && loc.state === location.state);
4037
4056
  });
4038
4057
  },
4039
- changeName: (projectKey, resource, {
4058
+ changeName: (context, resource, {
4040
4059
  name
4041
4060
  }) => {
4042
4061
  resource.name = name;
4043
4062
  },
4044
- setDescription: (projectKey, resource, {
4063
+ setDescription: (context, resource, {
4045
4064
  description
4046
4065
  }) => {
4047
4066
  resource.description = description;
4048
4067
  },
4049
- setKey: (projectKey, resource, {
4068
+ setKey: (context, resource, {
4050
4069
  key
4051
4070
  }) => {
4052
4071
  resource.key = key;
@@ -4058,14 +4077,14 @@ class ZoneRepository extends AbstractResourceRepository {
4058
4077
  return 'zone';
4059
4078
  }
4060
4079
 
4061
- create(projectKey, draft) {
4080
+ create(context, draft) {
4062
4081
  const resource = { ...getBaseResourceProperties(),
4063
4082
  key: draft.key,
4064
4083
  locations: draft.locations || [],
4065
4084
  name: draft.name,
4066
4085
  description: draft.description
4067
4086
  };
4068
- this.save(projectKey, resource);
4087
+ this.save(context, resource);
4069
4088
  return resource;
4070
4089
  }
4071
4090
 
@@ -4107,7 +4126,7 @@ class MyCustomerService extends AbstractService {
4107
4126
  }
4108
4127
 
4109
4128
  getMe(request, response) {
4110
- const resource = this.repository.getMe(request.params.projectKey);
4129
+ const resource = this.repository.getMe(getRepositoryContext(request));
4111
4130
 
4112
4131
  if (!resource) {
4113
4132
  return response.status(404).send('Not found');
@@ -4118,7 +4137,7 @@ class MyCustomerService extends AbstractService {
4118
4137
 
4119
4138
  signUp(request, response) {
4120
4139
  const draft = request.body;
4121
- const resource = this.repository.create(request.params.projectKey, draft);
4140
+ const resource = this.repository.create(getRepositoryContext(request), draft);
4122
4141
 
4123
4142
  const result = this._expandWithId(request, resource.id);
4124
4143
 
@@ -4133,7 +4152,7 @@ class MyCustomerService extends AbstractService {
4133
4152
  password
4134
4153
  } = request.body;
4135
4154
  const encodedPassword = Buffer.from(password).toString('base64');
4136
- const result = this.repository.query(request.params.projectKey, {
4155
+ const result = this.repository.query(getRepositoryContext(request), {
4137
4156
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4138
4157
  });
4139
4158
 
@@ -4154,10 +4173,22 @@ class MyCustomerService extends AbstractService {
4154
4173
 
4155
4174
  }
4156
4175
 
4176
+ class MyOrderRepository extends OrderRepository {
4177
+ create(context, draft) {
4178
+ assert(draft.id, 'draft.id is missing');
4179
+ const cartIdentifier = {
4180
+ id: draft.id,
4181
+ typeId: 'cart'
4182
+ };
4183
+ return this.createFromCart(context, cartIdentifier);
4184
+ }
4185
+
4186
+ }
4187
+
4157
4188
  class MyOrderService extends AbstractService {
4158
4189
  constructor(parent, storage) {
4159
4190
  super(parent);
4160
- this.repository = new OrderRepository(storage);
4191
+ this.repository = new MyOrderRepository(storage);
4161
4192
  }
4162
4193
 
4163
4194
  getBasePath() {
@@ -4260,8 +4291,10 @@ class CommercetoolsMock {
4260
4291
 
4261
4292
  if (this.options.enableAuthentication) {
4262
4293
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4294
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4263
4295
  } else {
4264
4296
  app.use('/:projectKey', projectRouter);
4297
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4265
4298
  }
4266
4299
 
4267
4300
  this._projectService = new ProjectService(projectRouter, this._storage);