@labdigital/commercetools-mock 0.6.0 → 0.6.3

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