@labdigital/commercetools-mock 0.5.19 → 0.5.22

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 (41) hide show
  1. package/dist/commercetools-mock.cjs.development.js +478 -335
  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 +478 -335
  6. package/dist/commercetools-mock.esm.js.map +1 -1
  7. package/dist/repositories/customer.d.ts +5 -1
  8. package/dist/repositories/order.d.ts +2 -1
  9. package/dist/repositories/store.d.ts +1 -0
  10. package/dist/services/cart.d.ts +3 -0
  11. package/dist/services/customer.d.ts +1 -0
  12. package/dist/services/my-customer.d.ts +1 -0
  13. package/dist/services/my-order.d.ts +10 -0
  14. package/dist/services/store.d.ts +3 -1
  15. package/dist/types.d.ts +1 -1
  16. package/package.json +1 -1
  17. package/src/.env +0 -0
  18. package/src/ctMock.ts +2 -0
  19. package/src/lib/masking.ts +1 -3
  20. package/src/repositories/abstract.ts +1 -0
  21. package/src/repositories/category.ts +6 -6
  22. package/src/repositories/customer.ts +14 -0
  23. package/src/repositories/helpers.ts +0 -1
  24. package/src/repositories/order.ts +22 -0
  25. package/src/repositories/product-type.ts +3 -3
  26. package/src/repositories/project.ts +0 -1
  27. package/src/repositories/shipping-method.ts +11 -12
  28. package/src/repositories/store.ts +15 -0
  29. package/src/repositories/subscription.ts +2 -2
  30. package/src/repositories/tax-category.ts +1 -1
  31. package/src/repositories/type.ts +10 -10
  32. package/src/repositories/zone.ts +3 -1
  33. package/src/services/cart.ts +32 -0
  34. package/src/services/customer.ts +21 -0
  35. package/src/services/my-customer.ts +25 -0
  36. package/src/services/my-order.ts +35 -0
  37. package/src/services/product.test.ts +1 -5
  38. package/src/services/project.ts +1 -1
  39. package/src/services/shipping-method.ts +1 -1
  40. package/src/services/store.ts +16 -1
  41. package/src/types.ts +1 -0
@@ -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
  }
@@ -1,4 +1,4 @@
1
- import { Order, OrderAddPaymentAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderFromCartDraft, OrderImportDraft, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, ReferenceTypeId } from '@commercetools/platform-sdk';
1
+ import { Order, OrderAddPaymentAction, OrderChangeOrderStateAction, OrderChangePaymentStateAction, OrderFromCartDraft, OrderImportDraft, OrderSetBillingAddressAction, OrderSetCustomerEmailAction, OrderSetCustomFieldAction, OrderSetCustomTypeAction, OrderSetLocaleAction, OrderSetOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, OrderTransitionStateAction, ReferenceTypeId } from '@commercetools/platform-sdk';
2
2
  import { AbstractResourceRepository, QueryParams } from './abstract';
3
3
  import { Writable } from '../types';
4
4
  export declare class OrderRepository extends AbstractResourceRepository {
@@ -12,6 +12,7 @@ export declare class OrderRepository extends AbstractResourceRepository {
12
12
  addPayment: (projectKey: string, resource: Writable<Order>, { payment }: OrderAddPaymentAction) => void;
13
13
  changeOrderState: (projectKey: string, resource: Writable<Order>, { orderState }: OrderChangeOrderStateAction) => void;
14
14
  changePaymentState: (projectKey: string, resource: Writable<Order>, { paymentState }: OrderChangePaymentStateAction) => void;
15
+ transitionState: (projectKey: string, resource: Writable<Order>, { state }: OrderTransitionStateAction) => void;
15
16
  setBillingAddress: (projectKey: string, resource: Writable<Order>, { address }: OrderSetBillingAddressAction) => void;
16
17
  setCustomerEmail: (projectKey: string, resource: Writable<Order>, { email }: OrderSetCustomerEmailAction) => void;
17
18
  setCustomField: (projectKey: string, resource: Order, { name, value }: OrderSetCustomFieldAction) => void;
@@ -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
  }
@@ -2,8 +2,11 @@ import AbstractService from './abstract';
2
2
  import { Router } from 'express';
3
3
  import { CartRepository } from '../repositories/cart';
4
4
  import { AbstractStorage } from '../storage';
5
+ import { OrderRepository } from '../repositories/order';
5
6
  export declare class CartService extends AbstractService {
6
7
  repository: CartRepository;
8
+ orderRepository: OrderRepository;
7
9
  constructor(parent: Router, storage: AbstractStorage);
8
10
  getBasePath(): string;
11
+ extraRoutes(parent: Router): void;
9
12
  }
@@ -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
  }
