diff --git a/src/clients/duc/client.config.ts b/src/clients/duc/client.config.ts index ada4aca..59ff8a8 100644 --- a/src/clients/duc/client.config.ts +++ b/src/clients/duc/client.config.ts @@ -9,7 +9,9 @@ export class DucClient { options: { url: process.env.DUC_URL, package: ProtoPackages.WritePackage, - credentials: credentials.createSsl(), + credentials: process.env.LOCAL_ENV + ? undefined + : credentials.createSsl(), protoPath: ProtoPaths.WriteFilePath, loader: { keepCase: true, diff --git a/src/modules/connector/client.config.ts b/src/modules/connector/client.config.ts new file mode 100644 index 0000000..9f5c0f0 --- /dev/null +++ b/src/modules/connector/client.config.ts @@ -0,0 +1,30 @@ +import { credentials } from '@grpc/grpc-js'; +import { ClientOptions, Transport } from '@nestjs/microservices'; +import { ConnectorManager } from 'protospack-v2'; + +export class ConnectorClientConfiguration { + config(): ClientOptions { + return { + transport: Transport.GRPC, + options: { + url: process.env.INFACTORY_URL, + package: [ + ConnectorManager.ProtoPackages.WritePackage, + ConnectorManager.ProtoPackages.ReadPackage, + ], + credentials: process.env.LOCAL_ENV + ? undefined + : credentials.createSsl(), + protoPath: [ + ConnectorManager.ProtoPaths.WriteFilePath, + ConnectorManager.ProtoPaths.ReadFilePath, + ], + loader: { + enums: String, + objects: true, + arrays: true, + }, + }, + }; + } +} diff --git a/src/modules/connector/client.service.ts b/src/modules/connector/client.service.ts new file mode 100644 index 0000000..2370b5a --- /dev/null +++ b/src/modules/connector/client.service.ts @@ -0,0 +1,175 @@ +import { + OnModuleInit, + Inject, + HttpException, + HttpStatus, +} from '@nestjs/common'; +import { ClientGrpc } from '@nestjs/microservices'; +import { ConnectorManager } from 'protospack-v2'; +import { lastValueFrom } from 'rxjs'; + +export class ConnectorClientService implements OnModuleInit { + private connectorServiceRead: ConnectorManager.ReadService.ConnectorManagerReadServices; + private connectorServiceWrite: ConnectorManager.WriteService.ConnectorManagerWriteServices; + constructor( + @Inject('CONNECTOR_PACKAGE') private readonly grpcClient: ClientGrpc, + ) {} + + onModuleInit() { + this.connectorServiceWrite = + this.grpcClient.getService( + ConnectorManager.ProtoServices.ConnectorManagerWriteServices, + ); + + this.connectorServiceRead = + this.grpcClient.getService( + ConnectorManager.ProtoServices.ConnectorManagerReadServices, + ); + } + + async uploadConnector(uploadConnector) { + const connector: ConnectorManager.Entities.ConnectorCreateRequest = { + file: { + buffer: uploadConnector.file.buffer, + mimetypes: uploadConnector.file.mimetype, + }, + connector: JSON.stringify(uploadConnector.connector), + }; + const serviceBody: ConnectorManager.Messages.RegisterConnectorRequest = { + connector, + }; + + return lastValueFrom( + this.connectorServiceWrite.RegisterConnector(serviceBody), + ).catch((err) => { + console.log(err.details); + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async uploadFile(uploadFile) { + const body: ConnectorManager.Messages.UploadFileRequest = { + file: { + buffer: uploadFile.file.buffer, + mimetypes: uploadFile.file.mimetype, + }, + name: uploadFile.name, + }; + + return lastValueFrom(this.connectorServiceWrite.UploadFile(body)).catch( + (err) => { + console.log(err.details); + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }, + ); + } + + async uploadConnectorsFile(uploadFile) { + const connectors = JSON.parse(uploadFile.file.buffer.toString('utf8')); + + const body = connectors.map((connector) => { + return JSON.stringify(connector); + }); + + const request = { + connectors: body, + }; + + return lastValueFrom( + this.connectorServiceWrite.RegisterMultipleConnectorsWithoutImage( + request, + ), + ).catch((err) => { + console.log(err.details); + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getAllConnectors(body) { + return lastValueFrom( + this.connectorServiceRead.GetAllConnectors({ + search: body.search, + filters: JSON.stringify(body.filters), + }), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getConnectorDetails(plugin: string) { + return lastValueFrom( + this.connectorServiceRead.GetConnectorDetails({ + plugin, + }), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getConnector(plugin: string) { + return lastValueFrom( + this.connectorServiceRead.GetConnector({ + plugin, + }), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getConnectorsTags() { + return lastValueFrom(this.connectorServiceRead.GetConnectorsTags({})).catch( + (err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }, + ); + } + + async deleteConnector(plugin: string, version: string) { + return lastValueFrom( + this.connectorServiceWrite.DeleteConnector({ + version, + plugin, + }), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async updateConnector({ plugin, changes }) { + return lastValueFrom( + this.connectorServiceWrite.UpdateConnector({ + plugin, + changes, + }), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } +} diff --git a/src/modules/connector/connector.module.ts b/src/modules/connector/connector.module.ts new file mode 100644 index 0000000..2ab2e1e --- /dev/null +++ b/src/modules/connector/connector.module.ts @@ -0,0 +1,21 @@ +import { Module } from '@nestjs/common'; +import { ClientsModule } from '@nestjs/microservices'; +import { ConnectorClientService } from './client.service'; +import { ConnectorClientConfiguration } from './client.config'; +import { ConnectorController } from './connector.controller'; + +const connectorClient = new ConnectorClientConfiguration(); + +@Module({ + controllers: [ConnectorController], + providers: [ConnectorClientService], + imports: [ + ClientsModule.register([ + { + name: 'CONNECTOR_PACKAGE', + ...connectorClient.config(), + }, + ]), + ], +}) +export class ConnectorModule {} diff --git a/src/modules/connector/dtos/create-multiple-connector.ts b/src/modules/connector/dtos/create-multiple-connector.ts new file mode 100644 index 0000000..979e105 --- /dev/null +++ b/src/modules/connector/dtos/create-multiple-connector.ts @@ -0,0 +1,6 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class UploadConnectorsFileDto { + @ApiProperty({ format: 'binary' }) + file: string; +}