From 59fad95b47758c5a6111b44cd4768a252250bc1e Mon Sep 17 00:00:00 2001 From: thyagopacher Date: Sat, 17 May 2025 08:24:04 -0300 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20padroniza=C3=A7=C3=A3o=20dos=20Cont?= =?UTF-8?q?rollers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/database.js | 11 ++- pages/api/v1/status/index.js | 86 +++++++++----------- tests/integration/api/v1/status/post.test.js | 26 ++++++ 3 files changed, 73 insertions(+), 50 deletions(-) create mode 100755 tests/integration/api/v1/status/post.test.js diff --git a/infra/database.js b/infra/database.js index 1bdcee8..ab22a04 100755 --- a/infra/database.js +++ b/infra/database.js @@ -1,4 +1,5 @@ import { Client } from "pg"; +import { ServiceError } from "./errors.js"; async function query(queryObject) { let client; @@ -7,9 +8,11 @@ async function query(queryObject) { const result = await client.query(queryObject); return result; } catch (error) { - console.log("\n Erro dentro do catch do database.js:"); - console.error(error); - throw error; + const serviceErrorObject = new ServiceError({ + message: "Erro na conexão com Banco ou na Query.", + cause: error, + }); + throw serviceErrorObject; } finally { await client?.end(); } @@ -44,4 +47,4 @@ function getSSLValues() { } return process.env.NODE_ENV === "production" ? true : false; -} +} \ No newline at end of file diff --git a/pages/api/v1/status/index.js b/pages/api/v1/status/index.js index 84fc01b..99e4488 100755 --- a/pages/api/v1/status/index.js +++ b/pages/api/v1/status/index.js @@ -1,47 +1,41 @@ -import database from "infra/database.js"; -import { InternalServerError } from "infra/errors"; - -async function status(request, response) { - try { - const updatedAt = new Date().toISOString(); - - const databaseVersionResult = await database.query("SHOW server_version;"); - const databaseVersionValue = databaseVersionResult.rows[0].server_version; - - const databaseMaxConnectionsResult = await database.query( - "SHOW max_connections;", - ); - const databaseMaxConnectionsValue = - databaseMaxConnectionsResult.rows[0].max_connections; - - const databaseName = process.env.POSTGRES_DB; - const databaseOpenedConnectionsResult = await database.query({ - text: "select count(*)::int from pg_stat_activity where datname=$1;", - values: [databaseName], - }); - const databaseOpenedConnectionsValue = - databaseOpenedConnectionsResult.rows[0].count; - - response.status(200).json({ - updated_at: updatedAt, - dependencies: { - database: { - version: databaseVersionValue, - max_connections: parseInt(databaseMaxConnectionsValue), - opened_connections: parseInt(databaseOpenedConnectionsValue), - }, +import { createRouter } from "next-connect"; +import database from "infra/database"; +import controller from "infra/controller.js"; + +const router = createRouter(); + +router.get(getHandler); + +export default router.handler(controller.errorHandlers); + +async function getHandler(request, response) { + const updatedAt = new Date().toISOString(); + + const databaseVersionResult = await database.query("SHOW server_version;"); + const databaseVersionValue = databaseVersionResult.rows[0].server_version; + + const databaseMaxConnectionsResult = await database.query( + "SHOW max_connections;", + ); + const databaseMaxConnectionsValue = + databaseMaxConnectionsResult.rows[0].max_connections; + + const databaseName = process.env.POSTGRES_DB; + const databaseOpenedConnectionsResult = await database.query({ + text: "SELECT count(*)::int FROM pg_stat_activity WHERE datname = $1;", + values: [databaseName], + }); + const databaseOpenedConnectionsValue = + databaseOpenedConnectionsResult.rows[0].count; + + response.status(200).json({ + updated_at: updatedAt, + dependencies: { + database: { + version: databaseVersionValue, + max_connections: parseInt(databaseMaxConnectionsValue), + opened_connections: databaseOpenedConnectionsValue, }, - }); - } catch (error) { - const publicErrorObject = new InternalServerError({ - cause: error, - }); - - console.log("\n Erro dentro do catch do controller:"); - console.error(publicErrorObject); - - response.status(500).json(publicErrorObject); - } -} - -export default status; + }, + }); +} \ No newline at end of file diff --git a/tests/integration/api/v1/status/post.test.js b/tests/integration/api/v1/status/post.test.js new file mode 100755 index 0000000..cd02f37 --- /dev/null +++ b/tests/integration/api/v1/status/post.test.js @@ -0,0 +1,26 @@ +import orchestrator from "tests/orchestrator.js"; + +beforeAll(async () => { + await orchestrator.waitForAllServices(); +}); + +describe("POST /api/v1/status", () => { + describe("Anonymous user", () => { + test("Retrieving current system status", async () => { + const response = await fetch("http://localhost:3000/api/v1/status", { + method: "POST", + }); + expect(response.status).toBe(405); + + const responseBody = await response.json(); + + expect(responseBody).toEqual({ + name: "MethodNotAllowedError", + message: "Método não permitido para este endpoint.", + action: + "Verifique se o método HTTP enviado é válido para este endpoint.", + status_code: 405, + }); + }); + }); +}); \ No newline at end of file From 5e7b8e47a726b5a7e7ad253bfd26068d49ca1a05 Mon Sep 17 00:00:00 2001 From: thyagopacher Date: Sat, 17 May 2025 08:25:27 -0300 Subject: [PATCH 2/2] feat: fix prettier --- infra/database.js | 2 +- pages/api/v1/status/index.js | 2 +- tests/integration/api/v1/status/post.test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/infra/database.js b/infra/database.js index ab22a04..58961c9 100755 --- a/infra/database.js +++ b/infra/database.js @@ -47,4 +47,4 @@ function getSSLValues() { } return process.env.NODE_ENV === "production" ? true : false; -} \ No newline at end of file +} diff --git a/pages/api/v1/status/index.js b/pages/api/v1/status/index.js index 99e4488..a7a8e7a 100755 --- a/pages/api/v1/status/index.js +++ b/pages/api/v1/status/index.js @@ -38,4 +38,4 @@ async function getHandler(request, response) { }, }, }); -} \ No newline at end of file +} diff --git a/tests/integration/api/v1/status/post.test.js b/tests/integration/api/v1/status/post.test.js index cd02f37..348beac 100755 --- a/tests/integration/api/v1/status/post.test.js +++ b/tests/integration/api/v1/status/post.test.js @@ -23,4 +23,4 @@ describe("POST /api/v1/status", () => { }); }); }); -}); \ No newline at end of file +});