diff --git a/.changeset/empty-hats-bow.md b/.changeset/empty-hats-bow.md new file mode 100644 index 00000000..553f3f53 --- /dev/null +++ b/.changeset/empty-hats-bow.md @@ -0,0 +1,5 @@ +--- +'@labdigital/commercetools-mock': minor +--- + +Improve support for /me endpoint diff --git a/src/repositories/index.ts b/src/repositories/index.ts index d5395d66..c7352131 100644 --- a/src/repositories/index.ts +++ b/src/repositories/index.ts @@ -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), diff --git a/src/services/index.ts b/src/services/index.ts index 37ae8800..821f3d4b 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -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' @@ -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'] diff --git a/src/services/my-customer.test.ts b/src/services/my-customer.test.ts index d81777f2..2fd81224 100644 --- a/src/services/my-customer.test.ts +++ b/src/services/my-customer.test.ts @@ -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() @@ -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: 'foo@example.org', + 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: 'foo@example.org', + 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) + }) +}) diff --git a/src/services/my-customer.ts b/src/services/my-customer.ts index 35072737..14d13ee2 100644 --- a/src/services/my-customer.ts +++ b/src/services/my-customer.ts @@ -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 @@ -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)) @@ -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( diff --git a/src/services/my-shopping-list.ts b/src/services/my-shopping-list.ts new file mode 100644 index 00000000..4f184406 --- /dev/null +++ b/src/services/my-shopping-list.ts @@ -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' + } +}