@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
@@ -998,7 +998,6 @@ class AbstractService {
998
998
  return response.status(404).send();
999
999
  }
1000
1000
 
1001
- console.log(JSON.stringify(result, null, 4));
1002
1001
  return response.status(200).send(result);
1003
1002
  }
1004
1003
 
@@ -1604,11 +1603,11 @@ class CategoryRepository extends AbstractResourceRepository {
1604
1603
  var _resource$assets;
1605
1604
 
1606
1605
  (_resource$assets = resource.assets) == null ? void 0 : _resource$assets.forEach(asset => {
1607
- if (assetId && assetId == asset.id) {
1606
+ if (assetId && assetId === asset.id) {
1608
1607
  asset.name = name;
1609
1608
  }
1610
1609
 
1611
- if (assetKey && assetKey == asset.key) {
1610
+ if (assetKey && assetKey === asset.key) {
1612
1611
  asset.name = name;
1613
1612
  }
1614
1613
  });
@@ -1631,11 +1630,11 @@ class CategoryRepository extends AbstractResourceRepository {
1631
1630
  var _resource$assets2;
1632
1631
 
1633
1632
  (_resource$assets2 = resource.assets) == null ? void 0 : _resource$assets2.forEach(asset => {
1634
- if (assetId && assetId == asset.id) {
1633
+ if (assetId && assetId === asset.id) {
1635
1634
  asset.description = description;
1636
1635
  }
1637
1636
 
1638
- if (assetKey && assetKey == asset.key) {
1637
+ if (assetKey && assetKey === asset.key) {
1639
1638
  asset.description = description;
1640
1639
  }
1641
1640
  });
@@ -1648,11 +1647,11 @@ class CategoryRepository extends AbstractResourceRepository {
1648
1647
  var _resource$assets3;
1649
1648
 
1650
1649
  (_resource$assets3 = resource.assets) == null ? void 0 : _resource$assets3.forEach(asset => {
1651
- if (assetId && assetId == asset.id) {
1650
+ if (assetId && assetId === asset.id) {
1652
1651
  asset.sources = sources;
1653
1652
  }
1654
1653
 
1655
- if (assetKey && assetKey == asset.key) {
1654
+ if (assetKey && assetKey === asset.key) {
1656
1655
  asset.sources = sources;
1657
1656
  }
1658
1657
  });
@@ -1692,6 +1691,7 @@ class CategoryRepository extends AbstractResourceRepository {
1692
1691
  name: draft.name,
1693
1692
  slug: draft.slug,
1694
1693
  orderHint: draft.orderHint || '',
1694
+ externalId: draft.externalId || '',
1695
1695
  parent: draft.parent ? {
1696
1696
  typeId: 'category',
1697
1697
  id: draft.parent.id
@@ -1800,6 +1800,17 @@ class CustomerGroupService extends AbstractService {
1800
1800
  }
1801
1801
 
1802
1802
  class CustomerRepository extends AbstractResourceRepository {
1803
+ constructor() {
1804
+ super(...arguments);
1805
+ this.actions = {
1806
+ changeEmail: (_projectKey, resource, {
1807
+ email
1808
+ }) => {
1809
+ resource.email = email;
1810
+ }
1811
+ };
1812
+ }
1813
+
1803
1814
  getTypeId() {
1804
1815
  return 'customer';
1805
1816
  }
@@ -1807,7 +1818,7 @@ class CustomerRepository extends AbstractResourceRepository {
1807
1818
  create(projectKey, draft) {
1808
1819
  const resource = { ...getBaseResourceProperties(),
1809
1820
  email: draft.email,
1810
- password: draft.password,
1821
+ password: Buffer.from(draft.password).toString('base64'),
1811
1822
  isEmailVerified: draft.isEmailVerified || false,
1812
1823
  addresses: []
1813
1824
  };
@@ -1815,6 +1826,17 @@ class CustomerRepository extends AbstractResourceRepository {
1815
1826
  return resource;
1816
1827
  }
1817
1828
 
1829
+ getMe(projectKey) {
1830
+ const results = this._storage.query(projectKey, this.getTypeId(), {}); // grab the first customer you can find
1831
+
1832
+
1833
+ if (results.count > 0) {
1834
+ return results.results[0];
1835
+ }
1836
+
1837
+ return;
1838
+ }
1839
+
1818
1840
  }
1819
1841
 
1820
1842
  class CustomerService extends AbstractService {
@@ -1827,6 +1849,24 @@ class CustomerService extends AbstractService {
1827
1849
  return 'customers';
1828
1850
  }
1829
1851
 
1852
+ extraRoutes(parent) {
1853
+ parent.post('/password-token', (request, response) => {
1854
+ const customer = this.repository.query(request.params.projectKey, {
1855
+ where: [`email="${request.body.email}"`]
1856
+ });
1857
+ const ttlMinutes = request.params.ttlMinutes ? +request.params.ttlMinutes : 34560;
1858
+ const {
1859
+ version,
1860
+ ...rest
1861
+ } = getBaseResourceProperties();
1862
+ return response.status(200).send({ ...rest,
1863
+ customerId: customer.results[0].id,
1864
+ expiresAt: new Date(Date.now() + ttlMinutes * 60).toISOString(),
1865
+ value: v4()
1866
+ });
1867
+ });
1868
+ }
1869
+
1830
1870
  }
1831
1871
 
1832
1872
  class CustomObjectRepository extends AbstractResourceRepository {
@@ -1990,7 +2030,6 @@ class DiscountCodeRepository extends AbstractResourceRepository {
1990
2030
  }
1991
2031
 
1992
2032
  create(projectKey, draft) {
1993
- console.log(draft);
1994
2033
  const resource = { ...getBaseResourceProperties(),
1995
2034
  applicationVersion: 1,
1996
2035
  cartDiscounts: draft.cartDiscounts.map(obj => ({
@@ -2261,6 +2300,31 @@ class PaymentRepository extends AbstractResourceRepository {
2261
2300
  transaction
2262
2301
  }) => {
2263
2302
  resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
2303
+ },
2304
+ changeTransactionState: (_projectKey, resource, {
2305
+ transactionId,
2306
+ state
2307
+ }) => {
2308
+ const index = resource.transactions.findIndex(e => e.id === transactionId);
2309
+ const updatedTransaction = { ...resource.transactions[index],
2310
+ state
2311
+ };
2312
+ resource.transactions[index] = updatedTransaction;
2313
+ },
2314
+ transitionState: (projectKey, resource, {
2315
+ state
2316
+ }) => {
2317
+ const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
2318
+
2319
+ if (!stateObj) {
2320
+ throw new Error(`State ${state} not found`);
2321
+ }
2322
+
2323
+ resource.paymentStatus.state = {
2324
+ typeId: 'state',
2325
+ id: stateObj.id,
2326
+ obj: stateObj
2327
+ };
2264
2328
  }
2265
2329
  };
2266
2330
  }
@@ -2890,7 +2954,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2890
2954
  switch (type.name) {
2891
2955
  case 'lenum':
2892
2956
  type.values.forEach(v => {
2893
- if (v.key == newValue.key) {
2957
+ if (v.key === newValue.key) {
2894
2958
  v.label = newValue.label;
2895
2959
  }
2896
2960
  });
@@ -2903,7 +2967,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2903
2967
  };
2904
2968
 
2905
2969
  (_resource$attributes = resource.attributes) == null ? void 0 : _resource$attributes.forEach(value => {
2906
- if (value.name == attributeName) {
2970
+ if (value.name === attributeName) {
2907
2971
  updateAttributeType(value.type);
2908
2972
  }
2909
2973
  });
@@ -2915,7 +2979,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2915
2979
  var _resource$attributes2;
2916
2980
 
2917
2981
  (_resource$attributes2 = resource.attributes) == null ? void 0 : _resource$attributes2.forEach(value => {
2918
- if (value.name == attributeName) {
2982
+ if (value.name === attributeName) {
2919
2983
  value.label = label;
2920
2984
  }
2921
2985
  });
@@ -2995,7 +3059,7 @@ const maskSecretValue = (resource, path) => {
2995
3059
  const part = parts[i];
2996
3060
  val = val[part];
2997
3061
 
2998
- if (val == undefined) {
3062
+ if (val === undefined) {
2999
3063
  return resource;
3000
3064
  }
3001
3065
  }
@@ -3078,7 +3142,6 @@ class ProjectRepository extends AbstractRepository {
3078
3142
  changeCartsConfiguration: (projectKey, resource, {
3079
3143
  cartsConfiguration
3080
3144
  }) => {
3081
- console.log(cartsConfiguration);
3082
3145
  resource.carts = cartsConfiguration || {
3083
3146
  countryTaxRateFallbackEnabled: false,
3084
3147
  deleteDaysAfterLastModification: 90
@@ -3088,8 +3151,6 @@ class ProjectRepository extends AbstractRepository {
3088
3151
  }
3089
3152
 
3090
3153
  get(projectKey) {
3091
- const data = this._storage.getProject(projectKey);
3092
-
3093
3154
  const resource = this._storage.getProject(projectKey);
3094
3155
 
3095
3156
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
@@ -3143,7 +3204,7 @@ class ProjectService {
3143
3204
  return response.status(404).send({});
3144
3205
  }
3145
3206
 
3146
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3207
+ this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3147
3208
  return response.status(200).send({});
3148
3209
  }
3149
3210
 
@@ -3171,14 +3232,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3171
3232
  };
3172
3233
 
3173
3234
  this.actions = {
3174
- addShippingRate: (projectKey, resource, {
3235
+ addShippingRate: (_projectKey, resource, {
3175
3236
  shippingRate,
3176
3237
  zone
3177
3238
  }) => {
3178
3239
  const rate = this._transformShippingRate(shippingRate);
3179
3240
 
3180
3241
  resource.zoneRates.forEach(zoneRate => {
3181
- if (zoneRate.zone.id == zone.id) {
3242
+ if (zoneRate.zone.id === zone.id) {
3182
3243
  zoneRate.shippingRates.push(rate);
3183
3244
  return;
3184
3245
  }
@@ -3191,14 +3252,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3191
3252
  shippingRates: [rate]
3192
3253
  });
3193
3254
  },
3194
- removeShippingRate: (projectKey, resource, {
3255
+ removeShippingRate: (_projectKey, resource, {
3195
3256
  shippingRate,
3196
3257
  zone
3197
3258
  }) => {
3198
3259
  const rate = this._transformShippingRate(shippingRate);
3199
3260
 
3200
3261
  resource.zoneRates.forEach(zoneRate => {
3201
- if (zoneRate.zone.id == zone.id) {
3262
+ if (zoneRate.zone.id === zone.id) {
3202
3263
  zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
3203
3264
  return !deepEqual(rate, otherRate);
3204
3265
  });
@@ -3219,39 +3280,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3219
3280
  shippingRates: []
3220
3281
  });
3221
3282
  },
3222
- removeZone: (projectKey, resource, {
3283
+ removeZone: (_projectKey, resource, {
3223
3284
  zone
3224
3285
  }) => {
3225
3286
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3226
3287
  return zoneRate.zone.id !== zone.id;
3227
3288
  });
3228
3289
  },
3229
- setKey: (projectKey, resource, {
3290
+ setKey: (_projectKey, resource, {
3230
3291
  key
3231
3292
  }) => {
3232
3293
  resource.key = key;
3233
3294
  },
3234
- setDescription: (projectKey, resource, {
3295
+ setDescription: (_projectKey, resource, {
3235
3296
  description
3236
3297
  }) => {
3237
3298
  resource.description = description;
3238
3299
  },
3239
- setLocalizedDescription: (projectKey, resource, {
3300
+ setLocalizedDescription: (_projectKey, resource, {
3240
3301
  localizedDescription
3241
3302
  }) => {
3242
3303
  resource.localizedDescription = localizedDescription;
3243
3304
  },
3244
- setPredicate: (projectKey, resource, {
3305
+ setPredicate: (_projectKey, resource, {
3245
3306
  predicate
3246
3307
  }) => {
3247
3308
  resource.predicate = predicate;
3248
3309
  },
3249
- changeIsDefault: (projectKey, resource, {
3310
+ changeIsDefault: (_projectKey, resource, {
3250
3311
  isDefault
3251
3312
  }) => {
3252
3313
  resource.isDefault = isDefault;
3253
3314
  },
3254
- changeName: (projectKey, resource, {
3315
+ changeName: (_projectKey, resource, {
3255
3316
  name
3256
3317
  }) => {
3257
3318
  resource.name = name;
@@ -3282,12 +3343,17 @@ class ShippingMethodService extends AbstractService {
3282
3343
  constructor(parent, storage) {
3283
3344
  super(parent);
3284
3345
  this.repository = new ShippingMethodRepository(storage);
3346
+ this.registerRoutes(parent);
3285
3347
  }
3286
3348
 
3287
3349
  getBasePath() {
3288
3350
  return 'shipping-methods';
3289
3351
  }
3290
3352
 
3353
+ extraRoutes(parent) {
3354
+ parent.get('/matching-cart', this.get.bind(this));
3355
+ }
3356
+
3291
3357
  }
3292
3358
 
3293
3359
  class ShoppingListRepository extends AbstractResourceRepository {
@@ -3442,6 +3508,22 @@ class StoreRepository extends AbstractResourceRepository {
3442
3508
  return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3443
3509
  }
3444
3510
 
3511
+ getWithKey(projectKey, key) {
3512
+ const result = this._storage.query(projectKey, this.getTypeId(), {
3513
+ where: [`key="${key}"`]
3514
+ });
3515
+
3516
+ if (result.count === 1) {
3517
+ return result.results[0];
3518
+ }
3519
+
3520
+ if (result.count > 1) {
3521
+ throw new Error('Duplicate store key');
3522
+ }
3523
+
3524
+ return;
3525
+ }
3526
+
3445
3527
  }
3446
3528
 
3447
3529
  class StoreService extends AbstractService {
@@ -3454,6 +3536,20 @@ class StoreService extends AbstractService {
3454
3536
  return 'stores';
3455
3537
  }
3456
3538
 
3539
+ extraRoutes(router) {
3540
+ router.get('/key=:key', this.getWithKey.bind(this));
3541
+ }
3542
+
3543
+ getWithKey(request, response) {
3544
+ const resource = this.repository.getWithKey(request.params.projectKey, request.params.key);
3545
+
3546
+ if (resource) {
3547
+ return response.status(200).send(resource);
3548
+ }
3549
+
3550
+ return response.status(404).send('Not found');
3551
+ }
3552
+
3457
3553
  }
3458
3554
 
3459
3555
  class SubscriptionRepository extends AbstractResourceRepository {
@@ -3464,11 +3560,11 @@ class SubscriptionRepository extends AbstractResourceRepository {
3464
3560
  create(projectKey, draft) {
3465
3561
  // TODO: We could actually test this here by using the aws sdk. For now
3466
3562
  // hardcode a failed check when account id is 0000000000
3467
- if (draft.destination.type == 'SQS') {
3563
+ if (draft.destination.type === 'SQS') {
3468
3564
  const queueURL = new URL(draft.destination.queueUrl);
3469
3565
  const accountId = queueURL.pathname.split('/')[1];
3470
3566
 
3471
- if (accountId == '0000000000') {
3567
+ if (accountId === '0000000000') {
3472
3568
  const dest = draft.destination;
3473
3569
  throw new CommercetoolsError({
3474
3570
  code: 'InvalidInput',
@@ -3548,7 +3644,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3548
3644
  for (let i = 0; i < resource.rates.length; i++) {
3549
3645
  const rate = resource.rates[i];
3550
3646
 
3551
- if (rate.id == taxRateId) {
3647
+ if (rate.id === taxRateId) {
3552
3648
  resource.rates[i] = taxRateObj;
3553
3649
  }
3554
3650
  }
@@ -3686,11 +3782,11 @@ class TypeRepository extends AbstractResourceRepository {
3686
3782
  value
3687
3783
  }) => {
3688
3784
  resource.fieldDefinitions.forEach(field => {
3689
- if (field.name == fieldName) {
3785
+ if (field.name === fieldName) {
3690
3786
  // TODO, should be done better i suppose
3691
- if (field.type.name == 'Enum') {
3787
+ if (field.type.name === 'Enum') {
3692
3788
  field.type.values.push(value);
3693
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3789
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3694
3790
  field.type.elementType.values.push(value);
3695
3791
  } else {
3696
3792
  throw new Error('Type is not a Enum (or Set of Enum)');
@@ -3703,17 +3799,17 @@ class TypeRepository extends AbstractResourceRepository {
3703
3799
  value
3704
3800
  }) => {
3705
3801
  resource.fieldDefinitions.forEach(field => {
3706
- if (field.name == fieldName) {
3802
+ if (field.name === fieldName) {
3707
3803
  // TODO, should be done better i suppose
3708
- if (field.type.name == 'Enum') {
3804
+ if (field.type.name === 'Enum') {
3709
3805
  field.type.values.forEach(v => {
3710
- if (v.key == value.key) {
3806
+ if (v.key === value.key) {
3711
3807
  v.label = value.label;
3712
3808
  }
3713
3809
  });
3714
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3810
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3715
3811
  field.type.elementType.values.forEach(v => {
3716
- if (v.key == value.key) {
3812
+ if (v.key === value.key) {
3717
3813
  v.label = value.label;
3718
3814
  }
3719
3815
  });
@@ -3769,7 +3865,7 @@ class ZoneRepository extends AbstractResourceRepository {
3769
3865
  location
3770
3866
  }) => {
3771
3867
  resource.locations = resource.locations.filter(loc => {
3772
- return !(loc.country == location.country && loc.state == location.state);
3868
+ return !(loc.country === location.country && loc.state === location.state);
3773
3869
  });
3774
3870
  },
3775
3871
  changeName: (projectKey, resource, {
@@ -3819,6 +3915,51 @@ class ZoneService extends AbstractService {
3819
3915
 
3820
3916
  }
3821
3917
 
3918
+ class MyCustomerService extends AbstractService {
3919
+ constructor(parent, storage) {
3920
+ super(parent);
3921
+ this.repository = new CustomerRepository(storage);
3922
+ }
3923
+
3924
+ getBasePath() {
3925
+ return 'me';
3926
+ }
3927
+
3928
+ registerRoutes(parent) {
3929
+ // Overwrite this function to be able to handle /me path.
3930
+ const basePath = this.getBasePath();
3931
+ const router = Router({
3932
+ mergeParams: true
3933
+ });
3934
+ this.extraRoutes(router);
3935
+ router.get('', this.getMe.bind(this));
3936
+ router.post('/signup', this.signUp.bind(this));
3937
+ parent.use(`/${basePath}`, router);
3938
+ }
3939
+
3940
+ getMe(request, response) {
3941
+ const resource = this.repository.getMe(request.params.projectKey);
3942
+
3943
+ if (!resource) {
3944
+ return response.status(404).send('Not found');
3945
+ }
3946
+
3947
+ return response.status(200).send(resource);
3948
+ }
3949
+
3950
+ signUp(request, response) {
3951
+ const draft = request.body;
3952
+ const resource = this.repository.create(request.params.projectKey, draft);
3953
+
3954
+ const result = this._expandWithId(request, resource.id);
3955
+
3956
+ return response.status(this.createStatusCode).send({
3957
+ customer: result
3958
+ });
3959
+ }
3960
+
3961
+ }
3962
+
3822
3963
  const DEFAULT_OPTIONS = {
3823
3964
  enableAuthentication: false,
3824
3965
  validateCredentials: false,
@@ -3917,6 +4058,7 @@ class CommercetoolsMock {
3917
4058
  order: new OrderService(projectRouter, this._storage),
3918
4059
  payment: new PaymentService(projectRouter, this._storage),
3919
4060
  'my-cart': new MyCartService(projectRouter, this._storage),
4061
+ 'my-customer': new MyCustomerService(projectRouter, this._storage),
3920
4062
  'my-payment': new MyPaymentService(projectRouter, this._storage),
3921
4063
  'shipping-method': new ShippingMethodService(projectRouter, this._storage),
3922
4064
  'product-type': new ProductTypeService(projectRouter, this._storage),