@labdigital/commercetools-mock 0.5.18 → 0.5.21

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.
@@ -17,7 +17,6 @@ var bodyParser = _interopDefault(require('body-parser'));
17
17
  var crypto = require('crypto');
18
18
  var uuid = require('uuid');
19
19
  var deepEqual = _interopDefault(require('deep-equal'));
20
- var myCustomer = require('services/my-customer');
21
20
 
22
21
  const parseExpandClause = clause => {
23
22
  const result = {
@@ -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
  }
@@ -1845,6 +1856,24 @@ class CustomerService extends AbstractService {
1845
1856
  return 'customers';
1846
1857
  }
1847
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
+
1848
1877
  }
1849
1878
 
1850
1879
  class CustomObjectRepository extends AbstractResourceRepository {
@@ -2278,6 +2307,31 @@ class PaymentRepository extends AbstractResourceRepository {
2278
2307
  transaction
2279
2308
  }) => {
2280
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
+ };
2281
2335
  }
2282
2336
  };
2283
2337
  }
@@ -2907,7 +2961,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2907
2961
  switch (type.name) {
2908
2962
  case 'lenum':
2909
2963
  type.values.forEach(v => {
2910
- if (v.key == newValue.key) {
2964
+ if (v.key === newValue.key) {
2911
2965
  v.label = newValue.label;
2912
2966
  }
2913
2967
  });
@@ -2920,7 +2974,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2920
2974
  };
2921
2975
 
2922
2976
  (_resource$attributes = resource.attributes) == null ? void 0 : _resource$attributes.forEach(value => {
2923
- if (value.name == attributeName) {
2977
+ if (value.name === attributeName) {
2924
2978
  updateAttributeType(value.type);
2925
2979
  }
2926
2980
  });
@@ -2932,7 +2986,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2932
2986
  var _resource$attributes2;
2933
2987
 
2934
2988
  (_resource$attributes2 = resource.attributes) == null ? void 0 : _resource$attributes2.forEach(value => {
2935
- if (value.name == attributeName) {
2989
+ if (value.name === attributeName) {
2936
2990
  value.label = label;
2937
2991
  }
2938
2992
  });
@@ -3012,7 +3066,7 @@ const maskSecretValue = (resource, path) => {
3012
3066
  const part = parts[i];
3013
3067
  val = val[part];
3014
3068
 
3015
- if (val == undefined) {
3069
+ if (val === undefined) {
3016
3070
  return resource;
3017
3071
  }
3018
3072
  }
@@ -3104,8 +3158,6 @@ class ProjectRepository extends AbstractRepository {
3104
3158
  }
3105
3159
 
3106
3160
  get(projectKey) {
3107
- const data = this._storage.getProject(projectKey);
3108
-
3109
3161
  const resource = this._storage.getProject(projectKey);
3110
3162
 
3111
3163
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
@@ -3159,7 +3211,7 @@ class ProjectService {
3159
3211
  return response.status(404).send({});
3160
3212
  }
3161
3213
 
3162
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3214
+ this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3163
3215
  return response.status(200).send({});
3164
3216
  }
3165
3217
 
@@ -3187,14 +3239,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3187
3239
  };
3188
3240
 
3189
3241
  this.actions = {
3190
- addShippingRate: (projectKey, resource, {
3242
+ addShippingRate: (_projectKey, resource, {
3191
3243
  shippingRate,
3192
3244
  zone
3193
3245
  }) => {
3194
3246
  const rate = this._transformShippingRate(shippingRate);
3195
3247
 
3196
3248
  resource.zoneRates.forEach(zoneRate => {
3197
- if (zoneRate.zone.id == zone.id) {
3249
+ if (zoneRate.zone.id === zone.id) {
3198
3250
  zoneRate.shippingRates.push(rate);
3199
3251
  return;
3200
3252
  }
@@ -3207,14 +3259,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3207
3259
  shippingRates: [rate]
3208
3260
  });
3209
3261
  },
3210
- removeShippingRate: (projectKey, resource, {
3262
+ removeShippingRate: (_projectKey, resource, {
3211
3263
  shippingRate,
3212
3264
  zone
3213
3265
  }) => {
3214
3266
  const rate = this._transformShippingRate(shippingRate);
3215
3267
 
3216
3268
  resource.zoneRates.forEach(zoneRate => {
3217
- if (zoneRate.zone.id == zone.id) {
3269
+ if (zoneRate.zone.id === zone.id) {
3218
3270
  zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
3219
3271
  return !deepEqual(rate, otherRate);
3220
3272
  });
@@ -3235,39 +3287,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3235
3287
  shippingRates: []
3236
3288
  });
3237
3289
  },
3238
- removeZone: (projectKey, resource, {
3290
+ removeZone: (_projectKey, resource, {
3239
3291
  zone
3240
3292
  }) => {
3241
3293
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3242
3294
  return zoneRate.zone.id !== zone.id;
3243
3295
  });
3244
3296
  },
3245
- setKey: (projectKey, resource, {
3297
+ setKey: (_projectKey, resource, {
3246
3298
  key
3247
3299
  }) => {
3248
3300
  resource.key = key;
3249
3301
  },
3250
- setDescription: (projectKey, resource, {
3302
+ setDescription: (_projectKey, resource, {
3251
3303
  description
3252
3304
  }) => {
3253
3305
  resource.description = description;
3254
3306
  },
3255
- setLocalizedDescription: (projectKey, resource, {
3307
+ setLocalizedDescription: (_projectKey, resource, {
3256
3308
  localizedDescription
3257
3309
  }) => {
3258
3310
  resource.localizedDescription = localizedDescription;
3259
3311
  },
3260
- setPredicate: (projectKey, resource, {
3312
+ setPredicate: (_projectKey, resource, {
3261
3313
  predicate
3262
3314
  }) => {
3263
3315
  resource.predicate = predicate;
3264
3316
  },
3265
- changeIsDefault: (projectKey, resource, {
3317
+ changeIsDefault: (_projectKey, resource, {
3266
3318
  isDefault
3267
3319
  }) => {
3268
3320
  resource.isDefault = isDefault;
3269
3321
  },
3270
- changeName: (projectKey, resource, {
3322
+ changeName: (_projectKey, resource, {
3271
3323
  name
3272
3324
  }) => {
3273
3325
  resource.name = name;
@@ -3463,6 +3515,22 @@ class StoreRepository extends AbstractResourceRepository {
3463
3515
  return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3464
3516
  }
3465
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
+
3466
3534
  }
3467
3535
 
3468
3536
  class StoreService extends AbstractService {
@@ -3475,6 +3543,20 @@ class StoreService extends AbstractService {
3475
3543
  return 'stores';
3476
3544
  }
3477
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
+
3478
3560
  }
3479
3561
 
3480
3562
  class SubscriptionRepository extends AbstractResourceRepository {
@@ -3485,11 +3567,11 @@ class SubscriptionRepository extends AbstractResourceRepository {
3485
3567
  create(projectKey, draft) {
3486
3568
  // TODO: We could actually test this here by using the aws sdk. For now
3487
3569
  // hardcode a failed check when account id is 0000000000
3488
- if (draft.destination.type == 'SQS') {
3570
+ if (draft.destination.type === 'SQS') {
3489
3571
  const queueURL = new URL(draft.destination.queueUrl);
3490
3572
  const accountId = queueURL.pathname.split('/')[1];
3491
3573
 
3492
- if (accountId == '0000000000') {
3574
+ if (accountId === '0000000000') {
3493
3575
  const dest = draft.destination;
3494
3576
  throw new CommercetoolsError({
3495
3577
  code: 'InvalidInput',
@@ -3569,7 +3651,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3569
3651
  for (let i = 0; i < resource.rates.length; i++) {
3570
3652
  const rate = resource.rates[i];
3571
3653
 
3572
- if (rate.id == taxRateId) {
3654
+ if (rate.id === taxRateId) {
3573
3655
  resource.rates[i] = taxRateObj;
3574
3656
  }
3575
3657
  }
@@ -3707,11 +3789,11 @@ class TypeRepository extends AbstractResourceRepository {
3707
3789
  value
3708
3790
  }) => {
3709
3791
  resource.fieldDefinitions.forEach(field => {
3710
- if (field.name == fieldName) {
3792
+ if (field.name === fieldName) {
3711
3793
  // TODO, should be done better i suppose
3712
- if (field.type.name == 'Enum') {
3794
+ if (field.type.name === 'Enum') {
3713
3795
  field.type.values.push(value);
3714
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3796
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3715
3797
  field.type.elementType.values.push(value);
3716
3798
  } else {
3717
3799
  throw new Error('Type is not a Enum (or Set of Enum)');
@@ -3724,17 +3806,17 @@ class TypeRepository extends AbstractResourceRepository {
3724
3806
  value
3725
3807
  }) => {
3726
3808
  resource.fieldDefinitions.forEach(field => {
3727
- if (field.name == fieldName) {
3809
+ if (field.name === fieldName) {
3728
3810
  // TODO, should be done better i suppose
3729
- if (field.type.name == 'Enum') {
3811
+ if (field.type.name === 'Enum') {
3730
3812
  field.type.values.forEach(v => {
3731
- if (v.key == value.key) {
3813
+ if (v.key === value.key) {
3732
3814
  v.label = value.label;
3733
3815
  }
3734
3816
  });
3735
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3817
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3736
3818
  field.type.elementType.values.forEach(v => {
3737
- if (v.key == value.key) {
3819
+ if (v.key === value.key) {
3738
3820
  v.label = value.label;
3739
3821
  }
3740
3822
  });
@@ -3790,7 +3872,7 @@ class ZoneRepository extends AbstractResourceRepository {
3790
3872
  location
3791
3873
  }) => {
3792
3874
  resource.locations = resource.locations.filter(loc => {
3793
- return !(loc.country == location.country && loc.state == location.state);
3875
+ return !(loc.country === location.country && loc.state === location.state);
3794
3876
  });
3795
3877
  },
3796
3878
  changeName: (projectKey, resource, {
@@ -3840,6 +3922,77 @@ class ZoneService extends AbstractService {
3840
3922
 
3841
3923
  }
3842
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
+ router.post('/login', this.signIn.bind(this));
3945
+ parent.use(`/${basePath}`, router);
3946
+ }
3947
+
3948
+ getMe(request, response) {
3949
+ const resource = this.repository.getMe(request.params.projectKey);
3950
+
3951
+ if (!resource) {
3952
+ return response.status(404).send('Not found');
3953
+ }
3954
+
3955
+ return response.status(200).send(resource);
3956
+ }
3957
+
3958
+ signUp(request, response) {
3959
+ const draft = request.body;
3960
+ const resource = this.repository.create(request.params.projectKey, draft);
3961
+
3962
+ const result = this._expandWithId(request, resource.id);
3963
+
3964
+ return response.status(this.createStatusCode).send({
3965
+ customer: result
3966
+ });
3967
+ }
3968
+
3969
+ signIn(request, response) {
3970
+ const {
3971
+ email,
3972
+ password
3973
+ } = request.body;
3974
+ const encodedPassword = Buffer.from(password).toString('base64');
3975
+ const result = this.repository.query(request.params.projectKey, {
3976
+ where: [`email = "${email}"`, `password = "${encodedPassword}"`]
3977
+ });
3978
+
3979
+ if (result.count === 0) {
3980
+ return response.status(400).send({
3981
+ message: 'Account with the given credentials not found.',
3982
+ errors: [{
3983
+ code: 'InvalidCredentials',
3984
+ message: 'Account with the given credentials not found.'
3985
+ }]
3986
+ });
3987
+ }
3988
+
3989
+ return response.status(200).send({
3990
+ customer: result.results[0]
3991
+ });
3992
+ }
3993
+
3994
+ }
3995
+
3843
3996
  const DEFAULT_OPTIONS = {
3844
3997
  enableAuthentication: false,
3845
3998
  validateCredentials: false,
@@ -3938,7 +4091,7 @@ class CommercetoolsMock {
3938
4091
  order: new OrderService(projectRouter, this._storage),
3939
4092
  payment: new PaymentService(projectRouter, this._storage),
3940
4093
  'my-cart': new MyCartService(projectRouter, this._storage),
3941
- 'my-customer': new myCustomer.MyCustomerService(projectRouter, this._storage),
4094
+ 'my-customer': new MyCustomerService(projectRouter, this._storage),
3942
4095
  'my-payment': new MyPaymentService(projectRouter, this._storage),
3943
4096
  'shipping-method': new ShippingMethodService(projectRouter, this._storage),
3944
4097
  'product-type': new ProductTypeService(projectRouter, this._storage),