@labdigital/commercetools-mock 0.5.17 → 0.5.20

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 (39) hide show
  1. package/dist/commercetools-mock.cjs.development.js +182 -40
  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 +182 -40
  6. package/dist/commercetools-mock.esm.js.map +1 -1
  7. package/dist/repositories/customer.d.ts +6 -1
  8. package/dist/repositories/payment.d.ts +3 -1
  9. package/dist/repositories/store.d.ts +1 -0
  10. package/dist/services/customer.d.ts +1 -0
  11. package/dist/services/my-customer.d.ts +12 -0
  12. package/dist/services/shipping-method.d.ts +2 -1
  13. package/dist/services/store.d.ts +3 -1
  14. package/dist/types.d.ts +1 -1
  15. package/package.json +2 -16
  16. package/src/ctMock.ts +2 -0
  17. package/src/lib/masking.ts +1 -3
  18. package/src/repositories/category.ts +7 -6
  19. package/src/repositories/customer.ts +21 -1
  20. package/src/repositories/discount-code.ts +0 -1
  21. package/src/repositories/payment.ts +38 -2
  22. package/src/repositories/product-type.ts +3 -3
  23. package/src/repositories/project.ts +0 -2
  24. package/src/repositories/shipping-method.ts +11 -12
  25. package/src/repositories/store.ts +15 -0
  26. package/src/repositories/subscription.ts +2 -2
  27. package/src/repositories/tax-category.ts +1 -1
  28. package/src/repositories/type.ts +10 -10
  29. package/src/repositories/zone.ts +3 -1
  30. package/src/services/abstract.ts +0 -1
  31. package/src/services/customer.ts +21 -0
  32. package/src/services/my-customer.test.ts +51 -0
  33. package/src/services/my-customer.ts +46 -0
  34. package/src/services/product.test.ts +1 -5
  35. package/src/services/project.ts +1 -1
  36. package/src/services/shipping-method.test.ts +27 -0
  37. package/src/services/shipping-method.ts +6 -1
  38. package/src/services/store.ts +16 -1
  39. package/src/types.ts +5 -1
@@ -1005,7 +1005,6 @@ class AbstractService {
1005
1005
  return response.status(404).send();
1006
1006
  }
1007
1007
 
1008
- console.log(JSON.stringify(result, null, 4));
1009
1008
  return response.status(200).send(result);
1010
1009
  }
1011
1010
 
