FEAT: Connection test creating connection

- If any error occurs, connection is deleted
 - If operation_result is false, connection is deleted
This commit is contained in:
Gabriel Rosa
2022-08-03 11:31:57 -03:00
parent a1cdad42ab
commit 688bf49f3b
11 changed files with 201 additions and 124 deletions
@@ -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<ConnectionTest.ReadService.ConnectionTestReadServices>(
@@ -44,19 +49,45 @@ export class ConnectionTestService {
}
async connectionTestCredentials(
body: ConnectionTestCredentialsReq,
customer_name: string,
user: RequestUser,
): Promise<ConnectionTestCredentialsRes> {
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(