From 17bc2738237c93cbb8a49865923dec46ebbe3311 Mon Sep 17 00:00:00 2001 From: Jose Noriega Date: Mon, 2 May 2022 09:30:52 -0700 Subject: [PATCH 1/2] chore(api-gateway): add husky actions to api-gateway --- .husky/pre-commit | 6 ++++++ packages/api-gateway/package.json | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 70ece6e..e5018dc 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -9,3 +9,9 @@ if [ ${#messagesPackageFiles} -gt 0 ]; then # Run pre-commit actions only if there are changes in the messages package. cd packages/messages && yarn precommit fi + +apiGatewayPackageFiles=$(echo "$fileList" | grep -E 'packages\/api-gateway\/.+$' || echo '') +if [ ${#apiGatewayPackageFiles} -gt 0 ]; then + # Run pre-commit actions only if there are changes in the api-gateway package. + cd packages/api-gateway && yarn precommit +fi diff --git a/packages/api-gateway/package.json b/packages/api-gateway/package.json index 2831c87..14d65dd 100644 --- a/packages/api-gateway/package.json +++ b/packages/api-gateway/package.json @@ -27,7 +27,8 @@ "test": "jest --runInBand", "test:watch": "jest --watch", "lint": "eslint .", - "lint:fix": "eslint . --fix" + "lint:fix": "eslint . --fix", + "precommit": "yarn lint && yarn test" }, "bugs": { "url": "https://github.com/gh0stl1m/backend-services/issues" From 9efe9f63c1f23fc15e5a6d1912cdea5df3f65000 Mon Sep 17 00:00:00 2001 From: Jose Noriega Date: Mon, 2 May 2022 09:31:18 -0700 Subject: [PATCH 2/2] fix(api-gateway): eslint warnings and unit tests --- .../__tests__/places/getPlacesFail.test.js | 2 +- packages/api-gateway/__tests__/setupTest.js | 5 ++- .../routes/chats/get-chat-messages.test.js | 31 ++++++++++--------- .../drivers/http/adapters/users/handlers.js | 16 +++++++--- .../src/drivers/http/routes/user/index.js | 2 +- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/api-gateway/__tests__/places/getPlacesFail.test.js b/packages/api-gateway/__tests__/places/getPlacesFail.test.js index 3731b7e..7881d8b 100644 --- a/packages/api-gateway/__tests__/places/getPlacesFail.test.js +++ b/packages/api-gateway/__tests__/places/getPlacesFail.test.js @@ -6,7 +6,7 @@ describe('Given a client that wants to get the Places data', () => { describe('When the server cannot connect to db', () => { beforeAll(() => { getPlaces.mockImplementation(new Error()); - }) + }); test('Then the back must return a 500 status code', async () => { const response = await fastify.inject({ method: 'GET', diff --git a/packages/api-gateway/__tests__/setupTest.js b/packages/api-gateway/__tests__/setupTest.js index 2bb2ad3..657b821 100644 --- a/packages/api-gateway/__tests__/setupTest.js +++ b/packages/api-gateway/__tests__/setupTest.js @@ -43,10 +43,9 @@ jest.mock('@booking-services/places', () => ({ getPlaces: jest.fn(() => []), deletePlace: jest.fn((id) => { if (typeof id !== 'number' || id >= 1000) { - throw new Error(); + throw new Error(); } - return; -}), + }), })); jest.mock('@booking-services/messages'); diff --git a/packages/api-gateway/__tests__/src/drivers/http/routes/chats/get-chat-messages.test.js b/packages/api-gateway/__tests__/src/drivers/http/routes/chats/get-chat-messages.test.js index 1a75837..5215e11 100644 --- a/packages/api-gateway/__tests__/src/drivers/http/routes/chats/get-chat-messages.test.js +++ b/packages/api-gateway/__tests__/src/drivers/http/routes/chats/get-chat-messages.test.js @@ -6,25 +6,26 @@ const { listChatMessages } = require('@booking-services/messages'); const { fastify } = require('../../../../../../src/drivers/http/server'); // Mocks -const customerID = faker.datatype.uuid(); -const hostID = faker.datatype.uuid(); - -const mockListChatMessages = [ - { - _id: faker.datatype.uuid(), - bookingId: faker.datatype.uuid(), - hostId: hostID, - customerId: customerID, - createdAt: faker.datatype.datetime(), - updatedAt: faker.datatype.datetime(), - deletedAt: null, - }, -]; +const mockUserId = faker.datatype.uuid(); + +const mockListChatMessages = { + page: 1, + messages: [ + { + _id: faker.datatype.uuid(), + chatId: faker.datatype.uuid(), + text: faker.lorem.sentence(), + createdBy: mockUserId, + createdAt: faker.datatype.datetime(), + deletedAt: null, + }, + ], +}; describe('GET /chats/{chatId}/messages', () => { describe('given an authenticated user and a valid chatId', () => { // TODO: Change this once we have authentication ready - const bearerToken = customerID; + const bearerToken = mockUserId; const chatId = 1; const headers = { diff --git a/packages/api-gateway/src/drivers/http/adapters/users/handlers.js b/packages/api-gateway/src/drivers/http/adapters/users/handlers.js index 5ba503a..f869fbb 100644 --- a/packages/api-gateway/src/drivers/http/adapters/users/handlers.js +++ b/packages/api-gateway/src/drivers/http/adapters/users/handlers.js @@ -47,7 +47,7 @@ async function createUser(req, reply) { return reply .code(200) - .header("Content-Type", "application/json; chartset:utf-8") + .header('Content-Type', 'application/json; chartset:utf-8') .send({ result }); } catch (error) { return errorHandler(error, reply); @@ -70,11 +70,13 @@ async function validateUser(req, reply) { phoneNumber, emergencyNumber, passport, - address: { country, city, state, address, zip }, + address: { + country, city, state, address, zip, + }, } = req.body; const { userId } = req.params; - req.log.info("[http-server]: Creating user with: ", { + req.log.info('[http-server]: Creating user with: ', { userId, firstName, secondName, @@ -89,7 +91,9 @@ async function validateUser(req, reply) { phoneNumber, emergencyNumber, passport, - address: { country, city, state, address, zip }, + address: { + country, city, state, address, zip, + }, }); const result = await this.userServices.validateUser({ @@ -107,7 +111,9 @@ async function validateUser(req, reply) { phoneNumber, emergencyNumber, passport, - address: { country, city, state, address, zip }, + address: { + country, city, state, address, zip, + }, }); if (result.isBoom === true) { diff --git a/packages/api-gateway/src/drivers/http/routes/user/index.js b/packages/api-gateway/src/drivers/http/routes/user/index.js index d39c6ea..05be102 100644 --- a/packages/api-gateway/src/drivers/http/routes/user/index.js +++ b/packages/api-gateway/src/drivers/http/routes/user/index.js @@ -1,5 +1,5 @@ const { userAdapter } = require('../../adapters'); -const { createUserSchema, validateUserSchema} = require('./schema'); +const { createUserSchema, validateUserSchema } = require('./schema'); async function UserRouter(fastify) { await fastify.post('/', { schema: createUserSchema }, userAdapter.createUser);