@@ -1611,11 +1610,11 @@ class CategoryRepository extends AbstractResourceRepository {
1611
1610
  var _resource$assets;
1612
1611
 
1613
1612
  (_resource$assets = resource.assets) == null ? void 0 : _resource$assets.forEach(asset => {
1614
- if (assetId && assetId == asset.id) {
1613
+ if (assetId && assetId === asset.id) {
1615
1614
  asset.name = name;
1616
1615
  }
1617
1616
 
1618
- if (assetKey && assetKey == asset.key) {
1617
+ if (assetKey && assetKey === asset.key) {
1619
1618
  asset.name = name;
1620
1619
  }
1621
1620
  });
@@ -1638,11 +1637,11 @@ class CategoryRepository extends AbstractResourceRepository {
1638
1637
  var _resource$assets2;
1639
1638
 
1640
1639
  (_resource$assets2 = resource.assets) == null ? void 0 : _resource$assets2.forEach(asset => {
1641
- if (assetId && assetId == asset.id) {
1640
+ if (assetId && assetId === asset.id) {
1642
1641
  asset.description = description;
1643
1642
  }
1644
1643
 
1645
- if (assetKey && assetKey == asset.key) {
1644
+ if (assetKey && assetKey === asset.key) {
1646
1645
  asset.description = description;
1647
1646
  }
1648
1647
  });
@@ -1655,11 +1654,11 @@ class CategoryRepository extends AbstractResourceRepository {
1655
1654
  var _resource$assets3;
1656
1655
 
1657
1656
  (_resource$assets3 = resource.assets) == null ? void 0 : _resource$assets3.forEach(asset => {
1658
- if (assetId && assetId == asset.id) {
1657
+ if (assetId && assetId === asset.id) {
1659
1658
  asset.sources = sources;
1660
1659
  }
1661
1660
 
1662
- if (assetKey && assetKey == asset.key) {
1661
+ if (assetKey && assetKey === asset.key) {
1663
1662
  asset.sources = sources;
1664
1663
  }
1665
1664
  });
@@ -1699,6 +1698,7 @@ class CategoryRepository extends AbstractResourceRepository {
1699
1698
  name: draft.name,
1700
1699
  slug: draft.slug,
1701
1700
  orderHint: draft.orderHint || '',
1701
+ externalId: draft.externalId || '',
1702
1702
  parent: draft.parent ? {
1703
1703
  typeId: 'category',
1704
1704
  id: draft.parent.id
@@ -1807,6 +1807,17 @@ class CustomerGroupService extends AbstractService {
1807
1807
  }
1808
1808
 
1809
1809
  class CustomerRepository extends AbstractResourceRepository {
1810
+ constructor() {
1811
+ super(...arguments);
1812
+ this.actions = {
1813
+ changeEmail: (_projectKey, resource, {
1814
+ email
1815
+ }) => {
1816
+ resource.email = email;
1817
+ }
1818
+ };
1819
+ }
1820
+
1810
1821
  getTypeId() {
1811
1822
  return 'customer';
1812
1823
  }
@@ -1814,7 +1825,7 @@ class CustomerRepository extends AbstractResourceRepository {
1814
1825
  create(projectKey, draft) {
1815
1826
  const resource = { ...getBaseResourceProperties(),
1816
1827
  email: draft.email,
1817
- password: draft.password,
1828
+ password: Buffer.from(draft.password).toString('base64'),
1818
1829
  isEmailVerified: draft.isEmailVerified || false,
1819
1830
  addresses: []
1820
1831
  };
@@ -1822,6 +1833,17 @@ class CustomerRepository extends AbstractResourceRepository {
1822
1833
  return resource;
1823
1834
  }
1824
1835
 
1836
+ getMe(projectKey) {
1837
+ const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
1838
+
1839
+
1840
+ if (results.count > 0) {
1841
+ return results.results[0];
1842
+ }
1843
+
1844
+ return;
1845
+ }
1846
+
1825
1847
  }
1826
1848
 
1827
1849
  class CustomerService extends AbstractService {
@@ -1834,6 +1856,24 @@ class CustomerService extends AbstractService {
1834
1856
  return 'customers';
1835
1857
  }
1836
1858
 
1859
+ extraRoutes(parent) {
1860
+ parent.post('/password-token', (request, response) => {
1861
+ const customer = this.repository.query(request.params.projectKey, {
1862
+ where: [`email="${request.body.email}"`]
1863
+ });
1864
+ const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
1865
+ const {
1866
+ version,
1867
+ ...rest
1868
+ } = getBaseResourceProperties();
1869
+ return response.status(200).send({ ...rest,
1870
+ customerId: customer.results[0].id,
1871
+ expiresAt: new Date(Date.now() + ttlMinutes * 60).toISOString(),
1872
+ value: uuid.v4()
1873
+ });
1874
+ });
1875
+ }
1876
+
1837
1877
  }
1838
1878
 
1839
1879
  class CustomObjectRepository extends AbstractResourceRepository {
@@ -1997,7 +2037,6 @@ class DiscountCodeRepository extends AbstractResourceRepository {
1997
2037
  }
1998
2038
 
1999
2039
  create(projectKey, draft) {
2000
- console.log(draft);
2001
2040
  const resource = { ...getBaseResourceProperties(),
2002
2041
  applicationVersion: 1,
2003
2042
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2268,6 +2307,31 @@ class PaymentRepository extends AbstractResourceRepository {
2268
2307
  transaction
2269
2308
  }) => {
2270
2309
  resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2310
+ },
2311
+ changeTransactionState: (_projectKey, resource, {
2312
+ transactionId,
2313
+ state
2314
+ }) => {
2315
+ const index = resource.transactions.findIndex(e => e.id === transactionId);
2316
+ const updatedTransaction = { ...resource.transactions[index],
2317
+ state
2318
+ };
2319
+ resource.transactions[index] = updatedTransaction;
2320
+ },
2321
+ transitionState: (projectKey, resource, {
2322
+ state
2323
+ }) => {
2324
+ const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2325
+
2326
+ if (!stateObj) {
2327
+ throw new Error(`State ${state} not found`);
2328
+ }
2329
+
2330
+ resource.paymentStatus.state = {
2331
+ typeId: 'state',
2332
+ id: stateObj.id,
2333
+ obj: stateObj
2334
+ };
2271
2335
  }
2272
2336
  };
2273
2337
  }
@@ -2897,7 +2961,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2897
2961
  switch (type.name) {
2898
2962
  case 'lenum':
2899
2963
  type.values.forEach(v => {
2900
- if (v.key == newValue.key) {
2964
+ if (v.key === newValue.key) {
2901
2965
  v.label = newValue.label;
2902
2966
  }
2903
2967
  });
@@ -2910,7 +2974,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2910
2974
  };
2911
2975
 
2912
2976
  (_resource$attributes = resource.attributes) == null ? void 0 : _resource$attributes.forEach(value => {
2913
- if (value.name == attributeName) {
2977
+ if (value.name === attributeName) {
2914
2978
  updateAttributeType(value.type);
2915
2979
  }
2916
2980
  });
@@ -2922,7 +2986,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2922
2986
  var _resource$attributes2;
2923
2987
 
2924
2988
  (_resource$attributes2 = resource.attributes) == null ? void 0 : _resource$attributes2.forEach(value => {
2925
- if (value.name == attributeName) {
2989
+ if (value.name === attributeName) {
2926
2990
  value.label = label;
2927
2991
  }
2928
2992
  });
@@ -3002,7 +3066,7 @@ const maskSecretValue = (resource, path) => {
3002
3066
  const part = parts[i];
3003
3067
  val = val[part];
3004
3068
 
3005
- if (val == undefined) {
3069
+ if (val === undefined) {
3006
3070
  return resource;
3007
3071
  }
3008
3072
  }
@@ -3085,7 +3149,6 @@ class ProjectRepository extends AbstractRepository {
3085
3149
  changeCartsConfiguration: (projectKey, resource, {
3086
3150
  cartsConfiguration
3087
3151
  }) => {
3088
- console.log(cartsConfiguration);
3089
3152
  resource.carts = cartsConfiguration || {
3090
3153
  countryTaxRateFallbackEnabled: false,
3091
3154
  deleteDaysAfterLastModification: 90
@@ -3095,8 +3158,6 @@ class ProjectRepository extends AbstractRepository {
3095
3158
  }
3096
3159
 
3097
3160
  get(projectKey) {
3098
- const data = this._storage.getProject(projectKey);
3099
-
3100
3161
  const resource = this._storage.getProject(projectKey);
3101
3162
 
3102
3163
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
@@ -3150,7 +3211,7 @@ class ProjectService {
3150
3211
  return response.status(404).send({});
3151
3212
  }
3152
3213
 
3153
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3214
+ this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3154
3215
  return response.status(200).send({});
3155
3216
  }
3156
3217
 
@@ -3178,14 +3239,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3178
3239
  };
3179
3240
 
3180
3241
  this.actions = {
3181
- addShippingRate: (projectKey, resource, {
3242
+ addShippingRate: (_projectKey, resource, {
3182
3243
  shippingRate,
3183
3244
  zone
3184
3245
  }) => {
3185
3246
  const rate = this._transformShippingRate(shippingRate);
3186
3247
 
3187
3248
  resource.zoneRates.forEach(zoneRate => {
3188
- if (zoneRate.zone.id == zone.id) {
3249
+ if (zoneRate.zone.id === zone.id) {
3189
3250
  zoneRate.shippingRates.push(rate);
3190
3251
  return;
3191
3252
  }
@@ -3198,14 +3259,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3198
3259
  shippingRates: [rate]
3199
3260
  });
3200
3261
  },
3201
- removeShippingRate: (projectKey, resource, {
3262
+ removeShippingRate: (_projectKey, resource, {
3202
3263
  shippingRate,
3203
3264
  zone
3204
3265
  }) => {
3205
3266
  const rate = this._transformShippingRate(shippingRate);
3206
3267
 
3207
3268
  resource.zoneRates.forEach(zoneRate => {
3208
- if (zoneRate.zone.id == zone.id) {
3269
+ if (zoneRate.zone.id === zone.id) {
3209
3270
  zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
3210
3271
  return !deepEqual(rate, otherRate);
3211
3272
  });
@@ -3226,39 +3287,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3226
3287
  shippingRates: []
3227
3288
  });
3228
3289
  },
3229
- removeZone: (projectKey, resource, {
3290
+ removeZone: (_projectKey, resource, {
3230
3291
  zone
3231
3292
  }) => {
3232
3293
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3233
3294
  return zoneRate.zone.id !== zone.id;
3234
3295
  });
3235
3296
  },
3236
- setKey: (projectKey, resource, {
3297
+ setKey: (_projectKey, resource, {
3237
3298
  key
3238
3299
  }) => {
3239
3300
  resource.key = key;
3240
3301
  },
3241
- setDescription: (projectKey, resource, {
3302
+ setDescription: (_projectKey, resource, {
3242
3303
  description
3243
3304
  }) => {
3244
3305
  resource.description = description;
3245
3306
  },
3246
- setLocalizedDescription: (projectKey, resource, {
3307
+ setLocalizedDescription: (_projectKey, resource, {
3247
3308
  localizedDescription
3248
3309
  }) => {
3249
3310
  resource.localizedDescription = localizedDescription;
3250
3311
  },
3251
- setPredicate: (projectKey, resource, {
3312
+ setPredicate: (_projectKey, resource, {
3252
3313
  predicate
3253
3314
  }) => {
3254
3315
  resource.predicate = predicate;
3255
3316
  },
3256
- changeIsDefault: (projectKey, resource, {
3317
+ changeIsDefault: (_projectKey, resource, {
3257
3318
  isDefault
3258
3319
  }) => {
3259
3320
  resource.isDefault = isDefault;
3260
3321
  },
3261
- changeName: (projectKey, resource, {
3322
+ changeName: (_projectKey, resource, {
3262
3323
  name
3263
3324
  }) => {
3264
3325
  resource.name = name;
@@ -3289,12 +3350,17 @@ class ShippingMethodService extends AbstractService {
3289
3350
  constructor(parent, storage) {
3290
3351
  super(parent);
3291
3352
  this.repository = new ShippingMethodRepository(storage);
3353
+ this.registerRoutes(parent);
3292
3354
  }
3293
3355
 
3294
3356
  getBasePath() {
3295
3357
  return 'shipping-methods';
3296
3358
  }
3297
3359
 
3360
+ extraRoutes(parent) {
3361
+ parent.get('/matching-cart', this.get.bind(this));
3362
+ }
3363
+
3298
3364
  }
3299
3365
 
3300
3366
  class ShoppingListRepository extends AbstractResourceRepository {
@@ -3449,6 +3515,22 @@ class StoreRepository extends AbstractResourceRepository {
3449
3515
  return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3450
3516
  }
3451
3517
 
3518
+ getWithKey(projectKey, key) {
3519
+ const result = this._storage.query(projectKey, this.getTypeId(), {
3520
+ where: [`key="${key}"`]
3521
+ });
3522
+
3523
+ if (result.count === 1) {
3524
+ return result.results[0];
3525
+ }
3526
+
3527
+ if (result.count > 1) {
3528
+ throw new Error('Duplicate store key');
3529
+ }
3530
+
3531
+ return;
3532
+ }
3533
+
3452
3534
  }
3453
3535
 
3454
3536
  class StoreService extends AbstractService {
@@ -3461,6 +3543,20 @@ class StoreService extends AbstractService {
3461
3543
  return 'stores';
3462
3544
  }
3463
3545
 
3546
+ extraRoutes(router) {
3547
+ router.get('/key=:key', this.getWithKey.bind(this));
3548
+ }
3549
+
3550
+ getWithKey(request, response) {
3551
+ const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3552
+
3553
+ if (resource) {
3554
+ return response.status(200).send(resource);
3555
+ }
3556
+
3557
+ return response.status(404).send('Not found');
3558
+ }
3559
+
3464
3560
  }
3465
3561
 
3466
3562
  class SubscriptionRepository extends AbstractResourceRepository {
@@ -3471,11 +3567,11 @@ class SubscriptionRepository extends AbstractResourceRepository {
3471
3567
  create(projectKey, draft) {
3472
3568
  // TODO: We could actually test this here by using the aws sdk. For now
3473
3569
  // hardcode a failed check when account id is 0000000000
3474
- if (draft.destination.type == 'SQS') {
3570
+ if (draft.destination.type === 'SQS') {
3475
3571
  const queueURL = new URL(draft.destination.queueUrl);
3476
3572
  const accountId = queueURL.pathname.split('/')[1];
3477
3573
 
3478
- if (accountId == '0000000000') {
3574
+ if (accountId === '0000000000') {
3479
3575
  const dest = draft.destination;
3480
3576
  throw new CommercetoolsError({
3481
3577
  code: 'InvalidInput',
@@ -3555,7 +3651,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3555
3651
  for (let i = 0; i < resource.rates.length; i++) {
3556
3652
  const rate = resource.rates[i];
3557
3653
 
3558
- if (rate.id == taxRateId) {
3654
+ if (rate.id === taxRateId) {
3559
3655
  resource.rates[i] = taxRateObj;
3560
3656
  }
3561
3657
  }
@@ -3693,11 +3789,11 @@ class TypeRepository extends AbstractResourceRepository {
3693
3789
  value
3694
3790
  }) => {
3695
3791
  resource.fieldDefinitions.forEach(field => {
3696
- if (field.name == fieldName) {
3792
+ if (field.name === fieldName) {
3697
3793
  // TODO, should be done better i suppose
3698
- if (field.type.name == 'Enum') {
3794
+ if (field.type.name === 'Enum') {
3699
3795
  field.type.values.push(value);
3700
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3796
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3701
3797
  field.type.elementType.values.push(value);
3702
3798
  } else {
3703
3799
  throw new Error('Type is not a Enum (or Set of Enum)');
@@ -3710,17 +3806,17 @@ class TypeRepository extends AbstractResourceRepository {
3710
3806
  value
3711
3807
  }) => {
3712
3808
  resource.fieldDefinitions.forEach(field => {
3713
- if (field.name == fieldName) {
3809
+ if (field.name === fieldName) {
3714
3810
  // TODO, should be done better i suppose
3715
- if (field.type.name == 'Enum') {
3811
+ if (field.type.name === 'Enum') {
3716
3812
  field.type.values.forEach(v => {
3717
- if (v.key == value.key) {
3813
+ if (v.key === value.key) {
3718
3814
  v.label = value.label;
3719
3815
  }
3720
3816
  });
3721
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3817
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3722
3818
  field.type.elementType.values.forEach(v => {
3723
- if (v.key == value.key) {
3819
+ if (v.key === value.key) {
3724
3820
  v.label = value.label;
3725
3821
  }
3726
3822
  });
@@ -3776,7 +3872,7 @@ class ZoneRepository extends AbstractResourceRepository {
3776
3872
  location
3777
3873
  }) => {
3778
3874
  resource.locations = resource.locations.filter(loc => {
3779
- return !(loc.country == location.country && loc.state == location.state);
3875
+ return !(loc.country === location.country && loc.state === location.state);
3780
3876
  });
3781
3877
  },
3782
3878
  changeName: (projectKey, resource, {
@@ -3826,6 +3922,51 @@ class ZoneService extends AbstractService {
3826
3922
 
3827
3923
  }
3828
3924
 
3925
+ class MyCustomerService extends AbstractService {
3926
+ constructor(parent, storage) {
3927
+ super(parent);
3928
+ this.repository = new CustomerRepository(storage);
3929
+ }
3930
+
3931
+ getBasePath() {
3932
+ return 'me';
3933
+ }
3934
+
3935
+ registerRoutes(parent) {
3936
+ // Overwrite this function to be able to handle /me path.
3937
+ const basePath = this.getBasePath();
3938
+ const router = express.Router({
3939
+ mergeParams: true
3940
+ });
3941
+ this.extraRoutes(router);
3942
+ router.get('', this.getMe.bind(this));
3943
+ router.post('/signup', this.signUp.bind(this));
3944
+ parent.use(`/${basePath}`, router);
3945
+ }
3946
+
3947
+ getMe(request, response) {
3948
+ const resource = this.repository.getMe(request.params.projectKey);
3949
+
3950
+ if (!resource) {
3951
+ return response.status(404).send('Not found');
3952
+ }
3953
+
3954
+ return response.status(200).send(resource);
3955
+ }
3956
+
3957
+ signUp(request, response) {
3958
+ const draft = request.body;
3959
+ const resource = this.repository.create(request.params.projectKey, draft);
3960
+
3961
+ const result = this._expandWithId(request, resource.id);
3962
+
3963
+ return response.status(this.createStatusCode).send({
3964
+ customer: result
3965
+ });
3966
+ }
3967
+
3968
+ }
3969
+
3829
3970
  const DEFAULT_OPTIONS = {
3830
3971
  enableAuthentication: false,
3831
3972
  validateCredentials: false,
@@ -3924,6 +4065,7 @@ class CommercetoolsMock {
3924
4065
  order: new OrderService(projectRouter, this._storage),
3925
4066
  payment: new PaymentService(projectRouter, this._storage),
3926
4067
  'my-cart': new MyCartService(projectRouter, this._storage),
4068
+ 'my-customer': new MyCustomerService(projectRouter, this._storage),
3927
4069
  'my-payment': new MyPaymentService(projectRouter, this._storage),
3928
4070
  'shipping-method': new ShippingMethodService(projectRouter, this._storage),
3929
4071
  'product-type': new ProductTypeService(projectRouter, this._storage),