Skip to content

Commit fabe776

Browse files
Implement signin, my order, transition order state and replicate cart/order (#31)
* create sign in function * cart route to check
1 parent 4222143 commit fabe776

File tree

7 files changed

+93
-1
lines changed

7 files changed

+93
-1
lines changed

src/ctMock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { TaxCategoryService } from './services/tax-category'
3838
import { TypeService } from './services/type'
3939
import { ZoneService } from './services/zone'
4040
import { MyCustomerService } from './services/my-customer'
41+
import { MyOrderService } from './services/my-order'
4142

4243
export type CommercetoolsMockOptions = {
4344
validateCredentials: boolean
@@ -164,6 +165,7 @@ export class CommercetoolsMock {
164165
order: new OrderService(projectRouter, this._storage),
165166
payment: new PaymentService(projectRouter, this._storage),
166167
'my-cart': new MyCartService(projectRouter, this._storage),
168+
'my-order': new MyOrderService(projectRouter, this._storage),
167169
'my-customer': new MyCustomerService(projectRouter, this._storage),
168170
'my-payment': new MyPaymentService(projectRouter, this._storage),
169171
'shipping-method': new ShippingMethodService(

src/repositories/abstract.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export abstract class AbstractRepository {
4444

4545
actions.forEach(action => {
4646
const updateFunc = this.actions[action.action]
47+
4748
if (!updateFunc) {
4849
console.error(`No mock implemented for update action ${action.action}`)
4950
return

src/repositories/helpers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const createCustomFields = (
2424
if (!draft.type) return undefined
2525
if (!draft.type.typeId) return undefined
2626
if (!draft.fields) return undefined
27-
2827
const typeResource = storage.getByResourceIdentifier(
2928
projectKey,
3029
draft.type

src/repositories/order.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import {
2020
OrderSetOrderNumberAction,
2121
OrderSetShippingAddressAction,
2222
OrderSetStoreAction,
23+
OrderState,
24+
OrderStateTransitionMessage,
25+
OrderTransitionStateAction,
2326
Product,
2427
ProductPagedQueryResponse,
2528
ProductVariant,
2629
ReferenceTypeId,
30+
State,
2731
Store,
2832
} from '@commercetools/platform-sdk'
2933
import { AbstractResourceRepository, QueryParams } from './abstract'
@@ -252,6 +256,24 @@ export class OrderRepository extends AbstractResourceRepository {
252256
) => {
253257
resource.paymentState = paymentState
254258
},
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+
},
255277
setBillingAddress: (
256278
projectKey: string,
257279
resource: Writable<Order>,

src/services/cart.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,48 @@ import AbstractService from './abstract'
22
import { Router } from 'express'
33
import { CartRepository } from '../repositories/cart'
44
import { AbstractStorage } from '../storage'
5+
import { Cart, Order } from '@commercetools/platform-sdk'
6+
import { OrderRepository } from '../repositories/order'
57

68
export class CartService extends AbstractService {
79
public repository: CartRepository
10+
public orderRepository: OrderRepository
811

912
constructor(parent: Router, storage: AbstractStorage) {
1013
super(parent)
1114
this.repository = new CartRepository(storage)
15+
this.orderRepository = new OrderRepository(storage)
1216
}
1317

1418
getBasePath() {
1519
return 'carts'
1620
}
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+
}
1749
}

src/services/my-order.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type RepositoryTypes = ReferenceTypeId | 'product-projection'
2121
export type ServiceTypes =
2222
| RepositoryTypes
2323
| 'my-cart'
24+
| 'my-order'
2425
| 'my-payment'
2526
| 'my-customer'
2627

0 commit comments

Comments
 (0)