@labdigital/commercetools-mock 0.5.16 → 0.5.19

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.
@@ -978,9 +978,15 @@ class AbstractService {
978
978
  }
979
979
 
980
980
  get(request, response) {
981
+ const limit = this._parseParam(request.query.limit);
982
+
983
+ const offset = this._parseParam(request.query.offset);
984
+
981
985
  const result = this.repository.query(request.params.projectKey, {
982
986
  expand: this._parseParam(request.query.expand),
983
- where: this._parseParam(request.query.where)
987
+ where: this._parseParam(request.query.where),
988
+ limit: limit !== undefined ? Number(limit) : undefined,
989
+ offset: offset !== undefined ? Number(offset) : undefined
984
990
  });
985
991
  return response.status(200).send(result);
986
992
  }
@@ -992,7 +998,6 @@ class AbstractService {
992
998
  return response.status(404).send();
993
999
  }
994
1000
 
995
- console.log(JSON.stringify(result, null, 4));
996
1001
  return response.status(200).send(result);
997
1002
  }
998
1003
 
@@ -1121,7 +1126,9 @@ class AbstractResourceRepository extends AbstractRepository {
1121
1126
  query(projectKey, params = {}) {
1122
1127
  return this._storage.query(projectKey, this.getTypeId(), {
1123
1128
  expand: params.expand,
1124
- where: params.where
1129
+ where: params.where,
1130
+ offset: params.offset,
1131
+ limit: params.limit
1125
1132
  });
1126
1133
  }
1127
1134
 
@@ -1684,6 +1691,7 @@ class CategoryRepository extends AbstractResourceRepository {
1684
1691
  name: draft.name,
1685
1692
  slug: draft.slug,
1686
1693
  orderHint: draft.orderHint || '',
1694
+ externalId: draft.externalId || '',
1687
1695
  parent: draft.parent ? {
1688
1696
  typeId: 'category',
1689
1697
  id: draft.parent.id
@@ -1799,7 +1807,7 @@ class CustomerRepository extends AbstractResourceRepository {
1799
1807
  create(projectKey, draft) {
1800
1808
  const resource = { ...getBaseResourceProperties(),
1801
1809
  email: draft.email,
1802
- password: draft.password,
1810
+ password: Buffer.from(draft.password).toString('base64'),
1803
1811
  isEmailVerified: draft.isEmailVerified || false,
1804
1812
  addresses: []
1805
1813
  };
@@ -1807,6 +1815,17 @@ class CustomerRepository extends AbstractResourceRepository {
1807
1815
  return resource;
1808
1816
  }
1809
1817
 
1818
+ getMe(projectKey) {
1819
+ const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
1820
+
1821
+
1822
+ if (results.count > 0) {
1823
+ return results.results[0];
1824
+ }
1825
+
1826
+ return;
1827
+ }
1828
+
1810
1829
  }
1811
1830
 
1812
1831
  class CustomerService extends AbstractService {
@@ -1982,7 +2001,6 @@ class DiscountCodeRepository extends AbstractResourceRepository {
1982
2001
  }
1983
2002
 
1984
2003
  create(projectKey, draft) {
1985
- console.log(draft);
1986
2004
  const resource = { ...getBaseResourceProperties(),
1987
2005
  applicationVersion: 1,
1988
2006
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2253,6 +2271,31 @@ class PaymentRepository extends AbstractResourceRepository {
2253
2271
  transaction
2254
2272
  }) => {
2255
2273
  resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2274
+ },
2275
+ changeTransactionState: (_projectKey, resource, {
2276
+ transactionId,
2277
+ state
2278
+ }) => {
2279
+ const index = resource.transactions.findIndex(e => e.id === transactionId);
2280
+ const updatedTransaction = { ...resource.transactions[index],
2281
+ state
2282
+ };
2283
+ resource.transactions[index] = updatedTransaction;
2284
+ },
2285
+ transitionState: (projectKey, resource, {
2286
+ state
2287
+ }) => {
2288
+ const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2289
+
2290
+ if (!stateObj) {
2291
+ throw new Error(`State ${state} not found`);
2292
+ }
2293
+
2294
+ resource.paymentStatus.state = {
2295
+ typeId: 'state',
2296
+ id: stateObj.id,
2297
+ obj: stateObj
2298
+ };
2256
2299
  }
2257
2300
  };
2258
2301
  }
@@ -2660,7 +2703,9 @@ class ProductProjectionRepository extends AbstractResourceRepository {
2660
2703
  const wherePredicate = filter ? parseFilterExpression(filter) : undefined;
2661
2704
 
2662
2705
  const results = this._storage.query(projectKey, this.getTypeId(), {
2663
- where: wherePredicate
2706
+ where: wherePredicate,
2707
+ offset: query.offset ? Number(query.offset) : undefined,
2708
+ limit: query.limit ? Number(query.limit) : undefined
2664
2709
  }); //TODO: this is a partial implementation, but I don't really have the time to implement an actual search API right now
2665
2710
 
2666
2711
 
@@ -3068,7 +3113,6 @@ class ProjectRepository extends AbstractRepository {
3068
3113
  changeCartsConfiguration: (projectKey, resource, {
3069
3114
  cartsConfiguration
3070
3115
  }) => {
3071
- console.log(cartsConfiguration);
3072
3116
  resource.carts = cartsConfiguration || {
3073
3117
  countryTaxRateFallbackEnabled: false,
3074
3118
  deleteDaysAfterLastModification: 90
@@ -3272,12 +3316,17 @@ class ShippingMethodService extends AbstractService {
3272
3316
  constructor(parent, storage) {
3273
3317
  super(parent);
3274
3318
  this.repository = new ShippingMethodRepository(storage);
3319
+ this.registerRoutes(parent);
3275
3320
  }
3276
3321
 
3277
3322
  getBasePath() {
3278
3323
  return 'shipping-methods';
3279
3324
  }
3280
3325
 
3326
+ extraRoutes(parent) {
3327
+ parent.get('/matching-cart', this.get.bind(this));
3328
+ }
3329
+
3281
3330
  }
3282
3331
 
3283
3332
  class ShoppingListRepository extends AbstractResourceRepository {
@@ -3809,6 +3858,51 @@ class ZoneService extends AbstractService {
3809
3858
 
3810
3859
  }
3811
3860
 
3861
+ class MyCustomerService extends AbstractService {
3862
+ constructor(parent, storage) {
3863
+ super(parent);
3864
+ this.repository = new CustomerRepository(storage);
3865
+ }
3866
+
3867
+ getBasePath() {
3868
+ return 'me';
3869
+ }
3870
+
3871
+ registerRoutes(parent) {
3872
+ // Overwrite this function to be able to handle /me path.
3873
+ const basePath = this.getBasePath();
3874
+ const router = Router({
3875
+ mergeParams: true
3876
+ });
3877
+ this.extraRoutes(router);
3878
+ router.get('', this.getMe.bind(this));
3879
+ router.post('/signup', this.signUp.bind(this));
3880
+ parent.use(`/${basePath}`, router);
3881
+ }
3882
+
3883
+ getMe(request, response) {
3884
+ const resource = this.repository.getMe(request.params.projectKey);
3885
+
3886
+ if (!resource) {
3887
+ return response.status(404).send('Not found');
3888
+ }
3889
+
3890
+ return response.status(200).send(resource);
3891
+ }
3892
+
3893
+ signUp(request, response) {
3894
+ const draft = request.body;
3895
+ const resource = this.repository.create(request.params.projectKey, draft);
3896
+
3897
+ const result = this._expandWithId(request, resource.id);
3898
+
3899
+ return response.status(this.createStatusCode).send({
3900
+ customer: result
3901
+ });
3902
+ }
3903
+
3904
+ }
3905
+
3812
3906
  const DEFAULT_OPTIONS = {
3813
3907
  enableAuthentication: false,
3814
3908
  validateCredentials: false,
@@ -3907,6 +4001,7 @@ class CommercetoolsMock {
3907
4001
  order: new OrderService(projectRouter, this._storage),
3908
4002
  payment: new PaymentService(projectRouter, this._storage),
3909
4003
  'my-cart': new MyCartService(projectRouter, this._storage),
4004
+ 'my-customer': new MyCustomerService(projectRouter, this._storage),
3910
4005
  'my-payment': new MyPaymentService(projectRouter, this._storage),
3911
4006
  'shipping-method': new ShippingMethodService(projectRouter, this._storage),
3912
4007
  'product-type': new ProductTypeService(projectRouter, this._storage),