diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ec5331d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug NestJs", + "request": "launch", + "runtimeArgs": [ + "run", + "start:debug" + ], + "runtimeExecutable": "npm", + "skipFiles": [ + "/**" + ], + "type": "node", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e5b6c4b..e3f1384 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "UNLICENSED", "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", - "@dadosfera/protospack-v2": "3.10.2", + "@dadosfera/protospack-v2": "3.10.3", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -1226,11 +1226,12 @@ } }, "node_modules/@babel/generator/node_modules/@jridgewell/gen-mapping": { - "version": "0.3.1", + "version": "0.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", "dev": true, - "license": "MIT", "dependencies": { - "@jridgewell/set-array": "^1.0.0", + "@jridgewell/set-array": "^1.0.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.9" }, @@ -1695,15 +1696,6 @@ "node": ">=12" } }, - "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { - "version": "0.3.9", - "dev": true, - "license": "MIT", - "dependencies": { - "@jridgewell/resolve-uri": "^3.0.3", - "@jridgewell/sourcemap-codec": "^1.4.10" - } - }, "node_modules/@dabh/diagnostics": { "version": "2.0.3", "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dabh/diagnostics/-/diagnostics-2.0.3.tgz", @@ -1715,9 +1707,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "3.10.2", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.2.tgz", - "integrity": "sha512-21rbav6VXVo+7Io82/O+350DLmf8ONQw8kkG728F7ySD9uEosJgTz3hzZFpTmVuSew822zS+iHcpGJBpVmq49w==", + "version": "3.10.3", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.3.tgz", + "integrity": "sha512-AQZeWwM7m/OU/+H5X5fQkWspJJrnLBFwrDWSEFLmYchmhdgy9Kb8D5AeHXcsp9ku8VbdiYs6yo6MvcbZh+bsyw==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -2260,11 +2252,12 @@ } }, "node_modules/@jridgewell/source-map/node_modules/@jridgewell/gen-mapping": { - "version": "0.3.1", + "version": "0.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", "dev": true, - "license": "MIT", "dependencies": { - "@jridgewell/set-array": "^1.0.0", + "@jridgewell/set-array": "^1.0.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.9" }, @@ -2278,9 +2271,10 @@ "license": "MIT" }, "node_modules/@jridgewell/trace-mapping": { - "version": "0.3.13", + "version": "0.3.9", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", "dev": true, - "license": "MIT", "dependencies": { "@jridgewell/resolve-uri": "^3.0.3", "@jridgewell/sourcemap-codec": "^1.4.10" @@ -3491,9 +3485,10 @@ } }, "node_modules/acorn": { - "version": "8.7.1", + "version": "8.8.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/acorn/-/acorn-8.8.0.tgz", + "integrity": "sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==", "dev": true, - "license": "MIT", "bin": { "acorn": "bin/acorn" }, @@ -3906,14 +3901,6 @@ ], "license": "MIT" }, - "node_modules/base64url": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz", - "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==", - "engines": { - "node": ">=6.0.0" - } - }, "node_modules/basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -5952,6 +5939,17 @@ "version": "1.0.0", "license": "ISC" }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "license": "MIT" @@ -6082,9 +6080,10 @@ "license": "BSD-2-Clause" }, "node_modules/globals": { - "version": "13.15.0", + "version": "13.17.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/globals/-/globals-13.17.0.tgz", + "integrity": "sha512-1C+6nQRb1GwGMKm2dH/E7enFAMxGTmGI7/dEdhy/DNelv85w9B72t3uc5frtMNXIbzrarJJ/lTCjcaZwbLJmyw==", "dev": true, - "license": "MIT", "dependencies": { "type-fest": "^0.20.2" }, @@ -8683,19 +8682,6 @@ "node": ">= 0.4.0" } }, - "node_modules/passport-forcedotcom/node_modules/passport-oauth2": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.3.0.tgz", - "integrity": "sha512-AisRXcC7Vx8WSx5C9O8UNib/Y91/IOphs5tImlOMs7T+QTEJdmS+OxgXSskbnDdGQT/8mU5T3yMT6136rBjYXg==", - "dependencies": { - "oauth": "0.9.x", - "passport-strategy": "1.x.x", - "uid2": "0.0.x" - }, - "engines": { - "node": ">= 0.4.0" - } - }, "node_modules/passport-google-oauth20": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz", @@ -8766,22 +8752,16 @@ } }, "node_modules/passport-oauth2": { - "version": "1.6.1", - "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.6.1.tgz", - "integrity": "sha512-ZbV43Hq9d/SBSYQ22GOiglFsjsD1YY/qdiptA+8ej+9C1dL1TVB+mBE5kDH/D4AJo50+2i8f4bx0vg4/yDDZCQ==", + "version": "1.3.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/passport-oauth2/-/passport-oauth2-1.3.0.tgz", + "integrity": "sha512-AisRXcC7Vx8WSx5C9O8UNib/Y91/IOphs5tImlOMs7T+QTEJdmS+OxgXSskbnDdGQT/8mU5T3yMT6136rBjYXg==", "dependencies": { - "base64url": "3.x.x", "oauth": "0.9.x", "passport-strategy": "1.x.x", - "uid2": "0.0.x", - "utils-merge": "1.x.x" + "uid2": "0.0.x" }, "engines": { "node": ">= 0.4.0" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/jaredhanson" } }, "node_modules/passport-strategy": { @@ -9356,8 +9336,9 @@ } }, "node_modules/rxjs": { - "version": "7.5.5", - "license": "Apache-2.0", + "version": "7.5.6", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/rxjs/-/rxjs-7.5.6.tgz", + "integrity": "sha512-dnyv2/YsXhnm461G+R/Pe5bWP41Nm6LBXEYWI6eiFP4fiwx6WRI/CD0zbdVAudd9xwLEF2IDcKXLHit0FYjUzw==", "dependencies": { "tslib": "^2.1.0" } @@ -11955,10 +11936,12 @@ }, "dependencies": { "@jridgewell/gen-mapping": { - "version": "0.3.1", + "version": "0.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", "dev": true, "requires": { - "@jridgewell/set-array": "^1.0.0", + "@jridgewell/set-array": "^1.0.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.9" } @@ -12255,16 +12238,6 @@ "dev": true, "requires": { "@jridgewell/trace-mapping": "0.3.9" - }, - "dependencies": { - "@jridgewell/trace-mapping": { - "version": "0.3.9", - "dev": true, - "requires": { - "@jridgewell/resolve-uri": "^3.0.3", - "@jridgewell/sourcemap-codec": "^1.4.10" - } - } } }, "@dabh/diagnostics": { @@ -12278,9 +12251,9 @@ } }, "@dadosfera/protospack-v2": { - "version": "3.10.2", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.2.tgz", - "integrity": "sha512-21rbav6VXVo+7Io82/O+350DLmf8ONQw8kkG728F7ySD9uEosJgTz3hzZFpTmVuSew822zS+iHcpGJBpVmq49w==", + "version": "3.10.3", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.10.3.tgz", + "integrity": "sha512-AQZeWwM7m/OU/+H5X5fQkWspJJrnLBFwrDWSEFLmYchmhdgy9Kb8D5AeHXcsp9ku8VbdiYs6yo6MvcbZh+bsyw==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -12668,10 +12641,12 @@ }, "dependencies": { "@jridgewell/gen-mapping": { - "version": "0.3.1", + "version": "0.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz", + "integrity": "sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==", "dev": true, "requires": { - "@jridgewell/set-array": "^1.0.0", + "@jridgewell/set-array": "^1.0.1", "@jridgewell/sourcemap-codec": "^1.4.10", "@jridgewell/trace-mapping": "^0.3.9" } @@ -12683,7 +12658,9 @@ "dev": true }, "@jridgewell/trace-mapping": { - "version": "0.3.13", + "version": "0.3.9", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", "dev": true, "requires": { "@jridgewell/resolve-uri": "^3.0.3", @@ -13506,7 +13483,9 @@ } }, "acorn": { - "version": "8.7.1", + "version": "8.8.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/acorn/-/acorn-8.8.0.tgz", + "integrity": "sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==", "dev": true }, "acorn-globals": { @@ -13786,11 +13765,6 @@ "version": "1.5.1", "dev": true }, - "base64url": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz", - "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==" - }, "basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -15204,6 +15178,13 @@ "fs.realpath": { "version": "1.0.0" }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + }, "function-bind": { "version": "1.1.1" }, @@ -15282,7 +15263,9 @@ "dev": true }, "globals": { - "version": "13.15.0", + "version": "13.17.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/globals/-/globals-13.17.0.tgz", + "integrity": "sha512-1C+6nQRb1GwGMKm2dH/E7enFAMxGTmGI7/dEdhy/DNelv85w9B72t3uc5frtMNXIbzrarJJ/lTCjcaZwbLJmyw==", "dev": true, "requires": { "type-fest": "^0.20.2" @@ -17023,18 +17006,6 @@ "integrity": "sha512-raDRQqBJtTEHDxLkxLG+9sYE0xpPy9PqriMPbMy9jmpIcU2+mOq9PgczvUPrU8UjfcZp3gqkDHqujcUWiGOVNQ==", "requires": { "passport-oauth2": "1.3.x" - }, - "dependencies": { - "passport-oauth2": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.3.0.tgz", - "integrity": "sha512-AisRXcC7Vx8WSx5C9O8UNib/Y91/IOphs5tImlOMs7T+QTEJdmS+OxgXSskbnDdGQT/8mU5T3yMT6136rBjYXg==", - "requires": { - "oauth": "0.9.x", - "passport-strategy": "1.x.x", - "uid2": "0.0.x" - } - } } }, "passport-google-oauth20": { @@ -17090,15 +17061,13 @@ } }, "passport-oauth2": { - "version": "1.6.1", - "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.6.1.tgz", - "integrity": "sha512-ZbV43Hq9d/SBSYQ22GOiglFsjsD1YY/qdiptA+8ej+9C1dL1TVB+mBE5kDH/D4AJo50+2i8f4bx0vg4/yDDZCQ==", + "version": "1.3.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/passport-oauth2/-/passport-oauth2-1.3.0.tgz", + "integrity": "sha512-AisRXcC7Vx8WSx5C9O8UNib/Y91/IOphs5tImlOMs7T+QTEJdmS+OxgXSskbnDdGQT/8mU5T3yMT6136rBjYXg==", "requires": { - "base64url": "3.x.x", "oauth": "0.9.x", "passport-strategy": "1.x.x", - "uid2": "0.0.x", - "utils-merge": "1.x.x" + "uid2": "0.0.x" } }, "passport-strategy": { @@ -17461,7 +17430,9 @@ } }, "rxjs": { - "version": "7.5.5", + "version": "7.5.6", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/rxjs/-/rxjs-7.5.6.tgz", + "integrity": "sha512-dnyv2/YsXhnm461G+R/Pe5bWP41Nm6LBXEYWI6eiFP4fiwx6WRI/CD0zbdVAudd9xwLEF2IDcKXLHit0FYjUzw==", "requires": { "tslib": "^2.1.0" } diff --git a/package.json b/package.json index 67f5ba6..07ed7a0 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", + "@dadosfera/protospack-v2": "3.10.3", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -53,7 +54,6 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "@dadosfera/protospack-v2": "3.10.2", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", diff --git a/src/error/grpc-to-http-exception.filter.ts b/src/error/grpc-to-http-exception.filter.ts index 3e187db..f27ed9a 100644 --- a/src/error/grpc-to-http-exception.filter.ts +++ b/src/error/grpc-to-http-exception.filter.ts @@ -1,16 +1,32 @@ -import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common'; +import { + ExceptionFilter, + Catch, + ArgumentsHost, + HttpException, + HttpStatus, +} from '@nestjs/common'; import { Response } from 'express'; import ErrorBuilder from '../utils/ErrorBuilder'; +/** + * ExceptionFilter to handle RpcException thrown by our microservices + * + * If it detects that the exception is already an **HttpException** or it is generated by our **ErrorBuilder**, + * it will return the exception without any treatment. + */ @Catch(Error) export class GrpcToHttpExceptionFilter implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); - // return this exception directly if is already of type ErrorBuilder, - // else transform it using the ErrorBuilder + if (exception instanceof HttpException) { + return response + .status(exception.getStatus()) + .json(exception.getResponse()); + } + const err = exception.constructor.name === ErrorBuilder.name ? exception diff --git a/src/modules/connection-test/connection-test.controller.ts b/src/modules/connection-test/connection-test.controller.ts index 95ed102..f78309f 100644 --- a/src/modules/connection-test/connection-test.controller.ts +++ b/src/modules/connection-test/connection-test.controller.ts @@ -5,6 +5,7 @@ import { HttpStatus, Inject, Post, + UseFilters, ValidationPipe, } from '@nestjs/common'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; @@ -24,9 +25,11 @@ import { } from './dto/connection-test'; import { DadosferaLogger } from 'dadosfera-logs'; import { Authenticated } from 'src/authentication/authentication.decorator'; +import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; @ApiTags('Connection Test') @Controller('connection-test') +@UseFilters(new GrpcToHttpExceptionFilter()) @Authenticated() export class ConnectionTestController { logger: any; @@ -60,16 +63,13 @@ export class ConnectionTestController { @HttpCode(HttpStatus.OK) async connectionTestCredentials( @User() user: RequestUser, - @Body(new ValidationPipe()) body: ConnectionTestCredentialsReq, + @Body() body: ConnectionTestCredentialsReq, ) { this.logger.info('/connection-test/credentials', { user: user.user_id, customer: user.customer_name, }); - return this.connectionTestService.connectionTestCredentials( - body, - user.customer_name, - ); + return this.connectionTestService.connectionTestCredentials(body, user); } @Post('schemas') @@ -113,7 +113,7 @@ export class ConnectionTestController { @User() user: RequestUser, @Body(new ValidationPipe()) body: GetTableMetadataReq, ) { - this.logger.info('/connection-test/tables', { + this.logger.info('/connection-test/table-metadata', { user: user.user_id, customer: user.customer_name, }); diff --git a/src/modules/connection-test/connection-test.module.ts b/src/modules/connection-test/connection-test.module.ts index 687df06..42faaa5 100644 --- a/src/modules/connection-test/connection-test.module.ts +++ b/src/modules/connection-test/connection-test.module.ts @@ -4,10 +4,11 @@ import { ConnectionTestService } from './connection-test.service'; import { DadosferaLogger } from 'dadosfera-logs'; import { ClientsModule } from '@nestjs/microservices'; import { ConnectionTestClientConfiguration } from './connection-test-client.config'; +import { ConnectionModule } from '../connection/connection.module'; const client = new ConnectionTestClientConfiguration(); @Module({ controllers: [ConnectionTestController], providers: [ConnectionTestService, DadosferaLogger], - imports: [ClientsModule.register([client.providerOptions])], + imports: [ClientsModule.register([client.providerOptions]), ConnectionModule], }) export class ConnectionTestModule {} diff --git a/src/modules/connection-test/connection-test.service.ts b/src/modules/connection-test/connection-test.service.ts index 300e765..dae74a6 100644 --- a/src/modules/connection-test/connection-test.service.ts +++ b/src/modules/connection-test/connection-test.service.ts @@ -14,12 +14,17 @@ import { GetTableMetadataReq, GetTableMetadataRes, } from './dto/connection-test'; +import { ConnectionClientService } from '../connection/client.service'; +import { CreateConnectionDto } from '../connection/dtos/create-connection'; +import { Info } from 'protospack/dist/lib/interfaces'; +import { RequestUser } from 'src/authentication/user.decorator'; @Injectable() export class ConnectionTestService { private connectionTestReadClient: ConnectionTest.ReadService.ConnectionTestReadServices; constructor( @Inject('ConnectionTestGrpcClient') private readonly grpcClient: ClientGrpc, + private connectionsService: ConnectionClientService, ) { this.connectionTestReadClient = grpcClient.getService( @@ -44,19 +49,45 @@ export class ConnectionTestService { } async connectionTestCredentials( body: ConnectionTestCredentialsReq, - customer_name: string, + user: RequestUser, ): Promise { - const { connector_id, plugin } = body; - //CREATE CONNECTION - const connection_id = ''; - /***********************/ + const { plugin, ...connectionBody } = body; + let { connection_id } = body; + const { customer_id, customer_name, user_id } = user; + + /********* CREATE CONNECTION *********/ + if (!connection_id) { + if (!connectionBody.properties.credentials_type) + connectionBody.properties.credentials_type = 'basic_auth'; + + const createConnectionBody: CreateConnectionDto = { + ...connectionBody, + }; + const { connection } = await this.connectionsService.createConnection({ + body: createConnectionBody, + info: { customer_id, customer_name, user_id }, + }); + connection_id = connection.id; + } + /************************************/ const { operation_result } = await lastValueFrom( this.connectionTestReadClient.ConnectionTestCredentials({ connection_id, customer_name, plugin, }), - ); + ).catch(async (e) => { + await this.connectionsService.deleteConnection({ + body: { id: connection_id }, + info: { customer_id, customer_name, user_id }, + }); + throw e; + }); + if (!operation_result) + await this.connectionsService.deleteConnection({ + body: { id: connection_id }, + info: { customer_id, customer_name, user_id }, + }); return { operation_result, connection_id }; } async connectionTestListSchemas( diff --git a/src/modules/connection-test/dto/connection-test.ts b/src/modules/connection-test/dto/connection-test.ts index 7deadc0..9d2e5b1 100644 --- a/src/modules/connection-test/dto/connection-test.ts +++ b/src/modules/connection-test/dto/connection-test.ts @@ -1,5 +1,5 @@ import { ApiProperty, ApiPropertyOptional, OmitType } from '@nestjs/swagger'; -import { IsString, IsObject, IsOptional } from 'class-validator'; +import { IsString, IsOptional } from 'class-validator'; import { DatabaseConnectionPropertiesDto } from 'src/modules/connection/dtos/connection'; import { CreateConnectionDto } from 'src/modules/connection/dtos/create-connection'; export class ColumnDto { @@ -44,11 +44,13 @@ export class ConnectionTestCredentialsReq extends OmitType( ['properties'], ) { @ApiProperty() - @IsString() plugin: string; + @ApiProperty() - @IsObject() properties: DatabaseConnectionPropertiesDto; + + @ApiPropertyOptional() + connection_id?: string; } export class ConnectionTestCredentialsRes { @@ -116,5 +118,5 @@ export class GetTableMetadataReq { export class GetTableMetadataRes { @ApiProperty({ type: [TableMetadataDto] }) - table_metadata: TableMetadataDto[]; + tables_metadata: TableMetadataDto[]; } diff --git a/src/modules/connection/connection.module.ts b/src/modules/connection/connection.module.ts index dde02b3..3ce6485 100644 --- a/src/modules/connection/connection.module.ts +++ b/src/modules/connection/connection.module.ts @@ -18,5 +18,6 @@ const client = new ConnectionClientConfiguration(); }, ]), ], + exports: [ConnectionClientService], }) export class ConnectionModule {} diff --git a/src/utils/ErrorBuilder.ts b/src/utils/ErrorBuilder.ts index e6c0eb8..5bf9304 100644 --- a/src/utils/ErrorBuilder.ts +++ b/src/utils/ErrorBuilder.ts @@ -171,6 +171,31 @@ export function EnrichErrorCode(code: string) { message: 'Id encontra-se num formato inválido', code, }; + case ErrorCodes.CONNECTION_TEST.ERROR: + return { + statusCode: HttpStatus.BAD_REQUEST, + error: 'Erro ao realizar o teste', + message: 'Verifique os dados de conexão e tente novamente', + code, + }; + + case ErrorCodes.CONNECTION_TEST.EXPIRED: + return { + statusCode: HttpStatus.NOT_FOUND, + error: 'Teste de conexão expirado', + message: 'Favor realizar novo teste', + code, + }; + + case ErrorCodes.CONNECTION_TEST.TIMEOUT: + return { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: 'Tempo limite excedido', + message: + 'O teste excedeu o tempo limite. Verifique os dados de conexão e tente novamente', + code, + }; + case ErrorCodes.INTERNAL: case ErrorCodes.UNKNOWN: default: @@ -193,7 +218,7 @@ export default class ErrorBuilder extends HttpException { constructor(code: string | RpcException = '') { if (typeof code !== 'string') { logger.log(code?.stack); - code = (code as any).details as string; + code = (code as any)?.details as string; } else { logger.log(code); } diff --git a/src/utils/errorCodes.ts b/src/utils/errorCodes.ts index b679510..992243f 100644 --- a/src/utils/errorCodes.ts +++ b/src/utils/errorCodes.ts @@ -38,6 +38,13 @@ const TERMS_OF_USE = { REQUIRED: 'TERMS_OF_USE.REQUIRED', }; +const CONNECTION_TEST = { + ERROR: 'CONNECTION_TEST.ERROR', + EXPIRED: 'CONNECTION_TEST.EXPIRED', + TIMEOUT: 'CONNECTION_TEST.TIMEOUT', + UNKNOWN: 'CONNECTION_TEST.UNKNOWN', +}; + const ErrorCodes = { UNKNOWN: 'UNKNOWN', RATE_LIMIT: 'RATE_LIMIT', @@ -47,6 +54,7 @@ const ErrorCodes = { TERMS_OF_USE, USER, CUSTOMER, + CONNECTION_TEST, }; export default ErrorCodes;