Skip to content

Commit

Permalink
Improve support for /me endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Dec 22, 2023
1 parent f6f9069 commit b1370cf
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/empty-hats-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@labdigital/commercetools-mock': minor
---

Improve support for /me endpoint
1 change: 1 addition & 0 deletions src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const createRepositories = (storage: AbstractStorage) => ({
'my-order': new MyOrderRepository(storage),
'my-customer': new CustomerRepository(storage),
'my-payment': new PaymentRepository(storage),
'my-shopping-list': new ShoppingListRepository(storage),
product: new ProductRepository(storage),
'product-type': new ProductTypeRepository(storage),
'product-discount': new ProductDiscountRepository(storage),
Expand Down
2 changes: 2 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { MyCartService } from './my-cart.js'
import { MyCustomerService } from './my-customer.js'
import { MyOrderService } from './my-order.js'
import { MyPaymentService } from './my-payment.js'
import { MyShoppingListService } from './my-shopping-list.js'
import { OrderService } from './order.js'
import { PaymentService } from './payment.js'
import { ProductDiscountService } from './product-discount.js'
Expand Down Expand Up @@ -61,6 +62,7 @@ export const createServices = (router: any, repos: any) => ({
'my-order': new MyOrderService(router, repos['my-order']),
'my-customer': new MyCustomerService(router, repos['my-customer']),
'my-payment': new MyPaymentService(router, repos['my-payment']),
'my-shopping-list': new MyShoppingListService(router, repos['my-shopping-list']),
'shipping-method': new ShippingMethodService(
router,
repos['shipping-method']
Expand Down
57 changes: 56 additions & 1 deletion src/services/my-customer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MyCustomerDraft } from '@commercetools/platform-sdk'
import supertest from 'supertest'
import { afterEach, describe, expect, test } from 'vitest'
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
import { CommercetoolsMock } from '../index.js'

const ctMock = new CommercetoolsMock()
Expand Down Expand Up @@ -51,3 +51,58 @@ describe('Me', () => {
expect(response.body).toEqual(createResponse.body.customer)
})
})

describe('/me', () => {
afterEach(() => {
ctMock.clear()
})

beforeEach(() => {
ctMock.project('dummy').add('customer', {
id: '123',
createdAt: '2021-03-18T14:00:00.000Z',
version: 2,
lastModifiedAt: '2021-03-18T14:00:00.000Z',
email: '[email protected]',
addresses: [],
isEmailVerified: true,
authenticationMode: 'password',
custom: { type: { typeId: 'type', id: '' }, fields: {} },
})
})

test('Get me', async () => {
const response = await supertest(ctMock.app).get('/dummy/me')

expect(response.status).toBe(200)
expect(response.body).toEqual({
id: '123',
createdAt: '2021-03-18T14:00:00.000Z',
version: 2,
lastModifiedAt: '2021-03-18T14:00:00.000Z',
email: '[email protected]',
addresses: [],
isEmailVerified: true,
authenticationMode: 'password',
custom: {
fields: {},
type: {
id: '',
typeId: 'type',
},
},
})
})

test('setCustomField', async () => {
const response = await supertest(ctMock.app)
.post(`/dummy/me`)
.send({
version: 2,
actions: [{ action: 'setCustomField', name: 'foobar', value: true }],
})
expect(response.status).toBe(200)
expect(response.body.version).toBe(3)
expect(response.body.custom.fields.foobar).toBe(true)
})
})
20 changes: 20 additions & 0 deletions src/services/my-customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CustomerRepository } from '../repositories/customer.js'
import { getRepositoryContext } from '../repositories/helpers.js'
import AbstractService from './abstract.js'
import { hashPassword } from '../lib/password.js'
import { Update } from '@commercetools/platform-sdk'

export class MyCustomerService extends AbstractService {
public repository: CustomerRepository
Expand All @@ -24,6 +25,7 @@ export class MyCustomerService extends AbstractService {
this.extraRoutes(router)

router.get('', this.getMe.bind(this))
router.post('', this.updateMe.bind(this))

router.post('/signup', this.signUp.bind(this))

Expand All @@ -40,6 +42,24 @@ export class MyCustomerService extends AbstractService {
return response.status(200).send(resource)
}

updateMe(request: Request, response: Response) {
const resource = this.repository.getMe(getRepositoryContext(request))

if (!resource) {
return response.status(404).send('Not found')
}
const updateRequest: Update = request.body
const updatedResource = this.repository.processUpdateActions(
getRepositoryContext(request),
resource,
updateRequest.version,
updateRequest.actions
)

const result = this._expandWithId(request, updatedResource.id)
return response.status(200).send(result)
}

signUp(request: Request, response: Response) {
const draft = request.body
const resource = this.repository.create(
Expand Down
16 changes: 16 additions & 0 deletions src/services/my-shopping-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Router } from 'express'
import { ShoppingListRepository } from '../repositories/shopping-list.js'
import AbstractService from './abstract.js'

export class MyShoppingListService extends AbstractService {
public repository: ShoppingListRepository

constructor(parent: Router, repository: ShoppingListRepository) {
super(parent)
this.repository = repository
}

getBasePath() {
return 'me/shopping-lists'
}
}

0 comments on commit b1370cf

Please sign in to comment.