@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
@@ -957,6 +957,67 @@ const copyHeaders = headers => {
957
957
  const DEFAULT_API_HOSTNAME = /^https:\/\/api\..*?\.commercetools.com:443$/;
958
958
  const DEFAULT_AUTH_HOSTNAME = /^https:\/\/auth\..*?\.commercetools.com:443$/;
959
959
 
960
+ const createCustomFields = (draft, projectKey, storage) => {
961
+ if (!draft) return undefined;
962
+ if (!draft.type) return undefined;
963
+ if (!draft.type.typeId) return undefined;
964
+ if (!draft.fields) return undefined;
965
+ const typeResource = storage.getByResourceIdentifier(projectKey, draft.type);
966
+
967
+ if (!typeResource) {
968
+ throw new Error(`No type '${draft.type.typeId}' with id=${draft.type.id} or key=${draft.type.key}`);
969
+ }
970
+
971
+ return {
972
+ type: {
973
+ typeId: draft.type.typeId,
974
+ id: typeResource.id
975
+ },
976
+ fields: draft.fields
977
+ };
978
+ };
979
+ const createPrice = draft => {
980
+ return {
981
+ id: uuid.v4(),
982
+ value: createTypedMoney(draft.value)
983
+ };
984
+ };
985
+ const createTypedMoney = value => {
986
+ return {
987
+ type: 'centPrecision',
988
+ fractionDigits: 2,
989
+ ...value
990
+ };
991
+ };
992
+ const resolveStoreReference = (ref, projectKey, storage) => {
993
+ if (!ref) return undefined;
994
+ const resource = storage.getByResourceIdentifier(projectKey, ref);
995
+
996
+ if (!resource) {
997
+ throw new Error('No such store');
998
+ }
999
+
1000
+ const store = resource;
1001
+ return {
1002
+ typeId: 'store',
1003
+ key: store.key
1004
+ };
1005
+ };
1006
+ const getReferenceFromResourceIdentifier = (resourceIdentifier, projectKey, storage) => {
1007
+ const resource = storage.getByResourceIdentifier(projectKey, resourceIdentifier);
1008
+ if (!resource) throw new Error(`resource type ${resourceIdentifier.typeId} with id ${resourceIdentifier.id} and key ${resourceIdentifier.key} not found`);
1009
+ return {
1010
+ typeId: resourceIdentifier.typeId,
1011
+ id: resource == null ? void 0 : resource.id
1012
+ };
1013
+ };
1014
+ const getRepositoryContext = request => {
1015
+ return {
1016
+ projectKey: request.params.projectKey,
1017
+ storeKey: request.params.storeKey
1018
+ };
1019
+ };
1020
+
960
1021
  class AbstractService {
961
1022
  constructor(parent) {
962
1023
  this.createStatusCode = 201;
@@ -989,7 +1050,7 @@ class AbstractService {
989
1050
 
990
1051
  const offset = this._parseParam(request.query.offset);
991
1052
 
992
- const result = this.repository.query(request.params.projectKey, {
1053
+ const result = this.repository.query(getRepositoryContext(request), {
993
1054
  expand: this._parseParam(request.query.expand),
994
1055
  where: this._parseParam(request.query.where),
995
1056
  limit: limit !== undefined ? Number(limit) : undefined,
@@ -1009,7 +1070,7 @@ class AbstractService {
1009
1070
  }
1010
1071
 
1011
1072
  getWithKey(request, response) {
1012
- const result = this.repository.getByKey(request.params.projectKey, request.params['key'], {
1073
+ const result = this.repository.getByKey(getRepositoryContext(request), request.params['key'], {
1013
1074
  expand: this._parseParam(request.query.expand)
1014
1075
  });
1015
1076
  if (!result) return response.status(404).send();
@@ -1017,7 +1078,7 @@ class AbstractService {
1017
1078
  }
1018
1079
 
1019
1080
  deletewithId(request, response) {
1020
- const result = this.repository.delete(request.params.projectKey, request.params['id'], {
1081
+ const result = this.repository.delete(getRepositoryContext(request), request.params['id'], {
1021
1082
  expand: this._parseParam(request.query.expand)
1022
1083
  });
1023
1084
 
@@ -1034,7 +1095,7 @@ class AbstractService {
1034
1095
 
1035
1096
  post(request, response) {
1036
1097
  const draft = request.body;
1037
- const resource = this.repository.create(request.params.projectKey, draft);
1098
+ const resource = this.repository.create(getRepositoryContext(request), draft);
1038
1099
 
1039
1100
  const result = this._expandWithId(request, resource.id);
1040
1101
 
@@ -1043,7 +1104,7 @@ class AbstractService {
1043
1104
 
1044
1105
  postWithId(request, response) {
1045
1106
  const updateRequest = request.body;
1046
- const resource = this.repository.get(request.params.projectKey, request.params['id']);
1107
+ const resource = this.repository.get(getRepositoryContext(request), request.params['id']);
1047
1108
 
1048
1109
  if (!resource) {
1049
1110
  return response.status(404).send('Not found');
@@ -1053,7 +1114,7 @@ class AbstractService {
1053
1114
  return response.status(409).send('Concurrent modification');
1054
1115
  }
1055
1116
 
1056
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, resource, updateRequest.actions);
1117
+ const updatedResource = this.repository.processUpdateActions(getRepositoryContext(request), resource, updateRequest.actions);
1057
1118
 
1058
1119
  const result = this._expandWithId(request, updatedResource.id);
1059
1120
 
@@ -1065,7 +1126,7 @@ class AbstractService {
1065
1126
  }
1066
1127
 
1067
1128
  _expandWithId(request, resourceId) {
1068
- const result = this.repository.get(request.params.projectKey, resourceId, {
1129
+ const result = this.repository.get(getRepositoryContext(request), resourceId, {
1069
1130
  expand: this._parseParam(request.query.expand)
1070
1131
  });
1071
1132
  return result;
@@ -1101,7 +1162,7 @@ class AbstractRepository {
1101
1162
  this._storage = storage;
1102
1163
  }
1103
1164
 
1104
- processUpdateActions(projectKey, resource, actions) {
1165
+ processUpdateActions(context, resource, actions) {
1105
1166
  // Deep-copy
1106
1167
  const modifiedResource = JSON.parse(JSON.stringify(resource));
1107
1168
  actions.forEach(action => {
@@ -1112,11 +1173,11 @@ class AbstractRepository {
1112
1173
  return;
1113
1174
  }
1114
1175
 
1115
- updateFunc(projectKey, modifiedResource, action);
1176
+ updateFunc(context, modifiedResource, action);
1116
1177
  });
1117
1178
 
1118
1179
  if (!deepEqual(modifiedResource, resource)) {
1119
- this.save(projectKey, modifiedResource);
1180
+ this.save(context, modifiedResource);
1120
1181
  }
1121
1182
 
1122
1183
  return modifiedResource;
@@ -1130,8 +1191,8 @@ class AbstractResourceRepository extends AbstractRepository {
1130
1191
  this._storage.assertStorage(this.getTypeId());
1131
1192
  }
1132
1193
 
1133
- query(projectKey, params = {}) {
1134
- return this._storage.query(projectKey, this.getTypeId(), {
1194
+ query(context, params = {}) {
1195
+ return this._storage.query(context.projectKey, this.getTypeId(), {
1135
1196
  expand: params.expand,
1136
1197
  where: params.where,
1137
1198
  offset: params.offset,
@@ -1139,20 +1200,20 @@ class AbstractResourceRepository extends AbstractRepository {
1139
1200
  });
1140
1201
  }
1141
1202
 
1142
- get(projectKey, id, params = {}) {
1143
- return this._storage.get(projectKey, this.getTypeId(), id, params);
1203
+ get(context, id, params = {}) {
1204
+ return this._storage.get(context.projectKey, this.getTypeId(), id, params);
1144
1205
  }
1145
1206
 
1146
- getByKey(projectKey, key, params = {}) {
1147
- return this._storage.getByKey(projectKey, this.getTypeId(), key, params);
1207
+ getByKey(context, key, params = {}) {
1208
+ return this._storage.getByKey(context.projectKey, this.getTypeId(), key, params);
1148
1209
  }
1149
1210
 
1150
- delete(projectKey, id, params = {}) {
1151
- return this._storage.delete(projectKey, this.getTypeId(), id, params);
1211
+ delete(context, id, params = {}) {
1212
+ return this._storage.delete(context.projectKey, this.getTypeId(), id, params);
1152
1213
  }
1153
1214
 
1154
- save(projectKey, resource) {
1155
- const current = this.get(projectKey, resource.id);
1215
+ save(context, resource) {
1216
+ const current = this.get(context, resource.id);
1156
1217
 
1157
1218
  if (current) {
1158
1219
  checkConcurrentModification(current, resource.version);
@@ -1168,103 +1229,48 @@ class AbstractResourceRepository extends AbstractRepository {
1168
1229
 
1169
1230
  resource.version += 1;
1170
1231
 
1171
- this._storage.add(projectKey, this.getTypeId(), resource);
1232
+ this._storage.add(context.projectKey, this.getTypeId(), resource);
1172
1233
  }
1173
1234
 
1174
1235
  }
1175
1236
 
1176
- const createCustomFields = (draft, projectKey, storage) => {
1177
- if (!draft) return undefined;
1178
- if (!draft.type) return undefined;
1179
- if (!draft.type.typeId) return undefined;
1180
- if (!draft.fields) return undefined;
1181
- const typeResource = storage.getByResourceIdentifier(projectKey, draft.type);
1182
-
1183
- if (!typeResource) {
1184
- throw new Error(`No type '${draft.type.typeId}' with id=${draft.type.id} or key=${draft.type.key}`);
1185
- }
1186
-
1187
- return {
1188
- type: {
1189
- typeId: draft.type.typeId,
1190
- id: typeResource.id
1191
- },
1192
- fields: draft.fields
1193
- };
1194
- };
1195
- const createPrice = draft => {
1196
- return {
1197
- id: uuid.v4(),
1198
- value: createTypedMoney(draft.value)
1199
- };
1200
- };
1201
- const createTypedMoney = value => {
1202
- return {
1203
- type: 'centPrecision',
1204
- fractionDigits: 2,
1205
- ...value
1206
- };
1207
- };
1208
- const resolveStoreReference = (ref, projectKey, storage) => {
1209
- if (!ref) return undefined;
1210
- const resource = storage.getByResourceIdentifier(projectKey, ref);
1211
-
1212
- if (!resource) {
1213
- throw new Error('No such store');
1214
- }
1215
-
1216
- const store = resource;
1217
- return {
1218
- typeId: 'store',
1219
- key: store.key
1220
- };
1221
- };
1222
- const getReferenceFromResourceIdentifier = (resourceIdentifier, projectKey, storage) => {
1223
- const resource = storage.getByResourceIdentifier(projectKey, resourceIdentifier);
1224
- if (!resource) throw new Error(`resource type ${resourceIdentifier.typeId} with id ${resourceIdentifier.id} and key ${resourceIdentifier.key} not found`);
1225
- return {
1226
- typeId: resourceIdentifier.typeId,
1227
- id: resource == null ? void 0 : resource.id
1228
- };
1229
- };
1230
-
1231
1237
  class CartDiscountRepository extends AbstractResourceRepository {
1232
1238
  constructor() {
1233
1239
  super(...arguments);
1234
1240
  this.actions = {
1235
- setKey: (projectKey, resource, {
1241
+ setKey: (context, resource, {
1236
1242
  key
1237
1243
  }) => {
1238
1244
  resource.key = key;
1239
1245
  },
1240
- setDescription: (projectKey, resource, {
1246
+ setDescription: (context, resource, {
1241
1247
  description
1242
1248
  }) => {
1243
1249
  resource.description = description;
1244
1250
  },
1245
- setValidFrom: (projectKey, resource, {
1251
+ setValidFrom: (context, resource, {
1246
1252
  validFrom
1247
1253
  }) => {
1248
1254
  resource.validFrom = validFrom;
1249
1255
  },
1250
- setValidUntil: (projectKey, resource, {
1256
+ setValidUntil: (context, resource, {
1251
1257
  validUntil
1252
1258
  }) => {
1253
1259
  resource.validUntil = validUntil;
1254
1260
  },
1255
- setValidFromAndUntil: (projectKey, resource, {
1261
+ setValidFromAndUntil: (context, resource, {
1256
1262
  validFrom,
1257
1263
  validUntil
1258
1264
  }) => {
1259
1265
  resource.validFrom = validFrom;
1260
1266
  resource.validUntil = validUntil;
1261
1267
  },
1262
- changeSortOrder: (projectKey, resource, {
1268
+ changeSortOrder: (context, resource, {
1263
1269
  sortOrder
1264
1270
  }) => {
1265
1271
  resource.sortOrder = sortOrder;
1266
1272
  },
1267
- changeIsActive: (projectKey, resource, {
1273
+ changeIsActive: (context, resource, {
1268
1274
  isActive
1269
1275
  }) => {
1270
1276
  resource.isActive = isActive;
@@ -1276,7 +1282,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1276
1282
  return 'cart-discount';
1277
1283
  }
1278
1284
 
1279
- create(projectKey, draft) {
1285
+ create(context, draft) {
1280
1286
  const resource = { ...getBaseResourceProperties(),
1281
1287
  key: draft.key,
1282
1288
  description: draft.description,
@@ -1292,7 +1298,7 @@ class CartDiscountRepository extends AbstractResourceRepository {
1292
1298
  validUntil: draft.validUntil,
1293
1299
  value: this.transformValueDraft(draft.value)
1294
1300
  };
1295
- this.save(projectKey, resource);
1301
+ this.save(context, resource);
1296
1302
  return resource;
1297
1303
  }
1298
1304
 
@@ -1348,7 +1354,7 @@ class CartRepository extends AbstractResourceRepository {
1348
1354
  constructor() {
1349
1355
  super(...arguments);
1350
1356
  this.actions = {
1351
- addLineItem: (projectKey, resource, {
1357
+ addLineItem: (context, resource, {
1352
1358
  productId,
1353
1359
  variantId,
1354
1360
  sku,
@@ -1359,10 +1365,10 @@ class CartRepository extends AbstractResourceRepository {
1359
1365
 
1360
1366
  if (productId && variantId) {
1361
1367
  // Fetch product and variant by ID
1362
- product = this._storage.get(projectKey, 'product', productId, {});
1368
+ product = this._storage.get(context.projectKey, 'product', productId, {});
1363
1369
  } else if (sku) {
1364
1370
  // Fetch product and variant by SKU
1365
- const items = this._storage.query(projectKey, 'product', {
1371
+ const items = this._storage.query(context.projectKey, 'product', {
1366
1372
  where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
1367
1373
  });
1368
1374
 
@@ -1457,7 +1463,7 @@ class CartRepository extends AbstractResourceRepository {
1457
1463
 
1458
1464
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1459
1465
  },
1460
- removeLineItem: (projectKey, resource, {
1466
+ removeLineItem: (context, resource, {
1461
1467
  lineItemId,
1462
1468
  quantity
1463
1469
  }) => {
@@ -1491,15 +1497,15 @@ class CartRepository extends AbstractResourceRepository {
1491
1497
 
1492
1498
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1493
1499
  },
1494
- setBillingAddress: (projectKey, resource, {
1500
+ setBillingAddress: (context, resource, {
1495
1501
  address
1496
1502
  }) => {
1497
1503
  resource.billingAddress = address;
1498
1504
  },
1499
- setShippingMethod: (projectKey, resource, {
1505
+ setShippingMethod: (context, resource, {
1500
1506
  shippingMethod
1501
1507
  }) => {
1502
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
1508
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, //@ts-ignore
1503
1509
  shippingMethod);
1504
1510
 
1505
1511
  if (!resolvedType) {
@@ -1514,17 +1520,17 @@ class CartRepository extends AbstractResourceRepository {
1514
1520
  }
1515
1521
  };
1516
1522
  },
1517
- setCountry: (projectKey, resource, {
1523
+ setCountry: (context, resource, {
1518
1524
  country
1519
1525
  }) => {
1520
1526
  resource.country = country;
1521
1527
  },
1522
- setCustomerEmail: (projectKey, resource, {
1528
+ setCustomerEmail: (context, resource, {
1523
1529
  email
1524
1530
  }) => {
1525
1531
  resource.customerEmail = email;
1526
1532
  },
1527
- setCustomField: (projectKey, resource, {
1533
+ setCustomField: (context, resource, {
1528
1534
  name,
1529
1535
  value
1530
1536
  }) => {
@@ -1534,14 +1540,14 @@ class CartRepository extends AbstractResourceRepository {
1534
1540
 
1535
1541
  resource.custom.fields[name] = value;
1536
1542
  },
1537
- setCustomType: (projectKey, resource, {
1543
+ setCustomType: (context, resource, {
1538
1544
  type,
1539
1545
  fields
1540
1546
  }) => {
1541
1547
  if (!type) {
1542
1548
  resource.custom = undefined;
1543
1549
  } else {
1544
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1550
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1545
1551
 
1546
1552
  if (!resolvedType) {
1547
1553
  throw new Error(`Type ${type} not found`);
@@ -1556,12 +1562,12 @@ class CartRepository extends AbstractResourceRepository {
1556
1562
  };
1557
1563
  }
1558
1564
  },
1559
- setLocale: (projectKey, resource, {
1565
+ setLocale: (context, resource, {
1560
1566
  locale
1561
1567
  }) => {
1562
1568
  resource.locale = locale;
1563
1569
  },
1564
- setShippingAddress: (projectKey, resource, {
1570
+ setShippingAddress: (context, resource, {
1565
1571
  address
1566
1572
  }) => {
1567
1573
  resource.shippingAddress = address;
@@ -1648,10 +1654,10 @@ class CartRepository extends AbstractResourceRepository {
1648
1654
  return 'cart';
1649
1655
  }
1650
1656
 
1651
- create(projectKey, draft) {
1657
+ create(context, draft) {
1652
1658
  var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
1653
1659
 
1654
- const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1660
+ const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
1655
1661
  const resource = { ...getBaseResourceProperties(),
1656
1662
  cartState: 'Active',
1657
1663
  lineItems,
@@ -1669,11 +1675,11 @@ class CartRepository extends AbstractResourceRepository {
1669
1675
  locale: draft.locale,
1670
1676
  country: draft.country,
1671
1677
  origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
1672
- custom: createCustomFields(draft.custom, projectKey, this._storage)
1678
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
1673
1679
  }; // @ts-ignore
1674
1680
 
1675
1681
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1676
- this.save(projectKey, resource);
1682
+ this.save(context, resource);
1677
1683
  return resource;
1678
1684
  }
1679
1685
 
@@ -1719,10 +1725,10 @@ class OrderRepository extends AbstractResourceRepository {
1719
1725
  constructor() {
1720
1726
  super(...arguments);
1721
1727
  this.actions = {
1722
- addPayment: (projectKey, resource, {
1728
+ addPayment: (context, resource, {
1723
1729
  payment
1724
1730
  }) => {
1725
- const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
1731
+ const resolvedPayment = this._storage.getByResourceIdentifier(context.projectKey, payment);
1726
1732
 
1727
1733
  if (!resolvedPayment) {
1728
1734
  throw new Error(`Payment ${payment.id} not found`);
@@ -1739,20 +1745,20 @@ class OrderRepository extends AbstractResourceRepository {
1739
1745
  id: payment.id
1740
1746
  });
1741
1747
  },
1742
- changeOrderState: (projectKey, resource, {
1748
+ changeOrderState: (context, resource, {
1743
1749
  orderState
1744
1750
  }) => {
1745
1751
  resource.orderState = orderState;
1746
1752
  },
1747
- changePaymentState: (projectKey, resource, {
1753
+ changePaymentState: (context, resource, {
1748
1754
  paymentState
1749
1755
  }) => {
1750
1756
  resource.paymentState = paymentState;
1751
1757
  },
1752
- transitionState: (projectKey, resource, {
1758
+ transitionState: (context, resource, {
1753
1759
  state
1754
1760
  }) => {
1755
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
1761
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, state);
1756
1762
 
1757
1763
  if (!resolvedType) {
1758
1764
  throw new Error(`No state found with key=${state.key} or id=${state.key}`);
@@ -1763,17 +1769,17 @@ class OrderRepository extends AbstractResourceRepository {
1763
1769
  id: resolvedType.id
1764
1770
  };
1765
1771
  },
1766
- setBillingAddress: (projectKey, resource, {
1772
+ setBillingAddress: (context, resource, {
1767
1773
  address
1768
1774
  }) => {
1769
1775
  resource.billingAddress = address;
1770
1776
  },
1771
- setCustomerEmail: (projectKey, resource, {
1777
+ setCustomerEmail: (context, resource, {
1772
1778
  email
1773
1779
  }) => {
1774
1780
  resource.customerEmail = email;
1775
1781
  },
1776
- setCustomField: (projectKey, resource, {
1782
+ setCustomField: (context, resource, {
1777
1783
  name,
1778
1784
  value
1779
1785
  }) => {
@@ -1783,14 +1789,14 @@ class OrderRepository extends AbstractResourceRepository {
1783
1789
 
1784
1790
  resource.custom.fields[name] = value;
1785
1791
  },
1786
- setCustomType: (projectKey, resource, {
1792
+ setCustomType: (context, resource, {
1787
1793
  type,
1788
1794
  fields
1789
1795
  }) => {
1790
1796
  if (!type) {
1791
1797
  resource.custom = undefined;
1792
1798
  } else {
1793
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
1799
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
1794
1800
 
1795
1801
  if (!resolvedType) {
1796
1802
  throw new Error(`Type ${type} not found`);
@@ -1805,27 +1811,27 @@ class OrderRepository extends AbstractResourceRepository {
1805
1811
  };
1806
1812
  }
1807
1813
  },
1808
- setLocale: (projectKey, resource, {
1814
+ setLocale: (context, resource, {
1809
1815
  locale
1810
1816
  }) => {
1811
1817
  resource.locale = locale;
1812
1818
  },
1813
- setOrderNumber: (projectKey, resource, {
1819
+ setOrderNumber: (context, resource, {
1814
1820
  orderNumber
1815
1821
  }) => {
1816
1822
  resource.orderNumber = orderNumber;
1817
1823
  },
1818
- setShippingAddress: (projectKey, resource, {
1824
+ setShippingAddress: (context, resource, {
1819
1825
  address
1820
1826
  }) => {
1821
1827
  resource.shippingAddress = address;
1822
1828
  },
1823
- setStore: (projectKey, resource, {
1829
+ setStore: (context, resource, {
1824
1830
  store
1825
1831
  }) => {
1826
1832
  if (!store) return;
1827
1833
 
1828
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
1834
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, store);
1829
1835
 
1830
1836
  if (!resolvedType) {
1831
1837
  throw new Error(`No store found with key=${store.key}`);
@@ -1844,17 +1850,24 @@ class OrderRepository extends AbstractResourceRepository {
1844
1850
  return 'order';
1845
1851
  }
1846
1852
 
1847
- create(projectKey, draft) {
1853
+ create(context, draft) {
1848
1854
  assert(draft.cart, 'draft.cart is missing');
1855
+ return this.createFromCart(context, {
1856
+ id: draft.cart.id,
1857
+ typeId: 'cart'
1858
+ }, draft.orderNumber);
1859
+ }
1849
1860
 
1850
- const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
1861
+ createFromCart(context, cartReference, orderNumber) {
1862
+ const cart = this._storage.getByResourceIdentifier(context.projectKey, cartReference);
1851
1863
 
1852
1864
  if (!cart) {
1853
1865
  throw new Error('Cannot find cart');
1854
1866
  }
1855
1867
 
1856
1868
  const resource = { ...getBaseResourceProperties(),
1857
- orderNumber: draft.orderNumber,
1869
+ orderNumber,
1870
+ cart: cartReference,
1858
1871
  orderState: 'Open',
1859
1872
  lineItems: [],
1860
1873
  customLineItems: [],
@@ -1862,13 +1875,17 @@ class OrderRepository extends AbstractResourceRepository {
1862
1875
  refusedGifts: [],
1863
1876
  origin: 'Customer',
1864
1877
  syncInfo: [],
1878
+ store: context.storeKey ? {
1879
+ key: context.storeKey,
1880
+ typeId: 'store'
1881
+ } : undefined,
1865
1882
  lastMessageSequenceNumber: 0
1866
1883
  };
1867
- this.save(projectKey, resource);
1884
+ this.save(context, resource);
1868
1885
  return resource;
1869
1886
  }
1870
1887
 
1871
- import(projectKey, draft) {
1888
+ import(context, draft) {
1872
1889
  var _draft$lineItems, _draft$customLineItem;
1873
1890
 
1874
1891
  // TODO: Check if order with given orderNumber already exists
@@ -1876,7 +1893,7 @@ class OrderRepository extends AbstractResourceRepository {
1876
1893
  const resource = { ...getBaseResourceProperties(),
1877
1894
  billingAddress: draft.billingAddress,
1878
1895
  shippingAddress: draft.shippingAddress,
1879
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1896
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1880
1897
  customerEmail: draft.customerEmail,
1881
1898
  lastMessageSequenceNumber: 0,
1882
1899
  orderNumber: draft.orderNumber,
@@ -1884,21 +1901,21 @@ class OrderRepository extends AbstractResourceRepository {
1884
1901
  origin: draft.origin || 'Customer',
1885
1902
  paymentState: draft.paymentState,
1886
1903
  refusedGifts: [],
1887
- store: resolveStoreReference(draft.store, projectKey, this._storage),
1904
+ store: resolveStoreReference(draft.store, context.projectKey, this._storage),
1888
1905
  syncInfo: [],
1889
- lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1890
- customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
1906
+ lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(context, item))) || [],
1907
+ customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(context, item))) || [],
1891
1908
  totalPrice: {
1892
1909
  type: 'centPrecision',
1893
1910
  ...draft.totalPrice,
1894
1911
  fractionDigits: 2
1895
1912
  }
1896
1913
  };
1897
- this.save(projectKey, resource);
1914
+ this.save(context, resource);
1898
1915
  return resource;
1899
1916
  }
1900
1917
 
1901
- lineItemFromImportDraft(projectKey, draft) {
1918
+ lineItemFromImportDraft(context, draft) {
1902
1919
  let product;
1903
1920
  let variant;
1904
1921
 
@@ -1908,7 +1925,7 @@ class OrderRepository extends AbstractResourceRepository {
1908
1925
  sku: draft.variant.sku
1909
1926
  };
1910
1927
 
1911
- var items = this._storage.query(projectKey, 'product', {
1928
+ var items = this._storage.query(context.projectKey, 'product', {
1912
1929
  where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
1913
1930
  });
1914
1931
 
@@ -1935,7 +1952,7 @@ class OrderRepository extends AbstractResourceRepository {
1935
1952
  }
1936
1953
 
1937
1954
  const lineItem = { ...getBaseResourceProperties(),
1938
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1955
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1939
1956
  discountedPricePerQuantity: [],
1940
1957
  lineItemMode: 'Standard',
1941
1958
  name: draft.name,
@@ -1956,9 +1973,9 @@ class OrderRepository extends AbstractResourceRepository {
1956
1973
  return lineItem;
1957
1974
  }
1958
1975
 
1959
- customLineItemFromImportDraft(projectKey, draft) {
1976
+ customLineItemFromImportDraft(context, draft) {
1960
1977
  const lineItem = { ...getBaseResourceProperties(),
1961
- custom: createCustomFields(draft.custom, projectKey, this._storage),
1978
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
1962
1979
  discountedPricePerQuantity: [],
1963
1980
  money: createTypedMoney(draft.money),
1964
1981
  name: draft.name,
@@ -1970,8 +1987,8 @@ class OrderRepository extends AbstractResourceRepository {
1970
1987
  return lineItem;
1971
1988
  }
1972
1989
 
1973
- getWithOrderNumber(projectKey, orderNumber, params = {}) {
1974
- const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
1990
+ getWithOrderNumber(context, orderNumber, params = {}) {
1991
+ const result = this._storage.query(context.projectKey, this.getTypeId(), { ...params,
1975
1992
  where: [`orderNumber="${orderNumber}"`]
1976
1993
  });
1977
1994
 
@@ -2002,8 +2019,9 @@ class CartService extends AbstractService {
2002
2019
 
2003
2020
  extraRoutes(parent) {
2004
2021
  parent.post('/replicate', (request, response) => {
2005
- // @ts-ignore
2006
- const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(request.params.projectKey, request.body.reference.id) : this.repository.get(request.params.projectKey, request.body.reference.id);
2022
+ const context = getRepositoryContext(request); // @ts-ignore
2023
+
2024
+ const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(context, request.body.reference.id) : this.repository.get(context, request.body.reference.id);
2007
2025
 
2008
2026
  if (!cartOrOrder) {
2009
2027
  return response.status(400).send();
@@ -2019,7 +2037,7 @@ class CartService extends AbstractService {
2019
2037
  };
2020
2038
  })
2021
2039
  };
2022
- const newCart = this.repository.create(request.params.projectKey, cartDraft);
2040
+ const newCart = this.repository.create(context, cartDraft);
2023
2041
  return response.status(200).send(newCart);
2024
2042
  });
2025
2043
  }
@@ -2030,7 +2048,7 @@ class CategoryRepository extends AbstractResourceRepository {
2030
2048
  constructor() {
2031
2049
  super(...arguments);
2032
2050
  this.actions = {
2033
- changeAssetName: (projectKey, resource, {
2051
+ changeAssetName: (context, resource, {
2034
2052
  assetId,
2035
2053
  assetKey,
2036
2054
  name
@@ -2047,17 +2065,17 @@ class CategoryRepository extends AbstractResourceRepository {
2047
2065
  }
2048
2066
  });
2049
2067
  },
2050
- changeSlug: (projectKey, resource, {
2068
+ changeSlug: (context, resource, {
2051
2069
  slug
2052
2070
  }) => {
2053
2071
  resource.slug = slug;
2054
2072
  },
2055
- setKey: (projectKey, resource, {
2073
+ setKey: (context, resource, {
2056
2074
  key
2057
2075
  }) => {
2058
2076
  resource.key = key;
2059
2077
  },
2060
- setAssetDescription: (projectKey, resource, {
2078
+ setAssetDescription: (context, resource, {
2061
2079
  assetId,
2062
2080
  assetKey,
2063
2081
  description
@@ -2074,7 +2092,7 @@ class CategoryRepository extends AbstractResourceRepository {
2074
2092
  }
2075
2093
  });
2076
2094
  },
2077
- setAssetSources: (projectKey, resource, {
2095
+ setAssetSources: (context, resource, {
2078
2096
  assetId,
2079
2097
  assetKey,
2080
2098
  sources
@@ -2091,22 +2109,22 @@ class CategoryRepository extends AbstractResourceRepository {
2091
2109
  }
2092
2110
  });
2093
2111
  },
2094
- setDescription: (projectKey, resource, {
2112
+ setDescription: (context, resource, {
2095
2113
  description
2096
2114
  }) => {
2097
2115
  resource.description = description;
2098
2116
  },
2099
- setMetaDescription: (projectKey, resource, {
2117
+ setMetaDescription: (context, resource, {
2100
2118
  metaDescription
2101
2119
  }) => {
2102
2120
  resource.metaDescription = metaDescription;
2103
2121
  },
2104
- setMetaKeywords: (projectKey, resource, {
2122
+ setMetaKeywords: (context, resource, {
2105
2123
  metaKeywords
2106
2124
  }) => {
2107
2125
  resource.metaKeywords = metaKeywords;
2108
2126
  },
2109
- setMetaTitle: (projectKey, resource, {
2127
+ setMetaTitle: (context, resource, {
2110
2128
  metaTitle
2111
2129
  }) => {
2112
2130
  resource.metaTitle = metaTitle;
@@ -2118,7 +2136,7 @@ class CategoryRepository extends AbstractResourceRepository {
2118
2136
  return 'category';
2119
2137
  }
2120
2138
 
2121
- create(projectKey, draft) {
2139
+ create(context, draft) {
2122
2140
  var _draft$assets;
2123
2141
 
2124
2142
  const resource = { ...getBaseResourceProperties(),
@@ -2140,11 +2158,11 @@ class CategoryRepository extends AbstractResourceRepository {
2140
2158
  sources: d.sources,
2141
2159
  tags: d.tags,
2142
2160
  key: d.key,
2143
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2161
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2144
2162
  };
2145
2163
  })) || []
2146
2164
  };
2147
- this.save(projectKey, resource);
2165
+ this.save(context, resource);
2148
2166
  return resource;
2149
2167
  }
2150
2168
 
@@ -2167,12 +2185,12 @@ class ChannelRepository extends AbstractResourceRepository {
2167
2185
  return 'channel';
2168
2186
  }
2169
2187
 
2170
- create(projectKey, draft) {
2188
+ create(context, draft) {
2171
2189
  const resource = { ...getBaseResourceProperties(),
2172
2190
  key: draft.key,
2173
2191
  roles: draft.roles || []
2174
2192
  };
2175
- this.save(projectKey, resource);
2193
+ this.save(context, resource);
2176
2194
  return resource;
2177
2195
  }
2178
2196
 
@@ -2194,12 +2212,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2194
2212
  constructor() {
2195
2213
  super(...arguments);
2196
2214
  this.actions = {
2197
- setKey: (projectKey, resource, {
2215
+ setKey: (context, resource, {
2198
2216
  key
2199
2217
  }) => {
2200
2218
  resource.key = key;
2201
2219
  },
2202
- changeName: (projectKey, resource, {
2220
+ changeName: (context, resource, {
2203
2221
  name
2204
2222
  }) => {
2205
2223
  resource.name = name;
@@ -2211,12 +2229,12 @@ class CustomerGroupRepository extends AbstractResourceRepository {
2211
2229
  return 'customer';
2212
2230
  }
2213
2231
 
2214
- create(projectKey, draft) {
2232
+ create(context, draft) {
2215
2233
  const resource = { ...getBaseResourceProperties(),
2216
2234
  key: draft.key,
2217
2235
  name: draft.groupName
2218
2236
  };
2219
- this.save(projectKey, resource);
2237
+ this.save(context, resource);
2220
2238
  return resource;
2221
2239
  }
2222
2240
 
@@ -2238,7 +2256,7 @@ class CustomerRepository extends AbstractResourceRepository {
2238
2256
  constructor() {
2239
2257
  super(...arguments);
2240
2258
  this.actions = {
2241
- changeEmail: (_projectKey, resource, {
2259
+ changeEmail: (_context, resource, {
2242
2260
  email
2243
2261
  }) => {
2244
2262
  resource.email = email;
@@ -2250,19 +2268,19 @@ class CustomerRepository extends AbstractResourceRepository {
2250
2268
  return 'customer';
2251
2269
  }
2252
2270
 
2253
- create(projectKey, draft) {
2271
+ create(context, draft) {
2254
2272
  const resource = { ...getBaseResourceProperties(),
2255
2273
  email: draft.email,
2256
2274
  password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
2257
2275
  isEmailVerified: draft.isEmailVerified || false,
2258
2276
  addresses: []
2259
2277
  };
2260
- this.save(projectKey, resource);
2278
+ this.save(context, resource);
2261
2279
  return resource;
2262
2280
  }
2263
2281
 
2264
- getMe(projectKey) {
2265
- const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
2282
+ getMe(context) {
2283
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {}); // grab the first customer you can find
2266
2284
 
2267
2285
 
2268
2286
  if (results.count > 0) {
@@ -2286,10 +2304,12 @@ class CustomerService extends AbstractService {
2286
2304
 
2287
2305
  extraRoutes(parent) {
2288
2306
  parent.post('/password-token', (request, response) => {
2289
- const customer = this.repository.query(request.params.projectKey, {
2307
+ const customer = this.repository.query(getRepositoryContext(request), {
2290
2308
  where: [`email="${request.body.email}"`]
2291
- });
2292
- const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
2309
+ }); // @ts-ignore
2310
+
2311
+ const ttlMinutes = request.params.ttlMinutes ? // @ts-ignore
2312
+ +request.params.ttlMinutes : 34560;
2293
2313
  const {
2294
2314
  version,
2295
2315
  ...rest
@@ -2309,8 +2329,8 @@ class CustomObjectRepository extends AbstractResourceRepository {
2309
2329
  return 'key-value-document';
2310
2330
  }
2311
2331
 
2312
- create(projectKey, draft) {
2313
- const current = this.getWithContainerAndKey(projectKey, draft.container, draft.key);
2332
+ create(context, draft) {
2333
+ const current = this.getWithContainerAndKey(context, draft.container, draft.key);
2314
2334
  const baseProperties = getBaseResourceProperties();
2315
2335
 
2316
2336
  if (current) {
@@ -2338,12 +2358,12 @@ class CustomObjectRepository extends AbstractResourceRepository {
2338
2358
  key: draft.key,
2339
2359
  value: draft.value
2340
2360
  };
2341
- this.save(projectKey, resource);
2361
+ this.save(context, resource);
2342
2362
  return resource;
2343
2363
  }
2344
2364
 
2345
- getWithContainerAndKey(projectKey, container, key) {
2346
- const items = this._storage.all(projectKey, this.getTypeId());
2365
+ getWithContainerAndKey(context, container, key) {
2366
+ const items = this._storage.all(context.projectKey, this.getTypeId());
2347
2367
 
2348
2368
  return items.find(item => item.container === container && item.key === key);
2349
2369
  }
@@ -2367,7 +2387,7 @@ class CustomObjectService extends AbstractService {
2367
2387
  }
2368
2388
 
2369
2389
  getWithContainerAndKey(request, response) {
2370
- const result = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2390
+ const result = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2371
2391
 
2372
2392
  if (!result) {
2373
2393
  return response.status(404).send('Not Found');
@@ -2381,18 +2401,18 @@ class CustomObjectService extends AbstractService {
2381
2401
  key: request.params.key,
2382
2402
  container: request.params.container
2383
2403
  };
2384
- const result = this.repository.create(request.params.projectKey, draft);
2404
+ const result = this.repository.create(getRepositoryContext(request), draft);
2385
2405
  return response.status(200).send(result);
2386
2406
  }
2387
2407
 
2388
2408
  deleteWithContainerAndKey(request, response) {
2389
- const current = this.repository.getWithContainerAndKey(request.params.projectKey, request.params.container, request.params.key);
2409
+ const current = this.repository.getWithContainerAndKey(getRepositoryContext(request), request.params.container, request.params.key);
2390
2410
 
2391
2411
  if (!current) {
2392
2412
  return response.status(404).send('Not Found');
2393
2413
  }
2394
2414
 
2395
- const result = this.repository.delete(request.params.projectKey, current.id);
2415
+ const result = this.repository.delete(getRepositoryContext(request), current.id);
2396
2416
  return response.status(200).send(result);
2397
2417
  }
2398
2418
 
@@ -2402,12 +2422,12 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2402
2422
  constructor() {
2403
2423
  super(...arguments);
2404
2424
  this.actions = {
2405
- changeIsActive: (projectKey, resource, {
2425
+ changeIsActive: (context, resource, {
2406
2426
  isActive
2407
2427
  }) => {
2408
2428
  resource.isActive = isActive;
2409
2429
  },
2410
- changeCartDiscounts: (projectKey, resource, {
2430
+ changeCartDiscounts: (context, resource, {
2411
2431
  cartDiscounts
2412
2432
  }) => {
2413
2433
  resource.cartDiscounts = cartDiscounts.map(obj => ({
@@ -2415,42 +2435,42 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2415
2435
  id: obj.id
2416
2436
  }));
2417
2437
  },
2418
- setDescription: (projectKey, resource, {
2438
+ setDescription: (context, resource, {
2419
2439
  description
2420
2440
  }) => {
2421
2441
  resource.description = description;
2422
2442
  },
2423
- setCartPredicate: (projectKey, resource, {
2443
+ setCartPredicate: (context, resource, {
2424
2444
  cartPredicate
2425
2445
  }) => {
2426
2446
  resource.cartPredicate = cartPredicate;
2427
2447
  },
2428
- setName: (projectKey, resource, {
2448
+ setName: (context, resource, {
2429
2449
  name
2430
2450
  }) => {
2431
2451
  resource.name = name;
2432
2452
  },
2433
- setMaxApplications: (projectKey, resource, {
2453
+ setMaxApplications: (context, resource, {
2434
2454
  maxApplications
2435
2455
  }) => {
2436
2456
  resource.maxApplications = maxApplications;
2437
2457
  },
2438
- setMaxApplicationsPerCustomer: (projectKey, resource, {
2458
+ setMaxApplicationsPerCustomer: (context, resource, {
2439
2459
  maxApplicationsPerCustomer
2440
2460
  }) => {
2441
2461
  resource.maxApplicationsPerCustomer = maxApplicationsPerCustomer;
2442
2462
  },
2443
- setValidFrom: (projectKey, resource, {
2463
+ setValidFrom: (context, resource, {
2444
2464
  validFrom
2445
2465
  }) => {
2446
2466
  resource.validFrom = validFrom;
2447
2467
  },
2448
- setValidUntil: (projectKey, resource, {
2468
+ setValidUntil: (context, resource, {
2449
2469
  validUntil
2450
2470
  }) => {
2451
2471
  resource.validUntil = validUntil;
2452
2472
  },
2453
- setValidFromAndUntil: (projectKey, resource, {
2473
+ setValidFromAndUntil: (context, resource, {
2454
2474
  validFrom,
2455
2475
  validUntil
2456
2476
  }) => {
@@ -2464,7 +2484,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2464
2484
  return 'cart-discount';
2465
2485
  }
2466
2486
 
2467
- create(projectKey, draft) {
2487
+ create(context, draft) {
2468
2488
  const resource = { ...getBaseResourceProperties(),
2469
2489
  applicationVersion: 1,
2470
2490
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2483,7 +2503,7 @@ class DiscountCodeRepository extends AbstractResourceRepository {
2483
2503
  maxApplications: draft.maxApplications,
2484
2504
  maxApplicationsPerCustomer: draft.maxApplicationsPerCustomer
2485
2505
  };
2486
- this.save(projectKey, resource);
2506
+ this.save(context, resource);
2487
2507
  return resource;
2488
2508
  }
2489
2509
 
@@ -2505,22 +2525,22 @@ class ExtensionRepository extends AbstractResourceRepository {
2505
2525
  constructor() {
2506
2526
  super(...arguments);
2507
2527
  this.actions = {
2508
- setKey: (projectKey, resource, {
2528
+ setKey: (context, resource, {
2509
2529
  key
2510
2530
  }) => {
2511
2531
  resource.key = key;
2512
2532
  },
2513
- setTimeoutInMs: (projectKey, resource, {
2533
+ setTimeoutInMs: (context, resource, {
2514
2534
  timeoutInMs
2515
2535
  }) => {
2516
2536
  resource.timeoutInMs = timeoutInMs;
2517
2537
  },
2518
- changeTriggers: (projectKey, resource, {
2538
+ changeTriggers: (context, resource, {
2519
2539
  triggers
2520
2540
  }) => {
2521
2541
  resource.triggers = triggers;
2522
2542
  },
2523
- changeDestination: (projectKey, resource, {
2543
+ changeDestination: (context, resource, {
2524
2544
  destination
2525
2545
  }) => {
2526
2546
  resource.destination = destination;
@@ -2532,14 +2552,14 @@ class ExtensionRepository extends AbstractResourceRepository {
2532
2552
  return 'extension';
2533
2553
  }
2534
2554
 
2535
- create(projectKey, draft) {
2555
+ create(context, draft) {
2536
2556
  const resource = { ...getBaseResourceProperties(),
2537
2557
  key: draft.key,
2538
2558
  timeoutInMs: draft.timeoutInMs,
2539
2559
  destination: draft.destination,
2540
2560
  triggers: draft.triggers
2541
2561
  };
2542
- this.save(projectKey, resource);
2562
+ this.save(context, resource);
2543
2563
  return resource;
2544
2564
  }
2545
2565
 
@@ -2561,19 +2581,19 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2561
2581
  constructor() {
2562
2582
  super(...arguments);
2563
2583
  this.actions = {
2564
- changeQuantity: (projectKey, resource, {
2584
+ changeQuantity: (context, resource, {
2565
2585
  quantity
2566
2586
  }) => {
2567
2587
  resource.quantityOnStock = quantity; // don't know active reservations so just set to same value
2568
2588
 
2569
2589
  resource.availableQuantity = quantity;
2570
2590
  },
2571
- setExpectedDelivery: (projectKey, resource, {
2591
+ setExpectedDelivery: (context, resource, {
2572
2592
  expectedDelivery
2573
2593
  }) => {
2574
2594
  resource.expectedDelivery = new Date(expectedDelivery).toISOString();
2575
2595
  },
2576
- setCustomField: (projectKey, resource, {
2596
+ setCustomField: (context, resource, {
2577
2597
  name,
2578
2598
  value
2579
2599
  }) => {
@@ -2583,14 +2603,14 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2583
2603
 
2584
2604
  resource.custom.fields[name] = value;
2585
2605
  },
2586
- setCustomType: (projectKey, resource, {
2606
+ setCustomType: (context, resource, {
2587
2607
  type,
2588
2608
  fields
2589
2609
  }) => {
2590
2610
  if (!type) {
2591
2611
  resource.custom = undefined;
2592
2612
  } else {
2593
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2613
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2594
2614
 
2595
2615
  if (!resolvedType) {
2596
2616
  throw new Error(`Type ${type} not found`);
@@ -2605,7 +2625,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2605
2625
  };
2606
2626
  }
2607
2627
  },
2608
- setRestockableInDays: (projectKey, resource, {
2628
+ setRestockableInDays: (context, resource, {
2609
2629
  restockableInDays
2610
2630
  }) => {
2611
2631
  resource.restockableInDays = restockableInDays;
@@ -2617,7 +2637,7 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2617
2637
  return 'inventory-entry';
2618
2638
  }
2619
2639
 
2620
- create(projectKey, draft) {
2640
+ create(context, draft) {
2621
2641
  var _draft$supplyChannel$, _draft$supplyChannel;
2622
2642
 
2623
2643
  const resource = { ...getBaseResourceProperties(),
@@ -2630,9 +2650,9 @@ class InventoryEntryRepository extends AbstractResourceRepository {
2630
2650
  typeId: 'channel',
2631
2651
  id: (_draft$supplyChannel$ = (_draft$supplyChannel = draft.supplyChannel) == null ? void 0 : _draft$supplyChannel.id) != null ? _draft$supplyChannel$ : ''
2632
2652
  },
2633
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2653
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2634
2654
  };
2635
- this.save(projectKey, resource);
2655
+ this.save(context, resource);
2636
2656
  return resource;
2637
2657
  }
2638
2658
 
@@ -2692,14 +2712,14 @@ class PaymentRepository extends AbstractResourceRepository {
2692
2712
  constructor() {
2693
2713
  super(...arguments);
2694
2714
 
2695
- this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
2715
+ this.transactionFromTransactionDraft = (draft, context) => ({ ...draft,
2696
2716
  id: uuid.v4(),
2697
2717
  amount: createTypedMoney(draft.amount),
2698
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2718
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2699
2719
  });
2700
2720
 
2701
2721
  this.actions = {
2702
- setCustomField: (projectKey, resource, {
2722
+ setCustomField: (context, resource, {
2703
2723
  name,
2704
2724
  value
2705
2725
  }) => {
@@ -2709,14 +2729,14 @@ class PaymentRepository extends AbstractResourceRepository {
2709
2729
 
2710
2730
  resource.custom.fields[name] = value;
2711
2731
  },
2712
- setCustomType: (projectKey, resource, {
2732
+ setCustomType: (context, resource, {
2713
2733
  type,
2714
2734
  fields
2715
2735
  }) => {
2716
2736
  if (!type) {
2717
2737
  resource.custom = undefined;
2718
2738
  } else {
2719
- const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
2739
+ const resolvedType = this._storage.getByResourceIdentifier(context.projectKey, type);
2720
2740
 
2721
2741
  if (!resolvedType) {
2722
2742
  throw new Error(`Type ${type} not found`);
@@ -2731,12 +2751,12 @@ class PaymentRepository extends AbstractResourceRepository {
2731
2751
  };
2732
2752
  }
2733
2753
  },
2734
- addTransaction: (projectKey, resource, {
2754
+ addTransaction: (context, resource, {
2735
2755
  transaction
2736
2756
  }) => {
2737
- resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2757
+ resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, context)];
2738
2758
  },
2739
- changeTransactionState: (_projectKey, resource, {
2759
+ changeTransactionState: (_context, resource, {
2740
2760
  transactionId,
2741
2761
  state
2742
2762
  }) => {
@@ -2746,10 +2766,10 @@ class PaymentRepository extends AbstractResourceRepository {
2746
2766
  };
2747
2767
  resource.transactions[index] = updatedTransaction;
2748
2768
  },
2749
- transitionState: (projectKey, resource, {
2769
+ transitionState: (context, resource, {
2750
2770
  state
2751
2771
  }) => {
2752
- const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2772
+ const stateObj = this._storage.getByResourceIdentifier(context.projectKey, state);
2753
2773
 
2754
2774
  if (!stateObj) {
2755
2775
  throw new Error(`State ${state} not found`);
@@ -2768,18 +2788,18 @@ class PaymentRepository extends AbstractResourceRepository {
2768
2788
  return 'payment';
2769
2789
  }
2770
2790
 
2771
- create(projectKey, draft) {
2791
+ create(context, draft) {
2772
2792
  const resource = { ...getBaseResourceProperties(),
2773
2793
  amountPlanned: createTypedMoney(draft.amountPlanned),
2774
2794
  paymentMethodInfo: draft.paymentMethodInfo,
2775
2795
  paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
2776
- state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
2796
+ state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, context.projectKey, this._storage) : undefined
2777
2797
  } : {},
2778
- transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
2779
- interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
2780
- custom: createCustomFields(draft.custom, projectKey, this._storage)
2798
+ transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, context)),
2799
+ interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, context.projectKey, this._storage)),
2800
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
2781
2801
  };
2782
- this.save(projectKey, resource);
2802
+ this.save(context, resource);
2783
2803
  return resource;
2784
2804
  }
2785
2805
 
@@ -2814,12 +2834,12 @@ class OrderService extends AbstractService {
2814
2834
 
2815
2835
  import(request, response) {
2816
2836
  const importDraft = request.body;
2817
- const resource = this.repository.import(request.params.projectKey, importDraft);
2837
+ const resource = this.repository.import(getRepositoryContext(request), importDraft);
2818
2838
  return response.status(200).send(resource);
2819
2839
  }
2820
2840
 
2821
2841
  getWithOrderNumber(request, response) {
2822
- const resource = this.repository.getWithOrderNumber(request.params.projectKey, request.params.orderNumber, request.query);
2842
+ const resource = this.repository.getWithOrderNumber(getRepositoryContext(request), request.params.orderNumber, request.query);
2823
2843
 
2824
2844
  if (resource) {
2825
2845
  return response.status(200).send(resource);
@@ -2871,7 +2891,7 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2871
2891
  return 'product-projection';
2872
2892
  }
2873
2893
 
2874
- create(projectKey, draft) {
2894
+ create(context, draft) {
2875
2895
  var _draft$variants$map, _draft$variants;
2876
2896
 
2877
2897
  if (!draft.masterVariant) {
@@ -2896,17 +2916,17 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2896
2916
  // @ts-ignore
2897
2917
  searchKeywords: draft.searchKeywords
2898
2918
  };
2899
- this.save(projectKey, resource);
2919
+ this.save(context, resource);
2900
2920
  return resource;
2901
2921
  }
2902
2922
 
2903
- search(projectKey, query) {
2923
+ search(context, query) {
2904
2924
  var _query$filterQuery;
2905
2925
 
2906
2926
  const filter = (_query$filterQuery = query['filter.query']) != null ? _query$filterQuery : query.filter;
2907
2927
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2908
2928
 
2909
- const results = this._storage.query(projectKey, this.getTypeId(), {
2929
+ const results = this._storage.query(context.projectKey, this.getTypeId(), {
2910
2930
  where: wherePredicate,
2911
2931
  offset: query.offset ? Number(query.offset) : undefined,
2912
2932
  limit: query.limit ? Number(query.limit) : undefined
@@ -2941,7 +2961,7 @@ class ProductProjectionService extends AbstractService {
2941
2961
  }
2942
2962
 
2943
2963
  search(request, response) {
2944
- const resource = this.repository.search(request.params.projectKey, request.query);
2964
+ const resource = this.repository.search(getRepositoryContext(request), request.query);
2945
2965
  return response.status(200).send(resource);
2946
2966
  }
2947
2967
 
@@ -2951,7 +2971,7 @@ class ProductRepository extends AbstractResourceRepository {
2951
2971
  constructor() {
2952
2972
  super(...arguments);
2953
2973
  this.actions = {
2954
- publish: (projectKey, resource, {
2974
+ publish: (context, resource, {
2955
2975
  scope
2956
2976
  }) => {
2957
2977
  if (resource.masterData.staged) {
@@ -2963,7 +2983,7 @@ class ProductRepository extends AbstractResourceRepository {
2963
2983
  resource.masterData.hasStagedChanges = false;
2964
2984
  resource.masterData.published = true;
2965
2985
  },
2966
- setAttribute: (projectKey, resource, {
2986
+ setAttribute: (context, resource, {
2967
2987
  variantId,
2968
2988
  sku,
2969
2989
  name,
@@ -3024,7 +3044,7 @@ class ProductRepository extends AbstractResourceRepository {
3024
3044
  return 'product';
3025
3045
  }
3026
3046
 
3027
- create(projectKey, draft) {
3047
+ create(context, draft) {
3028
3048
  var _draft$publish, _draft$publish2;
3029
3049
 
3030
3050
  const productData = {
@@ -3048,7 +3068,7 @@ class ProductRepository extends AbstractResourceRepository {
3048
3068
  published: (_draft$publish2 = draft.publish) != null ? _draft$publish2 : false
3049
3069
  }
3050
3070
  };
3051
- this.save(projectKey, resource);
3071
+ this.save(context, resource);
3052
3072
  return resource;
3053
3073
  }
3054
3074
 
@@ -3108,7 +3128,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3108
3128
  constructor() {
3109
3129
  super(...arguments);
3110
3130
 
3111
- this.attributeDefinitionFromAttributeDefinitionDraft = (_projectKey, draft) => {
3131
+ this.attributeDefinitionFromAttributeDefinitionDraft = (_context, draft) => {
3112
3132
  var _draft$attributeConst, _draft$inputHint, _draft$isSearchable;
3113
3133
 
3114
3134
  return { ...draft,
@@ -3119,7 +3139,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3119
3139
  };
3120
3140
 
3121
3141
  this.actions = {
3122
- changeLocalizedEnumValueLabel: (projectKey, resource, {
3142
+ changeLocalizedEnumValueLabel: (context, resource, {
3123
3143
  attributeName,
3124
3144
  newValue
3125
3145
  }) => {
@@ -3147,7 +3167,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
3147
3167
  }
3148
3168
  });
3149
3169
  },
3150
- changeLabel: (projectKey, resource, {
3170
+ changeLabel: (context, resource, {
3151
3171
  attributeName,
3152
3172
  label
3153
3173
  }) => {
@@ -3166,21 +3186,21 @@ class ProductTypeRepository extends AbstractResourceRepository {
3166
3186
  return 'product-type';
3167
3187
  }
3168
3188
 
3169
- create(projectKey, draft) {
3189
+ create(context, draft) {
3170
3190
  var _draft$attributes;
3171
3191
 
3172
3192
  const resource = { ...getBaseResourceProperties(),
3173
3193
  key: draft.key,
3174
3194
  name: draft.name,
3175
3195
  description: draft.description,
3176
- attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(projectKey, a))
3196
+ attributes: ((_draft$attributes = draft.attributes) != null ? _draft$attributes : []).map(a => this.attributeDefinitionFromAttributeDefinitionDraft(context, a))
3177
3197
  };
3178
- this.save(projectKey, resource);
3198
+ this.save(context, resource);
3179
3199
  return resource;
3180
3200
  }
3181
3201
 
3182
- getWithKey(projectKey, key) {
3183
- const result = this._storage.query(projectKey, this.getTypeId(), {
3202
+ getWithKey(context, key) {
3203
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3184
3204
  where: [`key="${key}"`]
3185
3205
  });
3186
3206
 
@@ -3213,7 +3233,7 @@ class ProductTypeService extends AbstractService {
3213
3233
  }
3214
3234
 
3215
3235
  getWithKey(request, response) {
3216
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3236
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3217
3237
 
3218
3238
  if (resource) {
3219
3239
  return response.status(200).send(resource);
@@ -3250,32 +3270,32 @@ class ProjectRepository extends AbstractRepository {
3250
3270
  constructor() {
3251
3271
  super(...arguments);
3252
3272
  this.actions = {
3253
- changeName: (projectKey, resource, {
3273
+ changeName: (context, resource, {
3254
3274
  name
3255
3275
  }) => {
3256
3276
  resource.name = name;
3257
3277
  },
3258
- changeCurrencies: (projectKey, resource, {
3278
+ changeCurrencies: (context, resource, {
3259
3279
  currencies
3260
3280
  }) => {
3261
3281
  resource.currencies = currencies;
3262
3282
  },
3263
- changeCountries: (projectKey, resource, {
3283
+ changeCountries: (context, resource, {
3264
3284
  countries
3265
3285
  }) => {
3266
3286
  resource.countries = countries;
3267
3287
  },
3268
- changeLanguages: (projectKey, resource, {
3288
+ changeLanguages: (context, resource, {
3269
3289
  languages
3270
3290
  }) => {
3271
3291
  resource.languages = languages;
3272
3292
  },
3273
- changeMessagesEnabled: (projectKey, resource, {
3293
+ changeMessagesEnabled: (context, resource, {
3274
3294
  messagesEnabled
3275
3295
  }) => {
3276
3296
  resource.messages.enabled = messagesEnabled;
3277
3297
  },
3278
- changeProductSearchIndexingEnabled: (projectKey, resource, {
3298
+ changeProductSearchIndexingEnabled: (context, resource, {
3279
3299
  enabled
3280
3300
  }) => {
3281
3301
  var _resource$searchIndex;
@@ -3287,7 +3307,7 @@ class ProjectRepository extends AbstractRepository {
3287
3307
  resource.searchIndexing.products.status = enabled ? 'Activated' : 'Deactivated';
3288
3308
  resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
3289
3309
  },
3290
- changeOrderSearchStatus: (projectKey, resource, {
3310
+ changeOrderSearchStatus: (context, resource, {
3291
3311
  status
3292
3312
  }) => {
3293
3313
  var _resource$searchIndex2;
@@ -3299,22 +3319,22 @@ class ProjectRepository extends AbstractRepository {
3299
3319
  resource.searchIndexing.orders.status = status;
3300
3320
  resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
3301
3321
  },
3302
- setShippingRateInputType: (projectKey, resource, {
3322
+ setShippingRateInputType: (context, resource, {
3303
3323
  shippingRateInputType
3304
3324
  }) => {
3305
3325
  resource.shippingRateInputType = shippingRateInputType;
3306
3326
  },
3307
- setExternalOAuth: (projectKey, resource, {
3327
+ setExternalOAuth: (context, resource, {
3308
3328
  externalOAuth
3309
3329
  }) => {
3310
3330
  resource.externalOAuth = externalOAuth;
3311
3331
  },
3312
- changeCountryTaxRateFallbackEnabled: (projectKey, resource, {
3332
+ changeCountryTaxRateFallbackEnabled: (context, resource, {
3313
3333
  countryTaxRateFallbackEnabled
3314
3334
  }) => {
3315
3335
  resource.carts.countryTaxRateFallbackEnabled = countryTaxRateFallbackEnabled;
3316
3336
  },
3317
- changeCartsConfiguration: (projectKey, resource, {
3337
+ changeCartsConfiguration: (context, resource, {
3318
3338
  cartsConfiguration
3319
3339
  }) => {
3320
3340
  resource.carts = cartsConfiguration || {
@@ -3325,15 +3345,15 @@ class ProjectRepository extends AbstractRepository {
3325
3345
  };
3326
3346
  }
3327
3347
 
3328
- get(projectKey) {
3329
- const resource = this._storage.getProject(projectKey);
3348
+ get(context) {
3349
+ const resource = this._storage.getProject(context.projectKey);
3330
3350
 
3331
3351
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
3332
3352
  return masked;
3333
3353
  }
3334
3354
 
3335
- save(projectKey, resource) {
3336
- const current = this.get(projectKey);
3355
+ save(context, resource) {
3356
+ const current = this.get(context);
3337
3357
 
3338
3358
  if (current) {
3339
3359
  checkConcurrentModification(current, resource.version);
@@ -3366,20 +3386,19 @@ class ProjectService {
3366
3386
  }
3367
3387
 
3368
3388
  get(request, response) {
3369
- const projectKey = request.params.projectKey;
3370
- const project = this.repository.get(projectKey);
3389
+ const project = this.repository.get(getRepositoryContext(request));
3371
3390
  return response.status(200).send(project);
3372
3391
  }
3373
3392
 
3374
3393
  post(request, response) {
3375
3394
  const updateRequest = request.body;
3376
- const project = this.repository.get(request.params.projectKey);
3395
+ const project = this.repository.get(getRepositoryContext(request));
3377
3396
 
3378
3397
  if (!project) {
3379
3398
  return response.status(404).send({});
3380
3399
  }
3381
3400
 
3382
- this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3401
+ this.repository.processUpdateActions(getRepositoryContext(request), project, updateRequest.actions);
3383
3402
  return response.status(200).send({});
3384
3403
  }
3385
3404
 
@@ -3389,11 +3408,11 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3389
3408
  constructor() {
3390
3409
  super(...arguments);
3391
3410
 
3392
- this._transformZoneRateDraft = (projectKey, draft) => {
3411
+ this._transformZoneRateDraft = (context, draft) => {
3393
3412
  var _draft$shippingRates;
3394
3413
 
3395
3414
  return { ...draft,
3396
- zone: getReferenceFromResourceIdentifier(draft.zone, projectKey, this._storage),
3415
+ zone: getReferenceFromResourceIdentifier(draft.zone, context.projectKey, this._storage),
3397
3416
  shippingRates: (_draft$shippingRates = draft.shippingRates) == null ? void 0 : _draft$shippingRates.map(this._transformShippingRate)
3398
3417
  };
3399
3418
  };
@@ -3407,7 +3426,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3407
3426
  };
3408
3427
 
3409
3428
  this.actions = {
3410
- addShippingRate: (_projectKey, resource, {
3429
+ addShippingRate: (_context, resource, {
3411
3430
  shippingRate,
3412
3431
  zone
3413
3432
  }) => {
@@ -3427,7 +3446,7 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3427
3446
  shippingRates: [rate]
3428
3447
  });
3429
3448
  },
3430
- removeShippingRate: (_projectKey, resource, {
3449
+ removeShippingRate: (_context, resource, {
3431
3450
  shippingRate,
3432
3451
  zone
3433
3452
  }) => {
@@ -3441,10 +3460,10 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3441
3460
  }
3442
3461
  });
3443
3462
  },
3444
- addZone: (projectKey, resource, {
3463
+ addZone: (context, resource, {
3445
3464
  zone
3446
3465
  }) => {
3447
- const zoneReference = getReferenceFromResourceIdentifier(zone, projectKey, this._storage);
3466
+ const zoneReference = getReferenceFromResourceIdentifier(zone, context.projectKey, this._storage);
3448
3467
 
3449
3468
  if (resource.zoneRates === undefined) {
3450
3469
  resource.zoneRates = [];
@@ -3455,39 +3474,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3455
3474
  shippingRates: []
3456
3475
  });
3457
3476
  },
3458
- removeZone: (_projectKey, resource, {
3477
+ removeZone: (_context, resource, {
3459
3478
  zone
3460
3479
  }) => {
3461
3480
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3462
3481
  return zoneRate.zone.id !== zone.id;
3463
3482
  });
3464
3483
  },
3465
- setKey: (_projectKey, resource, {
3484
+ setKey: (_context, resource, {
3466
3485
  key
3467
3486
  }) => {
3468
3487
  resource.key = key;
3469
3488
  },
3470
- setDescription: (_projectKey, resource, {
3489
+ setDescription: (_context, resource, {
3471
3490
  description
3472
3491
  }) => {
3473
3492
  resource.description = description;
3474
3493
  },
3475
- setLocalizedDescription: (_projectKey, resource, {
3494
+ setLocalizedDescription: (_context, resource, {
3476
3495
  localizedDescription
3477
3496
  }) => {
3478
3497
  resource.localizedDescription = localizedDescription;
3479
3498
  },
3480
- setPredicate: (_projectKey, resource, {
3499
+ setPredicate: (_context, resource, {
3481
3500
  predicate
3482
3501
  }) => {
3483
3502
  resource.predicate = predicate;
3484
3503
  },
3485
- changeIsDefault: (_projectKey, resource, {
3504
+ changeIsDefault: (_context, resource, {
3486
3505
  isDefault
3487
3506
  }) => {
3488
3507
  resource.isDefault = isDefault;
3489
3508
  },
3490
- changeName: (_projectKey, resource, {
3509
+ changeName: (_context, resource, {
3491
3510
  name
3492
3511
  }) => {
3493
3512
  resource.name = name;
@@ -3499,16 +3518,16 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3499
3518
  return 'shipping-method';
3500
3519
  }
3501
3520
 
3502
- create(projectKey, draft) {
3521
+ create(context, draft) {
3503
3522
  var _draft$zoneRates;
3504
3523
 
3505
3524
  const resource = { ...getBaseResourceProperties(),
3506
3525
  ...draft,
3507
- taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, this._storage),
3508
- zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(projectKey, z)),
3509
- custom: createCustomFields(draft.custom, projectKey, this._storage)
3526
+ taxCategory: getReferenceFromResourceIdentifier(draft.taxCategory, context.projectKey, this._storage),
3527
+ zoneRates: (_draft$zoneRates = draft.zoneRates) == null ? void 0 : _draft$zoneRates.map(z => this._transformZoneRateDraft(context, z)),
3528
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage)
3510
3529
  };
3511
- this.save(projectKey, resource);
3530
+ this.save(context, resource);
3512
3531
  return resource;
3513
3532
  }
3514
3533
 
@@ -3536,13 +3555,13 @@ class ShoppingListRepository extends AbstractResourceRepository {
3536
3555
  return 'shopping-list';
3537
3556
  }
3538
3557
 
3539
- create(projectKey, draft) {
3558
+ create(context, draft) {
3540
3559
  var _draft$lineItems, _draft$store;
3541
3560
 
3542
3561
  // const product =
3543
3562
  const resource = { ...getBaseResourceProperties(),
3544
3563
  ...draft,
3545
- custom: createCustomFields(draft.custom, projectKey, this._storage),
3564
+ custom: createCustomFields(draft.custom, context.projectKey, this._storage),
3546
3565
  textLineItems: [],
3547
3566
  lineItems: (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(e => {
3548
3567
  var _e$addedAt, _e$productId, _e$quantity;
@@ -3557,16 +3576,16 @@ class ShoppingListRepository extends AbstractResourceRepository {
3557
3576
  typeId: 'product-type',
3558
3577
  id: ''
3559
3578
  },
3560
- custom: createCustomFields(e.custom, projectKey, this._storage)
3579
+ custom: createCustomFields(e.custom, context.projectKey, this._storage)
3561
3580
  };
3562
3581
  }),
3563
- customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, projectKey, this._storage) : undefined,
3582
+ customer: draft.customer ? getReferenceFromResourceIdentifier(draft.customer, context.projectKey, this._storage) : undefined,
3564
3583
  store: (_draft$store = draft.store) != null && _draft$store.key ? {
3565
3584
  typeId: 'store',
3566
3585
  key: draft.store.key
3567
3586
  } : undefined
3568
3587
  };
3569
- this.save(projectKey, resource);
3588
+ this.save(context, resource);
3570
3589
  return resource;
3571
3590
  }
3572
3591
 
@@ -3588,22 +3607,22 @@ class StateRepository extends AbstractResourceRepository {
3588
3607
  constructor() {
3589
3608
  super(...arguments);
3590
3609
  this.actions = {
3591
- changeKey: (projectKey, resource, {
3610
+ changeKey: (context, resource, {
3592
3611
  key
3593
3612
  }) => {
3594
3613
  resource.key = key;
3595
3614
  },
3596
- setDescription: (projectKey, resource, {
3615
+ setDescription: (context, resource, {
3597
3616
  description
3598
3617
  }) => {
3599
3618
  resource.description = description;
3600
3619
  },
3601
- setName: (projectKey, resource, {
3620
+ setName: (context, resource, {
3602
3621
  name
3603
3622
  }) => {
3604
3623
  resource.name = name;
3605
3624
  },
3606
- setRoles: (projectKey, resource, {
3625
+ setRoles: (context, resource, {
3607
3626
  roles
3608
3627
  }) => {
3609
3628
  resource.roles = roles;
@@ -3615,14 +3634,14 @@ class StateRepository extends AbstractResourceRepository {
3615
3634
  return 'state';
3616
3635
  }
3617
3636
 
3618
- create(projectKey, draft) {
3637
+ create(context, draft) {
3619
3638
  const resource = { ...getBaseResourceProperties(),
3620
3639
  ...draft,
3621
3640
  builtIn: false,
3622
3641
  initial: draft.initial || false,
3623
- transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, projectKey, this._storage))
3642
+ transitions: (draft.transitions || []).map(t => getReferenceFromResourceIdentifier(t, context.projectKey, this._storage))
3624
3643
  };
3625
- this.save(projectKey, resource);
3644
+ this.save(context, resource);
3626
3645
  return resource;
3627
3646
  }
3628
3647
 
@@ -3644,17 +3663,17 @@ class StoreRepository extends AbstractResourceRepository {
3644
3663
  constructor() {
3645
3664
  super(...arguments);
3646
3665
  this.actions = {
3647
- setName: (projectKey, resource, {
3666
+ setName: (context, resource, {
3648
3667
  name
3649
3668
  }) => {
3650
3669
  resource.name = name;
3651
3670
  },
3652
- setDistributionChannels: (projectKey, resource, {
3671
+ setDistributionChannels: (context, resource, {
3653
3672
  distributionChannels
3654
3673
  }) => {
3655
- resource.distributionChannels = this.transformChannels(projectKey, distributionChannels);
3674
+ resource.distributionChannels = this.transformChannels(context, distributionChannels);
3656
3675
  },
3657
- setLanguages: (projectKey, resource, {
3676
+ setLanguages: (context, resource, {
3658
3677
  languages
3659
3678
  }) => {
3660
3679
  resource.languages = languages;
@@ -3666,25 +3685,25 @@ class StoreRepository extends AbstractResourceRepository {
3666
3685
  return 'store';
3667
3686
  }
3668
3687
 
3669
- create(projectKey, draft) {
3688
+ create(context, draft) {
3670
3689
  const resource = { ...getBaseResourceProperties(),
3671
3690
  key: draft.key,
3672
3691
  name: draft.name,
3673
3692
  languages: draft.languages,
3674
- distributionChannels: this.transformChannels(projectKey, draft.distributionChannels),
3675
- supplyChannels: this.transformChannels(projectKey, draft.supplyChannels)
3693
+ distributionChannels: this.transformChannels(context, draft.distributionChannels),
3694
+ supplyChannels: this.transformChannels(context, draft.supplyChannels)
3676
3695
  };
3677
- this.save(projectKey, resource);
3696
+ this.save(context, resource);
3678
3697
  return resource;
3679
3698
  }
3680
3699
 
3681
- transformChannels(projectKey, channels) {
3700
+ transformChannels(context, channels) {
3682
3701
  if (!channels) return [];
3683
- return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3702
+ return channels.map(ref => getReferenceFromResourceIdentifier(ref, context.projectKey, this._storage));
3684
3703
  }
3685
3704
 
3686
- getWithKey(projectKey, key) {
3687
- const result = this._storage.query(projectKey, this.getTypeId(), {
3705
+ getWithKey(context, key) {
3706
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3688
3707
  where: [`key="${key}"`]
3689
3708
  });
3690
3709
 
@@ -3716,7 +3735,7 @@ class StoreService extends AbstractService {
3716
3735
  }
3717
3736
 
3718
3737
  getWithKey(request, response) {
3719
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3738
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3720
3739
 
3721
3740
  if (resource) {
3722
3741
  return response.status(200).send(resource);
@@ -3732,7 +3751,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3732
3751
  return 'subscription';
3733
3752
  }
3734
3753
 
3735
- create(projectKey, draft) {
3754
+ create(context, draft) {
3736
3755
  // TODO: We could actually test this here by using the aws sdk. For now
3737
3756
  // hardcode a failed check when account id is 0000000000
3738
3757
  if (draft.destination.type === 'SQS') {
@@ -3758,7 +3777,7 @@ class SubscriptionRepository extends AbstractResourceRepository {
3758
3777
  messages: draft.messages || [],
3759
3778
  status: 'Healthy'
3760
3779
  };
3761
- this.save(projectKey, resource);
3780
+ this.save(context, resource);
3762
3781
  return resource;
3763
3782
  }
3764
3783
 
@@ -3786,7 +3805,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3786
3805
  });
3787
3806
 
3788
3807
  this.actions = {
3789
- addTaxRate: (projectKey, resource, {
3808
+ addTaxRate: (context, resource, {
3790
3809
  taxRate
3791
3810
  }) => {
3792
3811
  if (resource.rates === undefined) {
@@ -3795,7 +3814,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3795
3814
 
3796
3815
  resource.rates.push(this.taxRateFromTaxRateDraft(taxRate));
3797
3816
  },
3798
- removeTaxRate: (projectKey, resource, {
3817
+ removeTaxRate: (context, resource, {
3799
3818
  taxRateId
3800
3819
  }) => {
3801
3820
  if (resource.rates === undefined) {
@@ -3806,7 +3825,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3806
3825
  return taxRate.id !== taxRateId;
3807
3826
  });
3808
3827
  },
3809
- replaceTaxRate: (projectKey, resource, {
3828
+ replaceTaxRate: (context, resource, {
3810
3829
  taxRateId,
3811
3830
  taxRate
3812
3831
  }) => {
@@ -3824,17 +3843,17 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3824
3843
  }
3825
3844
  }
3826
3845
  },
3827
- setDescription: (projectKey, resource, {
3846
+ setDescription: (context, resource, {
3828
3847
  description
3829
3848
  }) => {
3830
3849
  resource.description = description;
3831
3850
  },
3832
- setKey: (projectKey, resource, {
3851
+ setKey: (context, resource, {
3833
3852
  key
3834
3853
  }) => {
3835
3854
  resource.key = key;
3836
3855
  },
3837
- changeName: (projectKey, resource, {
3856
+ changeName: (context, resource, {
3838
3857
  name
3839
3858
  }) => {
3840
3859
  resource.name = name;
@@ -3846,19 +3865,19 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3846
3865
  return 'tax-category';
3847
3866
  }
3848
3867
 
3849
- create(projectKey, draft) {
3868
+ create(context, draft) {
3850
3869
  var _draft$rates;
3851
3870
 
3852
3871
  const resource = { ...getBaseResourceProperties(),
3853
3872
  ...draft,
3854
3873
  rates: ((_draft$rates = draft.rates) == null ? void 0 : _draft$rates.map(this.taxRateFromTaxRateDraft)) || []
3855
3874
  };
3856
- this.save(projectKey, resource);
3875
+ this.save(context, resource);
3857
3876
  return resource;
3858
3877
  }
3859
3878
 
3860
- getWithKey(projectKey, key) {
3861
- const result = this._storage.query(projectKey, this.getTypeId(), {
3879
+ getWithKey(context, key) {
3880
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
3862
3881
  where: [`key="${key}"`]
3863
3882
  });
3864
3883
 
@@ -3891,7 +3910,7 @@ class TaxCategoryService extends AbstractService {
3891
3910
  }
3892
3911
 
3893
3912
  getWithKey(request, response) {
3894
- const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3913
+ const resource = this.repository.getWithKey(getRepositoryContext(request), request.params.key);
3895
3914
 
3896
3915
  if (resource) {
3897
3916
  return response.status(200).send(resource);
@@ -3906,29 +3925,29 @@ class TypeRepository extends AbstractResourceRepository {
3906
3925
  constructor() {
3907
3926
  super(...arguments);
3908
3927
  this.actions = {
3909
- addFieldDefinition: (projectKey, resource, {
3928
+ addFieldDefinition: (context, resource, {
3910
3929
  fieldDefinition
3911
3930
  }) => {
3912
3931
  resource.fieldDefinitions.push(fieldDefinition);
3913
3932
  },
3914
- removeFieldDefinition: (projectKey, resource, {
3933
+ removeFieldDefinition: (context, resource, {
3915
3934
  fieldName
3916
3935
  }) => {
3917
3936
  resource.fieldDefinitions = resource.fieldDefinitions.filter(f => {
3918
3937
  return f.name !== fieldName;
3919
3938
  });
3920
3939
  },
3921
- setDescription: (projectKey, resource, {
3940
+ setDescription: (context, resource, {
3922
3941
  description
3923
3942
  }) => {
3924
3943
  resource.description = description;
3925
3944
  },
3926
- changeName: (projectKey, resource, {
3945
+ changeName: (context, resource, {
3927
3946
  name
3928
3947
  }) => {
3929
3948
  resource.name = name;
3930
3949
  },
3931
- changeFieldDefinitionOrder: (projectKey, resource, {
3950
+ changeFieldDefinitionOrder: (context, resource, {
3932
3951
  fieldNames
3933
3952
  }) => {
3934
3953
  const fields = new Map(resource.fieldDefinitions.map(item => [item.name, item]));
@@ -3952,7 +3971,7 @@ class TypeRepository extends AbstractResourceRepository {
3952
3971
 
3953
3972
  resource.fieldDefinitions.push(...current);
3954
3973
  },
3955
- addEnumValue: (projectKey, resource, {
3974
+ addEnumValue: (context, resource, {
3956
3975
  fieldName,
3957
3976
  value
3958
3977
  }) => {
@@ -3969,7 +3988,7 @@ class TypeRepository extends AbstractResourceRepository {
3969
3988
  }
3970
3989
  });
3971
3990
  },
3972
- changeEnumValueLabel: (projectKey, resource, {
3991
+ changeEnumValueLabel: (context, resource, {
3973
3992
  fieldName,
3974
3993
  value
3975
3994
  }) => {
@@ -4001,7 +4020,7 @@ class TypeRepository extends AbstractResourceRepository {
4001
4020
  return 'type';
4002
4021
  }
4003
4022
 
4004
- create(projectKey, draft) {
4023
+ create(context, draft) {
4005
4024
  const resource = { ...getBaseResourceProperties(),
4006
4025
  key: draft.key,
4007
4026
  name: draft.name,
@@ -4009,7 +4028,7 @@ class TypeRepository extends AbstractResourceRepository {
4009
4028
  fieldDefinitions: draft.fieldDefinitions || [],
4010
4029
  description: draft.description
4011
4030
  };
4012
- this.save(projectKey, resource);
4031
+ this.save(context, resource);
4013
4032
  return resource;
4014
4033
  }
4015
4034
 
@@ -4031,29 +4050,29 @@ class ZoneRepository extends AbstractResourceRepository {
4031
4050
  constructor() {
4032
4051
  super(...arguments);
4033
4052
  this.actions = {
4034
- addLocation: (projectKey, resource, {
4053
+ addLocation: (context, resource, {
4035
4054
  location
4036
4055
  }) => {
4037
4056
  resource.locations.push(location);
4038
4057
  },
4039
- removeLocation: (projectKey, resource, {
4058
+ removeLocation: (context, resource, {
4040
4059
  location
4041
4060
  }) => {
4042
4061
  resource.locations = resource.locations.filter(loc => {
4043
4062
  return !(loc.country === location.country && loc.state === location.state);
4044
4063
  });
4045
4064
  },
4046
- changeName: (projectKey, resource, {
4065
+ changeName: (context, resource, {
4047
4066
  name
4048
4067
  }) => {
4049
4068
  resource.name = name;
4050
4069
  },
4051
- setDescription: (projectKey, resource, {
4070
+ setDescription: (context, resource, {
4052
4071
  description
4053
4072
  }) => {
4054
4073
  resource.description = description;
4055
4074
  },
4056
- setKey: (projectKey, resource, {
4075
+ setKey: (context, resource, {
4057
4076
  key
4058
4077
  }) => {
4059
4078
  resource.key = key;
@@ -4065,14 +4084,14 @@ class ZoneRepository extends AbstractResourceRepository {
4065
4084
  return 'zone';
4066
4085
  }
4067
4086
 
4068
- create(projectKey, draft) {
4087
+ create(context, draft) {
4069
4088
  const resource = { ...getBaseResourceProperties(),
4070
4089
  key: draft.key,
4071
4090
  locations: draft.locations || [],
4072
4091
  name: draft.name,
4073
4092
  description: draft.description
4074
4093
  };
4075
- this.save(projectKey, resource);
4094
+ this.save(context, resource);
4076
4095
  return resource;
4077
4096
  }
4078
4097
 
@@ -4114,7 +4133,7 @@ class MyCustomerService extends AbstractService {
4114
4133
  }
4115
4134
 
4116
4135
  getMe(request, response) {
4117
- const resource = this.repository.getMe(request.params.projectKey);
4136
+ const resource = this.repository.getMe(getRepositoryContext(request));
4118
4137
 
4119
4138
  if (!resource) {
4120
4139
  return response.status(404).send('Not found');
@@ -4125,7 +4144,7 @@ class MyCustomerService extends AbstractService {
4125
4144
 
4126
4145
  signUp(request, response) {
4127
4146
  const draft = request.body;
4128
- const resource = this.repository.create(request.params.projectKey, draft);
4147
+ const resource = this.repository.create(getRepositoryContext(request), draft);
4129
4148
 
4130
4149
  const result = this._expandWithId(request, resource.id);
4131
4150
 
@@ -4140,7 +4159,7 @@ class MyCustomerService extends AbstractService {
4140
4159
  password
4141
4160
  } = request.body;
4142
4161
  const encodedPassword = Buffer.from(password).toString('base64');
4143
- const result = this.repository.query(request.params.projectKey, {
4162
+ const result = this.repository.query(getRepositoryContext(request), {
4144
4163
  where: [`email = "${email}"`, `password = "${encodedPassword}"`]
4145
4164
  });
4146
4165
 
@@ -4161,10 +4180,22 @@ class MyCustomerService extends AbstractService {
4161
4180
 
4162
4181
  }
4163
4182
 
4183
+ class MyOrderRepository extends OrderRepository {
4184
+ create(context, draft) {
4185
+ assert(draft.id, 'draft.id is missing');
4186
+ const cartIdentifier = {
4187
+ id: draft.id,
4188
+ typeId: 'cart'
4189
+ };
4190
+ return this.createFromCart(context, cartIdentifier);
4191
+ }
4192
+
4193
+ }
4194
+
4164
4195
  class MyOrderService extends AbstractService {
4165
4196
  constructor(parent, storage) {
4166
4197
  super(parent);
4167
- this.repository = new OrderRepository(storage);
4198
+ this.repository = new MyOrderRepository(storage);
4168
4199
  }
4169
4200
 
4170
4201
  getBasePath() {
@@ -4267,8 +4298,10 @@ class CommercetoolsMock {
4267
4298
 
4268
4299
  if (this.options.enableAuthentication) {
4269
4300
  app.use('/:projectKey', this._oauth2.createMiddleware(), projectRouter);
4301
+ app.use('/:projectKey/in-store/key=:storeKey', this._oauth2.createMiddleware(), projectRouter);
4270
4302
  } else {
4271
4303
  app.use('/:projectKey', projectRouter);
4304
+ app.use('/:projectKey/in-store/key=:storeKey', projectRouter);
4272
4305
  }
4273
4306
 
4274
4307
  this._projectService = new ProjectService(projectRouter, this._storage);