@@ -9,4 +9,5 @@ export declare class MyCustomerService extends AbstractService {
9
9
  registerRoutes(parent: Router): void;
10
10
  getMe(request: Request, response: Response): Response<any, Record<string, any>>;
11
11
  signUp(request: Request, response: Response): Response<any, Record<string, any>>;
12
+ signIn(request: Request, response: Response): Response<any, Record<string, any>>;
12
13
  }
@@ -0,0 +1,10 @@
1
+ import AbstractService from './abstract';
2
+ import { Router } from 'express';
3
+ import { AbstractStorage } from '../storage';
4
+ import { OrderRepository } from '../repositories/order';
5
+ export declare class MyOrderService extends AbstractService {
6
+ repository: OrderRepository;
7
+ constructor(parent: Router, storage: AbstractStorage);
8
+ getBasePath(): string;
9
+ registerRoutes(parent: Router): void;
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/dist/types.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare type Writable<T> = {
18
18
  -readonly [P in keyof T]: Writable<T[P]>;
19
19
  };
20
20
  export declare type RepositoryTypes = ReferenceTypeId | 'product-projection';
21
- export declare type ServiceTypes = RepositoryTypes | 'my-cart' | 'my-payment' | 'my-customer';
21
+ export declare type ServiceTypes = RepositoryTypes | 'my-cart' | 'my-order' | 'my-payment' | 'my-customer';
22
22
  export declare type Services = Partial<{
23
23
  [index in ServiceTypes]: AbstractService;
24
24
  }>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.5.19",
2
+ "version": "0.5.22",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
package/src/.env ADDED
File without changes
package/src/ctMock.ts CHANGED
@@ -38,6 +38,7 @@ import { TaxCategoryService } from './services/tax-category'
38
38
  import { TypeService } from './services/type'
39
39
  import { ZoneService } from './services/zone'
40
40
  import { MyCustomerService } from './services/my-customer'
41
+ import { MyOrderService } from './services/my-order'
41
42
 
42
43
  export type CommercetoolsMockOptions = {
43
44
  validateCredentials: boolean
@@ -164,6 +165,7 @@ export class CommercetoolsMock {
164
165
  order: new OrderService(projectRouter, this._storage),
165
166
  payment: new PaymentService(projectRouter, this._storage),
166
167
  'my-cart': new MyCartService(projectRouter, this._storage),
168
+ 'my-order': new MyOrderService(projectRouter, this._storage),
167
169
  'my-customer': new MyCustomerService(projectRouter, this._storage),
168
170
  'my-payment': new MyPaymentService(projectRouter, this._storage),
169
171
  'shipping-method': new ShippingMethodService(
@@ -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
  }
@@ -44,6 +44,7 @@ export abstract class AbstractRepository {
44
44
 
45
45
  actions.forEach(action => {
46
46
  const updateFunc = this.actions[action.action]
47
+
47
48
  if (!updateFunc) {
48
49
  console.error(`No mock implemented for update action ${action.action}`)
49
50
  return
@@ -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
 
@@ -10,6 +12,7 @@ export class CustomerRepository extends AbstractResourceRepository {
10
12
  getTypeId(): ReferenceTypeId {
11
13
  return 'customer'
12
14
  }
15
+
13
16
  create(projectKey: string, draft: CustomerDraft): Customer {
14
17
  const resource: Customer = {
15
18
  ...getBaseResourceProperties(),
@@ -21,6 +24,7 @@ export class CustomerRepository extends AbstractResourceRepository {
21
24
  this.save(projectKey, resource)
22
25
  return resource
23
26
  }
27
+
24
28
  getMe(projectKey: string): Customer | undefined {
25
29
  const results = this._storage.query(projectKey, this.getTypeId(), {}) // grab the first customer you can find
26
30
  if (results.count > 0) {
@@ -29,4 +33,14 @@ export class CustomerRepository extends AbstractResourceRepository {
29
33
 
30
34
  return
31
35
  }
36
+
37
+ actions = {
38
+ changeEmail: (
39
+ _projectKey: string,
40
+ resource: Writable<Customer>,
41
+ { email }: CustomerChangeEmailAction
42
+ ) => {
43
+ resource.email = email
44
+ },
45
+ }
32
46
  }
@@ -24,7 +24,6 @@ export const createCustomFields = (
24
24
  if (!draft.type) return undefined
25
25
  if (!draft.type.typeId) return undefined
26
26
  if (!draft.fields) return undefined
27
-
28
27
  const typeResource = storage.getByResourceIdentifier(
29
28
  projectKey,
30
29
  draft.type
@@ -20,10 +20,14 @@ import {
20
20
  OrderSetOrderNumberAction,
21
21
  OrderSetShippingAddressAction,
22
22
  OrderSetStoreAction,
23
+ OrderState,
24
+ OrderStateTransitionMessage,
25
+ OrderTransitionStateAction,
23
26
  Product,
24
27
  ProductPagedQueryResponse,
25
28
  ProductVariant,
26
29
  ReferenceTypeId,
30
+ State,
27
31
  Store,
28
32
  } from '@commercetools/platform-sdk'
29
33
  import { AbstractResourceRepository, QueryParams } from './abstract'
@@ -252,6 +256,24 @@ export class OrderRepository extends AbstractResourceRepository {
252
256
  ) => {
253
257
  resource.paymentState = paymentState
254
258
  },
259
+ transitionState: (
260
+ projectKey: string,
261
+ resource: Writable<Order>,
262
+ { state }: OrderTransitionStateAction
263
+ ) => {
264
+ const resolvedType = this._storage.getByResourceIdentifier(
265
+ projectKey,
266
+ state
267
+ ) as State | null
268
+
269
+ if (!resolvedType) {
270
+ throw new Error(
271
+ `No state found with key=${state.key} or id=${state.key}`
272
+ )
273
+ }
274
+
275
+ resource.state = { typeId: 'state', id: resolvedType.id }
276
+ },
255
277
  setBillingAddress: (
256
278
  projectKey: string,
257
279
  resource: Writable<Order>,
@@ -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,16 +2,48 @@ import AbstractService from './abstract'
2
2
  import { Router } from 'express'
3
3
  import { CartRepository } from '../repositories/cart'
4
4
  import { AbstractStorage } from '../storage'
5
+ import { Cart, Order } from '@commercetools/platform-sdk'
6
+ import { OrderRepository } from '../repositories/order'
5
7
 
6
8
  export class CartService extends AbstractService {
7
9
  public repository: CartRepository
10
+ public orderRepository: OrderRepository
8
11
 
9
12
  constructor(parent: Router, storage: AbstractStorage) {
10
13
  super(parent)
11
14
  this.repository = new CartRepository(storage)
15
+ this.orderRepository = new OrderRepository(storage)
12
16
  }
13
17
 
14
18
  getBasePath() {
15
19
  return 'carts'
16
20
  }
21
+
22
+ extraRoutes(parent: Router) {
23
+ parent.post('/replicate', (request, response) => {
24
+ // @ts-ignore
25
+ const cartOrOrder: Cart | Order | null =
26
+ request.body.reference.typeId === 'order'
27
+ ? this.orderRepository.get(
28
+ request.params.projectKey,
29
+ request.body.reference.id
30
+ )
31
+ : this.repository.get(
32
+ request.params.projectKey,
33
+ request.body.reference.id
34
+ )
35
+
36
+ if (!cartOrOrder) {
37
+ return response.status(400).send()
38
+ }
39
+
40
+ const newCart = this.repository.create(request.params.projectKey, {
41
+ ...cartOrOrder,
42
+ currency: cartOrOrder.totalPrice.currencyCode,
43
+ discountCodes: [],
44
+ })
45
+
46
+ return response.status(200).send(newCart)
47
+ })
48
+ }
17
49
  }
@@ -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
  }
@@ -26,6 +26,8 @@ export class MyCustomerService extends AbstractService {
26
26
 
27
27
  router.post('/signup', this.signUp.bind(this))
28
28
 
29
+ router.post('/login', this.signIn.bind(this))
30
+
29
31
  parent.use(`/${basePath}`, router)
30
32
  }
31
33
 
@@ -43,4 +45,27 @@ export class MyCustomerService extends AbstractService {
43
45
  const result = this._expandWithId(request, resource.id)
44
46
  return response.status(this.createStatusCode).send({ customer: result })
45
47
  }
48
+
49
+ signIn(request: Request, response: Response) {
50
+ const { email, password } = request.body
51
+ const encodedPassword = Buffer.from(password).toString('base64')
52
+
53
+ const result = this.repository.query(request.params.projectKey, {
54
+ where: [`email = "${email}"`, `password = "${encodedPassword}"`],
55
+ })
56
+
57
+ if (result.count === 0) {
58
+ return response.status(400).send({
59
+ message: 'Account with the given credentials not found.',
60
+ errors: [
61
+ {
62
+ code: 'InvalidCredentials',
63
+ message: 'Account with the given credentials not found.',
64
+ },
65
+ ],
66
+ })
67
+ }
68
+
69
+ return response.status(200).send({ customer: result.results[0] })
70
+ }
46
71
  }