@labdigital/commercetools-mock 0.5.19 → 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.
@@ -1,7 +1,11 @@
1
- import { Customer, CustomerDraft, ReferenceTypeId } from '@commercetools/platform-sdk';
1
+ import { Customer, CustomerChangeEmailAction, CustomerDraft, ReferenceTypeId } from '@commercetools/platform-sdk';
2
+ import { Writable } from 'types';
2
3
  import { AbstractResourceRepository } from './abstract';
3
4
  export declare class CustomerRepository extends AbstractResourceRepository {
4
5
  getTypeId(): ReferenceTypeId;
5
6
  create(projectKey: string, draft: CustomerDraft): Customer;
6
7
  getMe(projectKey: string): Customer | undefined;
8
+ actions: {
9
+ changeEmail: (_projectKey: string, resource: Writable<Customer>, { email }: CustomerChangeEmailAction) => void;
10
+ };
7
11
  }
@@ -5,5 +5,6 @@ export declare class StoreRepository extends AbstractResourceRepository {
5
5
  getTypeId(): ReferenceTypeId;
6
6
  create(projectKey: string, draft: StoreDraft): Store;
7
7
  private transformChannels;
8
+ getWithKey(projectKey: string, key: string): Store | undefined;
8
9
  actions: Partial<Record<StoreUpdateAction['action'], (projectKey: string, resource: Writable<Store>, action: any) => void>>;
9
10
  }
@@ -6,4 +6,5 @@ export declare class CustomerService extends AbstractService {
6
6
  repository: CustomerRepository;
7
7
  constructor(parent: Router, storage: AbstractStorage);
8
8
  getBasePath(): string;
9
+ extraRoutes(parent: Router): void;
9
10
  }
@@ -1,9 +1,11 @@
1
1
  import AbstractService from './abstract';
2
- import { Router } from 'express';
2
+ import { Router, Request, Response } from 'express';
3
3
  import { StoreRepository } from '../repositories/store';
4
4
  import { AbstractStorage } from '../storage';
