@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.
@@ -10,7 +10,6 @@ import bodyParser from 'body-parser';
10
10
  import { randomBytes } from 'crypto';
11
11
  import { v4 } from 'uuid';
12
12
  import deepEqual from 'deep-equal';
13
- import { MyCustomerService } from 'services/my-customer';
14
13
 
15
14
  const parseExpandClause = clause => {
16
15
  const result = {
@@ -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
  }
@@ -1838,6 +1849,24 @@ class CustomerService extends AbstractService {
1838
1849
  return 'customers';
1839
1850
  }
1840
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
+
1841
1870
  }
1842
1871
 
1843
1872
  class CustomObjectRepository extends AbstractResourceRepository {
@@ -2271,6 +2300,31 @@ class PaymentRepository extends AbstractResourceRepository {
2271
2300
  transaction
2272
2301
  }) => {
2273
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
+ };
2274
2328
  }
2275
2329
  };
2276
2330
  }
@@ -2900,7 +2954,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2900
2954
  switch (type.name) {
2901
2955
  case 'lenum':
2902
2956
  type.values.forEach(v => {
2903
- if (v.key == newValue.key) {
2957
+ if (v.key === newValue.key) {
2904
2958
  v.label = newValue.label;
2905
2959
  }
2906
2960
  });
@@ -2913,7 +2967,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2913
2967
  };
2914
2968
 
2915
2969
  (_resource$attributes = resource.attributes) == null ? void 0 : _resource$attributes.forEach(value => {
2916
- if (value.name == attributeName) {
2970
+ if (value.name === attributeName) {
2917
2971
  updateAttributeType(value.type);
2918
2972
  }
2919
2973
  });
@@ -2925,7 +2979,7 @@ class ProductTypeRepository extends AbstractResourceRepository {
2925
2979
  var _resource$attributes2;
2926
2980
 
2927
2981
  (_resource$attributes2 = resource.attributes) == null ? void 0 : _resource$attributes2.forEach(value => {
2928
- if (value.name == attributeName) {
2982
+ if (value.name === attributeName) {
2929
2983
  value.label = label;
2930
2984
  }
2931
2985
  });
@@ -3005,7 +3059,7 @@ const maskSecretValue = (resource, path) => {
3005
3059
  const part = parts[i];
3006
3060
  val = val[part];
3007
3061
 
3008
- if (val == undefined) {
3062
+ if (val === undefined) {
3009
3063
  return resource;
3010
3064
  }
3011
3065
  }
@@ -3097,8 +3151,6 @@ class ProjectRepository extends AbstractRepository {
3097
3151
  }
3098
3152
 
3099
3153
  get(projectKey) {
3100
- const data = this._storage.getProject(projectKey);
3101
-
3102
3154
  const resource = this._storage.getProject(projectKey);
3103
3155
 
3104
3156
  const masked = maskSecretValue(resource, 'externalOAuth.authorizationHeader');
@@ -3152,7 +3204,7 @@ class ProjectService {
3152
3204
  return response.status(404).send({});
3153
3205
  }
3154
3206
 
3155
- const updatedResource = this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3207
+ this.repository.processUpdateActions(request.params.projectKey, project, updateRequest.actions);
3156
3208
  return response.status(200).send({});
3157
3209
  }
3158
3210
 
@@ -3180,14 +3232,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3180
3232
  };
3181
3233
 
3182
3234
  this.actions = {
3183
- addShippingRate: (projectKey, resource, {
3235
+ addShippingRate: (_projectKey, resource, {
3184
3236
  shippingRate,
3185
3237
  zone
3186
3238
  }) => {
3187
3239
  const rate = this._transformShippingRate(shippingRate);
3188
3240
 
3189
3241
  resource.zoneRates.forEach(zoneRate => {
3190
- if (zoneRate.zone.id == zone.id) {
3242
+ if (zoneRate.zone.id === zone.id) {
3191
3243
  zoneRate.shippingRates.push(rate);
3192
3244
  return;
3193
3245
  }
@@ -3200,14 +3252,14 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3200
3252
  shippingRates: [rate]
3201
3253
  });
3202
3254
  },
3203
- removeShippingRate: (projectKey, resource, {
3255
+ removeShippingRate: (_projectKey, resource, {
3204
3256
  shippingRate,
3205
3257
  zone
3206
3258
  }) => {
3207
3259
  const rate = this._transformShippingRate(shippingRate);
3208
3260
 
3209
3261
  resource.zoneRates.forEach(zoneRate => {
3210
- if (zoneRate.zone.id == zone.id) {
3262
+ if (zoneRate.zone.id === zone.id) {
3211
3263
  zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
3212
3264
  return !deepEqual(rate, otherRate);
3213
3265
  });
@@ -3228,39 +3280,39 @@ class ShippingMethodRepository extends AbstractResourceRepository {
3228
3280
  shippingRates: []
3229
3281
  });
3230
3282
  },
3231
- removeZone: (projectKey, resource, {
3283
+ removeZone: (_projectKey, resource, {
3232
3284
  zone
3233
3285
  }) => {
3234
3286
  resource.zoneRates = resource.zoneRates.filter(zoneRate => {
3235
3287
  return zoneRate.zone.id !== zone.id;
3236
3288
  });
3237
3289
  },
3238
- setKey: (projectKey, resource, {
3290
+ setKey: (_projectKey, resource, {
3239
3291
  key
3240
3292
  }) => {
3241
3293
  resource.key = key;
3242
3294
  },
3243
- setDescription: (projectKey, resource, {
3295
+ setDescription: (_projectKey, resource, {
3244
3296
  description
3245
3297
  }) => {
3246
3298
  resource.description = description;
3247
3299
  },
3248
- setLocalizedDescription: (projectKey, resource, {
3300
+ setLocalizedDescription: (_projectKey, resource, {
3249
3301
  localizedDescription
3250
3302
  }) => {
3251
3303
  resource.localizedDescription = localizedDescription;
3252
3304
  },
3253
- setPredicate: (projectKey, resource, {
3305
+ setPredicate: (_projectKey, resource, {
3254
3306
  predicate
3255
3307
  }) => {
3256
3308
  resource.predicate = predicate;
3257
3309
  },
3258
- changeIsDefault: (projectKey, resource, {
3310
+ changeIsDefault: (_projectKey, resource, {
3259
3311
  isDefault
3260
3312
  }) => {
3261
3313
  resource.isDefault = isDefault;
3262
3314
  },
3263
- changeName: (projectKey, resource, {
3315
+ changeName: (_projectKey, resource, {
3264
3316
  name
3265
3317
  }) => {
3266
3318
  resource.name = name;
@@ -3456,6 +3508,22 @@ class StoreRepository extends AbstractResourceRepository {
3456
3508
  return channels.map(ref => getReferenceFromResourceIdentifier(ref, projectKey, this._storage));
3457
3509
  }
3458
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
+
3459
3527
  }
3460
3528
 
3461
3529
  class StoreService extends AbstractService {
@@ -3468,6 +3536,20 @@ class StoreService extends AbstractService {
3468
3536
  return 'stores';
3469
3537
  }
3470
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
+
3471
3553
  }
3472
3554
 
3473
3555
  class SubscriptionRepository extends AbstractResourceRepository {
@@ -3478,11 +3560,11 @@ class SubscriptionRepository extends AbstractResourceRepository {
3478
3560
  create(projectKey, draft) {
3479
3561
  // TODO: We could actually test this here by using the aws sdk. For now
3480
3562
  // hardcode a failed check when account id is 0000000000
3481
- if (draft.destination.type == 'SQS') {
3563
+ if (draft.destination.type === 'SQS') {
3482
3564
  const queueURL = new URL(draft.destination.queueUrl);
3483
3565
  const accountId = queueURL.pathname.split('/')[1];
3484
3566
 
3485
- if (accountId == '0000000000') {
3567
+ if (accountId === '0000000000') {
3486
3568
  const dest = draft.destination;
3487
3569
  throw new CommercetoolsError({
3488
3570
  code: 'InvalidInput',
@@ -3562,7 +3644,7 @@ class TaxCategoryRepository extends AbstractResourceRepository {
3562
3644
  for (let i = 0; i < resource.rates.length; i++) {
3563
3645
  const rate = resource.rates[i];
3564
3646
 
3565
- if (rate.id == taxRateId) {
3647
+ if (rate.id === taxRateId) {
3566
3648
  resource.rates[i] = taxRateObj;
3567
3649
  }
3568
3650
  }
@@ -3700,11 +3782,11 @@ class TypeRepository extends AbstractResourceRepository {
3700
3782
  value
3701
3783
  }) => {
3702
3784
  resource.fieldDefinitions.forEach(field => {
3703
- if (field.name == fieldName) {
3785
+ if (field.name === fieldName) {
3704
3786
  // TODO, should be done better i suppose
3705
- if (field.type.name == 'Enum') {
3787
+ if (field.type.name === 'Enum') {
3706
3788
  field.type.values.push(value);
3707
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3789
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3708
3790
  field.type.elementType.values.push(value);
3709
3791
  } else {
3710
3792
  throw new Error('Type is not a Enum (or Set of Enum)');
@@ -3717,17 +3799,17 @@ class TypeRepository extends AbstractResourceRepository {
3717
3799
  value
3718
3800
  }) => {
3719
3801
  resource.fieldDefinitions.forEach(field => {
3720
- if (field.name == fieldName) {
3802
+ if (field.name === fieldName) {
3721
3803
  // TODO, should be done better i suppose
3722
- if (field.type.name == 'Enum') {
3804
+ if (field.type.name === 'Enum') {
3723
3805
  field.type.values.forEach(v => {
3724
- if (v.key == value.key) {
3806
+ if (v.key === value.key) {
3725
3807
  v.label = value.label;
3726
3808
  }
3727
3809
  });
3728
- } else if (field.type.name == 'Set' && field.type.elementType.name == 'Enum') {
3810
+ } else if (field.type.name === 'Set' && field.type.elementType.name === 'Enum') {
3729
3811
  field.type.elementType.values.forEach(v => {
3730
- if (v.key == value.key) {
3812
+ if (v.key === value.key) {
3731
3813
  v.label = value.label;
3732
3814
  }
3733
3815
  });
@@ -3783,7 +3865,7 @@ class ZoneRepository extends AbstractResourceRepository {
3783
3865
  location
3784
3866
  }) => {
3785
3867
  resource.locations = resource.locations.filter(loc => {
3786
- return !(loc.country == location.country && loc.state == location.state);
3868
+ return !(loc.country === location.country && loc.state === location.state);
3787
3869
  });
3788
3870
  },
3789
3871
  changeName: (projectKey, resource, {
@@ -3833,6 +3915,77 @@ class ZoneService extends AbstractService {
3833
3915
 
3834
3916
  }
3835
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
+ router.post('/login', this.signIn.bind(this));
3938
+ parent.use(`/${basePath}`, router);
3939
+ }
3940
+
3941
+ getMe(request, response) {
3942
+ const resource = this.repository.getMe(request.params.projectKey);
3943
+
3944
+ if (!resource) {
3945
+ return response.status(404).send('Not found');
3946
+ }
3947
+
3948
+ return response.status(200).send(resource);
3949
+ }
3950
+
3951
+ signUp(request, response) {
3952
+ const draft = request.body;
3953
+ const resource = this.repository.create(request.params.projectKey, draft);
3954
+
3955
+ const result = this._expandWithId(request, resource.id);
3956
+
3957
+ return response.status(this.createStatusCode).send({
3958
+ customer: result
3959
+ });
3960
+ }
3961
+
3962
+ signIn(request, response) {
3963
+ const {
3964
+ email,
3965
+ password
3966
+ } = request.body;
3967
+ const encodedPassword = Buffer.from(password).toString('base64');
3968
+ const result = this.repository.query(request.params.projectKey, {
3969
+ where: [`email = "${email}"`, `password = "${encodedPassword}"`]
3970
+ });
3971
+
3972
+ if (result.count === 0) {
3973
+ return response.status(400).send({
3974
+ message: 'Account with the given credentials not found.',
3975
+ errors: [{
3976
+ code: 'InvalidCredentials',
3977
+ message: 'Account with the given credentials not found.'
3978
+ }]
3979
+ });
3980
+ }
3981
+
3982
+ return response.status(200).send({
3983
+ customer: result.results[0]
3984
+ });
3985
+ }
3986
+
3987
+ }
3988
+
3836
3989
  const DEFAULT_OPTIONS = {
3837
3990
  enableAuthentication: false,
3838
3991
  validateCredentials: false,