@labdigital/commercetools-mock 0.5.21 → 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.
@@ -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;
@@ -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
  }
@@ -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
+ }
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.21",
2
+ "version": "0.5.22",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
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(
@@ -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
@@ -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>,
@@ -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
  }
@@ -0,0 +1,35 @@
1
+ import AbstractService from './abstract'
2
+ import { Router } from 'express'
3
+ import { AbstractStorage } from '../storage'
4
+ import { OrderRepository } from '../repositories/order'
5
+
6
+ export class MyOrderService extends AbstractService {
7
+ public repository: OrderRepository
8
+
9
+ constructor(parent: Router, storage: AbstractStorage) {
10
+ super(parent)
11
+ this.repository = new OrderRepository(storage)
12
+ }
13
+
14
+ getBasePath() {
15
+ return 'me'
16
+ }
17
+
18
+ registerRoutes(parent: Router) {
19
+ // Overwrite this function to be able to handle /me/active-cart path.
20
+ const basePath = this.getBasePath()
21
+ const router = Router({ mergeParams: true })
22
+
23
+ this.extraRoutes(router)
24
+
25
+ router.get('/orders/', this.get.bind(this))
26
+ router.get('/orders/:id', this.getWithId.bind(this))
27
+
28
+ router.delete('/orders/:id', this.deletewithId.bind(this))
29
+
30
+ router.post('/orders/', this.post.bind(this))
31
+ router.post('/orders/:id', this.postWithId.bind(this))
32
+
33
+ parent.use(`/${basePath}`, router)
34
+ }
35
+ }
package/src/types.ts CHANGED
@@ -21,6 +21,7 @@ export type RepositoryTypes = ReferenceTypeId | 'product-projection'
21
21
  export type ServiceTypes =
22
22
  | RepositoryTypes
23
23
  | 'my-cart'
24
+ | 'my-order'
24
25
  | 'my-payment'
25
26
  | 'my-customer'
26
27