5
5
  export declare class StoreService extends AbstractService {
6
6
  repository: StoreRepository;
7
7
  constructor(parent: Router, storage: AbstractStorage);
8
8
  getBasePath(): string;
9
+ extraRoutes(router: Router): void;
10
+ getWithKey(request: Request, response: Response): Response<any, Record<string, any>>;
9
11
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.5.19",
2
+ "version": "0.5.20",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,5 +1,3 @@
1
- import { json } from 'express'
2
-
3
1
  export const maskSecretValue = <T>(resource: T, path: string): T => {
4
2
  const parts = path.split('.')
5
3
  const clone = JSON.parse(JSON.stringify(resource))
@@ -10,7 +8,7 @@ export const maskSecretValue = <T>(resource: T, path: string): T => {
10
8
  const part = parts[i]
11
9
  val = val[part]
12
10
 
13
- if (val == undefined) {
11
+ if (val === undefined) {
14
12
  return resource
15
13
  }
16
14
  }
@@ -59,10 +59,10 @@ export class CategoryRepository extends AbstractResourceRepository {
59
59
  { assetId, assetKey, name }: CategoryChangeAssetNameAction
60
60
  ) => {
61
61
  resource.assets?.forEach(asset => {
62
- if (assetId && assetId == asset.id) {
62
+ if (assetId && assetId === asset.id) {
63
63
  asset.name = name
64
64
  }
65
- if (assetKey && assetKey == asset.key) {
65
+ if (assetKey && assetKey === asset.key) {
66
66
  asset.name = name
67
67
  }
68
68
  })
@@ -87,10 +87,10 @@ export class CategoryRepository extends AbstractResourceRepository {
87
87
  { assetId, assetKey, description }: CategorySetAssetDescriptionAction
88
88
  ) => {
89
89
  resource.assets?.forEach(asset => {
90
- if (assetId && assetId == asset.id) {
90
+ if (assetId && assetId === asset.id) {
91
91
  asset.description = description
92
92
  }
93
- if (assetKey && assetKey == asset.key) {
93
+ if (assetKey && assetKey === asset.key) {
94
94
  asset.description = description
95
95
  }
96
96
  })
@@ -101,10 +101,10 @@ export class CategoryRepository extends AbstractResourceRepository {
101
101
  { assetId, assetKey, sources }: CategorySetAssetSourcesAction
102
102
  ) => {
103
103
  resource.assets?.forEach(asset => {
104
- if (assetId && assetId == asset.id) {
104
+ if (assetId && assetId === asset.id) {
105
105
  asset.sources = sources
106
106
  }
107
- if (assetKey && assetKey == asset.key) {
107
+ if (assetKey && assetKey === asset.key) {
108
108
  asset.sources = sources
109
109
  }
110
110
  })
@@ -1,8 +1,10 @@
1
1
  import {
2
2
  Customer,
3
+ CustomerChangeEmailAction,
3
4
  CustomerDraft,
4
5
  ReferenceTypeId,
5
6
  } from '@commercetools/platform-sdk'
7
+ import { Writable } from 'types'
6
8
  import { getBaseResourceProperties } from '../helpers'
7
9
  import { AbstractResourceRepository } from './abstract'
8
10
 
@@ -29,4 +31,14 @@ export class CustomerRepository extends AbstractResourceRepository {
29
31
 
30
32
  return
31
33
  }
34
+
35
+ actions = {
36
+ changeEmail: (
37
+ _projectKey: string,
38
+ resource: Writable<Customer>,
39
+ { email }: CustomerChangeEmailAction
40
+ ) => {
41
+ resource.email = email
42
+ },
43
+ }
32
44
  }
@@ -77,7 +77,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
77
77
  switch (type.name) {
78
78
  case 'lenum':
79
79
  type.values.forEach(v => {
80
- if (v.key == newValue.key) {
80
+ if (v.key === newValue.key) {
81
81
  v.label = newValue.label
82
82
  }
83
83
  })
@@ -89,7 +89,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
89
89
  }
90
90
 
91
91
  resource.attributes?.forEach(value => {
92
- if (value.name == attributeName) {
92
+ if (value.name === attributeName) {
93
93
  updateAttributeType(value.type)
94
94
  }
95
95
  })
@@ -100,7 +100,7 @@ export class ProductTypeRepository extends AbstractResourceRepository {
100
100
  { attributeName, label }: ProductTypeChangeLabelAction
101
101
  ) => {
102
102
  resource.attributes?.forEach(value => {
103
- if (value.name == attributeName) {
103
+ if (value.name === attributeName) {
104
104
  value.label = label
105
105
  }
106
106
  })
@@ -22,7 +22,6 @@ import { maskSecretValue } from '../lib/masking'
22
22
 
23
23
  export class ProjectRepository extends AbstractRepository {
24
24
  get(projectKey: string): Project | null {
25
- const data = this._storage.getProject(projectKey)
26
25
  const resource = this._storage.getProject(projectKey)
27
26
  const masked = maskSecretValue<Project>(
28
27
  resource,
@@ -26,7 +26,6 @@ import {
26
26
  import { getBaseResourceProperties } from '../helpers'
27
27
  import { AbstractResourceRepository } from './abstract'
28
28
  import { Writable } from 'types'
29
- import { _ } from 'ajv'
30
29
  import deepEqual from 'deep-equal'
31
30
 
32
31
  export class ShippingMethodRepository extends AbstractResourceRepository {
@@ -84,14 +83,14 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
84
83
  >
85
84
  > = {
86
85
  addShippingRate: (
87
- projectKey: string,
86
+ _projectKey: string,
88
87
  resource: Writable<ShippingMethod>,
89
88
  { shippingRate, zone }: ShippingMethodAddShippingRateAction
90
89
  ) => {
91
90
  const rate = this._transformShippingRate(shippingRate)
92
91
 
93
92
  resource.zoneRates.forEach(zoneRate => {
94
- if (zoneRate.zone.id == zone.id) {
93
+ if (zoneRate.zone.id === zone.id) {
95
94
  zoneRate.shippingRates.push(rate)
96
95
  return
97
96
  }
@@ -105,14 +104,14 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
105
104
  })
106
105
  },
107
106
  removeShippingRate: (
108
- projectKey: string,
107
+ _projectKey: string,
109
108
  resource: Writable<ShippingMethod>,
110
109
  { shippingRate, zone }: ShippingMethodAddShippingRateAction
111
110
  ) => {
112
111
  const rate = this._transformShippingRate(shippingRate)
113
112
 
114
113
  resource.zoneRates.forEach(zoneRate => {
115
- if (zoneRate.zone.id == zone.id) {
114
+ if (zoneRate.zone.id === zone.id) {
116
115
  zoneRate.shippingRates = zoneRate.shippingRates.filter(otherRate => {
117
116
  return !deepEqual(rate, otherRate)
118
117
  })
@@ -140,7 +139,7 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
140
139
  })
141
140
  },
142
141
  removeZone: (
143
- projectKey: string,
142
+ _projectKey: string,
144
143
  resource: Writable<ShippingMethod>,
145
144
  { zone }: ShippingMethodRemoveZoneAction
146
145
  ) => {
@@ -149,42 +148,42 @@ export class ShippingMethodRepository extends AbstractResourceRepository {
149
148
  })
150
149
  },
151
150
  setKey: (
152
- projectKey: string,
151
+ _projectKey: string,
153
152
  resource: Writable<ShippingMethod>,
154
153
  { key }: ShippingMethodSetKeyAction
155
154
  ) => {
156
155
  resource.key = key
157
156
  },
158
157
  setDescription: (
159
- projectKey: string,
158
+ _projectKey: string,
160
159
  resource: Writable<ShippingMethod>,
161
160
  { description }: ShippingMethodSetDescriptionAction
162
161
  ) => {
163
162
  resource.description = description
164
163
  },
165
164
  setLocalizedDescription: (
166
- projectKey: string,
165
+ _projectKey: string,
167
166
  resource: Writable<ShippingMethod>,
168
167
  { localizedDescription }: ShippingMethodSetLocalizedDescriptionAction
169
168
  ) => {
170
169
  resource.localizedDescription = localizedDescription
171
170
  },
172
171
  setPredicate: (
173
- projectKey: string,
172
+ _projectKey: string,
174
173
  resource: Writable<ShippingMethod>,
175
174
  { predicate }: ShippingMethodSetPredicateAction
176
175
  ) => {
177
176
  resource.predicate = predicate
178
177
  },
179
178
  changeIsDefault: (
180
- projectKey: string,
179
+ _projectKey: string,
181
180
  resource: Writable<ShippingMethod>,
182
181
  { isDefault }: ShippingMethodChangeIsDefaultAction
183
182
  ) => {
184
183
  resource.isDefault = isDefault
185
184
  },
186
185
  changeName: (
187
- projectKey: string,
186
+ _projectKey: string,
188
187
  resource: Writable<ShippingMethod>,
189
188
  { name }: ShippingMethodChangeNameAction
190
189
  ) => {
@@ -50,6 +50,21 @@ export class StoreRepository extends AbstractResourceRepository {
50
50
  )
51
51
  }
52
52
 
53
+ getWithKey(projectKey: string, key: string): Store | undefined {
54
+ const result = this._storage.query(projectKey, this.getTypeId(), {
55
+ where: [`key="${key}"`],
56
+ })
57
+ if (result.count === 1) {
58
+ return result.results[0] as Store
59
+ }
60
+
61
+ if (result.count > 1) {
62
+ throw new Error('Duplicate store key')
63
+ }
64
+
65
+ return
66
+ }
67
+
53
68
  actions: Partial<
54
69
  Record<
55
70
  StoreUpdateAction['action'],
@@ -15,10 +15,10 @@ export class SubscriptionRepository extends AbstractResourceRepository {
15
15
  create(projectKey: string, draft: SubscriptionDraft): Subscription {
16
16
  // TODO: We could actually test this here by using the aws sdk. For now
17
17
  // hardcode a failed check when account id is 0000000000
18
- if (draft.destination.type == 'SQS') {
18
+ if (draft.destination.type === 'SQS') {
19
19
  const queueURL = new URL(draft.destination.queueUrl)
20
20
  const accountId = queueURL.pathname.split('/')[1]
21
- if (accountId == '0000000000') {
21
+ if (accountId === '0000000000') {
22
22
  const dest = draft.destination
23
23
  throw new CommercetoolsError<InvalidInputError>(
24
24
  {
@@ -94,7 +94,7 @@ export class TaxCategoryRepository extends AbstractResourceRepository {
94
94
  const taxRateObj = this.taxRateFromTaxRateDraft(taxRate)
95
95
  for (let i = 0; i < resource.rates.length; i++) {
96
96
  const rate = resource.rates[i]
97
- if (rate.id == taxRateId) {
97
+ if (rate.id === taxRateId) {
98
98
  resource.rates[i] = taxRateObj
99
99
  }
100
100
  }
@@ -104,13 +104,13 @@ export class TypeRepository extends AbstractResourceRepository {
104
104
  { fieldName, value }: TypeAddEnumValueAction
105
105
  ) => {
106
106
  resource.fieldDefinitions.forEach(field => {
107
- if (field.name == fieldName) {
107
+ if (field.name === fieldName) {
108
108
  // TODO, should be done better i suppose
109
- if (field.type.name == 'Enum') {
109
+ if (field.type.name === 'Enum') {
110
110
  field.type.values.push(value)
111
111
  } else if (
112
- field.type.name == 'Set' &&
113
- field.type.elementType.name == 'Enum'
112
+ field.type.name === 'Set' &&
113
+ field.type.elementType.name === 'Enum'
114
114
  ) {
115
115
  field.type.elementType.values.push(value)
116
116
  } else {
@@ -125,20 +125,20 @@ export class TypeRepository extends AbstractResourceRepository {
125
125
  { fieldName, value }: TypeChangeEnumValueLabelAction
126
126
  ) => {
127
127
  resource.fieldDefinitions.forEach(field => {
128
- if (field.name == fieldName) {
128
+ if (field.name === fieldName) {
129
129
  // TODO, should be done better i suppose
130
- if (field.type.name == 'Enum') {
130
+ if (field.type.name === 'Enum') {
131
131
  field.type.values.forEach(v => {
132
- if (v.key == value.key) {
132
+ if (v.key === value.key) {
133
133
  v.label = value.label
134
134
  }
135
135
  })
136
136
  } else if (
137
- field.type.name == 'Set' &&
138
- field.type.elementType.name == 'Enum'
137
+ field.type.name === 'Set' &&
138
+ field.type.elementType.name === 'Enum'
139
139
  ) {
140
140
  field.type.elementType.values.forEach(v => {
141
- if (v.key == value.key) {
141
+ if (v.key === value.key) {
142
142
  v.label = value.label
143
143
  }
144
144
  })
@@ -49,7 +49,9 @@ export class ZoneRepository extends AbstractResourceRepository {
49
49
  { location }: ZoneRemoveLocationAction
50
50
  ) => {
51
51
  resource.locations = resource.locations.filter(loc => {
52
- return !(loc.country == location.country && loc.state == location.state)
52
+ return !(
53
+ loc.country === location.country && loc.state === location.state
54
+ )
53
55
  })
54
56
  },
55
57
  changeName: (
@@ -2,6 +2,8 @@ import AbstractService from './abstract'
2
2
  import { Router } from 'express'
3
3
  import { CustomerRepository } from '../repositories/customer'
4
4
  import { AbstractStorage } from '../storage'
5
+ import { getBaseResourceProperties } from '../helpers'
6
+ import { v4 as uuidv4 } from 'uuid'
5
7
 
6
8
  export class CustomerService extends AbstractService {
7
9
  public repository: CustomerRepository
@@ -14,4 +16,23 @@ export class CustomerService extends AbstractService {
14
16
  getBasePath() {
15
17
  return 'customers'
16
18
  }
19
+
20
+ extraRoutes(parent: Router) {
21
+ parent.post('/password-token', (request, response) => {
22
+ const customer = this.repository.query(request.params.projectKey, {
23
+ where: [`email="${request.body.email}"`],
24
+ })
25
+ const ttlMinutes: number = request.params.ttlMinutes
26
+ ? +request.params.ttlMinutes
27
+ : 34560
28
+ const { version, ...rest } = getBaseResourceProperties()
29
+
30
+ return response.status(200).send({
31
+ ...rest,
32
+ customerId: customer.results[0].id,
33
+ expiresAt: new Date(Date.now() + ttlMinutes * 60).toISOString(),
34
+ value: uuidv4(),
35
+ })
36
+ })
37
+ }
17
38
  }
@@ -1,8 +1,4 @@
1
- import {
2
- CentPrecisionMoney,
3
- Product,
4
- ProductDraft,
5
- } from '@commercetools/platform-sdk'
1
+ import { Product, ProductDraft } from '@commercetools/platform-sdk'
6
2
  import supertest from 'supertest'
7
3
  import { CommercetoolsMock } from '../index'
8
4
  import assert from 'assert'
@@ -31,7 +31,7 @@ export class ProjectService {
31
31
  return response.status(404).send({})
32
32
  }
33
33
 
34
- const updatedResource = this.repository.processUpdateActions(
34
+ this.repository.processUpdateActions(
35
35
  request.params.projectKey,
36
36
  project,
37
37
  updateRequest.actions
@@ -1,7 +1,7 @@
1
1
  import { ShippingMethodRepository } from '../repositories/shipping-method'
2
2
  import AbstractService from './abstract'
3
3
  import { AbstractStorage } from '../storage'
4
- import { Request, Response, Router } from 'express'
4
+ import { Router } from 'express'
5
5
 
6
6
  export class ShippingMethodService extends AbstractService {
7
7
  public repository: ShippingMethodRepository
@@ -1,5 +1,5 @@
1
1
  import AbstractService from './abstract'
2
- import { Router } from 'express'
2
+ import { Router, Request, Response } from 'express'
3
3
  import { StoreRepository } from '../repositories/store'
4
4
  import { AbstractStorage } from '../storage'
5
5
 
@@ -14,4 +14,19 @@ export class StoreService extends AbstractService {
14
14
  getBasePath() {
15
15
  return 'stores'
16
16
  }
17
+
18
+ extraRoutes(router: Router) {
19
+ router.get('/key=:key', this.getWithKey.bind(this))
20
+ }
21
+
22
+ getWithKey(request: Request, response: Response) {
23
+ const resource = this.repository.getWithKey(
24
+ request.params.projectKey,
25
+ request.params.key
26
+ )
27
+ if (resource) {
28
+ return response.status(200).send(resource)
29
+ }
30
+ return response.status(404).send('Not found')
31
+ }
17
32
  }