import { Inject, Injectable } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import { ConnectionTest } from '@dadosfera/protospack-v2'; import { lastValueFrom } from 'rxjs'; import { ConnectionTestCredentialsReq, ConnectionTestCredentialsRes, ConnectionTestListSchemasReq, ConnectionTestListSchemasRes, ConnectionTestListTablesReq, ConnectionTestListTablesRes, ConnectionTestPingReq, ConnectionTestPingRes, GetTableMetadataReq, GetTableMetadataRes, } from './dto/connection-test'; import { ConnectionClientService } from '../connection/client.service'; import { CreateConnectionDto, DatabaseConnectionPropertiesDto, } from '../connection/dtos/connection'; import { RequestUser } from 'src/decorators/user.decorator'; import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; @Injectable() export class ConnectionTestService { private connectionTestReadClient: ConnectionTest.ReadService.ConnectionTestReadServices; constructor( @Inject('ConnectionTestGrpcClient') private readonly grpcClient: ClientGrpc, private connectionsService: ConnectionClientService, ) { this.connectionTestReadClient = grpcClient.getService( ConnectionTest.ProtoServices.ConnectionTestReadServices, ); } async connectionTestPing( body: ConnectionTestPingReq, user: RequestUser, ): Promise { const { host: endpoint, plugin, port, network_config_id, connection_id, } = body; const { user_id, customer_id, customer_name } = user; const metadata = PackTheMetadata({ customer_name, customer_id, user_id, }); if (connection_id) { const con = await this.connectionsService.getConnectionDetails({ body, metadata, }); const conPayload = JSON.parse(con.connection.properties); const { plugin, network_config_id } = con.connection; const { host, port, endpoint } = conPayload; return lastValueFrom( this.connectionTestReadClient.ConnectionTestPing({ customer_name, endpoint: endpoint || host, network_config_id, plugin, port: port ? port.toString() : undefined, connection_id, }), ); } else { return lastValueFrom( this.connectionTestReadClient.ConnectionTestPing({ customer_name, endpoint, network_config_id, plugin, port: port ? port.toString() : undefined, connection_id, }), ); } } async connectionTestCredentials( body: ConnectionTestCredentialsReq, user: RequestUser, ): Promise { const { plugin, ...connectionBody } = body; let { connection_id } = body; const { customer_id, customer_name, user_id, username } = user; const metadata = PackTheMetadata({ customer_id, customer_name, user_id, username, }); if (body.network_config_id == '') { body.network_config_id = undefined; } /********* CREATE CONNECTION *********/ if (!connection_id) { const properties: DatabaseConnectionPropertiesDto = { ...connectionBody.properties, plugin, credentials_type: 'basic_auth', }; const createConnectionBody: CreateConnectionDto = { ...body, properties, }; const { connection } = await this.connectionsService.createConnection( createConnectionBody, metadata, ); connection_id = connection.id; } /************************************/ const { operation_result } = await lastValueFrom( this.connectionTestReadClient.ConnectionTestCredentials({ connection_id, customer_name, plugin, }), ).catch(async (e) => { if (!body.connection_id) { await this.connectionsService.deleteConnection({ body: { id: connection_id }, metadata, }); } throw e; }); if (!operation_result && !body.connection_id) { await this.connectionsService.deleteConnection({ body: { id: connection_id }, metadata, }); connection_id = null; } return { operation_result, connection_id }; } async connectionTestListSchemas( body: ConnectionTestListSchemasReq, customer_name: string, ): Promise { const { connection_id, plugin } = body; return lastValueFrom( this.connectionTestReadClient.ListSchemas({ connection_id, customer_name, plugin, }), ); } async connectionTestListTables( body: ConnectionTestListTablesReq, customer_name: string, ): Promise { const { connection_id, plugin, schema } = body; return lastValueFrom( this.connectionTestReadClient.ListTables({ connection_id, customer_name, plugin, schema, }), ); } async getTableMetadata( body: GetTableMetadataReq, customer_name: string, ): Promise { const { schema, plugin, table_list, connection_id } = body; return lastValueFrom( this.connectionTestReadClient.GetTableMetadata({ connection_id, customer_name, plugin, schema, table_list, }), ); } }